Spring Cloud Config(配置中心)就是把应用原本放在本地文件的配置抽取出来放在中心服务器,从而能够提供更好的管理、发布能力。
提供基于以下3个维度的配置管理:
应用
每个配置都是属于某一个应用的
环境
每个配置都是区分环境的,如dev, test, prod等
版本
对同一份配置的不同版本管理,比如:可以通过Git进行版本控制。
Spring Cloud Config提供版本的支持,也就是说对于一个应用的不同部署实例,可以从服务端获取到不同版本的配置,
这对于一些特殊场景如:灰度发布,A/B测试等提供了很好的支持。
Spring Cloud Config
Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system.
With the Config Server you have a central place to manage external properties for applications across all environments.
Spring Cloud Config Server features:
HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content)
Encrypt and decrypt property values (symmetric or asymmetric)
Embeddable easily in a Spring Boot application using @EnableConfigServer
Config Client features (for Spring applications):
Bind to the Config Server and initialize Spring Environment with remote property sources
Encrypt and decrypt property values (symmetric or asymmetric)
Config Server 配置中心服务端
Config Server是需要独立部署的一个web应用,它负责把git上的配置返回给客户端
pom.xml中添加依赖
1 | <dependencies> |
启动类中添加@EnableEurekaServer注解
1 | @EnableConfigServer |
yml配置文件(本地配置)
1 | server: |
yml配置文件(git远程仓库配置)
1 | server: |
启动工程
在浏览器访问 http://localhost:8888/application.yml ,可看到相关配置文件的内容
Config Client 配置中心客户端
Config Client就是使用了Spring Cloud Config的应用
Spring Cloud Config提供了基于Spring的客户端,应用只要在代码中引入Spring Cloud Config Client的jar包即可工作。
pom.xml中添加依赖
1 | <dependencies> |
远程Git仓库 (Remote Git Repository)
远程Git仓库,一般而言,我们会把配置放在一个远程仓库,通过现成的git客户端来管理配置
本地Git仓库 (Local Git Repository)
Config Server的本地Git仓库
Config Server接到来自客户端的配置获取请求后,会先把远程仓库的配置clone到本地的临时目录,然后从临时目录读取配置并返回