springcloud系列29——Spring Cloud Config配置屬性重新整理之手動重新整理
阿新 • • 發佈:2018-12-10
前言
在以往的應用中,如果要更改應用的配置,要讓配置生效必須重啟應用程式。在Spring Cloud Config中我們可以重新整理配置屬性而不用重啟應用。
這1節說明在Spring Cloud Config如何手動重新整理配置屬性。
手動重新整理配置屬性
1.需要引入spring boot-actuator依賴(用到/refresh端點)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</ artifactId>
</dependency>
2.在Bean上增加@RefreshScope註解
比如:
@Controller
@RequestMapping("/user")
@RefreshScope
public class UserController {
@Value("${my.custom.message}")
private String customeMessage;
@GetMapping("/customMsg")
@ResponseBody
public String getCustomeMessage () {
return this.customeMessage;
}
}
Ok,準備工作完畢!
先來看看my.custom.message原來的配置
現在我們修改git倉庫中配置檔案的my.custom.message的配置項。
我們將value修改為:
my.custom.message=Hello,This message is changed!!!
現在通過/refresh重新整理配置屬性。
返回結果表示my.custom.message配置有更新。
現在在訪問/user/customeMsg
可以看到,已經使用了剛剛更新的配置。
注意
1.預設spring-boot-acturator開啟了安全認證,這裡作為演示將其關閉(management.security.enabled=false)。
2.如果有很多個微服務,使用手動重新整理配置的方式就不太合適了。