SrpingCloud 之SrpingCloud config分布式配置中心實時刷新
阿新 • • 發佈:2018-11-16
sco 中心 tor 動態 技術分享 pen web spa return
默認情況下是不能及時獲取變更的配置文件信息
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 調用一下
SrpingCloud 之SrpingCloud config分布式配置中心實時刷新