Spring Cloud(九)Config配置中心
阿新 • • 發佈:2018-11-02
1 概述
1.1 產生背景
分散式系統中,會將服務拆分成一個個獨立的服務,這些服務都要通過配置檔案配置相應資訊才能執行,隨著系統內微服務數量的增多,配置檔案也會不斷的增多,大量的配置檔案的管理成為一個繁瑣的問題。因此一套集中式的、動態的配置管理設施是必不可少的。
1.2 Spring Cloud Config配置中心
- SpringCloud Config是一個提供外部集中式配置管理的元件,配置伺服器為各種不同的微服務提供了一箇中心化的外部配置中心。
- SpringCloud Config分為服務端和客戶端兩部分:
- 服務端:分散式配置中心,是一個獨立的微服務,用來連線併為客戶端提供配置、加密/解密資訊等訪問入口。
- 客戶端:通過指定的配置中心獲取配置資源,推薦用git來儲存配置檔案。
1.3 解決的問題
- 集中管理配置檔案。
- 不同的環境對應不同的配置,動態化的更新配置。
- 不需要在每個服務部署的機器上編寫配置檔案,讓服務中心統一為服務拉取配置檔案。
- 將配置資訊以REST介面形式暴露。
2 案例
2.1 服務端
- 新建github遠端倉庫,如:spring-cloud-config。
- 將新建的遠端倉庫克隆到本地,新建date-dev.yml配置檔案,將該檔案上傳至遠端倉庫。
date:
description: today is 2018-10-31
# 儲存為UTF-8格式
- 新建config server服務模組,加入依賴。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
- appliacation.yml配置檔案。
server:
port: 2233
spring:
application:
name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/chenyulin19930815/spring-cloud-config.git #GitHub上面的git倉庫名字
username: xxx #git倉庫使用者名稱
password: xxxx #github倉庫密碼
- 編寫主啟動類,加入@EnableConfigServer註解。
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerApplication2233 {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication2233.class, args);
}
}
- 啟動服務,訪問配置檔案。
有以下訪問規則:
- {application}:配置檔案的檔名
- {profile}:讀取的環境
- {lable}:分支
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
- 本示例有以下訪問方式。
- http://localhost:2233/date-dev.yml
- http://localhost:2233/master/date-dev.yml
- http://localhost:2233/date-dev.yml/master
2.2 客戶端
- 在本地倉庫新建spring-cloud-config-client.yml上傳至遠端倉庫。
spring:
profiles:
active: dev
---
server:
port: 2244
spring:
profiles: dev
application:
name: spring-cloud-config-client
---
server:
port: 2245
spring:
profiles: test
application:
name: spring-cloud-config-client
- 新建config client服務模組,匯入依賴。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 新建bootstrap.yml1配置檔案。
spring:
cloud:
config:
name: spring-cloud-config-client #從git倉庫讀取的配置檔案資源名稱,沒有後綴
profile: dev
label: master
uri: http://localhost:2233 #本微服務啟動後先去找配置中心服務端,以獲取GitHub的服務地址
- 新建application.yml
spring:
application:
name: spring-cloud-config-client
- 啟動類。
@SpringBootApplication
public class SpringCloudConfigClientApplication2244 {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigClientApplication2244.class, args);
}
}
- web測試controller。
@RestController
public class ConfigController {
@Value("${spring.application.name}")
private String applicationName;
@Value("${spring.profiles}")
private String springProfiles;
@Value("${server.port}")
private String port;
@RequestMapping("/config")
public String getConfig() {
return "applicationName: " + applicationName +"<br/>springProfiles:"+springProfiles+"<br/>port: " + port;
}
}
- 測試:依次啟動config server,config client服務。
首先,dev啟動的埠為2244,訪問:http://localhost:2244/config
然後,更改bootstrap.yml中profile: dev為profile: test,埠切換為了2245,訪問:http://localhost:2245/config
遺留問題
將spring-cloud-config-client.yml檔案中test的spring.application.name=spring-cloud-config-client改為spring.application.name=spring-cloud-config-client-test,再次訪問發現名字沒有更換,說明修改github配置資訊後,系統不會主動去獲取。
一,bootstrap.yml比application.yml具有更高的優先順序。
二,bootstrap.yml是系統級的資源配置項,application.yml是使用者級的資源配置項。
三,SpringCloud會建立"BootStrap Context"作為"ApplicationContext"的父上下文。初始化的時候BootStrap Context負責從外部源載入配置屬性並解析。這兩個上下文共享一個"Environment",BootStrap 具有更高優先順序,他們不會被本地配置覆蓋。 ↩︎