一、上章節咱們講到了手動刷新配置,可是咱們假設若是微服務一多的話,那麼咱們是否是須要對每臺服務進行手動刷新呢? 二、答案確定是不須要的,咱們也能夠採用 rabbitmq 消息中間件產品來加強刷新機制; 三、這裏還順便列舉下配置路徑的規則: /**************************************************************************************** * 配置服務的路勁規則: * * /{application}/{profile}[/{label}] * /{application}-{profile}.yml * /{label}/{application}-{profile}.yml * /{application}-{profile}.properties * /{label}/{application}-{profile}.properties ****************************************************************************************/
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springms-config-client-refresh-bus</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.springms.cloud</groupId> <artifactId>springms-spring-cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <!-- 客戶端配置模塊 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- web模塊 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 監控和管理生產環境的模塊 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Rabbitmq模塊 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </dependencies> </project>
server: port: 8300 ##################################################################################################### # 配置服務客戶端Client應用入口(正常測試 ConfigClient ) # profile: profile-dev ##################################################################################################### ##################################################################################################### # 配置服務客戶端Client應用入口(連接 ClientServer 測試,同時本地也有一份配置文件,那麼該如何抉擇呢?) # profile: profile-local-dev #####################################################################################################
##################################################################################################### # 配置服務客戶端Client應用入口(連接 ClientServer 測試) spring: cloud: config: uri: http://localhost:8220 profile: refreshbus label: master #當 ConfigServer 的後端存儲的是 Git 的時候,默認就是 master bus: trace: enabled: true # 設置節點狀態跟蹤,也能夠經過網頁 http://localhost:8300/trace 能夠看到相關發送事件的數據內容 application: name: foobar #取 foobar-refreshbus.yml 這個文件的 application 名字,即爲 foobar 名稱 ##################################################################################################### ##################################################################################################### # rabbitmq 配置: rabbitmq: host: localhost # 登陸 Rabbitmq 後臺管理頁面地址爲:http://localhost:15672 port: 5672 username: guest # 默認帳戶 password: guest # 默認密碼 #####################################################################################################
package com.springms.cloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 配置客戶端Controller。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/10/18 * */ @RestController @RefreshScope public class ConfigClientRefreshBusController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getProfile(){ return this.profile; } }
package com.springms.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 經過bus/refresh半自動刷新ConfigClient配置。<br/> * * ConfigClient 配置客戶端服務想要實現自動刷新配置的話,ConfigServer 一端是不要作任何處理,只須要在 ConfigClient 一端處理便可。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/10/18 * */ @SpringBootApplication public class MsConfigClientRefreshBusApplication { public static void main(String[] args) { SpringApplication.run(MsConfigClientRefreshBusApplication.class, args); System.out.println("【【【【【【 ConfigClientRefreshBus微服務 】】】】】】已啓動."); } }
/**************************************************************************************** application.yml 涉及到的連接文件內容展現以下: 修改內容前: http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refreshbus.yml profile: profile-refreshbus 修改內容後: http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refreshbus.yml profile: profile-refreshbus-refresh ****************************************************************************************/ /**************************************************************************************** Rabbitmq 安裝步驟(進入 Rabbitmq 官網:http://www.rabbitmq.com): 一、下載 rabbitmq-server-3.6.11.exe、otp_win64_20.0-rc2.exe 兩個 windows 安裝軟件; 二、雙擊安裝 otp_win64_20.0-rc2.exe; 三、雙擊安裝 rabbitmq-server-3.6.11.exe; 四、兩個都安裝完後會發現服務中多了一個 Rabbitmq 的服務,服務名稱爲:RabbitMQ; 五、若是想查看管理界面的話,執行命令:rabbitmq-plugins enable rabbitmq_management,而後重啓 RabbitMQ 服務; 六、經過windows命令 netstat -aon|findstr "5672" 查看該端口是否被佔用,佔用的話,說明安裝基本上一切正常; 七、經過 http://localhost:15672 地址能夠進入服務端的管理頁面; 總結:到此爲止,Rabbitmq 已經安裝完成,接下來準備接入 SpringCloud 生態圈。 ****************************************************************************************/ /**************************************************************************************** 1、配置刷新服務客戶端Client應用入口(經過 bus/refresh 實現半自動動態刷新配置服務客戶端配置): 一、添加註解 RefreshScope,而後添加引用模塊 spring-boot-starter-actuator 監控和管理生產環境的模塊; 二、編輯 application.yml、bootstrap.yml 文件,添加相關客戶端配置; 三、啓動 springms-config-server 模塊服務,啓動1個端口; 四、啓動 springms-config-client-refresh-bus 模塊服務,啓動3個端口(8300、830一、8302); 五、在瀏覽器輸入地址 http://localhost:8300/profile 正常狀況下會輸出遠端服務的配置內容(內容爲:profile: profile-refreshbus); 六、在瀏覽器輸入地址 http://localhost:8301/profile 正常狀況下會輸出遠端服務的配置內容(內容爲:profile: profile-refreshbus); 七、在瀏覽器輸入地址 http://localhost:8302/profile 正常狀況下會輸出遠端服務的配置內容(內容爲:profile: profile-refreshbus); 八、修改 http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refreshbus.yml 內容,修改後爲 profile: profile-refreshbus-refresh; 九、打開windows命令窗口,執行命令: >curl.exe -X POST http://localhost:8300/bus/refresh 或者端口選擇 830一、8302 均可以生效; 十、而後刷新 http://localhost:8300/profile 網頁,正常狀況下會輸出遠端服務的配置內容(內容爲:profile: profile-refreshbus-refresh); 十一、而後刷新 http://localhost:8301/profile 網頁,正常狀況下會輸出遠端服務的配置內容(內容爲:profile: profile-refreshbus-refresh); 十二、而後刷新 http://localhost:8302/profile 網頁,正常狀況下會輸出遠端服務的配置內容(內容爲:profile: profile-refreshbus-refresh); 總結:這裏經過執行刷新命令,而後將多臺 ConfigClient 客戶端刷新,來達到獲取最新的遠端服務器配置。 可是這裏終究仍是得靠手動執行一條刷新命令,但總比每臺服務器執行刷新命令要好不少; ****************************************************************************************/ /**************************************************************************************** 2、配置刷新服務客戶端Client應用入口(設置 Git 的 WebHooks 屬性,經過 Git 提交代碼來實現全自動動態刷新配置服務客戶端配置): 總結:這裏我就不作過多的測試,WebHooks 能夠設置 POST 的地址,並附上密碼,提交代碼後動態通知相應服務來實現全自動動態刷新。 ****************************************************************************************/ /**************************************************************************************** 3、思考問題:憑什麼 8300、830一、8302 三臺服務器其中一臺要承受刷新配置服務的任務?不該該三臺服務的角色等級應該相同麼? 基於這種角色等同考慮,能夠在 ConfigServer 也配上 Rabbitmq 連接上,而後咱們在用命令刷新 ConfigServer 便可,這樣就實現了三臺 ConfigClient 服務器的角色又等同了; ****************************************************************************************/
https://gitee.com/ylimhhmily/SpringCloudTutorial.gitjava
SpringCloudTutorial交流QQ羣: 235322432git
SpringCloudTutorial交流微信羣: 微信溝通羣二維碼圖片連接web
歡迎關注,您的確定是對我最大的支持!!!spring