軟件版本:html
Spring Boot - 2.1.3.RELEASEjava
Spring Cloud - Greenwich.SR1web
Spring Config Server 端依賴:spring
<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 配置文件配置 :sql
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 官方文檔說明: json
Spring Cloud Bus 關於刷新配置信息端點bootstrap
Spring Cloud Bus 刷新配置信息端點類: org.springframework.cloud.bus.endpoint.RefreshBusEndpointapp
當配置發生變動後請求 Config Server 端的刷新配置端點 : http://host:port/actuator/bus-refresh/ , 請求方法必須是 POST ,Content-Type : application/json 。這樣 Config Server 會將內存中的 Environment 中存儲的配置信息與外部存儲中的配置信息進行比對 , 將發生變化的信息同步到內存中,同時經過 Spring Cloud Bus 將變化的配置信息以消息的方式發送到消息中間件中。Config Client 接收到該消息後就會將變化的配置項進行更新。ui
Spring Config Client 端依賴:spa
<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