1、Config手动刷新
a、使用@RefreshScope注解
1import org.springframework.beans.factory.annotation.Value; 2import org.springframework.cloud.context.config.annotation.RefreshScope; 3import org.springframework.web.bind.annotation.GetMapping; 4import org.springframework.web.bind.annotation.RestController; 5 6/** 7 * 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新 8 */ 9@RestController 10@RefreshScope 11public class ConfigClientController { 12 13 @Value("${env}") 14 private String env; 15 16 @Value("${password}") 17 private String password; 18 19 @Value("${username}") 20 private String username; 21 22 @GetMapping("/config/profile") 23 public String hello() { 24 return this.env+","+this.password+","+this.username; 25 } 26}
b、post请求config客户端的/refresh端点
http://localhost:6062/refresh
再次访问http://localhost:6062/config/profile,发现配置文件为最新配置。
2、Config自动刷新
1、WebHooks动态刷新
2、spring-cloud-bus动态刷新