1、Config手動刷新
a、使用@RefreshScope注解
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; } }
b、post請求config客戶端的/refresh端點
http://localhost:6062/refresh
再次訪問http://localhost:6062/config/profile,發現配置文件為最新配置。
2、Config自動刷新
1、WebHooks動態刷新
2、spring-cloud-bus動態刷新