Spring Cloud Config 用於爲分佈式系統中的基礎設施和微服務應用提供集中化的外部配置支持,分爲server端和client端。 server端爲分佈式配置中心,是一個獨立的微服務應用;client端爲分佈式系統中的基礎設置或微服務應用,經過指定配置中心來管理相關的配置。 Spring Cloud Config 構建的配置中心,除了適用於 Spring 構建的應用外,也能夠在任何其餘語言構建的應用中使用。 Spring Cloud Config 默認採用 Git 存儲配置信息,支持對配置信息的版本管理。html
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
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
@EnableConfigServer @SpringBootApplication public class CloudConfigServerApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigServerApplication.class, args); } }
<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>
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}部分java
spring.cloud.config.profile:對應{profile}部分git
spring.cloud.config.label:對應git的分支。若是配置中心使用的是本地存儲,則該參數無用github
spring.cloud.config.uri:配置中心的具體地址(sever端地址)web
spring.cloud.config.discovery.service-id:指定配置中心的service-id,便於擴展爲高可用配置集羣。spring
特別注意:Spring Cloud 構建於 Spring Boot 之上,在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另一種是 application, bootstrap 是應用程序的父上下文,也就是說 bootstrap 加載優先於 applicaton。bootstrap 主要用於從額外的資源來加載配置信息,還能夠在本地外部配置文件中解密屬性。這兩個上下文共用一個環境,它是任何Spring應用程序的外部屬性的來源。bootstrap 裏面的屬性會優先加載,它們默認也不能被本地相同配置覆蓋。數據庫
spring.application.name=cloud-config-client server.port=8002
// 開發環境 easy-config-dev.properties 內容爲:easy.hello=dev config // 測試環境 easy-config-test.properties 內容爲:easy.hello=test config // 生產環境 easy-config-pro.properties 內容爲:easy.hello=pro config
根據上面構建的代碼指定的項目地址爲:https://gitee.com/tqlin/spring-boot-demo.git 目錄爲: /cloud-config/config-repo/json
找到CloudConfigServerApplication.java、CloudConfigClientApplication.java分別運行bootstrap
直接訪問: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接口,訪問能夠參照如下的規則:
以easy-config-dev.properties爲例子,它的application是easy-config,profile是dev。client會根據填寫的參數來選擇讀取對應的配置。
訪問:http://localhost:8002/hello 咱們發現界面成功返回了 test config,說明測試配置文件client端讀取成功了
咱們修改bootstrap.properties配置的spring.cloud.config.profile的值爲dev,重啓client端,訪問:http://localhost:8002/hello 這時候界面返回 dev config,表示開發配置訪問成功。
原文出處:https://www.cnblogs.com/tqlin/p/11401870.html