SpringCloud(第 036 篇)單點手動動態刷新ConfigClient配置
一、大致介紹
1、當ConfigServer啟動后,假如我們新增配置內容的話,是不是要重新啟動一下ConfigServer呢?
2、答案肯定是不需要重新啟動的,因為 SpringCloud 給我們提供了一個刷新的觸發機制,這樣便可以在不重新的情況下重新加載最新配置文件內容;
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</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>
</dependencies>
</project>
2.2 添加應用配置文件(springms-config-client-refresh/src/main/resources/application.yml)
server:
port: 8295
#####################################################################################################
# 配置服務客戶端Client應用入口(正常測試 ConfigClient )
# profile: profile-dev
#####################################################################################################
#####################################################################################################
# 配置服務客戶端Client應用入口(鏈接 ClientServer 測試,同時本地也有一份配置文件,那么該如何抉擇呢?)
# profile: profile-local-dev
#####################################################################################################
2.3 添加 bootstrap.yml 應用配置文件(springms-config-client-refresh/src/main/resources/bootstrap.yml)
#####################################################################################################
# 配置服務客戶端Client應用入口(鏈接 ClientServer 測試)
spring:
cloud:
config:
uri: http://localhost:8220
profile: refresh
label: master #當 ConfigServer 的后端存儲的是 Git 的時候,默認就是 master
application:
name: foobar #取 foobar-refresh.yml 這個文件的 application 名字,即為 foobar 名稱
#####################################################################################################
2.4 添加Web控制層類(springms-config-client-refresh/src/main/java/com/springms/cloud/controller/ConfigClientRefreshController.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 ConfigClientRefreshController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String getProfile(){
return this.profile;
}
}
2.5 添加應用啟動類(springms-config-client-refresh/src/main/java/com/springms/cloud/MsConfigClientRefreshApplication.java)
package com.springms.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 單點手動動態刷新ConfigClient配置。<br/>
*
* ConfigClient 配置客戶端服務想要實現自動刷新配置的話,ConfigServer 一端是不要做任何處理,只需要在 ConfigClient 一端處理即可。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 17/10/18
*
*/
@SpringBootApplication
public class MsConfigClientRefreshApplication {
public static void main(String[] args) {
SpringApplication.run(MsConfigClientRefreshApplication.class, args);
System.out.println("【【【【【【 ConfigClientRefresh微服務 】】】】】】已啟動.");
}
}
三、測試
/****************************************************************************************
application.yml 涉及到的鏈接文件內容展示如下:
修改內容前:
http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refresh.yml
profile: profile-refresh
修改內容后:
http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refresh.yml
profile: profile-refresh-refresh
****************************************************************************************/
/****************************************************************************************
一、配置刷新服務客戶端Client應用入口(單點手動動態刷新配置服務客戶端配置):
1、添加注解 RefreshScope,然后添加引用模塊 spring-boot-starter-actuator 監控和管理生產環境的模塊;
2、編輯 application.yml 文件,添加相關客戶端配置;
spring:
cloud:
config:
uri: http://localhost:8220
profile: refresh
label: master #當 ConfigServer 的后端存儲的是 Git 的時候,默認就是 master
application:
name: foobar #取 foobar-refresh.yml 這個文件的 application 名字,即為 foobar 名稱
3、啟動 springms-config-server 模塊服務,啟動1個端口;
4、啟動 springms-config-client-refresh 模塊服務,啟動1個端口;
5、在瀏覽器輸入地址 http://localhost:8295/profile 正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refresh);
6、修改 http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refresh.yml 內容,修改后為 profile: profile-refresh-refresh;
7、打開windows命令窗口,執行命令: >curl.exe -X POST http://localhost:8295/refresh
8、然后刷新 http://localhost:8295/profile 網頁,正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refresh-refresh);
總結:這里通過執行刷新命令才得以將遠端配置內容刷新到配置服務客戶端。
****************************************************************************************/
四、下載地址
<font color=#4183C4 size=4>https://gitee.com/ylimhhmily/SpringCloudTutorial.git</font>
<font color=#4183C4 size=4>SpringCloudTutorial交流QQ群: 235322432</font>、<font color=#4183C4 size=4>微信溝通交流群</font>
<font color=red size=4>歡迎關注,您的肯定是對我最大的支持!!!</font>
SpringCloud(第 036 篇)單點手動動態刷新ConfigClient配置
一、大致介紹
1、當ConfigServer啟動后,假如我們新增配置內容的話,是不是要重新啟動一下ConfigServer呢?
2、答案肯定是不需要重新啟動的,因為 SpringCloud 給我們提供了一個刷新的觸發機制,這樣便可以在不重新的情況下重新加載最新配置文件內容;
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</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>
</dependencies>
</project>
2.2 添加應用配置文件(springms-config-client-refresh/src/main/resources/application.yml)
server:
port: 8295
#####################################################################################################
# 配置服務客戶端Client應用入口(正常測試 ConfigClient )
# profile: profile-dev
#####################################################################################################
#####################################################################################################
# 配置服務客戶端Client應用入口(鏈接 ClientServer 測試,同時本地也有一份配置文件,那么該如何抉擇呢?)
# profile: profile-local-dev
#####################################################################################################
2.3 添加 bootstrap.yml 應用配置文件(springms-config-client-refresh/src/main/resources/bootstrap.yml)
#####################################################################################################
# 配置服務客戶端Client應用入口(鏈接 ClientServer 測試)
spring:
cloud:
config:
uri: http://localhost:8220
profile: refresh
label: master #當 ConfigServer 的后端存儲的是 Git 的時候,默認就是 master
application:
name: foobar #取 foobar-refresh.yml 這個文件的 application 名字,即為 foobar 名稱
#####################################################################################################
2.4 添加Web控制層類(springms-config-client-refresh/src/main/java/com/springms/cloud/controller/ConfigClientRefreshController.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 ConfigClientRefreshController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String getProfile(){
return this.profile;
}
}
2.5 添加應用啟動類(springms-config-client-refresh/src/main/java/com/springms/cloud/MsConfigClientRefreshApplication.java)
package com.springms.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 單點手動動態刷新ConfigClient配置。<br/>
*
* ConfigClient 配置客戶端服務想要實現自動刷新配置的話,ConfigServer 一端是不要做任何處理,只需要在 ConfigClient 一端處理即可。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 17/10/18
*
*/
@SpringBootApplication
public class MsConfigClientRefreshApplication {
public static void main(String[] args) {
SpringApplication.run(MsConfigClientRefreshApplication.class, args);
System.out.println("【【【【【【 ConfigClientRefresh微服務 】】】】】】已啟動.");
}
}
三、測試
/****************************************************************************************
application.yml 涉及到的鏈接文件內容展示如下:
修改內容前:
http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refresh.yml
profile: profile-refresh
修改內容后:
http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refresh.yml
profile: profile-refresh-refresh
****************************************************************************************/
/****************************************************************************************
一、配置刷新服務客戶端Client應用入口(單點手動動態刷新配置服務客戶端配置):
1、添加注解 RefreshScope,然后添加引用模塊 spring-boot-starter-actuator 監控和管理生產環境的模塊;
2、編輯 application.yml 文件,添加相關客戶端配置;
spring:
cloud:
config:
uri: http://localhost:8220
profile: refresh
label: master #當 ConfigServer 的后端存儲的是 Git 的時候,默認就是 master
application:
name: foobar #取 foobar-refresh.yml 這個文件的 application 名字,即為 foobar 名稱
3、啟動 springms-config-server 模塊服務,啟動1個端口;
4、啟動 springms-config-client-refresh 模塊服務,啟動1個端口;
5、在瀏覽器輸入地址 http://localhost:8295/profile 正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refresh);
6、修改 http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refresh.yml 內容,修改后為 profile: profile-refresh-refresh;
7、打開windows命令窗口,執行命令: >curl.exe -X POST http://localhost:8295/refresh
8、然后刷新 http://localhost:8295/profile 網頁,正常情況下會輸出遠端服務的配置內容(內容為:profile: profile-refresh-refresh);
總結:這里通過執行刷新命令才得以將遠端配置內容刷新到配置服務客戶端。
****************************************************************************************/
四、下載地址
https://git.oschina.net/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關注,您的肯定是對我最大的支持!!!