分布式配置中心 Spring Cloud Config
Spring Cloud Config用來為分布式系統中的基礎設施和微服務應用提供集中化的外部配置支持,分為客戶端與服務端兩部分,其中服務端也成為分布式配置中心,是一個獨立的微服務應用,用來連接配置倉庫并為客戶端提供獲取配置資訊,加密/解密資訊等訪問介面;客戶端則是微服務架構中的各個微服務應用或基礎設施,通過指定的配置中心來管理應用資源與業務相關的配置內容,并在服務啟動的時候從配置中心獲取和加在配置資訊,
Spring Cloud Config實作了對服務端和客戶端中環境變數和屬性配置的抽象映射,Spring Cloud Config默認采用GIT來存盤配置資訊,所以使用Spring Cloud Config構建的配置服務器,天然就支持對微服務應用配置資訊的版本管理,同時也支持SVN倉庫,本地化檔案系統等其他存盤方式,
1. 快速集成 Spring Cloud Config
1.1 構建配置中心(服務端)
-
創建基礎Spring Boot工程:config-server
-
添加依賴: spring-cloud-config-server
-
在主類上使用
@EnableConfigServer注解開啟Spring Cloud Config的服務端功能 -
配置Spring Cloud Config服務的基本資訊及Git倉庫的相關資訊
spring.application.name=config-server server.port=7001 ## 配置Git倉庫位置 spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringCloud-Learning/ ## Git倉庫路徑下的相對搜索位置,可配置多個 spring.cloud.config.server.git.searchPaths=spring_cloud_in_action/config-repo ## Git倉庫用戶名 spring.cloud.config.server.git.username=username ## Git倉庫用戶密碼 spring.cloud.config.server.git.password=password
1.2 配置規則詳解
根據服務端配置在http://git.oschina.net/didispace/SpringCloud-Learning/spring_cloud_in_action/下創建一個config-repo目錄作為配置倉庫,并根據不同環境新建組態檔:
- didispace.properties
- didispace-dev.properties
- didispace-test.properties
- didispace-prod.properties
方便測驗版本控制,在該Git倉庫的master分支的四個組態檔中設定from屬性,并分別設定不同的值且使1.0作為后綴:
- from=git-default-1.0
- from=git-dev-1.0
- from=git-test-1.0
- from=git-prod-1.0
創建一個config-label-test分支,并將各組態檔中的from屬性后綴改為2.0,通過瀏覽器,POSTMAN或者其它CURL工具請求配置內容,配置資訊的URL與檔案的映射關系如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
構造不同的url來訪問不同的配置內容,獲取config-label-test分支,didispace應用的prod環境配置可以訪問http://localhost:7001/didispace/prod/config-label-test
1.3 客戶端配置映射
完成上述準備,確保配置中心已經正常開始作業,構建Spring Cloud Config客戶端并獲取上述配置資訊,
-
創建基礎Spring Boot工程:config-client
-
添加依賴: spring-cloud-starter-config
-
配置config server位置
## 組態檔規則中{application}部分 spring.application.name=didispace ## 組態檔規則中{profile}部分 spring.cloud.config.profile=dev ## 組態檔規則中{label}部分 spring.cloud.config.label=master ## 配置中心 config-server 的地址 spring.cloud.config.uri=http://localhost:7001/ server.port=7002 -
創建RESTFUL介面回傳配置中心from屬性,通過
@Value("${from}")注解系結配置的from屬性
2. 配置中心(服務端)詳解
2.1 基礎架構
基本結構:
- 遠程Git倉庫:存盤組態檔的地方
- Config Server:分布式配置中心
- 本地Git倉庫:在Config Server的檔案系統中,每次客戶端請求獲取配置資訊時,Config Server從Git倉庫中獲取最新的配置到本地,然后在本地Git倉庫中讀取并回傳,當遠程倉庫無法獲取時,直接將本地內容回傳,
- Service A, Service B:微服務應用,它們指定Config Server的地址,應用在啟動的時候,實作從外部化獲取應用自己需要的配置資訊,
客戶端應用從配置中心獲取配置資訊執行流程:
- 應用啟動時,根據bootstrap.yml配置的應用名{application},環境名{profile},分支名{label}向Config Server請求獲取配置資訊,
- Config Server根據自己維護的Git倉庫資訊和客戶端傳遞過來的配置定位資訊查找配置資訊,
- Config Server將找到的配置資訊下載到Config Server的檔案系統中,
- Config Server創建Spring的ApplicationContext實體,并從Git本地倉庫中加在組態檔,最后將這些配置內容讀取出來回傳給客戶端應用,
- 客戶端應用在獲得外部組態檔后加載到客戶端的ApplicationContext實體,該配置內容的優先級高于客戶端Jar包內部的配置內容,所以在Jar包中重復的內容將不再被加載,
2.2 Git配置倉庫
Spring Cloud Config中默認使用Git,對于Git的配置也非常簡單,只需要再Config Server中設定spring.cloud.config.server.git.uri屬性為其指定Git倉庫的網路地址和賬戶資訊即可(參考1.1章節),
將該屬性值使用file://前綴設定為一個檔案地址(windows系統中使用file:///定位檔案內容),那么它將以本地倉庫的方式運行,這樣就可以脫離Git服務端快速進行除錯與開發,如:
spring.cloud.config.server.git.uri=file://${user.home}/config-repo
2.2.1 占位符配置URI
{application},{profile},{label}這些占位符除了用于表示組態檔的規則之外,還可以用于Config Server中對Git倉庫地址的URI配置,可以通過{application}占位符來實作一個應用對應一個Git倉庫目錄的配置效果:
## 配置Git倉庫位置
spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/{application}
{application}代表應用名,當客戶端向Config Server發起獲取配置的請求時,Config Server會根據客戶端的Spring.application.name資訊來填充{application}占位符以定位配置資源的存盤位置,實作根據微服務應用屬性動態獲取不同位置的配置,對于{label}引數,若Git分支和標簽名包含"/",那么{label}引數在HTTP的URL中應該使用"(_)"代替,
通過在URI中使用占位符規劃和實作通用的倉庫配置:
-
代碼庫:使用服務名作為Git倉庫名
http://git.oschina.net/didispace/member-service
-
配置庫:使用服務名加上
-config后綴作為Git倉庫名稱:http://git.oschina.net/didispace/member-service-config
服務端倉庫地址通用配置:
## 配置Git倉庫位置
spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/{application}-config
2.2.2 配置多個倉庫
當有多個匹配規則的時候,可以用逗號分割多個{application}/{profile}配置規則
## 配置dev 本地檔案系統 profile可以為任意值
spring.cloud.config.server.git.repos.dev.pattern=dev/*
spring.cloud.config.server.git.repos.dev.uri=file://home/git/config-repo
## 配置test Git倉庫位置 profile為pp或oo開頭
spring.cloud.config.server.git.repos.test.pattern=test/pp*,test/oo*
spring.cloud.config.server.git.repos.test.uri=http://git.oschina.net/didispace/{application}-config
2.2.3 訪問權限
Config Server在訪問Git倉庫的時候,若采用HTTP方式進行認證需要設定username和password屬性配置賬戶(參考1.1章節),也可以采用SSH的方式,通過生成Key并在Git倉庫中進行配置匹配以實作訪問,
2.3 本地倉庫
在使用Git或SVN倉庫之后,檔案都會在Config Server的本地檔案系統中存盤一份,默認會被存盤于以config-repo為前綴的臨時目錄中,如名為/temp/config-repo-<亂數>的目錄,由于其隨機性以及臨時目錄的特性,可能會發生一些不可預知的后果,為避免這些問題,最好指定一個固定的位置存盤,通過spring.cloud.config.server.git.basedir或spring.cloud.config.server.svn.basedir來配置一個準備好的目錄,
2.4 本地檔案系統
Spring Cloud Config也提供了一種不使用Git倉庫或SVN倉庫的存盤方式,使用本地檔案系統的存盤方式來保存配置資訊,
設定屬性spring.profiles.active=native,Config Server會默認從應用的src/main/resource目錄下搜索組態檔,若需要指定搜索組態檔的路徑,通過spring.cloud.config.server.native.serchLocation屬性來指定具體的組態檔位置,
2.5 健康檢測
Spring Cloud Config服務端為spring-boot-actuator模塊的/health端點實作了對應的健康檢測器,它默認構建一個application為app的倉庫,當使用占位符配置URI時(以2.2.1章節為例),該檢測器會不斷檢查http://git.oschina.net/didispace/app-config倉庫是否可以連通,因此控制臺會出現警告資訊,
-
配置實際存在的倉庫進行連通檢測
spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/{application}-config spring.cloud.config.server.git.username=username spring.cloud.config.server.git.password=password ## name: 應用名 spring.cloud.config.server.health.repositories.check.name=check-repo ## label: 分支名 spring.cloud.config.server.health.repositories.check.label=master ## profiles: 環境名 spring.cloud.config.server.health.repositories.check.profiles=default實作對倉庫
check-repo-config的連通性檢測 -
關閉健康檢測器
spring.cloud.config.server.health.enabled=false
2.6 屬性覆寫
覆寫屬性配置的引數,不會被Spring Cloud客戶端修改,并且Spring Cloud客戶端從Config Server中獲取配置資訊時,都會取得這些配置資訊,覆寫屬性引數并非強制的,可以通過改變客戶端中更高優先級的配置方式選擇是否使用Config Server提供的默認值,
spring.cloud.config.server.overrides.name=didi
spring.cloud.config.server.overrides.from=shanghai
2.7 安全保護
為配置中心實作安全保護的方式有很多,如:物理網路限制,OAuth2授權等,由于微服務應用和配置中心構建于Spring Boot基礎上,所以與Spring Security結合更加方便,
只需在配置中心加入spring-boot-starter-security依賴即可實作對配置中心訪問的安全保護,默認情況下,會獲取一個名為user的用戶,在配置中心啟動時,在日志中列印出該用戶的隨機密碼:
INFO 22028 --- [ main] b.a.s.AuthenticationManagerConfiguration : Using default security password: 1a32a848-da0c-4590-9c58-e860be8c50dd
多數情況下不會使用隨機生成密碼的機制,可以在配置中心的組態檔中指定用戶和密碼:
security.user.name=user
security.user.password=1a32a848-da0c-4590-9c58-e860be8c50dd
在客戶端中加入安全資訊來通過校驗:
spring.cloud.config.username=user
spring.cloud.config.password=1a32a848-da0c-4590-9c58-e860be8c50dd
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/89870.html
標籤:Java
