SpringCloud(第 037 篇)通過bus/refresh半自動刷新ConfigClient配置
一、大致介紹
1、上章節我們講到了手動刷新配置,但是我們假設如果微服務一多的話,那么我們是不是需要對每臺服務進行手動刷新呢?
2、答案肯定是不需要的,我們也可以采用 rabbitmq 消息中間件產品來增強刷新機制;
3、這里還順便列舉下配置路徑的規則:
/****************************************************************************************
* 配置服務的路勁規則:
*
* /{application}/{profile}[/{label}]
* /{application}-{profile}.yml
* /{label}/{application}-{profile}.yml
* /{application}-{profile}.properties
* /{label}/{application}-{profile}.properties
****************************************************************************************/
二、實現步驟
2.1 添加 maven 引用包
<?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>
2.2 添加應用配置文件(springms-config-client-refresh-bus/src/main/resources/application.yml)
server:
port: 8300
#####################################################################################################
# 配置服務客戶端Client應用入口(正常測試 ConfigClient )
# profile: profile-dev
#####################################################################################################
#####################################################################################################
# 配置服務客戶端Client應用入口(鏈接 ClientServer 測試,同時本地也有一份配置文件,那么該如何抉擇呢?)
# profile: profile-local-dev
#####################################################################################################
2.3 添加 bootstrap.yml 應用配置文件(springms-config-client-refresh-bus/src/main/resources/bootstrap.yml)
#####################################################################################################
# 配置服務客戶端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 # 默認密碼
#####################################################################################################
2.4 添加Web控制層類(springms-config-client-refresh-bus/src/main/java/com/springms/cloud/controller/ConfigClientRefreshBusController.java)
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;
}
}
2.5 添加應用啟動類(springms-config-client-refresh-bus/src/main/java/com/springms/cloud/MsConfigClientRefreshBusApplication.java)
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):
1、下載 rabbitmq-server-3.6.11.exe、otp_win64_20.0-rc2.exe 兩個 windows 安裝軟件;
2、雙擊安裝 otp_win64_20.0-rc2.exe;
3、雙擊安裝 rabbitmq-server-3.6.11.exe;
4、兩個都安裝完后會發現服務中多了一個 Rabbitmq 的服務,服務名稱為:RabbitMQ;
5、如果想查看管理界面的話,執行命令:rabbitmq-plugins enable rabbitmq_management,然后重啟 RabbitMQ 服務;
6、通過windows命令 netstat -aon|findstr "5672" 查看該端口是否被占用,占用的話,說明安裝基本上一切正常;
7、通過 http://localhost:15672 地址可以進入服務端的管理頁面;
總結:到此為止,Rabbitmq 已經安裝完成,接下來準備接入 SpringCloud 生態圈。
****************************************************************************************/
/****************************************************************************************
一、配置刷新服務客戶端Client應用入口(通過 bus/refresh 實現半自動動態刷新配置服務客戶端配置):
1、添加注解 RefreshScope,然后添加引用模塊 spring-boot-starter-actuator 監控和管理生產環境的模塊;
2、編輯 application.yml、bootstrap.yml 文件,添加相關客戶端配置;
3、啟動 springms-config-server 模塊服務,啟動1個端口;
4、啟動 springms-config-client-refresh-bus 模塊服務,啟動3個端口(8300、8301、8302);
5、在瀏覽器輸入地址 http://localhost:8300/profile 正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refreshbus);
6、在瀏覽器輸入地址 http://localhost:8301/profile 正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refreshbus);
7、在瀏覽器輸入地址 http://localhost:8302/profile 正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refreshbus);
8、修改 http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refreshbus.yml 內容,修改后為 profile: profile-refreshbus-refresh;
9、打開windows命令窗口,執行命令: >curl.exe -X POST http://localhost:8300/bus/refresh 或者端口選擇 8301、8302 都可以生效;
10、然后刷新 http://localhost:8300/profile 網頁,正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refreshbus-refresh);
11、然后刷新 http://localhost:8301/profile 網頁,正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refreshbus-refresh);
12、然后刷新 http://localhost:8302/profile 網頁,正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refreshbus-refresh);
總結:這里通過執行刷新命令,然后將多臺 ConfigClient 客戶端刷新,來達到獲取最新的遠端服務器配置。
但是這里終究還是得靠手動執行一條刷新命令,但總比每臺服務器執行刷新命令要好很多;
****************************************************************************************/
/****************************************************************************************
二、配置刷新服務客戶端Client應用入口(設置 Git 的 WebHooks 屬性,通過 Git 提交代碼來實現全自動動態刷新配置服務客戶端配置):
總結:這里我就不做過多的測試,WebHooks 可以設置 POST 的地址,并附上密碼,提交代碼后動態通知相應服務來實現全自動動態刷新。
****************************************************************************************/
/****************************************************************************************
三、思考問題:憑什么 8300、8301、8302 三臺服務器其中一臺要承受刷新配置服務的任務?不應該三臺服務的角色等級應該相同么?
基于這種角色等同考慮,可以在 ConfigServer 也配上 Rabbitmq 鏈接上,然后我們在用命令刷新 ConfigServer 即可,這樣就實現了三臺 ConfigClient 服務器的角色又等同了;
****************************************************************************************/
四、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關注,您的肯定是對我最大的支持!!!