Spring Cloud Config 是 Spring Cloud 微服务体系中的配置中心,是微服务中不可或缺的一部分,其能够很好的将程序中配置日益增多的各种功能的开关、参数的配置、服务器的地址等配置修改后实时生效、灰度发布,分环境、分集群管理配置等进行全面的集中化管理,有利于系统的配置管理、维护。
spring cloud 配置中心功能图
配置中心各流程流转如图:
Spring cloud 配置中心流转图
配置中心的支撑体系大致有两类
开发管理体系运维管理体系
Spring 开发、运维体系
Spring Cloud Config 是一个集中化、外部配置的分布式系统,由服务端、客户端组成,它不依赖于注册中心,是一个独立的配置中心。Spring Cloud Config 支持多种存储配置信息的形式,目前主要有 jdbc、vault、Navicat、svn、git 等形式,默认为 git。
配置客户端启动时,会向服务端发起请求,服务端接收到客户端的请求后,根据配置的仓库地址,将 git 上的文件克隆到本地的一个临时目录中,这个目录是一个 git 的本地仓库,然后服务端再读取本地文件,返回给客户端。这样做的好处是:当 git 服务故障或网络请求异常时,保证服务端依然能正常工作。
Spring Cloud Config git 版工作原理
使用 git 做配置中心的配置文件存储,需要一个 git 仓库,用于保存配置文件。 本例仓库地址: https://gitee.com/laiyy0728/config-repo
在仓库中,新建一个文件夹:config-simple,在文件夹内新建 3 个文件:config-simple-dev.yml、config-simple-test.yml、config-simple-prod.yml
Spring Cloud Simple Config
源码:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-config/spring-cloud-config-simple
启动 config server,查看 endpoints mappings
Spring Cloud Config Endpoints
label:代表请求的是哪个分支,默认是 master 分支name:代表请求哪个名称的远程文件profile:代表哪个版本的文件,如:dev、test、prod 等从 mappings 中,可以看出,访问获取一个配置的信息,有多种方式,尝试获取 /config-simple/config-simple.dev.yml 配置信息:
http://localhost:9090/config-simple/dev/master 、http://localhost:9090/config-simple/dev
{ "name": "config-simple", "profiles": [ "dev" ], "label": "master", "version": "520b379e9c7f2e39bb56e599f914b6c08fe13c06", "state": null, "propertySources": [{ "name": "https://gitee.com/laiyy0728/config-repo/config-simple/config-simple-dev.yml", "source": { "com.laiyy.gitee.config": "dev 环境,git 版 spring cloud config" } }] }http://localhost:9090/master/config-simple-dev.yml 、http://localhost:9090/config-simple-dev.yml
com: laiyy: gitee: config: dev 环境,git 版 spring cloud config在 config server 中获取配置文件以及成功,接下来需要在 config client 中,通过 config server 获取对应的配置文件
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> </dependencies>application.yml
spring: cloud: config: label: master uri: http://localhost:9090 name: config-simple profile: dev application: name: spring-cloud-config-simple-client server: port: 9091 // 用于从远程 config server 获取配置文件内容 @Component @ConfigurationProperties(prefix = "com.laiyy.gitee") public class ConfigInfoProperties { private String config; public String getConfig() { return config; } public void setConfig(String config) { this.config = config; } } // 用于打印获取到的配置文件内容 @RestController public class ConfigController { private final ConfigInfoProperties configInfoProperties; @Autowired public ConfigController(ConfigInfoProperties configInfoProperties) { this.configInfoProperties = configInfoProperties; } @GetMapping(value = "/get-config-info") public String getConfigInfo(){ return configInfoProperties.getConfig(); } } // 启动类 @SpringBootApplication public class SpringCloudConfigSimpleClientApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigSimpleClientApplication.class, args); } }启动 config client,观察控制台,发现 config client 拉取配置的路径是:http://localhost:8888 ,而不是在 yml 中配置的 localhost:9090。这是因为 boot 启动时加载配置文件的顺序导致的。boot 默认先加载 bootstrap.yml 配置,再加载 application.yml 配置。所以需要将 config server 配置移到 bootstrap.yml 中
Config client default fetch server
bootstrap.yml
spring: cloud: config: label: master # 代表请求 git 哪个分支,默认 master uri: http://localhost:9090 # config server 地址 name: config-simple # 获取哪个名称的远程文件,可以有多个,英文逗号隔开 profile: dev # 代表哪个分支application.yml
spring: application: name: spring-cloud-config-simple-client server: port: 9091config client remote server
访问 http://localhost:9091/get-config-info
get config info
作者:laiyy0728 链接:https://www.jianshu.com/p/588084c959f5 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。