
當我修改存儲在 Gitee 上的配置時,Config Server 不需要重啟即可同步 Gitee 上的配置,但是 Config Client 需要重啟才能進行同步。如果每次修改都要重啟,那么將是一個噩夢。
<br/>
而下面將要學習的動態刷新就可以解決這個問題,當更改存儲在 Gitee 上的配置時,客戶端不再需要重啟即可同步配置。
<br/>
步驟如下:
**1. 在 cloud-config-client-3355 客戶端的`pom.xml`中添加 `actuator` 依賴**
```xml
<dependencies>
<!-- config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
...
</dependencies>
```
**2. 在 cloud-config-client-3355 客戶端的`bootstrap.yml`中暴露監控端口**
```yml
#暴露監控端點
management:
endpoints:
web:
exposure:
include: "*"
```
**3. 在 cloud-config-client-3355 客戶端的`ConfigController`上添加注解`@RefreshScope`**
```java
@RestController
@RefreshScope
public class ConfigController {
@Value("${app.version:default}")
private String version;
@Value("${app.name:default}")
private String name;
@GetMapping("/config")
public String getConfigInfo() {
return "app.name:" + name + ",version:" + version;
}
}
```
**4. 啟動 3344 服務端和 3355 客戶端**
在沒有更新Gitee上的 `cloud-config-client-3355-test.yml` 配置文件前,訪問 http://localhost:3355/config ,獲取如下內容。
```
app.name:cloud-config-client-3355,version:test-1.0
```
**5. 到 Gitee 上修改 `cloud-config-client-3355-test.yml` 文件**
修改內容如下:
```
app:
name: cloud-config-client-3355
version: test-2.0 #之前是 test-1.0
```
沒有重啟 cloud-config-client-3355 前提下,刷新 http://localhost:3355/config ,依舊是 `test-1.0`,說明 3355 客戶端沒有同步 Gitee 上的配置。
```
app.name:cloud-config-client-3355,version:test-1.0
```
**6. 運行下面的命令刷新 cloud-config-client-3355 客戶端**
```shell
curl -X POST http://localhost:3355/actuator/refresh
```

在沒有重啟 cloud-config-client-3355 客戶端的前提下,刷新 http://localhost:3355/config ,可以看到由原來的 `test-1.0` 更新為 `test-2.0`,說明 3355 客戶端已經與 Gitee 上的配置同步成功!
```
app.name:cloud-config-client-3355,version:test-2.0
```
<br/>
但是上面講的這種同步方式做不到這兩點需求:廣播通知和定點通知。
* 廣播通知:即只需要通知其中某一個 config client 客戶端,其它 config-client 也能知道配置更新了,這樣就不用發送那么多次的`curl -X POST ...`請求。
* 定點通知:即我想更新哪個 config client 的配置我就更新哪一個的,不想更新的就不更新。
而且這種方式需要運維人員手動發送`curl -X POST ...`請求,本質上還是手動刷新,不是自動刷新,要解決這些問題,需要引入 Spring Cloud Bus 消息總線。
- 微服務
- 微服務是什么?
- 微服務架構
- 微服務優缺點
- 微服務技術棧
- 微服務框架對比
- SpringCloud
- SpringCloud是什么
- SpringCloud與SpringBoot對比
- SpringCloud與Dubbo對比
- Rest微服務案例
- 總體介紹
- 父工程構建步驟
- 公共模塊構建步驟
- 服務端模塊構建步驟
- 消費端模塊構建步驟
- Eureka服務注冊與發現
- Eureka是什么
- Eureka原理
- Eureka注冊服務中心構建
- 向Eureka注冊已有微服務
- Eureka的自我保護機制
- Eureka服務發現
- Eureka集群配置
- Eureka與Zookeeper對比
- Ribbon負載均衡
- Ribbon是什么
- Ribbon負載均衡演示
- 構建服務端模塊
- 構建消費端模塊
- Ribbon核心組件IRule
- 自定義負載均衡策略
- Ribbon均衡策略優先級
- 輪詢策略算法
- OpenFeign負載均衡
- OpenFeign是什么
- 負載均衡演示
- 日志打印功能
- 導出功能
- Hystrix斷路器
- Hystrix是什么
- 服務熔斷
- Hystrix服務端構建
- 服務熔斷演示
- 服務熔斷類型
- HystrixProperty配置匯總
- 服務降級
- Hystrix客戶端構建
- 服務降級演示
- fallbackFactory
- 熔斷與降級
- 服務監控
- 網關服務Zuul
- Zuul是什么
- Zuul路由服務構建
- 設置訪問映射規則
- Config分布式配置中心
- Config分布式配置中心是什么
- Config服務端與Git通信
- Config客戶端獲取配置
- Config客戶端動態刷新
- Bus消息總線
- Bus消息總線是什么
- Bus消息總線原理
- 廣播通知設計思想
- 廣播通知演示
- 定點通知演示
- Stream消息驅動
- 為什么要引入Stream
- Stream消息驅動是什么
- Stream設計思想
- Stream流程和注解
- Stream案例演示
- 重復消費問題
- 消息持久化
- Sleuth分布式鏈路跟蹤
- Sleuth是什么
- 搭建鏈路監控
- SpringCloud Alibaba
- Nacos注冊與配置中心
- Nacos是什么
- 安裝并運行Nacos
- Nacos注冊中心
- 服務端入住Nacos
- 消費端入住Nacos
- Nacos負載均衡演示
- 服務注冊中心對比
- Nacos的AP和CP轉化
- Nacos配置中心
- 基礎配置演示
- Nacos分類配置
- Nacos集群搭建
- Sentinel實現熔斷與限流
- Sentinel是什么
- Sentinel環境搭建
- Sentinel監控微服務演示
- Sentinel流控規則
- 流量監控的作用
- 設置流控規則
- Sentinel降級規則
- 熔斷降級作用
- 設置降級規則
- Sentinel熱點限流
- 什么是熱點
- 設置熱點限流
- Sentinel系統限流
- @SentinelResource
- @SentinelResource屬性
- @SentinelResource限流演示
- @SentinelResource熔斷演示
- 規則持久化
- 熔斷框架比較
- Seata分布式事務
- 分布式事務問題
- Seata是什么
- Seata分布式事務過程
- Seata環境搭建
- 演示示例
- 業務說明
- 數據庫環境準備
- 微服務環境準備
- 測試
- Consul服務注冊與發現
- Consul是什么
- Consul能做什么
- 環境搭建
- Windows平臺
- 服務端入住Consul
- 消費端入住Consul
- 注冊中心對比
- Zookeeper服務注冊與發現
- Zookeeper是什么
- 環境搭建
- 服務端入住Zookeeper
- 消費端入住Zookeeper
- 網關服務Gateway
- Gateway是什么
- Gateway能做什么
- Gateway對比Zuul
- 三大核心概念
- Gateway工作流
- 環境搭建
- 網關路由配置方式
- 配置文件配置
- 代碼中配置
- 動態路由
- Predicate斷言
- 斷言是什么
- 常用斷言
- Filter過濾器
- 過濾器是什么
- 過濾器種類
- 自定義過濾器