配置中心-基础
配置文件
server:
port: 8888
spring:
application:
name: ConfigServer
cloud:
config:
server:
git:
# 配置git仓库地址
uri: https://gitee.com/_LK/test_work.git
# 访问git仓库的用户名
# username:
# 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
# password:
#支持带{application}和{profile}({label}如果需要)占位符的搜索路径
search-paths: '{application}'
# 配置仓库的分支,可不配
label: master
配置 git 仓库地址
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
访问 http://127.0.0.1:8888/ConfigClient/dev 返回配置内容
{"name":"ConfigClient","profiles":["dev"],"label":null,"version":"c9c8203c27fc04866a7a67523c6051a057048297","state":null,"propertySources":[{"name":"https://gitee.com/_LK/test_work.git/ConfigClient-dev.properties","source":{"git.value":"456asdasdada"}}]}
配置自动刷新
配置 git
服务器端的webHook
,更新后主动推送
Client
bootstrap.yml
建立bootstrap.yml
配置 configServer
spring:
application:
name: configclient
cloud:
config:
uri: http://localhost:8888/
label: master
profile: dev
server:
port: 10001
配置文件
加上@RefreshScope
注解,git.value
会从配置中心读取值
@SpringBootApplication
@RefreshScope
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${git.value}")
private String gitValue;
@RequestMapping("/")
public String git() {
return gitValue;
}
}