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