新建一个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/
成功!



