天天看點

Spring Cloud建構微服務架構分布式配置中心

Spring Cloud Config是Spring Cloud團隊建立的一個全新項目,用來為分布式系統中的基礎設施和微服務應用提供集中化的外部配置支援,它分為服務端與用戶端兩個部分。其中服務端也稱為分布式配置中心,它是一個獨立的微服務應用,用來連接配接配置倉庫并為用戶端提供擷取配置資訊、加密/解密資訊等通路接口;而用戶端則是微服務架構中的各個微服務應用或基礎設施,它們通過指定的配置中心來管理應用資源與業務相關的配置内容,并在啟動的時候從配置中心擷取和加載配置資訊。Spring Cloud Config實作了對服務端和用戶端中環境變量和屬性配置的抽象映射,是以它除了适用于Spring建構的應用程式之外,也可以在任何其他語言運作的應用程式中使用。由于Spring Cloud Config實作的配置中心預設采用Git來存儲配置資訊,是以使用Spring Cloud Config建構的配置伺服器,天然就支援對微服務應用配置資訊的版本管理,并且可以通過Git用戶端工具來友善的管理和通路配置内容。當然它也提供了對其他存儲方式的支援,比如:SVN倉庫、本地化檔案系統。

在本文中,我們将學習如何建構一個基于Git存儲的分布式配置中心,并對用戶端進行改造,并讓其能夠從配置中心擷取配置資訊并綁定到代碼中的整個過程。

準備配置倉庫

  • 準備一個git倉庫,可以在碼雲或Github上建立都可以。
  • 假設我們讀取配置中心的應用名為

    config-client

    ,那麼我們可以在git倉庫中該項目的預設配置檔案

    config-client.yml

1
     
     
      2
           
info:
     
     
        profile: default
           
  • 為了示範加載不同環境的配置,我們可以在git倉庫中再建立一個針對dev環境的配置檔案

    config-client-dev.yml

1
     
     
      2
           
info:
     
     
        profile: dev
           

建構配置中心

通過Spring Cloud Config來建構一個分布式配置中心非常簡單,隻需要三步:

  • 建立一個基礎的Spring Boot工程,命名為:

    config-server-git

    ,并在

    pom.xml

    中引入下面的依賴(省略了parent和dependencyManagement部分):
1
     
     
      2
     
     
      3
     
     
      4
     
     
      5
     
     
      6
           
<dependencies>
     
     	
      <dependency>
     
     		
      <groupId>org.springframework.cloud
      </groupId>
     
     		
      <artifactId>spring-cloud-config-server
      </artifactId>
     
     	
      </dependency>
     
     
      </dependencies>
           
  • 建立Spring Boot的程式主類,并添加

    @EnableConfigServer

    注解,開啟Spring Cloud Config的服務端功能。
1
     
     
      2
     
     
      3
     
     
      4
     
     
      5
     
     
      6
     
     
      7
     
     
      8
     
     
      9
           
@EnableConfigServer
     
     
      @SpringBootApplication
     
     
      public 
      class Application {
     
     
     	
      public static void main(String[] args) {
     
     		
      new SpringApplicationBuilder(Application.class).web(
      true).run(args);
     
     
      	}
     
     
     
      }
           
  • application.yml

    中添加配置服務的基本資訊以及Git倉庫的相關資訊,例如:
1
     
     
      2
     
     
      3
     
     
      4
     
     
      5
     
     
      6
     
     
      7
     
     
      8
     
     
      9
     
     
      10
           
spring
     
     
        application:
     
     
          name: config-server
     
     
        cloud:
     
     
          config:
     
     
            server:
     
     
              git:
     
     
                uri: http://git.oschina.net/didispace/config-repo-demo/
     
     
      server:
     
     
        port: 
      1201
           

到這裡,使用一個通過Spring Cloud Config實作,并使用Git管理配置内容的分布式配置中心就已經完成了。我們可以将該應用先啟動起來,確定沒有錯誤産生,然後再嘗試下面的内容。

如果我們的Git倉庫需要權限通路,那麼可以通過配置下面的兩個屬性來實作;

spring.cloud.config.server.git.username:通路Git倉庫的使用者名

spring.cloud.config.server.git.password:通路Git倉庫的使用者密碼

完成了這些準備工作之後,我們就可以通過浏覽器、POSTMAN或CURL等工具直接來通路到我們的配置内容了。通路配置資訊的URL與配置檔案的映射關系如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

上面的url會映射

{application}-{profile}.properties

對應的配置檔案,其中

{label}

對應Git上不同的分支,預設為master。我們可以嘗試構造不同的url來通路不同的配置内容 并獲得如下傳回:

1
     
     
      2
     
     
      3
     
     
      4
     
     
      5
     
     
      6
     
     
      7
     
     
      8
     
     
      9
     
     
      10
     
     
      11
     
     
      12
     
     
      13
     
     
      14
     
     
      15
     
     
      16
     
     
      17
     
     
      18
     
     
      19
     
     
      20
     
     
      21
     
     
      22
     
     
      23
           
{
     
         
      "name": 
      "config-client",
     
         
      "profiles": [
     
             
      "dev"
     
     
          ],
     
         
      "label": 
      "master",
     
         
      "version": 
      null,
     
         
      "state": 
      null,
     
         
      "propertySources": [
     
     
              {
     
                 
      "name": 
      "http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml",
     
                 
      "source": {
     
                     
      "info.profile": 
      "dev"
     
     
                  }
     
     
              },
     
     
              {
     
                 
      "name": 
      "http://git.oschina.net/didispace/config-repo-demo/config-client.yml",
     
                 
      "source": {
     
                     
      "info.profile": 
      "default"
     
     
                  }
     
     
              }
     
     
          ]
     
     
      }
           

我們可以看到該Json中傳回了應用名:config-client,環境名:dev,分支名:master,以及default環境和dev環境的配置内容。

從現在開始,我這邊會将近期研發的springcloud微服務雲架構的搭建過程和精髓記錄下來,幫助更多有興趣研發spring cloud架構的朋友,希望可以幫助更多的好學者。大家來一起探讨spring cloud架構的搭建過程及如何運用于企業項目。源碼來源