一、手动刷新
1.1 阐述
因为SpringCloud Config的服务端第一次读取远程git服务器的配置之后是会缓存一份配置在本地的,即使远程git修改了配置,config客户端也依然是读取缓存中的配置,而使用手动刷新可以强制刷新缓存,让config服务端读取最新配置文件。
1.2 实现
- 使用@RefreshScope注解
- post请求config客户端的/refresh端点:http://localhost:8080/refresh
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新
*/
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${env}")
private String env;
@Value("${password}")
private String password;
@Value("${username}")
private String username;
@GetMapping("/config/profile")
public String hello() {
return this.env+","+this.password+","+this.username;
}
}
二、自动刷新
2.1 阐述
SpringCloud Config的自动刷新需要配合springcloud-bus消息总线来使用,会有线程实时监控配置是否修改,修改则自动刷新,但是这种方式比较损耗性能,如无必要刻意使用手动刷新。
2.2 实现
- webhooks动态刷新
- SpringCloud Bus动态刷新