Spring Cloud Config 集成 Spring Cloud Bus 同步远程配置

原创
2019/04/02 23:18
阅读数 720

软件版本:

Spring Boot - 2.1.3.RELEASE

Spring Cloud - Greenwich.SR1

 

Spring Cloud Config Server 端

Spring Config Server 端依赖:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-server</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

 

bootstrap.properties 配置文件配置 :

spring.application.name=config-server
server.port=8000

# 这里我使用了 Spring Cloud Config 支持的 JDBC Backend 配置项存储方式 
# 该配置文件中省略了 JDBC 的配置
spring.cloud.config.server.jdbc.sql=select `key` , `value` from config_properties where application = ? and profile = ? and label = ?


spring.rabbitmq.addresses=localhost:5672


# 关键, 开启对应的端点 
management.endpoints.web.exposure.include=bus-refresh,bus-env

 

Spring Cloud Bus 官方文档说明: 

Spring Cloud Bus 关于刷新配置信息端点

Spring Cloud Bus 刷新配置信息端点类: org.springframework.cloud.bus.endpoint.RefreshBusEndpoint

当配置发生变更后请求 Config Server 端的刷新配置端点 : http://host:port/actuator/bus-refresh/    , 请求方法必须是 POST ,Content-Type : application/json 。这样 Config Server 会将内存中的 Environment 中存储的配置信息与外部存储中的配置信息进行比对 , 将发生变化的信息同步到内存中,同时通过 Spring Cloud Bus 将变化的配置信息以消息的方式发送到消息中间件中。Config Client 接收到该消息后就会将变化的配置项进行更新。

 

Spring Cloud Config Client 端

Spring Config Client 端依赖:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-client</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-bus-amqp</artifactId>
 </dependency>

 

bootstrap.properties 配置文件配置 :

spring.application.name=demo-service

spring.cloud.config.uri=http://localhost:8000

spring.rabbitmq.addresses=localhost:5672

spring.cloud.config.fail-fast=true

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部