項目模板
客戶端使用
spring boot項目目錄結構模板
├ {projectRoot}
├ pom.xml
├ test
│ └...
└ src
├ main
├ java
│ └ com...
└ resources
├ bootstrap.yml
├ logback-spring.yml
└ application-local.yml
要在應用程序中使用這些功能,只需將其構建爲依賴於spring-cloud-config-client的Spring Boot應用程序。
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEAS</spring-cloud.version>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
通常的bootstrap.yml配置方式
bootstrap.yml
spring:
profiles:
active: ${PROFILE:local}
application:
name: my-application
cloud:
config:
uri: http://config-server:8080
profile: ${spring.profiles.active}
username: yourusername
password: yourpassword
---
spring:
profiles: test,dev
cloud:
config:
uri: http://config-server:8080
— YAML可以在同一個文件中,使用—表示一個文檔的開始;
git配置倉庫目錄結構
├ {config-files-project}
├ my-application
│ ├ application-dev.yml
│ ├ application-test.yml
│ └ application-prod.yml
├ my-application1
├ application-dev.yml
├ application-test.yml
└ application-prod.yml
運行機制
如果springboot工程引入了spring-cloud-config-client,服務啓動後,configclient 默認會先訪問bootstrap.yml,獲取application.yml 配置。
環境庫
configclient會根據環境變量加載不同的配置文件,bootstrap.yml通過定義spring.profiles.active選擇不同的環境。上述模板配置spring.profiles.active=${PROFILE:local}定義了環境變量爲%PROFILE%,默認爲local。[參考][1]
本地開發
當我們在本地開發環境啓動springboot服務沒有傳任何環境變量時,會啓用local配置,也就是使用本地的application-local.yml。
開發聯調、測試環境部署
在測試環境、生產環境的配置文件一般都會不同於本地開發,這時我們可以通過啓動變量選擇不同的配置文件。
java -jar app.jar --spring.profiles.active=dev
參數 spring.profiles.active=dev將替換bootstrap.yml中的spring.profiles.active。這時應用將使用application-dev.yml配置文件,由於本地沒有application-dev.yml文件,spring-cloud-config-client將從config-server獲取配置文件。
spring-cloud-config-server
spring-cloud-config-server只是一個普通的spring boot web程序,真實的配置文件保存在git倉庫中(使用本地文件系統進行git存儲庫僅用於測試)。完整的配置文件獲取順序爲:
application -> config-server -> git repository
所以我們在gitlab或者BitBucket上必須有一個配置文件工程config-files-project,比如我們可以命名爲config-files-test。spring-cloud-config-server基於目錄結構和文件名獲取正確的配置文件。
spring-cloud-config-server默認從git上獲取資源的格式如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
爲了實現上文一個工程一個目錄的配置,我們需要在spring-cloud-config-server中做如下配置:
spring:
profiles: test
cloud:
config:
server:
git:
uri: https://gitlab.company.com/config-files-test/{application}
spring-cloud-config-client配置
spring.cloud.config.uri定義了spring config server的位置,spring-cloud-config-client根據三個變量從config server獲取變量:
{application}
映射到客戶端的“spring.application.name”;{profile}
映射到客戶端上的“spring.profiles.active”(逗號分隔列表); 和{label}
這是一個服務器端功能,標記“版本”的一組配置文件。默認master
在開發聯調環境通過以上配置,my-application這個工程就可以取到git倉庫中https://gitlab.company.com/config-files-test/my-application/application-dev.yml了。
生產環境部署
生產環境的配置文件一般由運維人員配置,開發人員通常沒有權限訪問。這時需要獨立一個git倉庫專門給生產使用,爲了做環境隔離,我們也單獨部署一個spring-config-server實例。在配置文件中spring-cloud-config-server增加這樣的配置:
---
spring:
profiles: prod
cloud:
config:
server:
git:
uri: https://gitlab.company.com/config-files-prod/{application}
在客戶端bootstrap.yml的配置中,可以根據不同的profile訪問不同的config-server,具體見bootstrap.yml配置模板。
[1]: https://blog.csdn.net/IT_faquir/article/details/80869578 “SpringBoot配置加載,各配置文件優先級對比”
[2]: https://springcloud.cc/spring-cloud-config.html “Spring Cloud Config”
[3]: https://my.oschina.net/u/1469495/blog/1522784 “Spring boot 激活 profile的幾種方式”
[4]: http://www.ruanyifeng.com/blog/2016/07/yaml.html “YAML 語言教程”
[5]: https://blog.csdn.net/J080624/article/details/80508606 “SpringBoot - 配置文件加載位置與優先級”
[6]: https://blog.csdn.net/it_faquir/article/details/79842885 “yaml語法及規範”
[7]: https://blog.51cto.com/blief/2118978 “spring boot/cloud 啓動方式說明”