09.SpringCloud Config (分布式配置中心)
1.概述
分布式系统面临的配置问题

是什么


能干嘛
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露 post、curl访问刷新均可....
与Github整合配置
由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)
官网

2.Config服务端配置与测试
Config服务端的创建
用你自己的账号在Github上新建一个名为sprincloud-config的新Repository
添加三个配置文件进这个仓库

config-dev.yml
config:
info: dev1config-prod.yml
config:
info: prod1config-test.yml
config:
info: test1新建Module模块cloud-config-center-3344
它为Cloud的配置中心模块CloudConfig Center
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud2020</artifactId>
<groupId>com.chl.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-center-3344</artifactId>
<dependencies>
<!--springcloud 的服务端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.chl.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config: #分支/config-环境名.yml
server: #http://config-3344.com:3344/master/config-dev.yml
git: #githud仓库的位置 用码云不行,识别不了,只能githud
uri: https://github.com/badbad001/springcloud-config.git
search-paths:
- springcloud-config #仓库名称
label: master #仓库的分支
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka启动类
@SpringBootApplication
@EnableConfigServer //开启服务配置中心
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344 .class,args);
}
}windows下修改hosts文件,增加映射
127.0.0.1 config-3344.com测试通过Config微服务是否可以从Github上获取配置内容
启动微服务3344
配置读取规则


/{label}/{application}-{profile}.yml(最推荐使用这种方式)
master分支
dev分支
/{application}-{profile}.yml
- http://config-3344.com:3344/config-dev.yml
- http://config-3344.com:3344/config-test.yml
- http://config-3344.com:3344/config-prod.yml
- http://config-3344.com:3344/config-xxxx.yml(不存在的配置)
/{application}-{profile}[/{label}]
重要配置细节总结

成功实现了用SpringCloud Config 通过GitHub获取配置信息
3.Config客户端配置与测试
新建cloud-config-client-3355
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud2020</artifactId>
<groupId>com.chl.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-client-3355</artifactId>
<dependencies>
<!--config 客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.chl.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>bootstap.yml

配置如下
#这个文件名 bootrap.yml 系统级别的 优先于 application.yml(用户界别)
server:
port: 3355
spring:
application:
name: config-client
cloud: #config客户端的配置
config: #组装成就是 http://localhost:3344/master/config-dev.yml 配置的是从哪里获取配置信息
uri: http://localhost:3344 #配置中心地址
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka

修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version
主启动
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run( ConfigClientMain3355.class,args);
}
}业务类
@RestController
public class ConfigClientController {
//获取统一配置的数据信息
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}测试
启动Config配置中心3344微服务并自测
启动3355作为Client准备访问
成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息
问题随时而来,分布式配置的动态刷新问题
- Linux运维修改GitHub上的配置文件内容做调整
- 刷新3344,发现ConfigServer配置中心立刻响应
- 刷新3355,发现ConfigServer客户端没有任何响应
- 3355没有变化除非自己重启或者重新加载
- 难道每次运维修改配置文件,客户端都需要重启??噩梦
4.Config客户端之动态刷新
避免每次更新配置都要重启客户端微服务3355
动态刷新步骤
修改3355模块
pom引入actuator监控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>yml
#这个文件名 bootrap.yml 系统级别的 优先于 application.yml(用户界别)
server:
port: 3355
spring:
application:
name: config-client
cloud: #config客户端的配置
config: #组装成就是 http://localhost:3344/master/config-dev.yml 配置的是从哪里获取配置信息
uri: http://localhost:3344 #配置中心地址
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
#暴露监控端口,用于刷新分布式的配置
management:
endpoints:
web:
exposure:
include: "*"@RefreshScope业务类Controller修改
@RestController
@RefreshScope //用于刷新分布式的配置
//可能需要服务中心先请求一次,而且还要运维人员发起一个请求去刷新相应的客户端
//例如运维人员改了,还要刷新客户端3355 curl -X POST "http://localhost:3355/actuator/refresh
public class ConfigClientController {
//获取统一配置的数据信息
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}此时修改github---> 3344 ---> 3355
3355改变了没有???
没有改变,(┬_┬)
为什么
需要运维人员发送Post请求刷新3355 必须是Post请求
curl -X POST "http://localhost:3355/actuator/refresh"再次访问
成功实现了客户端3355刷新到最新配置内容 避免了服务的重启
还存在的问题
假如有多个微服务客户端3355/3366/3377。。。。
每个微服务都要执行一次post请求,手动刷新?
可否广播,一次通知,处处生效?
我们想大范围的自动刷新,求方法 就要用到 SpringCloud Bus 消息总线

浙公网安备 33010602011771号