SpringCloud配置中心實戰
部分優質內容來源:
簡書:CD826:SpringCloud文集
1、統一配置中心(Config)
Spring Cloud Config具有中心化、版本控制、支持動態更新和語言獨立等特性。其特點是:
- 提供服務端和客戶端支持(Spring Cloud Config Server和Spring Cloud Config Client);
- 集中式管理分佈式環境下的應用配置;
- 基於Spring環境,實現了與Spring應用無縫集成;
- 可用於任何語言開發的程序;
- 默認實現基於Git倉庫(也支持SVN),從而可以進行配置的版本管理;
Spring Cloud Config的結構圖如下:
從圖中可以看出Spring Cloud Config有兩個角色(類似Eureka): Server和Client。Spring Cloud Config Server作爲配置中心的服務端承擔如下作用:
- 拉取配置時更新Git倉庫副本,保證是配置爲最新;
- 支持從yml、json、properties等文件加載配置;
- 配合Eureke可實現服務發現,配合Cloud Bus(這個後面我們在詳細說明)可實現配置推送更新;
- 默認配置存儲基於Git倉庫(可以切換爲SVN),從而支持配置的版本管理.
而對於,Spring Cloud Config Client則非常方便,只需要在啓動配置文件中增加使用Config Server上哪個配置文件即可。
1.1 Spring項目配置加載順序
- 這裏是列表文本命令行參數
- SPRING_APPLICATION_JSON 參數
- 從java:comp/env 加載 JNDI 屬性
- Java系統屬性 (System.getProperties())
- 操作系統環境變量
- 如果有使用 random.* 屬性配置,則使用 RandomValuePropertySource 產生
- 外部特定應用配置文件 例如:application-{profile}.properties 或者 YAML variants
- 內部特定應用配置文件 例如:application-{profile}.properties 或者 YAML variants
- 外部應用配置文件 例如:application.properties 或者 YAML variants
- 內部應用配置文件 例如:application.properties 或者 YAML variants
- 加載@Configuration類的 @PropertySource 或者 @ConfigurationProperties 指向的配置文件
- 默認配置,通過SpringApplication.setDefaultProperties 設置
1.2 配置規則詳解
Config Client從Config Server中獲取配置數據的流程:
- Config Client啓動時,根據bootstrap.properties中配置的應用名稱(application)、環境名(profile)和分支名(label),向Config Server請求獲取配置數據;
- Config Server根據Config Client的請求及配置,從Git倉庫(這裏以Git爲例)中查找符合的配置文件;
- Config Server將匹配到的Git倉庫拉取到本地,並建立本地緩存;
- Config Server創建Spring的ApplicationContext實例,並根據拉取的配置文件,填充配置信息,然後將該配置信息返回給Config Client;
- Config Client獲取到Config Server返回的配置數據後,將這些配置數據加載到自己的上下文中。同時,因爲這些配置數據的優先級高於本地Jar包中的配置,因此將不再加載本地的配置。
Config Server又是如何與Git倉庫中的配置文件進行匹配的呢?通常,我們會爲一個項目建立類似如下的配置文件:
- mallweb.properties: 基礎配置文件;
- mallweb-dev.properties: 開發使用的配置文件;
- mallweb-test.properties: 測試使用的配置文件;
- mallweb-prod.properties: 生產環境使用的配置文件;
當我們訪問Config Server的端點時,就會按照如下映射關係來匹配相應的配置文件:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
上面的Url將會映射爲格式爲:{application}-{profile}.properties(yml)的配置文件。另外,label則對應Git上分支名稱,是一個可選參數,如果沒有則爲默認的master分支。
而Config-Client的bootstrap.properties配置對應如下:
- spring.application.name <==> application;
- spring.cloud.config.profile <==> profile;
- spring.cloud.config.label <==> label.
1.3 Git倉庫配置
Config Server默認使用的就是Git,所以配置也非常簡單,如上面的配置(application.properties):
spring.cloud.config.server.git.uri=http://
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
那麼客戶端在請求時服務端就會到該倉庫中進行查找。
1.3.1 使用佔位符
在服務端配置中我們也可以使用{application}、{profile} 和 {label}佔位符,如下:
spring.cloud.config.server.git.uri=http://github.com/cd826/{application}
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
這樣,我們就可以爲每一個應用客戶端創建一個單獨的倉庫。
這裏需要注意的是,如果Git的分支或標籤中包含"/“時,在{label}參數中需要使用”(_)"替代,這個主要是避免與Http URL轉義符處理的衝突。
1.3.2 模式匹配
也可以使用{application}/{profile}進行模式匹配,以便獲取到相應的配置文件。配置示例如下:
spring.cloud.config.server.git.uri=https://github.com/spring-cloud-samples/config-repo
spring.cloud.config.server.git.repos.simple=https://github.com/simple/config-repo
spring.cloud.config.server.git.repos.special.pattern=special*/dev*,*special*/dev*
spring.cloud.config.server.git.repos.special.uri=https://github.com/special/config-repo
spring.cloud.config.server.git.repos.local.pattern=local*
spring.cloud.config.server.git.repos.local.uri=file:/home/configsvc/config-repo
如果模式中需要配置多個值,那麼可以使用逗號分隔。
如果{application}/{profile}沒有匹配到任何資源,則使用spring.cloud.config.server.git.uri配置的默認URI。
當我們使用yml類型的文件進行配置時,如果模式屬性是一個YAML數組,也可以使用YAML數組格式來定義。這樣可以設置成多個配個配置文件,如:
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
repos:
development:
pattern:
- */development
- */staging
uri: https://github.com/development/config-repo
staging:
pattern:
- */qa
- */production
uri: https://github.com/staging/config-repo
1.3.3 搜索目錄
當我們把配置文件存放在Git倉庫中子目錄中時,可以通過設置serch-path來指定該目錄。同樣,serch-path也支持上面的佔位符。示例如下:
spring.cloud.config.server.git.uri=https://github.com/spring-cloud-samples/config-repo
spring.cloud.config.server.git.searchPaths=foo,bar*
這樣系統就會自動搜索foo的子目錄,以及以bar開頭的文件夾中的子目錄。
2、配置中心服務端
2.1 pom.xml
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.SR5</version>
<relativePath/>
</parent>
·······
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
踩過的坑:
- 第一次添加依賴時,還加入了springboot starter web的依賴,導致項目無法啓動。後來發現,注入spring-cloud-starter-parent的父類依賴,然後引入spring-cloud-config-server依賴後,就可以導入對應的包。
- 需要找到對應的spring-cloud的父版本依賴庫。
2.2 啓動類Application
/**
* @author aishiyushijiepingxing
* @description:添加@EnableConfigServer註解
* @date 2020/6/28
*/
@EnableConfigServer
@SpringBootApplication
public class ConfigCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication.class, args);
}
}
2.3 配置application.yml
server:
port: 7900
spring:
application:
name: config-server
cloud:
config:
server:
git:
# Git倉庫地址
uri: xxxx
# Git倉庫賬號
username: xxxx
# Git倉庫密碼
password: xxxx
#search-paths: xxxx
# 讀取分支
label: master
3、配置中心客戶端
config-client可以是任何一個基於Spring boot的應用。
3.1 pom.xml
添加cloud客戶端依賴以及cloud-context上下文環境依賴。
<dependency>
<!--Spring Cloud 上下文依賴,爲了使bootstrap配置文件生效-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.1.0.RC2</version>
</dependency>
<!--Spring Cloud Config 客戶端依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.0.RC2</version>
</dependency>
3.2 bootstrap.yml 多環境
server:
port: 8081
management:
security:
enabled: false #SpringBoot 1.5.X 以上默認開通了安全認證,如果不關閉會要求權限
spring:
application:
name: config_client
---
spring:
profiles: dev
cloud:
config:
uri: xxx #Config server的uri
profile: dev #指定的環境
label: master #指定分支
---
spring:
profiles: test
cloud:
config:
uri: xxx #Config server的uri
profile: test #指定的環境
label: master #指定分支
---
spring:
profiles: prod
cloud:
config:
uri: xxx #Config server的uri
profile: prod #指定的環境
label: master #指定分支
4、動態刷新配置
Config-Client中提供了一個refresh端點來實現配置文件的刷新。要想使用該功能,我們需要在Config-Client的pom.xml文件中增加以下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
這樣,當修改配置文件並提交到Git倉庫後,就可以使用:http://localhost:8080/refresh刷新本地的配置數據。
最好的方式還是和Spring Cloud Bus進行整合,這樣才能實現配置的自動分發,而不是需要手工去刷新配置。