spring cloud config和bus組件實現自動刷新功能
1.啟動RabbitMQ
2.修改config服務端 依賴:(boot和cloud版本不要太高,不然刷新配置的請求不能用)
<dependencies> <!--consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--健康檢查--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--config--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--bus--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
配置:
spring: cloud: consul: host: localhost port: 8500 config: server: git: uri: https://gitee.com/xiang_pei/config.git default-label: master application: name: CONFIGSERVER rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: / #暴露所有端點,便於發送更新配置請求 management: endpoints: web: exposure: include: "*"
啟動:(啟動類加@EnableConfigServer)mq會有一條臨時隊列
3.修改微服務(即客戶端) 依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--config client的依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--重新導入bootstrap--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <!--bus--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </dependencies>
配置:不變(rabbitmq的配置應該寫到gitee上)bootstrap.properties
spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.application.name=CONFIGCLIENT spring.cloud.config.discovery.service-id=CONFIGSERVER spring.cloud.config.discovery.enabled=true spring.cloud.config.name=config spring.cloud.config.label=master spring.cloud.config.profile=dev
運行:
寫瞭一個測試方法
name屬性是gitee上的配置文件的name
沒問題
4.利用webhooks實現自動刷新配置
向config服務端發送post請求
http://localhost:8080/actuator/bus-refresh
具體路徑根據項目來
在gitee的config管理中,點擊webhooks
url就填上面那個,因為需要公網才能測試,所以暫時不演示瞭。
設置完,改gitee的配置就會自動刷新瞭
到此這篇關於spring cloud config和bus組件實現自動刷新功能的文章就介紹到這瞭,更多相關spring cloud config自動刷新內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- SpringCloud整合Consul的實現
- Spring Cloud Consul的服務註冊與發現
- 解析Spring Cloud Bus消息總線
- spring cloud如何集成nacos配置中心
- SpringCloud微服務之Config知識總結