nacos之配置文件實時刷新


當初為了解決nacos配置文件實時刷新問題,搜索了很多資料,仍無效,最后不經意間的嘗試卻解決了這個問題。

我的SpringCloud版本為:Hoxton.SR4;

我的SpringCloud Alibaba版本為:2.2.1.RELEASE;

我的Nacos版本為:1.3.1。

一、核心配置文件(一定要是bootstrap.yml,而非application.yml)

cloud:
  nacos:
    discovery:
      # 服務注冊地址
      server-addr: 127.0.0.1:8848
    config:
      # 配置中心地址
      server-addr: 127.0.0.1:8848
      # 配置文件格式
      file-extension: yml
      # 共享配置
      shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension},job.yml,test.properties
      refresh-enabled: true
      refreshable-dataids: job.yml,test.properties

上面是完整的,最關鍵的是如下兩行:

refresh-enabled: true
refreshable-dataids: job.yml,test.properties

二、讀取配置文件的類上,需加上@RefreshScope注解

@Component
@RefreshScope
@RestController
public class TestConfigJob {


    @Value("${job.live.cron}")
    private String val;

    @Autowired
    private Environment env;

    @Autowired
    private EqicsSaasApiProperties eqicsSaasApi;

    @Scheduled(cron = "1 * * * * ?")
    public void test() {
        System.out.println(DateUtil.date() + " val:" + val);

        System.out.println("api_url:" + eqicsSaasApi.getApiUrl());
    }

    @GetMapping("/getConfig")
    public String getConfig() {

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("val", val);
        return JSONUtil.parse(returnMap).toString();

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM