Spring Cloud Config 整合 Spring Cloud Bus 同步遠端配置
阿新 • • 發佈:2019-04-03
軟體版本:
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 重新整理配置資訊端點類: 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