SrpingCloud 之SrpingCloud config分布式配置中心實時刷新


默認情況下是不能及時獲取變更的配置文件信息

Spring Cloud分布式配置中心可以采用手動或者自動刷新

 1、手動需要人工調用接口   監控中心

 2、消息總線實時通知  springbus

 

動態刷新數據

在SpringCloud中有手動刷新配置文件和實時刷新配置文件兩種方式。

手動方式采用actuator端點刷新數據

實時刷新采用SpringCloud Bus消息總線

 

 

  

actuator端點刷新數據

在config clientr引入 

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId> 
     </dependency>

  

yml中開啟監控斷點

management:
  endpoints:
    web:
      exposure:
        include: "*"

 同時在controller加 @RefreshScope 

package com.toov5.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class TestController {
    @Value("${motto}")   //配置的key
  private String motto;
    
    @RequestMapping("/getMotto")
    public String getMotto() {
        return motto;
    }
}

 

開啟: 修改git上的配置文件信息

必須要用post請求!

http://127.0.0.1:8882/actuator/refresh

 

成功!

 每個客戶端都有監聽,效果不是很好這樣的方式。手動刷新比較好一些。改完了自己手動刷新下 post 調用一下

高級的spring cloud bus: https://www.cnblogs.com/toov5/p/10293755.html 


免責聲明!

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



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