使用Spring Cloud構建統一配置中心

Spring Boot有一個非常重要的改變就是簡化了配置,使用application.properties文件定義了很多默認配置(參考之前的文章:http://www.lxweimin.com/p/860addd7865d)。

但是配置文件分開管理來還是比較麻煩的,而且環境越多配置約容易出問題。Spring Cloud提供了一種統一配置的方案:Spring Cloud Config Server。

Spring Cloud Config項目是一個解決分布式系統的配置管理方案。它包含了ClientServer兩個部分。

Server端配置

Spring Cloud Config Server本質上也是一個Spring Boot的web項目,只需要添加對應的parent,然后加入相關的依賴就可以啟動這個工程了。

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

啟動項目的代碼:

@SpringBootApplication
@EnableConfigServer
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

其實和一般的SpringBoot項目啟動沒有什么區別,只是多了一個@EnableConfigServer注解。

配置環境倉庫( Environment Repository )

上面的application.properties中有一個

spring.cloud.config.server.git.uri=file://Users/whthomas/config-repo

這個配置指的項目配置倉庫的位置,這個位置可以是:git文件夾svn文件夾或者github項目位置,任何能訪問到文件的地方。

環境倉庫(例子中的文件夾中)中提供環境配置對象配資源給Config Server發布給各個consumer使用。

環境資源的命名規則由以下的三個參數確定:

  • {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注解的配置也可以順利讀取到對應的配置。

寫在后面

初步感受了下Spring Cloud Config項目,感覺還是一個相對底層的解決方案,各個方面還是特別成熟,比如在線更新配置還需要client開啟web hock才能實現,如果client不是一個web項目,那更新配置就瞎了。相比之下,百度開源的disconfhttps://github.com/knightliao/disconf )還是目前比較理想的配置中心方案。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容