1. 分布式系统面临的配置问题Spring Cloud Config
2. 是什么微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
2.1 服务端和客户端Spring Cloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置;
3. 能干嘛
- 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口;
- 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
4. Config Server
- 集中管理配置文件;
- 不同环境不同配置,动态化的配置更新,分环境部署(如:dev、test、prod、beta、release);
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取自己的配置信息;
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置;
- 将配置信息以REST接口形式暴露(post、curl访问刷新均可)。
4.1 新建Module-cloud-config-center3344 4.2 POM实现Config Server通过GitHub获取配置信息
4.3 YML(修改hosts文件)cloud-2020 com.qs.springcloud 1.0-SNAPSHOT 4.0.0 cloud-config-center3344 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
修改hosts文件
# 添加映射配置 127.0.0.1 config3344.com
server:
port: 3344
spring:
application:
# 注册进Eureka服务器的微服务名
name: cloud-config-center
cloud:
config:
server:
git:
# Git地址
uri: https://gitee.com/qshome/springcloud-config.git
# 搜索目录
search-paths:
- springcloud-config
# 读取分支
label: master
eureka:
client:
service-url:
# 注册中心地址
defaultZone: http://localhost:7001/eureka
4.4 主启动
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}
4.5 测试
5. 配置读取规则http://config3344.com:3344/master/config-dev.yml
/{label}/{application}-{profile}.yml(label分支、name服务名、profiles环境)
- master分支
http://config3344.com:3344/master/config-dev.yml
http://config3344.com:3344/master/config-test.yml
http://config3344.com:3344/master/config-prod.yml- dev分支
http://config3344.com:3344/dev/config-dev.yml
http://config3344.com:3344/dev/config-test.yml
http://config3344.com:3344/dev/config-prod.yml
/{application}-{profile}.yml
http://config3344.com:3344/config-dev.yml
http://config3344.com:3344/config-test.yml
http://config3344.com:3344/config-prod.yml
6. Config Client/{application}/{profile}[/{label}]
http://config3344.com:3344/config/dev/master
http://config3344.com:3344/config/test/master
http://config3344.com:3344/config/test/dev
6.1 新建Module-cloud-config-client3355 6.2 POM实现客户端3355访问Config Server 3344通过GitHub获取配置信息
6.3 YML(bootstrap.yml)cloud-2020 com.qs.springcloud 1.0-SNAPSHOT 4.0.0 cloud-config-client3355 org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
- bootstrap.yml是系统级的,优先级更加高;
- applicaiton.yml是用户级的资源配置项;
- Spring Cloud会创建一个"Bootstrap Context",作为 Spring应用的 Application Context的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。
- Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。
server:
port: 3355
spring:
application:
name: cloud-config-client
cloud:
# Config客户端配置http://config3344.com:3344/master/config-dev.yml
config:
# 分支名称
label: master
# 配置文件名称
name: config
# 读取后缀名称
profile: dev
# 配置中心地址
uri: http://localhost:3344
eureka:
client:
service-url:
# 注册中心地址
defaultZone: http://localhost:7001/eureka
6.4 主启动
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}
6.5 ConfigClientController
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
6.6 测试
7. Config Client 动态刷新http://localhost:3355/configInfo
7.1 修改Module-cloud-config-client3355 7.2 POM
- 分布式配置的动态刷新问题
- 运维修改GitHub上的配置文件内容;
- 刷新3344,发现Config Server配置中心立刻响应;
- 刷新3355,发现Config Client客户端没有任何响应;
- 3355没有变化除非自己重启或者重新加载;
引入actuator监控
7.3 YML(bootstrap.yml)org.springframework.boot spring-boot-starter-actuator
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
7.4 修改ConfigClientController
@RefreshScope
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
7.5 手动刷新
7.6 测试curl -X POST “http://localhost:3355/actuator/refresh”
http://config3344.com:3344/master/config-dev.yml ok
http://localhost:3355/configInfo ok



