1. 概述
1.1 是什么
官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/
SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个微服务应用的环境提供了一个中心化的外部配置。
SpringCloud Config 分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用, 用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
配置服务器默认采用 git 来存储配置信息,这样就有助于对环境配置进行版本管理,可以通过 git 客户端工来方便的管理和访问配置内容。
1.2 作用
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署 ,比如:dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以 REST 接口的形式暴露(post、curl访问)
2. 服务端配置
2.1 建仓库
Github上新建一个名为 springcloud-config 的 Repository
2.2 新建配置中心模块
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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
YML:
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: git@github.com:Zhangtao153/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);
}
}
@EnableConfigServer
测试:
2.3 配置读取规则
/{application}/{profile}[/{label}] eg: http://localhost:3344/config/dev/master
/{application}-{profile}.yml eg: http://localhost:3344/config-dev.yml
/{label}/{application}-{profile}.yml eg: http://localhost:3344/master/config-dev.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
【注】:
- label:分支 (branch)
- application:服务名
- profile:环境 (dev/test/prod)
3. 客户端配置
3.1 新建模块
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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
bootstrap.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://localhost:7001/eureka
applicaiton.yml 是用户级资源配置项
bootstrap.yml 是系统级资源配置项,优先级更加高
Spring Cloud 会创建一个 "Bootstrap Context",作为 Spring 应用的 Application Context 的父上下文。初始化的时候 ”Bootstrap
Context” 负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的 “Environment”。
“Bootstrap” 属性有高优先级,默认情况下,不会被本地配置覆盖。保证 Bootstrap Context 和 Application Context 配置的分离。
主启动:
@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;
}
}
测试:
配置文件类容:
访问结果:
3.2 存在的问题
修改 GitHub 上配置文件内容,ConfigServer 配置中心立刻响应,而 ConfigServer 客户端没有任何响应使用的还是旧配置,除非重启或者重新加载。
4. 客户端动态刷新
1. pom 引入 actuator 监控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 修改 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://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
3. 业务类controller 添加 @RefreshScope
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
4. 发送 Post 请求刷新3355
curl -X POST "http://localhost:3355/actuator/refresh"
5. 测试
成功实现了客户端刷新到最新配置内容,避免重启服务。