為了避免更改配置后重新啟動應(yīng)用,本文介紹了SpringBoot應(yīng)用在運行時重新加載配置的方法。
方案一: 使用spring-cloud-starter-config實現(xiàn)
spring cloud config 不僅可以刷新遠(yuǎn)程git的配置到內(nèi)存中,而且還可以結(jié)合actuator還可以刷新本地的配置文件到內(nèi)存中。
- 添加 maven 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--用于監(jiān)控和管理應(yīng)用自身信息, 可以動態(tài)refresh配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--用于配置管理-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
其中:
(1)spring-cloud-starter-config是為了實現(xiàn)刷新配置
(2)spring-boot-starter-actuator是為了暴露修改/刷新配置的接口
- 配置actuator 端點
management:
server:
port: 9001
endpoints:
web:
exposure:
include: "*"
- 便寫獲取配置的測試類
測試使用@Enviroment 、 @Value、自定義配置類三種方式
@RestController
@RequestMapping("/api/v1/config")
@RefreshScope
public class ConfigController {
@Autowired
ConfigProperties configProperties;
@Autowired
Environment environment;
@Value("${server.port}")
private String port;
@GetMapping("/getConfig")
public Map<String, Object> getConfig() {
Map<String, Object> data = new HashMap<>();
data.put("AppNameInConfigProperties", configProperties.getAppName());
data.put("PortInEnvironment", environment.getProperty("server.port"));
data.put("PortInValue", port);
return data;
}
}
package com.mochi.example.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix="mochi")
@Component
@RefreshScope
public class ConfigProperties {
private String appName;
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
}
配置項如下:
server:
port: 9311
mochi:
appName: mochi-app-test
4.1. 使用HTTP請求動態(tài)更改配置
使用 curl -X GET 'http://localhost:9311/api/v1/config/getConfig' 查看配置信息
{
AppNameInConfigProperties: "mochi-app-test",
PortInEnvironment: "9311",
PortInValue: "9311"
}
然后使用用 POST 請求訪問 http://localhost:9001/actuator/env 來修改具體配置;使用 POST訪問http://localhost:9001/actuator/refresh 進(jìn)行刷新
curl -X POST -H "Content-Type:application/json" --data "{\"name\": \"mochi.appName\", \"value\": \"newappname\"}" 'http://localhost:9001/actuator/env'
curl -X POST 'http://localhost:9001/actuator/refresh'
重新獲取配置信息,發(fā)現(xiàn)已經(jīng)更新成最新值了
{
AppNameInConfigProperties: "newappname",
PortInEnvironment: "9311",
PortInValue: "9311"
}
4.2 直接更改配置文件來動態(tài)刷新配置
更改 target/classes/application.yaml 中的配置,然后使用 POST訪問http://localhost:9001/actuator/refresh 進(jìn)行刷新即可
{
AppNameInConfigProperties: "mochi-app-test-new2",
PortInEnvironment: "9311",
PortInValue: "9311"
}
方案二: 使用監(jiān)聽配置文件變動來實現(xiàn)
主要使用FileAlterationMonitor、FileAlterationObserver、FileAlterationListenerAdapter 來實現(xiàn),主要流程如下,其中FileListenerAdaptor需要實現(xiàn)FileAlterationListenerAdapter的 onChange方法:
// 構(gòu)造觀察類主要提供要觀察的文件或目錄,當(dāng)然還有詳細(xì)信息的filter
FileAlterationObserver observer = new FileAlterationObserver(
new File(configPath), null, null);
// 構(gòu)造收聽類
FileListenerAdaptor listener = new FileListenerAdaptor();
// 為觀察對象添加收聽對象
observer.addListener(listener);
// 配置Monitor,第一個參數(shù)單位是毫秒,是監(jiān)聽的間隔;第二個參數(shù)就是綁定我們之前的觀察對象。
FileAlterationMonitor fileMonitor = new FileAlterationMonitor(
1000, new FileAlterationObserver[]{observer});
// 啟動開始監(jiān)聽
fileMonitor.start();
更多干貨,歡迎關(guān)注個人博客:http://mochi.website/