SpringCloud config分布式配置中心
分布式系统面临的配置问题
能干嘛?
集中管理配置文件
不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
将配置信息以REST接口的形式暴露,post、curl访问刷新均可….
与Github整合配置
由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)
Config服务端配置与测试
1.用你自己的账号在Github上新建一个名为sprincloud-config的新Repository
2.由上一步获得刚新建的git地址,写你自己的仓库地址
3.本地硬盘上新建git仓库并clone,
本地地址:D:\44\SpringCloud2020
git命令 git clone xxx
4.此时在本地D盘符下D:\44\SpringCloud2020\springcloud-config
表示多个环境的配置文件,保存格式必须为UTF-8,如果需要修改,
此处模拟运维人员操作git和github
git add
git commit -m “init yml”
git push origin master
5.新建Module模块cloud-config-center-3344它既为Cloud的配置中心模块cloudConfig Center
pom
<dependencies>
<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.kayleh.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</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>
yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
# uri: git@github.com:Kayleh/springcloud-config.git #填写你自己的github路径
uri: https://github.com/Kayleh/springcloud-config.git #填写你自己的github路径
search-paths:
- springcloud-config
username: Kayleh
password: #密码
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
主启动类: @EnableConfigServer
package com.kayleh.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@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
http://config-3344.com:3344/master/config-dev.yml
配置读取规则
/{label}/{application}-{profile}.yml(最推荐使用这种方式)
/{application}-{profile}.yml
/{application}-{profile}[/{label}]
重要配置细节总结
成功实现了用SpringCloud Config 通过GitHub获取配置信息
Config客户端配置与测试
新建cloud-config-client-3355
pom
<dependencies>
<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.kayleh.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</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>
bootstap.yml
是什么?
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version
主启动类:
package com.kayleh.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run( ConfigClientMain3355.class,args);
}
}
业务类
package com.kayleh.springcloud.Controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
启动Config配置中心3344微服务并自测
http://config-3344.com:3344/master/config-dev.yml
http://config-3344.com:3344/master/config-test.yml
启动3355作为Client准备访问
http://localhost:3355/configInfo
成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息
问题随时而来,分布式配置的动态刷新
Linux运维修改GitHub上的配置文件内容做调整
刷新3344,发现ConfigServer配置中心立刻响应
刷新3355,发现ConfigServer客户端没有任何响应
3355没有变化除非自己重启或者重新加载
难道每次运维修改配置文件,客户端都需要重启??噩梦
Config客户端之动态刷新
避免每次更新配置都要重启客户端微服务3355
修改3355模块
POM引入actuator监控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改YML,暴露监控端口
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
@RefreshScope业务类Controller修改
package com.kayleh.springcloud.Controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
此时修改去github
测试3344 和3355
http://config-3344.com:3344/master/config-dev.yml
http://localhost:3355/configInfo
3355没有改变,(只有重启才有效果)
需要运维人员发送Post请求刷新3355,必须是Post请求
curl -X POST "http://localhost:3355/actuator/refresh"
再次http://localhost:3355/configInfo
成功实现了客户端3355刷新到最新配置内容,避免了服务的重启
想想还有什么问题?
假如有多个微服务客户端3355/3366/3377。。。。
每个微服务都要执行一次post请求,手动刷新?
可否广播,一次通知,处处生效?
我们想大范围的自动刷新,求方法