Spring Cloud Config (六)

分布式配置中心 Spring Cloud Config

Spring Cloud Config用來為分布式系統中的基礎設施和微服務應用提供集中化的外部配置支持,分為客戶端與服務端兩部分。其中服務端也成為分布式配置中心,是一個獨立的微服務應用,用來連接配置倉庫并為客戶端提供獲取配置信息,加密/解密信息等訪問接口;客戶端則是微服務架構中的各個微服務應用或基礎設施,通過指定的配置中心來管理應用資源與業務相關的配置內容,并在服務啟動的時候從配置中心獲取和加在配置信息。

Spring Cloud Config實現了對服務端和客戶端中環境變量和屬性配置的抽象映射。Spring Cloud Config默認采用<font color=#FF0000>GIT</font>來存儲配置信息,所以使用Spring Cloud Config構建的配置服務器,天然就支持對微服務應用配置信息的版本管理,同時也支持SVN倉庫,本地化文件系統等其他存儲方式。

1. 快速集成 Spring Cloud Config

1.1 構建配置中心(服務端)

  1. 創建基礎Spring Boot工程:config-server

  2. 添加依賴: spring-cloud-config-server

  3. 在主類上使用@EnableConfigServer注解開啟Spring Cloud Config的服務端功能

  4. 配置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客戶端并獲取上述配置信息。

  1. 創建基礎Spring Boot工程:config-client

  2. 添加依賴: spring-cloud-starter-config

  3. 配置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
    
  4. 創建RESTFUL接口返回配置中心from屬性,通過@Value("${from}")注解綁定配置的from屬性

2. 配置中心(服務端)詳解

2.1 基礎架構

基本結構:

  • 遠程Git倉庫:存儲配置文件的地方
  • Config Server:分布式配置中心
  • 本地Git倉庫:在Config Server的文件系統中,每次客戶端請求獲取配置信息時,Config Server從Git倉庫中獲取最新的配置到本地,然后在本地Git倉庫中讀取并返回,當遠程倉庫無法獲取時,直接將本地內容返回。
  • Service A, Service B:微服務應用,它們指定Config Server的地址,應用在啟動的時候,實現從外部化獲取應用自己需要的配置信息。

客戶端應用從配置中心獲取配置信息執行流程:

  1. 應用啟動時,根據bootstrap.yml配置的應用名{application},環境名{profile},分支名{label}向Config Server請求獲取配置信息。
  2. Config Server根據自己維護的Git倉庫信息和客戶端傳遞過來的配置定位信息查找配置信息。
  3. Config Server將找到的配置信息下載到Config Server的文件系統中。
  4. Config Server創建Spring的ApplicationContext實例,并從Git本地倉庫中加在配置文件,最后將這些配置內容讀取出來返回給客戶端應用。
  5. 客戶端應用在獲得外部配置文件后加載到客戶端的ApplicationContext實例,該配置內容的優先級<font color=#FF0000>高于</font>客戶端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倉庫位置
spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/{application}-config

2.2.2 配置多個倉庫

當有多個匹配規則的時候,可以用<font color=#FF0000>逗號</font>分割多個{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方式進行認證需要設置usernamepassword屬性配置賬戶(參考1.1章節)。也可以采用SSH的方式,通過生成Key并在Git倉庫中進行配置匹配以實現訪問。

2.3 本地倉庫

在使用Git或SVN倉庫之后,文件都會在Config Server的本地文件系統中存儲一份,默認會被存儲于以config-repo為前綴的臨時目錄中,如名為/temp/config-repo-<隨機數>的目錄。由于其隨機性以及臨時目錄的特性,可能會發生一些不可預知的后果,為避免這些問題,最好指定一個固定的位置存儲。通過spring.cloud.config.server.git.basedirspring.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
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容