Spring Boot有一個非常重要的改變就是簡化了配置,使用application.properties文件定義了很多默認(rèn)配置(參考之前的文章:http://www.itdecent.cn/p/860addd7865d)。
但是配置文件分開管理來還是比較麻煩的,而且環(huán)境越多配置約容易出問題。Spring Cloud提供了一種統(tǒng)一配置的方案:Spring Cloud Config Server。
Spring Cloud Config項(xiàng)目是一個解決分布式系統(tǒng)的配置管理方案。它包含了Client和Server兩個部分。
Server端配置
Spring Cloud Config Server本質(zhì)上也是一個Spring Boot的web項(xiàng)目,只需要添加對應(yīng)的parent,然后加入相關(guān)的依賴就可以啟動這個工程了。
Maven的pom.xml中需要添加以下內(nèi)容:
<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
啟動項(xiàng)目的代碼:
@SpringBootApplication
@EnableConfigServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
其實(shí)和一般的SpringBoot項(xiàng)目啟動沒有什么區(qū)別,只是多了一個@EnableConfigServer注解。
配置環(huán)境倉庫( Environment Repository )
上面的application.properties中有一個
spring.cloud.config.server.git.uri=file://Users/whthomas/config-repo
這個配置指的項(xiàng)目配置倉庫的位置,這個位置可以是:git文件夾、svn文件夾或者github項(xiàng)目位置,任何能訪問到文件的地方。
環(huán)境倉庫(例子中的文件夾中)中提供環(huán)境配置對象配資源給Config Server發(fā)布給各個consumer使用。
環(huán)境資源的命名規(guī)則由以下的三個參數(shù)確定:
- {application}映射到Config客戶端的spring.application.name屬性
- {profile}映射到Config客戶端的spring.profiles.active屬性,可以用來區(qū)分環(huán)境,比如dev,test,produce等等
- {label}映射到Git服務(wù)器的commit id,分支名稱或者tag,默認(rèn)值為master
倉庫中的配置文件會被轉(zhuǎn)換成web接口,訪問可以參照以下的規(guī)則:
/{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會根據(jù)填寫的參數(shù)來選擇讀取對應(yīng)的配置。
那么接下去來看client端的處理。
Client端配置
創(chuàng)建一個普通的SpringBoot項(xiàng)目,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節(jié)點(diǎn)下添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
resource目錄下的application.properties添加這樣幾個配置:
# 配置中心服務(wù)的地址
spring.cloud.config.uri=localhost:8888
# 要讀取的配置文件application屬性
spring.cloud.config.name=cloud-config
# 要讀取的配置文件profile屬性,默認(rèn)是dev
spring.cloud.config.profile=${config.profile:dev}
以上的幾個配置也可以在命令行啟動jar時填寫。
以上配置完成之后,在遠(yuǎn)端配置中心的對應(yīng)的配置就會加載到項(xiàng)目中,和本地使用application.properties配置中添加配置是幾乎一樣的效果,使用@Value注解的配置也可以順利讀取到對應(yīng)的配置。
寫在后面
初步感受了下Spring Cloud Config項(xiàng)目,感覺還是一個相對底層的解決方案,各個方面還是特別成熟,比如在線更新配置還需要client開啟web hock才能實(shí)現(xiàn),如果client不是一個web項(xiàng)目,那更新配置就瞎了。相比之下,百度開源的disconf(https://github.com/knightliao/disconf )還是目前比較理想的配置中心方案。