Spring Cloud学习(七)-Spring Cloud Config
一、SpringCloud Config
概述
1.1、分布式项目面临的问题
微服务意味着要将单体应用中的业务拆分为一个个子服务,每个服务的粒度都相对较小,因此系统中会出现大量的服务。
由于每个微服务都需要必要的配置信息才能运行,每一个微服务都带着一个
application
配置文件,所以一套集中式、动态的配置管理设施必不可少。 Spring Cloud 提供了
ConfigServer
来解决这个问题
1.2、SpringCloud Config
SpringCloud Config
为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为 各个不同微服务应用 的所有环境提供了一个 中心化的外部配置。
SpringCloud Config
分为客户端和服务端两部分。 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并未客户端提供获取配置信息,加密 / 解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动时从配置中心获取和加载信息配置。服务器默认使用
GIT
来存储配置信息,这样有助于对环境配置进行版本管理,并且可以通过GIT
客户端工具来方便地管理和访问配置内容。
1.3、作用
集中管理配置文件
不同环境不同配置,动态化的配置更新,分环境部署比如
dev/test/prod/beta/release
运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
将配置信息以REST接口的形式暴露
POST 和 curl 都可以动态刷新。
1.4、与 GitHub
整合配置
由于
SpringCloud Config
默认使用Git来存储配置文件(也有其它方式,比如支持svn
和本地文件,但最推荐的还是Git
,而且使用的是http/https
访问的形式)
二、Config 服务端配置和测试
2.1、环境创建
1、创建 GitHub
仓库
在
github
上创建一个SpringCloud-config
仓库,复制地址。
2、在仓库中创建三个 yaml
配置文件,克隆仓库
- 使用
git clone
命令克隆仓库 - 使用
Git
关联远程GitHub
账号,并指定远程仓库别名为 origin
- 移除原有的远程地址 origin
1 | git remote rm origin |
- 为远程仓库起一个 origin 别名
1 | git remote add origin https://github.com/sutianxin/springcloud-config.git |
- 从远程仓库的 main 分支拉取最新配置(代码)
1 | git fetch origin main |
- 将拉取的代码与本地的 main 分支合并
1 | git merge origin/main |
3、本地文件
此时在本地可以看到文件
2.2、构建 Config 服务端微服务
1、依赖关系
1 | <dependencies> |
2、配置文件
- 使用
spring.cloud.config.server.git.uri
配置远程仓库地址 - 使用
spring.cloud.config.server.git.search-paths
配置路径 - 使用
spring.cloud.config.server.label
配置读取分支
1 | spring: |
3、主启动类
在主启动类上添加
@EnableConfigServer
注解
1 |
|
4、测试
启动主启动类,访问 http://localhost:3344/master/config-dev.yml 查看效果,发现可以获取远程仓库的配置信息
2.3、配置读取规则
1、/{label}/{application}-{profile}.yml
对于 master 分支
- http://localhost:3344/master/config-dev.yml
- http://localhost:3344/master/config-prod.yml
- http://localhost:3344/master/config-test.yml
对于 dev 分支
- http://localhost:3344/dev/config-dev.yml
- http://localhost:3344/dev/config-dev.yml
- http://localhost:3344/dev/config-dev.yml
2、/{application}-{profile}.yml
默认寻找
application.yml
中配置的读取分支,如果获取的文件不存在,那么会返回空
3、/{application}/{profile}/{label}
三、Config 客户端配置和测试
3.1、构建 Config 客户端微服务
1、依赖关系
1 | <dependencies> |
2、配置文件
- 需要编写一个系统级的配置文件
bootstrap.yml
,这个配置文件优先级比用户级的资源配置项application.yml
更加高
使用
bootstrap.yml
读取配置中心的配置,然后使用application.yml
配置自定义的配置
1 | server: port: 3355spring: application: name: config-client cloud: config: # 分支名称 label: master # 配置文件名称 name: config # 配置环境 profile: dev # 配置中心地址 uri: http://localhost:3344 nacos: discovery: server-addr: 120.78.198.32:8848 |
3、主启动类
1 | class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }} |
3.2、获取配置中心 ConfigServer
的配置
使用 Rest 风格的请求访问配置,读取
GitHub
中的config.info
配置
1、编写一个 Controller
1 | class ConfigClientController { private String configInfo; public String getConfigInfo() { return configInfo; }} |
2、启动测试
访问 http://localhost:3355/info ,查看结果,可以看到此时访问到
GitHub
上的
·
成功通过配置客户端获取了
GitHub
上的配置
3.3、分布式配置的动态刷新问题
1、修改 GitHub
上的配置文件
Linux 运维修改
GitHub
上的配置文件,对配置进行修改
1 | config: info: "config-dev.yml configuration info,version = 2,mock Dev Ops update configuration info in managed code" |
2、刷新 ConfigServer
服务端
刷新配置中心
ConfigServer
,发现配置中心此时立即响应。
3、刷新 ConfigClient
刷新配置中心客户端
ConfigClient
客户端,此时发现没有任何变化,除非重启ConfigClient
或者重新加载。访问 http://localhost:3355/info ,查看结果
4、动态刷新步骤
- 修改
ConfigClient
模块
添加监控模块依赖
1 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |
- 修改配置文件,暴露监控端点
1 | # 暴露监控端点management: endpoints: web: exposure: include: "*"- 在业务类中添加 `@RefreshScope` 注解```java@RefreshScope@RestControllerpublic class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("info") public String getConfigInfo() { return configInfo; }} |
- 此时先启动
ConfigClient
,查看结果
- 此时修改代码平台上的配置
发现客户端无法拉取更新后的配置,刷新没有生效
1 | config: info: "config-dev.yml configuration info,version = 3,mock Dev Ops update configuration info in managed code,test AutoRefresh" |
5、手动刷新,拉取最新配置
- 需要向运维人员发送 POST 请求,刷新
ConfigClient
配置。
1 | curl -X POST "http://localhost:3355/actuator/refresh" |
- 此时再次查看
ConfigClient
的最新配置,发现已经刷新