(九)Config:配置中心

(九)Config:配置中心,第1张

(九)Config:配置中心 配置Git仓库

新建一个git仓库(gittee也行)
添加一个测试用的代码application.yml

---
spring:
    profiles: dev
a: 1
---
spring:
    profiles: test
a: 2
新建配置中心模块

pom


	
		org.springframework.cloud
		spring-cloud-config-server
	
	
		org.eclipse.jgit
		org.eclipse.jgit
	

配置文件

server:
  port: 9000
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git: # git地址
          uri: https://gitee.com/zxing2021/spring-cloud-config-test.git

启动

@SpringBootApplication
@EnableConfigServer
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class);
	}
}

测试

启动配置中心,打开URL:
dev环境:http://localhost:9000/application-dev.yml

test环境:http://localhost:9000/application-test.yml

master分支test环境:http://localhost:9000/master/application-test.yml

成功!

新建配置客户端模块

pom


	
		org.springframework.cloud
		spring-cloud-starter-config
	
	
		org.springframework.boot
		spring-boot-starter-web
	

配置文件,bootstrap.yml,不是application.yml

server:
  port: 9001
spring:
  cloud:
    config:
      name: application # 文件名
      profile: dev # profile
      label: master # 分支
      uri: http://localhost:9000 # 配置中心地址

启动类

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class);
	}
}

测试Controller,读取a的值并返回

@RestController
public class HelloController {

	@Value("${a}")
	private Integer a;

	@GetMapping("/")
	public Integer fun1() {
		return a;
	}
}

测试

启动配置中心,配置客户端
http://localhost:9001/

成功!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5692818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存