很多場景下我們需要動態刷新配置。
一、手動刷新配置
1、複製項目config-client爲config-client-refresh
添加依賴,pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springclouddemo</groupId>
<artifactId>config-client-refresh</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client-refresh</name>
<description>刷新配置</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、將端口修改爲7402,其餘配置與config-client相同
3、在Controller上添加註解@RestController
package com.springclouddemo.configclientrefresh.controller;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 何昌傑
*/
@Reference
@RestController
public class DemoController {
@Value("${profile}")
private String profile;
@GetMapping("/demo")
public String demo(){
return this.profile;
}
}
測試:
1.啓動項目config-server、config-client-refresh
2.訪問http://localhost:7402/demo,得到響應如下:
dev-1.0
3.修改Git庫中的配置文件內容:profile=dev-1.0-change
4.發生POST請求到http://localhost:7402/actuator/refresh,刷新配置
5.再次訪問http://localhost:7402/demo,得到響應如下:
dev-1.0-change
說明配置已經刷新。
二、自動刷新配置
上一節討論了使用/actuator/refresh刷新配置,但是如果所有微服務都需要手動去刷新配置的話,工作量可想而知,不僅如此,隨着系統的擴張,將會越來越難以維護。因此,自動刷新配置是十分必要的。
Spring Cloud Bus使用輕量級消息代理(例如RabbitMQ、Kafka等)連接分佈式系統的節點,這樣就可以廣播傳播狀態的更改或者其他管理指令。使用Spring Cloud Bus的架構如果所示:
由圖可知,各個微服務通過消息總線連接到一起,每一個實例都會訂閱配置更新事件,當其中一個微服務節點的/bus/refresh端點被請求時,該實例會向消息總線發送一條配置更新事件,其他實例獲得該事件後也會更新配置。
1.安裝RabbisMQ(過程略)
2.爲項目添加RabbisMQ的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-cloud-bus-amqp</artifactId>
</dependency>
3.在bootstrap.properties添加以下內容:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
測試:
1.啓動項目config-server、config-client、改造後的config-client-refresh
2.訪問http://localhost:7402/demo,得到響應:dev-1.0-change
3.將Git倉庫的配置文件內容修改如下:dev-1.0-change2
4.發生POST請求到http://localhost:7402/bus/refresh,刷新配置
5.再次訪問http://localhost:7402/demo和http://localhost:7401/demo,得到響應如下:dev-1.0-change2
說明配置已經自動刷新了。
三、局部刷新
某些場景下(例如灰度發佈等),若只想刷新部分微服務的配置,可通過/bus/refresh端點的destination參數來定位要刷新的應用程序。
例如:/bus/refresh?destination=customers:7500,這樣消息總線上的微服務實例就會根據destination參數的值來判斷是否需要刷新。其中,customers:7500指的是微服務的ApplicationContext ID。
destination參數也可以用來定位特定的微服務。例如:/bus/refresh?destination=customers:**,這樣就可以觸發customers微服務的所有實例的配置刷新。
- 默認情況下:ApplicationContext ID參數的值是spring.application.name:server.port。
四、架構改進
通過請求某個微服務/bus/refresh端點的方式來實現配置刷新,這種方式並不優雅。原因如下:
- 破壞了微服務的職責單一原則。業務微服務只應關注自身業務,不應承擔配置刷新的職責。
- 破壞了微服務各節點的對等性。
- 有一定的侷限性。例如,微服務在遷移時,網絡地址常常發生變化。此時如果想自動刷新配置,就不得不修改WebHook配置。
改進架構如下:
從圖可知:將Config Server也加入到消息總線中,並使用Config Server的/bus/refresh端點來實現配置的刷新。這樣,各個微服務只需要關注自身的業務,而不再承擔配置刷新的職責。
五、Config與Eureka配合使用
前文在微服務中指定了Config Server的地址,這種方式無法利用服務發現組件的優勢,本節將說明Config與Eureka配合使用。
1.將Config Server於Config Client都註冊到Eureka Server上。
2.爲Config項目添加Eureka依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3.爲啓動類添加組件@EnableEurekaClient
4.Config Client的bootstrap.peoperties配置如下
spring.application.name=application
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
spring.cloud.config.profile=dev
spring.cloud.config.label=master
eureka.client.service-url.defaultZone=http://localhost:7000/eureka/
六、Spring Cloud Config用戶認證
Config Server是運行匿名訪問的,爲了防止配置內容外泄,應該保護Config Server的安全,可以通過物理網絡安全,或者Config Server添加用戶認證的方式。
1、Config Server添加用戶認證
1.複製項目config-server爲config-server-authentication
2.依賴如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springclouddemo</groupId>
<artifactId>config-server-authentication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server-authentication</name>
<description>用戶認證</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<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.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.在配置文件中配置一下內容:
server.port=7403
spring.application.name=config-server-authentication
spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/config-server
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
spring.security.user.name=hcj
spring.security.user.password=123
eureka.client.service-url.defaultZone=http://localhost:7000/eureka/
這樣就爲Config Server添加用戶認證了。
2、Config Client連接需要用戶認證的Config Server
方式一:
spring.cloud.config.uri=http://[user]:[password]@localhost:7400/
方式二:
spring.cloud.config.username=hcj
spring.cloud.config.password=123