SpringBoot 配置項動態(tài)實時刷新

為了避免更改配置后重新啟動應(yīng)用,本文介紹了SpringBoot應(yīng)用在運行時重新加載配置的方法。

方案一: 使用spring-cloud-starter-config實現(xiàn)

spring cloud config 不僅可以刷新遠(yuǎn)程git的配置到內(nèi)存中,而且還可以結(jié)合actuator還可以刷新本地的配置文件到內(nèi)存中。

  1. 添加 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是為了暴露修改/刷新配置的接口

  1. 配置actuator 端點
management:
  server:
    port: 9001
  endpoints:
    web:
      exposure:
        include: "*"
  1. 便寫獲取配置的測試類
    測試使用@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/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容