1、为什么需要配置中心?
1)、集中管理配置
2)、不同环境不同配置
3)、运行期间动态调整配置
4)、自动刷新

2、Spring Cloud Config配置中心介绍

Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。
Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容(也可使用Subversion、本地文件系统或Vault存储配置),因此可以方便的实现对配置的版本控制与内容审计。Config Client 是Config Server的客户端,用于操作存储在Config Server中的配置属性。

Spring Cloud Config 配置中心

使用config实现配置中心服务端及客户端,首先新增git配置仓库中心,地址为:https://gitee.com/aaronrao/spring-cloud-configtest 在仓库里增加如下配置文件:

Spring Cloud Config 配置中心

编写config配置中心服务端(使用git仓库存储) 见示例:10-ms-config-server 添加依赖,并在启动类上增加注解@EnableConfigServer

Spring Cloud Config 配置中心

配置文件application.yml如下:

Spring Cloud Config 配置中心

启动项目,访问地址:http://localhost:8080/ms-config/dev,得到整个项目的配置信息

Spring Cloud Config 配置中心

访问地址:http://localhost:8080/ms-config-dev.properties,得到配置文件内容

Spring Cloud Config 配置中心

配置文件映射规则如下:

Spring Cloud Config 配置中心

以上端点都可以映射到{application}-{profile}.properties这个配置文件,{application}表
示微服务的名称,{label}对应Git仓库的分支,默认是 master

 

3、编写config配置中心客户端
见示例:10-ms-config-client 添加依赖

Spring Cloud Config 配置中心

除了默认的application.yml配置文件,还需增加一个bootstrap.yml的配置文件,内容如下:

Spring Cloud Config 配置中心

spring cloud有一个“引导上下文"的概念,这是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.*(yml或 properties)中的属性不同,引导上下文加载(bootstrap.*)中的属性。
配置在 bootstrap.*中的属性有更高的优先级,因此默认情况下它们不能被本地配置覆盖。启动项目,访问地址:http://localhost:8081/profile,得到config配置中心的配置文件,内容如下:

Spring Cloud Config 配置中心

4、config配置中心服务端配置详解
见示例:10-ms-config-server 各种配置方法见项目配置文件,如下

Spring Cloud Config 配置中心

5、配置信息的加解密安全处理
前文是在 Git仓库中明文存储配置属性的。很多场景下,对于某些敏感的配置内容(例如数据库账号、密码等),应当加密存储。 config server为配置内容的加密与解密提供了支持。
安装JCE
config server的加解密功能依赖Java Cryptography Extension(JCE)
Java8 JCE下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
下载JCE并解压,将其中的jar包覆盖到JDK/jre/lib/security目录中

6、对称加密
config server提供了加密与解密的接口,分别是/encrypt与/decrypt 见示例:10-ms-config-server-encryption

Spring Cloud Config 配置中心
添加依赖,增加一个配置文件bootstrap.yml,配置对称加密的**

Spring Cloud Config 配置中心
运行项目,验证加解密
加密:curl http://localhost:8080/encrypt -d mySecret
解密:curl http://localhost:8080/decrypt -d
767552394ee539201214199d55b0a91b1485d959b667eeabad29a101b9c1c61e
在git仓库增加配置文件ms-config-encryption-dev.yml,内容如下:

Spring Cloud Config 配置中心
访问地址:http://localhost:8080/ms-config-encryption-dev.yml,得到**原文,说明 config server能自动解密配置内容。一些场景下,想要让 config server直接返回密文本身,而并非解密后的内容,可设置spring.cloud.config.server.encrypt.enabled=false,这时可由 ConfigCIient自行解密。


7、配置信息手动刷新
很多场景下,需要在运行期间动态调整配置。如果配置发生了修改,微服务要如何实现配置的刷新呢?
见示例:10-ms-config-client-refresh 添加依赖如下,其中spring-boot-starter-actuator提供了/refresh端点,用于配置的刷新

Spring Cloud Config 配置中心
在Controller上添加注解@RefreshScope,添加这个注解的类会在配置更改时得到特殊的处理

Spring Cloud Config 配置中心
1、启动项目(启动两个,一个端口8081,一个端口8082),访问地址:http://localhost:8081/profile,得到结果:dev-1.0,访问地址:http://localhost:8082/profile,得到结果:dev-1.0
2、修改git仓库里的配置文件ms-config-dev.properties的内容为:profile=dev1.0change
3、再次访问地址:http://localhost:8081/profile,得到结果还是dev-1.0,说明配置尚未刷新
4、发送post请求:http://localhost:8081/refresh,返回结果:"profile",表示profile这个配置属性已被刷新
5、再次访问http://localhost:8081/profile,得到结果:dev-1.0-change,说明属性已刷新
6、再次访问http://localhost:8082/profile,得到结果:dev-1.0,说明8082的服务并没有刷新,还需再次手动刷新才能更新配置


8、配置信息自动刷新
前文讨论了使用/refresh端点手动刷新配置,但如果所有微服务节点的配置都需要手动去刷新,工作量可想而知。不仅如此,随着系统的不断扩张,会越来越难以维护。因此,实现配置的自动刷新是很有必要的,Spring Cloud Bus就可以实现配置的自动刷新。Spring Cloud Bus使用轻量级的消息代理(例如 RabbitMQ、 Kafka等)连接分布式系统的节点,这样就可以广播传播状态的更改(例如配置的更新)或者其他的管理指令。可将Spring Cloud Bus想象成一个分布式的Spring Boot Actuator。

Spring Cloud Config 配置中心
见示例:10-ms-config-server-refresh-cloud-bus和10-ms-config-client-refresh-cloud-bus
服务端示例:10-ms-config-server-refresh-cloud-bus 添加依赖

Spring Cloud Config 配置中心
配置文件增加rabbitmq的配置:

Spring Cloud Config 配置中心
客户端示例:10-ms-config-client-refresh-cloud-bus 添加依赖

Spring Cloud Config 配置中心
配置文件增加rabbitmq的配置:

Spring Cloud Config 配置中心
运行项目(运行一个config server和两个config client),修改git仓库里的配置文件,然后用post方式请求地址:http://localhost:8080/bus/refresh,如果返回成功,则config的所有客户端的配置都会动态刷新,详细演示参看视频


9、config的安全认证
见示例:10-ms-config-server-authenticating和10-ms-config-clientauthenticating
config服务端:10-ms-config-server-authenticating 添加依赖:

Spring Cloud Config 配置中心
application.yml配置如下:

Spring Cloud Config 配置中心
config客户端:10-ms-config-client-authenticating 在配置文件里bootstrap.yml里增加如下配置:

Spring Cloud Config 配置中心


config与eureka配合使用。见示例:10-ms-config-server-eureka和10-ms-config-client-eureka


10、config配置中心的高可用
1、config server未注册到eureka上的情况,通过负载均衡器来实现

Spring Cloud Config 配置中心
2、config server注册到eureka上的情况,client端也注册到eureka上,则已经实现高可用

相关文章:

  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2021-08-14
  • 2021-11-02
  • 2018-04-12
  • 2020-04-10
猜你喜欢
  • 2021-11-07
  • 2021-11-23
  • 2021-11-20
  • 2022-12-23
  • 2021-07-23
相关资源
相似解决方案