文章目錄
- Spring Cloud(11)——Spring Cloud Config 分布式配置中心
-
- 1、Spring Cloud Config 概述
- 2、Config 服務端配置
- 3、Config 用戶端配置
- 4、Config 用戶端動态重新整理
Spring Cloud(11)——Spring Cloud Config 分布式配置中心
在Spring Cloud(10)——新一代網關Spring Cloud Gateway中,我們用新一代網關Gateway來保護、增強和控制對于 API 服務的通路。現在來解決分布式系統中配置繁雜的問題。
1、Spring Cloud Config 概述
微服務意味着要将單體應用中的業務拆分成一個個子服務,每個服務的粒度相對較小,是以系統中會出現大量的服務。由于每個服務都需要必要的配置資訊才能運作,是以一套集中式的、動态的配置管理設施是必不可少的。
Spring Cloud Config 就是一個解決分布式系統的配置管理方案。它為微服務架構中的微服務提供集中化的尾部配置支援,配置伺服器為各個不同微服務應用的所有環境提供了一個中心化的外部配置。
在spring cloud config 元件中,分兩個角色,一是config server,二是config client。
config server也稱為分布式配置中心,它是一個獨立的微服務應用,用來連接配接配置伺服器并為用戶端提供配置資訊,加密 / 解密資訊等通路接口。
client通過接口擷取資料、并依據此資料初始化自己的應用。Spring cloud使用git或svn存放配置檔案,預設情況下使用git。
一個配置中心提供的核心功能:
- 集中管理配置檔案
- 不同環境不同配置,動态化的配置更新,分環境部署如dev/test/prod
- 運作期間動态調整配置,不需要在每個服務上編寫配置,服務會向配置中心統一擷取配置資訊
- 當配置發生變動時,服務不需要重新開機即可感覺到配置的變化并應用新的配置
- 配置資訊以REST接口的形式暴露
Spring Cloud Config 中文文檔 https://www.springcloud.cc/spring-cloud-config.html
2、Config 服務端配置
1、在 github 上建立一個名為 springcloud-config 的 倉庫
2、克隆到本地的 E:\git-code\springcloud-config
使用git指令送出配置檔案到github倉庫中:
application-dev.yml
config:
info: "main branch,springcloud-config/application-dev.yml version=1"
3、建立cloud-config-center-3344子產品
4、導入pom依賴
<dependencies>
<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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.cheng.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
5、編寫yml配置檔案
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: [email protected]:Wpengcheng/springcloud-config.git
#搜尋目錄
search-paths:
- springcloud-config
#讀取分支
label: main
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
6、建立主啟動類
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
7、修改windows的hosts檔案,增加映射 :127.0.0.1 config-3344.com
C:\Windows\System32\drivers\etc
8、測試
- 啟動cloud-eureka-server7001子產品
- 啟動cloud-config-center-3344子產品
測試通過cloud-config-center-3344微服務是否可以從GitHub上擷取配置内容
通路請求:http://config-3344.com:3344/main/application-dev.yml
擷取成功!
配置讀取規則:/{分支}-{服務名}-{環境}.yml
分支:lable
服務名:name
環境:profiles
3、Config 用戶端配置
1、建立cloud-config-client-3355子產品
2、導入pom依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.cheng.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
3、編寫 application.yml 配置檔案
bootstrap.yml 使使用者級的資源配置項
application.yml 是系統級的,優先級更高
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: main #分支名稱
name: application #配置檔案名稱
profile: dev #讀取字尾名稱 上面三個拼接起來就是 main分支下的application-dev.yml配置檔案
uri: http://localhost:3344 #config服務端位址 http://config-3344.com:3344/main/application-dev.yml
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
4、建立主啟動類
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
5、編寫業務類
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo; //擷取配置資訊
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
6、測試
- 啟動cloud-eureka-server7001子產品
- 啟動cloud-config-center-3344子產品自測
通路請求:http://config-3344.com:3344/main/application-dev.yml 自測通過
- 啟動cloud-config-client-3355子產品
通路請求:http://localhost:3355/configInfo
成功實作了用戶端3355通過通路Config 服務端3344,擷取了GitHub上的配置資訊
4、Config 用戶端動态重新整理
當運維修改了GitHub上的配置檔案的内容時,ConfigServer配置中心立即響應,但ConfigClient沒有響應,除非重新開機。但每次修改配置檔案後,ConfigClient都要重新啟動嗎?下面就來解決這個問題。
1、在cloud-config-client-3355子產品的bootstrap.yml 中添加:
#暴露監控端口
management:
endopints:
web:
exposure:
include: "*"
2、controller類上加@RefreshScope注解 ,讓ConfigClient 具備重新整理的功能
3、測試
- 修改GItHub上的配置檔案的内容
-
啟動cloud-config-center-3344子產品自測
通路請求:http://config-3344.com:3344/main/application-dev.yml 自測通過
-
啟動cloud-config-client-3355子產品
這裡需要發送post請求重新整理Config:
指令行輸入:curl -X POST “http://localhost:3355/actuator/refresh”
通路請求:http://localhost:3355/configInfo測試
Config 用戶端成功實作動态重新整理!
存在問題:
- 假如有過個微服務,每個微服務都要執行一程post請求嗎?
- 能否自動重新整理
這個問題留給Spring Cloud Bus 消息總線解決。