Spring Boot有一個很是重要的改變就是簡化了配置,使用application.properties文件定義了不少默認配置。願意瞭解源碼的朋友直接求求交流分享技術 一零三八七七四六二六git
可是配置文件分開管理來仍是比較麻煩的,並且環境越多配置約容易出問題。Spring Cloud提供了一種統一配置的方案:Spring Cloud Config Server。github
Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分。web
Server端配置spring
Spring Cloud Config Server本質上也是一個Spring Boot的web項目,只須要添加對應的parent,而後加入相關的依賴就能夠啓動這個工程了。bash
Maven的pom.xml中須要添加如下內容:服務器
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.BUILD-SNAPSHOT</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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>
<!-- repositories also needed for snapshots and milestones -->
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
config server的resource目錄下的application.properties:
server.port=8888
spring.cloud.config.server.git.uri=file://Users/whthomas/config-repo
spring.application.name=configserver
spring.cloud.config.uri=http://localhost:8888
複製代碼
啓動項目的代碼:app
@SpringBootApplication
@EnableConfigServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
複製代碼
其實和通常的SpringBoot項目啓動沒有什麼區別,只是多了一個@EnableConfigServer註解。 配置環境倉庫( Environment Repository ) 上面的application.properties中有一個maven
spring.cloud.config.server.git.uri=file://Users/whthomas/config-repo
複製代碼
這個配置指的項目配置倉庫的位置,這個位置能夠是:git文件夾、svn文件夾或者github項目位置,任何能訪問到文件的地方。分佈式
環境倉庫(例子中的文件夾中)中提供環境配置對象配資源給Config Server發佈給各個consumer使用。svn
環境資源的命名規則由如下的三個參數肯定:
{application}映射到Config客戶端的spring.application.name屬性
{profile}映射到Config客戶端的spring.profiles.active屬性,能夠用來區分環境,好比dev,test,produce等等
{label}映射到Git服務器的commit id,分支名稱或者tag,默認值爲master
倉庫中的配置文件會被轉換成web接口,訪問能夠參照如下的規則:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
複製代碼
舉個栗子: 我在配置中心的目錄下放置文件:
cloud-config-rd.properties
cloud-config-dev.properties
cloud-config-test.properties
cloud-config-test.properties
複製代碼
以cloud-config-rd.properties爲例子,它的application是cloud-config,profile是rd.client會根據填寫的參數來選擇讀取對應的配置。
那麼接下去來看client端的處理。
Client端配置
建立一個普通的SpringBoot項目,pom.xml中加入Spring Cloud的配置。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RC2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
複製代碼
pom.xml中的dependencies節點下添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
複製代碼
resource目錄下的application.properties添加這樣幾個配置: 配置中心服務的地址 spring.cloud.config.uri=localhost:8888 要讀取的配置文件application屬性 spring.cloud.config.name=cloud-config 要讀取的配置文件profile屬性,默認是dev spring.cloud.config.profile=${config.profile:dev}
以上的幾個配置也能夠在命令行啓動jar時填寫。 以上配置完成以後,在遠端配置中心的對應的配置就會加載到項目中,和本地使用application.properties配置中添加配置是幾乎同樣的效果,使用@Value註解的配置也能夠順利讀取到對應的配置。 完整項目源碼