SpringCloud Config分布式配置中心
- 分布式配置中心概述
- Config服務端配置與測試
- Config用戶端配置與測試
- Config用戶端動态重新整理
分布式配置中心概述
1、分布式系統面臨的配置問題?
微服務意味着要将單體應用中心的業務拆分成一個個子服務,每個服務的粒度相對較小,是以系統中會出現大量的服務。 由于每個服務都需要必要的配置資訊才能運作,是以一套集中式的、動态的配置管理設定是必不可少的。
SpringCloud 提供了 ConfigServer來解決這個問題。
2、SpringCloud Config原理圖
為微服務架構中的微服務
SpringCloud Config
,配置伺服器為各個不同微服務應用的所有環境提供了
提供集中式的外部配置支援
一個中心化的外部配置。
![]()
SpringCloud Config分布式配置中心分布式配置中心概述Config服務端配置與測試Config用戶端配置與測試Config用戶端動态重新整理
3、SpringCloud Config 分為服務端和用戶端
(1),它是一個
服務端也成為分布式配置中心
,用來連接配接配置伺服器并
獨立的微服務應用
(2)
為用戶端提供擷取配置資訊,加密/解密資訊的通路接口。
則是通過指定的配置中心來管理應用資源,以及與業務相業務相關的配置内容,并
用戶端
。配置伺服器預設采用 git 來存儲配置資訊,這樣就有助于對環境配置進行版本管理,并且可以通過 git 用戶端工具來友善的管理和通路配置内容。
在啟動的時候從配置中心擷取和加載配置資訊
4、作用
(1)集中管理配置檔案。
(2)不同環境不同配置,動态話的配置更新,分環境部署比如:dev、test、prod、beta、release。
(3)運作期間動态調整配置,不在需要在每個服務部署的機制上編寫配置檔案,服務會向配置中心統一拉取配置自己的資訊,
(4)将配置資訊以
當配置發生變動時,服務不在需要重新開機即可感應到配置的變化并應用新的配置。(類似觀察者模式)
的形式暴露。
REST 接口
Config服務端配置與測試
一、在
github
上建立倉庫(本文以idea上建構檔案為例)
二、建構
cloud-config-center-server3344
子產品
1、引入 pom 依賴
<!--SpringCloud config配置中心服務端依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--Eureka用戶端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--導入公共實體或者API依賴-->
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、寫 yml 配置檔案
# 服務端口号
server:
port: 3344
# 服務名稱
spring:
application:
name: cloud-config-center-server
# 配置中心
cloud:
config:
# 讀取分支
label: master
server:
git:
#填寫自己的github路徑
uri: https://github.com/1914526816lhw/springcloud-config-repository.git
#搜尋目錄
search-paths:
- springcloud-config-repository
# 連接配接 github 的賬戶(登入的賬号密碼)
username: 賬号
password: 密碼
# 設定連接配接 github 逾時時間
timeout: 60
# 強制拉取
force-pull: true
# 服務注冊中心
eureka:
client:
#表示收将自己注冊到EurekaServer,預設為true
register-with-eureka: true
#是否從EurekaServer抓取已有的注冊資訊,預設為true,單節點無所謂,叢集必須設定為true才能配合ribbon使用負載均衡
fetchRegistry: true
service-url:
# defaultZone: http://localhost:7001/eureka #單機版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #叢集版
instance:
instance-id: config-center-server3344
#通路路徑可以顯示IP位址
prefer-ip-address: true
#eureka用戶端向服務端發送心跳的時間間隔,機關為秒(預設為30秒)
lease-renewal-interval-in-seconds: 1
#eureka服務端在收到最後一次心跳後等待時間上限,機關為秒(預設為90秒),逾時将删除服務
lease-expiration-duration-in-seconds: 2
3、主啟動類
@SpringBootApplication
//激活配置中心
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}
4、測試
浏覽器通路:
http://config3344.com:3344/master/config-dev.yml
能夠正常擷取到遠端倉庫config-dev.yml配置檔案内容。
三、Config配置讀取規則
:分支(branch)
label
:服務名
application
:環境(dev、test、prod)
profile
1、
/{label}/{application}-{profile}.yml(或 .properties)
【推薦使用這種方式】,如圖:
2、
/{application}-{profile}.yml(或 .properties)
在配置檔案中沒有配置
label
的情況下。預設讀取
master
分支,master 分支沒有的時候再去找其他分支,如果不存在,則傳回空。
3、
/{application}/{profile}/[{label}]
,讀取配置檔案資訊傳回的是
json格式
,可自己進行解析。
Config用戶端配置與測試
1、建構
cloud-config-center-client3355
子產品
2、引入 pom 依賴
<!--SpringCloud config配置中心用戶端依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--Eureka用戶端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--導入公共實體或者API依賴-->
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3、寫 yml 檔案配置(此處為:
bootstrap.yml
)
概念
bootstrap.yml
(1)application.yml 是SpringCloud 會建立一個
使用者級
的資源配置項。
(2)bootstrap.yml 是
的,
系統級
。
優先級更高
,作為 Spring 應用的
Bootstrap Context
,初始化的時候,Bootstrap Context 負責
Application Context 的父級上下文
。這兩個上下文共享一個外部擷取的
從外部源加載配置屬性并解析配置
Environment
。
Bootstrap 屬性有高優先級,預設情況下,他們不會被本地配置覆寫。Bootstrap Context 和Application Context 有着不同的約定,是以新增一個 bootstrap.yml 檔案,保證 Bootstrap Context 和 Application Context 配置的分離。要将 Client 子產品下的 application.yml 檔案改成 bootstrap.yml 檔案很關鍵。因為 bootstrap.yml 是比 application.yml 先加載的,
。
bootstrap.yml 優先級高于 application.yml
bootstrap.yml
server:
port: 3355
spring:
application:
name: cloud-config-center-client
cloud:
# Config 用戶端配置
config:
discovery:
#開啟服務發現
enabled: true
# 服務id
service-id: cloud-config-center-server
label: master #分支名稱
name: config #配置檔案名
profile: dev #配置檔案字尾名稱
# uri: http://localhost:3344 #配置中心服務端位址
uri: lb://cloud-config-center-server
username: 賬号
password: 密碼
# 服務注冊中心
eureka:
client:
#表示收将自己注冊到EurekaServer,預設為true
register-with-eureka: true
#是否從EurekaServer抓取已有的注冊資訊,預設為true,單節點無所謂,叢集必須設定為true才能配合ribbon使用負載均衡
fetchRegistry: true
service-url:
# defaultZone: http://localhost:7001/eureka #單機版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #叢集版
instance:
instance-id: config-center-client3355
#通路路徑可以顯示IP位址
prefer-ip-address: true
#eureka用戶端向服務端發送心跳的時間間隔,機關為秒(預設為30秒)
lease-renewal-interval-in-seconds: 1
#eureka服務端在收到最後一次心跳後等待時間上限,機關為秒(預設為90秒),逾時将删除服務
lease-expiration-duration-in-seconds: 2
4、controller
@RestController
public class ConfigClientController {
@Value(value = "${config.info}")
private String configInfo;
@GetMapping(value = "/getConfigInfo")
public String getConfigInfo() {
return configInfo;
}
}
5、測試:浏覽器通路
http://localhost:3355/getConfigInfo
得到與
http://config3344.com:3344/master/config-dev.yml
内容,即:遠端配置檔案中的值,如下:
config:
info: "master branch,springcloud-config-repository/config-dev.yml version=1"
問題:
當我們修改遠端倉庫配置檔案内容後,配置中心服務端通過重新整理可以擷取心的配置資訊,但是用戶端重新整理後沒有任何反應,還是原來的配置資訊。除了重新開機。那麼如何解決實時更新配置問題?接着往下 Config用戶端動态重新整理 。
Config用戶端動态重新整理
一、修改
cloud-config-center-client3355
子產品
1、引入 pom 的 actuator 監控依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、修改 yml 檔案,暴露監控端口
management:
endpoints:
web:
exposure:
include: "*"
3、controller業務類中加入
@RefreshScope
注解
@RefreshScope
@RestController
public class ConfigClientController {
@Value(value = "${config.info}")
private String configInfo;
@GetMapping(value = "/getConfigInfo")
public String getConfigInfo() {
return configInfo;
}
}
4、測試
理論上内容同步流程:
github --> 3344 --> 3355
,實際上情況如下:
(1)通路
http://config3344.com:3344/master/config-dev.yml
能夠正常通路到配置檔案内容。
(2)直接重新整理
沒有任何反應,擷取的是原來的值。需要通過
http://localhost:3355/getConfigInfo
重新整理3355:
POST請求
,再次通路時,更新了最新的配置内容。雖然避免了重新開機用戶端服務。但是每個服務都需要POST請求重新整理,還是很不理想。該如何處理呢?接着往下看。
curl -X POST "http://localhost:3355/actuator/refresh"
二、廣播(觀察者模式)
SpringCloud Bus 消息總線:
待更新