[TOC]
上一篇文章講了`SpringCloudConfig` 集成`Git`倉庫,配和 `Eureka` 注冊中心一起使用,但是我們會發現,修改了`Git`倉庫的配置后,需要重啟服務,才可以得到最新的配置,這一篇我們嘗試使用 `Refresh` 實現主動獲取 `Config Server` 配置服務中心的最新配置
# 準備工作
把上一篇,示例代碼下載,才可以進行一下的操作,下載地址在文章末尾
`spring-cloud-eureka-service`
`spring-cloud-config-server`
`spring-cloud-eureka-provider-1`
`spring-cloud-eureka-provider-2`
`spring-cloud-eureka-provider-3`
`spring-cloud-feign-consumer`
# Config Client
修改第九篇文章項目
`spring-cloud-eureka-provider-1`
`spring-cloud-eureka-provider-2`
`spring-cloud-eureka-provider-3`
# 添加依賴
```xml
<!-- actuator 監控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
```
# 安全認證
在 `application.properties` 添加以下配置.關閉安全認證
```sh
#關閉刷新安全認證
management.security.enabled=false
```
值是`false`的話,除開`health`接口還依賴`endpoints.health.sensitive`的配置外,其他接口都不需要輸入用戶名和密碼了
# 開啟 refresh
在程序的啟動類 `EurekaProviderApplication` 通過 `@RefreshScope` 開啟 SpringCloudConfig 客戶端的 `refresh` 刷新范圍,來獲取服務端的最新配置,`@RefreshScope`要加在聲明`@Controller`聲明的類上,否則`refres`h之后`Conroller`拿不到最新的值,會默認調用緩存。
```java
package io.ymq.example.eureka.provider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
@EnableEurekaClient
@SpringBootApplication
public class EurekaProviderApplication {
@Value("${content}")
String content;
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home() {
return "Hello world ,port:" + port+",content="+content;
}
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
}
```
# 測試服務
按照順序依次啟動項目
`spring-cloud-eureka-service`
`spring-cloud-config-server`
`spring-cloud-eureka-provider-1`
`spring-cloud-eureka-provider-2`
`spring-cloud-eureka-provider-3`
`spring-cloud-feign-consumer`
啟動該工程后,訪問服務注冊中心,查看服務是否都已注冊成功:[http://localhost:8761/](http://localhost:8761/)
![查看服務注冊情況][11]
## 修改Git倉庫
修改`Git`倉庫配置,在 `content=hello dev` 后面加個 `123456`
![修改Git倉庫][22]
## 訪問服務
命令窗口,通過`curl http://127.0.0.1:9000/hello` 訪問服務,或者在瀏覽器訪問`http://127.0.0.1:9000/hello` F5 刷新
**發現沒有得到最新的值**
![訪問服務][33]
# 刷新配置
通過 `Postman` 發送 `POST`請求到:[http://localhost:8081/refresh](http://localhost:8081/refresh),[http://localhost:8083/refresh](http://localhost:8083/refresh),我們可以看到以下內容:
![刷新配置][44]
## 訪問服務
命令窗口,通過`curl http://127.0.0.1:9000/hello` 訪問服務,或者在瀏覽器訪問`http://127.0.0.1:9000/hello` F5 刷新
**發現:服務8082 沒有刷新到最新配置** 因為沒有手動觸發更新
![訪問服務][55]
# 源碼下載
**GitHub:**[https://github.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-config-eureka-refresh](https://github.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-config-eureka-refresh)
**碼云:**[https://gitee.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-config-eureka-refresh](https://gitee.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-config-eureka-refresh)
[11]: https://www.souyunku.com/images/2017/SpringCloud/config-refresh/11.png
[22]: https://www.souyunku.com/images/2017/SpringCloud/config-refresh/22.png
[33]: https://www.souyunku.com/images/2017/SpringCloud/config-refresh/33.png
[44]: https://www.souyunku.com/images/2017/SpringCloud/config-refresh/44.png
[55]: https://www.souyunku.com/images/2017/SpringCloud/config-refresh/55.png
# 下篇預告
留了一個懸念,`Config Client` 實現配置的實時更新,我們可以使用 `/refresh` 接口觸發,如果所有配置的更改,都需要手動觸發,那豈不是維護成本很高,而使用 `Spring Cloud Bus` 消息總線實現方案,可以優雅的解決以上問題,下篇文章我們講`Spring Cloud Bus` 的使用
- Spring Cloud(一)服務的注冊與發現(Eureka)
- Spring Cloud(二)Consul 服務治理實現
- Spring Cloud(三)服務提供者 Eureka + 服務消費者(rest + Ribbon)
- Spring Cloud(四)服務提供者 Eureka + 服務消費者 Feign
- Spring Cloud(五)斷路器監控(Hystrix Dashboard)
- Spring Cloud(六)服務網關 zuul 快速入門
- Spring Cloud(七)服務網關 Zuul Filter 使用
- Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config
- Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服務
- Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh
- Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息總線集成(RabbitMQ)