spring-cloud-config 示例
spring-cloud-config 配置中心實現
Spring Cloud Config 用於為分散式系統中的基礎設施和微服務應用提供集中化的外部配置支援,分為server端和client端。server端為分散式配置中心,是一個獨立的微服務應用;client端為分散式系統中的基礎設定或微服務應用,通過指定配置中心來管理相關的配置。Spring Cloud Config 構建的配置中心,除了適用於 Spring 構建的應用外,也可以在任何其他語言構建的應用中使用, 預設採用 Git 儲存配置資訊,支援對配置資訊的版本管理。
本示例主要內容
- 配置中心演示client端和server端實現
- 配置檔案放在git(因github有時候不太穩定,我放到了國內伺服器)
- 版本切換(test、pro、dev)
Spring Cloud Config 特點
- 提供server端和client端支援(Spring Cloud Config Server和Spring Cloud Config Client);
- 集中式管理分散式環境下的應用配置;
- 基於Spring環境,實現了與Spring應用無縫整合;
- 可用於任何語言開發的程式;
- 預設實現基於Git倉庫(也支援SVN),從而可以進行配置的版本管理;同時也支援配置從本地檔案或資料庫讀取。
程式碼構建
server端實現
1.pom.xml新增maven依賴
<dependencies>
<dependency >
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
複製程式碼
2.application.yml配置
server:
port: 8001
spring:
application:
name: cloud-config-server
cloud:
config:
server:
git:
uri: https://gitee.com/tqlin/spring-boot-demo.git #因為國內github不穩定,我這裡改到了碼雲倉
searchPaths: /cloud-config/config-repo/ #配置檔案目錄
force-pull: true
複製程式碼
3.CloudConfigServerApplication.java啟動類
@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigServerApplication.class,args);
}
}
複製程式碼
client端實現
1.pom.xml新增maven依賴
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
複製程式碼
2.bootstrap.properties配置檔案
spring.cloud.config.name=easy-config
spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=master
複製程式碼
-
spring.application.name:對應{application}部分
-
spring.cloud.config.profile:對應{profile}部分
-
spring.cloud.config.label:對應git的分支。如果配置中心使用的是本地儲存,則該引數無用
-
spring.cloud.config.uri:配置中心的具體地址(sever端地址)
-
spring.cloud.config.discovery.service-id:指定配置中心的service-id,便於擴充套件為高可用配置叢集。
特別注意:Spring Cloud 構建於 Spring Boot 之上,在 Spring Boot 中有兩種上下文,一種是 bootstrap,另外一種是 application,bootstrap 是應用程式的父上下文,也就是說 bootstrap 載入優先於 applicaton。bootstrap 主要用於從額外的資源來載入配置資訊,還可以在本地外部配置檔案中解密屬性。這兩個上下文共用一個環境,它是任何Spring應用程式的外部屬性的來源。bootstrap 裡面的屬性會優先載入,它們預設也不能被本地相同配置覆蓋。
3.application.properties配置檔案
spring.application.name=cloud-config-client
server.port=8002
複製程式碼
執行示例
1.首先在碼雲上面建立一個資料夾config-repo用來存放配置檔案,我們建立以下三個配置檔案:
// 開發環境
easy-config-dev.properties 內容為:easy.hello=dev config
// 測試環境
easy-config-test.properties 內容為:easy.hello=test config
// 生產環境
easy-config-pro.properties 內容為:easy.hello=pro config
複製程式碼
根據上面構建的程式碼指定的專案地址為:gitee.com/tqlin/sprin… 目錄為: /cloud-config/config-repo/
2.分別執行server端和client端
找到CloudConfigServerApplication.java、CloudConfigClientApplication.java分別執行
3.測試server端
直接訪問:http://localhost:8001/easy-config/dev
我們看到成功返回了開發配置檔案資訊
{
name: "easy-config",profiles: [
"dev"
],label: null,version: "6053b4c1c2343ac27e822b2a9b60c6343be72f96",state: null,propertySources: [
{
name: "https://gitee.com/tqlin/spring-boot-demo.git/cloud-config/config-repo/easy-config-dev.properties",source: {
easy.hello: "dev config"
}
}
]
}
複製程式碼
訪問:http://localhost:8001/easy-config/test、http://localhost:8001/easy-config/pro,相應的會返回測試及正式環境的配置
倉庫中的配置檔案會被轉換成web介面,訪問可以參照以下的規則:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
以easy-config-dev.properties為例子,它的application是easy-config,profile是dev。client會根據填寫的引數來選擇讀取對應的配置。
4.測試client端
訪問:http://localhost:8002/hello 我們發現介面成功返回了 test config,說明測試配置檔案client端讀取成功了
我們修改bootstrap.properties配置的spring.cloud.config.profile的值為dev,重啟client端,訪問:http://localhost:8002/hello 這時候介面返回 dev config,表示開發配置訪問成功。