Spring Cloud Config:统一管理微服务配置

一. 简介

1. 微服务的配置管理一般有以下需求:

集中管理配置;

不同环境,不同配置;

运行期间可动态调整;

配置修改后可自动更新;

2. 简介

Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,包括Config Server 和Config Client两部分;

Config Server是一个可横向扩展,集中式的配置服务器,用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容;

Config Client是Config Server的客户端,用于操作存储在Config Server中的配置属性;

3. 编写Config Server

在Git仓库中新增几个配置文件;

添加spring-cloud-config-server依赖;

在启动类上添加@EnableConfigServer注解;

在配置文件中配置git相关参数;

4. 编写Config Client

添加spring-cloud-config-client依赖;

创建配置文件bootstrap.yml:

  spring.application.name:对应Config Server所获取的配置文件中的{application};

  spring.cloud.config.uri:指定Config Server的地址;

  spring.cloud.config.profile:profile对应Config Server所获取的配置文件中的{profile};

  spring.cloud.config.label:指定Git仓库分支,对应Config Server所获取的配置文件的{label};

5. 健康状况指示器

Config Server自带了一个健康状况指示器,用于检查所配置的EnvironmentRepository是否正常工作;可使用Config Server的/health端点查询当前健康状态;

6. 刷新配置

手动刷新:

  添加spring-boot-starter-actuator依赖,该依赖包含/refresh端点,用于配置的刷新;

  在Controller上添加@RefreshScope注解,在配置更改时得到特殊的处理;

自动刷新:

  Spring Cloud Bus使用轻量级的消息代理连接分布式系统的节点,这样就可以广播传播状态的更改或者其他的管理指令;

  将Config Server加入消息总线中,使用Config Server的/bus/refresh端点来实现配置的刷新;

猜你喜欢

转载自www.cnblogs.com/bbbbs/p/12566578.html
今日推荐