跟我學Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Config

通過前文講解,至此,微服務架構已經日趨完善——如今已經能夠作一個大型的應用了!然而,隨着項目的迭代,微服務數目每每與日俱增,如何高效地管理配置成爲咱們必須解決的問題。本節來討論如何使用Spring Cloud Config管理配置。git

爲何要使用配置中心

  • 集中管理配置。一個使用微服務架構的應用系統可能會包含成百上千個微服務,所以集中管理配置是很是有必要的;
  • 不一樣環境,不一樣配置。例如,數據源配置在不一樣的環境(開發、測試、預發佈、生產等)中是不一樣的;
  • 運行期間可動態調整。例如,咱們可根據各個微服務的負載狀況,動態調整數據源鏈接池大小或熔斷閾值,而且在調整配置時不中止微服務;
  • 配置修改後可自動更新。如配置內容發生變化,微服務可以自動更新配置。

Spring Cloud Config簡介

Spring Cloud Config爲分佈式系統外部化配置提供了服務器端和客戶端的支持,它包括Config Server和Config Client兩部分。因爲Config Server和Config Client都實現了對Spring Environment和PropertySource抽象的映射,所以,Spring Cloud Config很是適合Spring應用程序,固然也可與任何其餘語言編寫的應用程序配合使用。github

Config Server是一個可橫向擴展、集中式的配置服務器,它用於集中管理應用程序各個環境下的配置,默認使用Git存儲配置內容(也可以使用Subversion、MySQL、本地文件系統或Vault存儲配置,本博客以Git爲例進行講解),所以能夠很方便地實現對配置的版本控制與內容審計。spring

Config Client是Config Server的客戶端,用於操做存儲在Config Server中的配置屬性。引入Spring Cloud Config後的架構以下:bootstrap

Spring Cloud Config架構圖

TIPS服務器

Spring Cloud Config的GitHub:https://github.com/spring-cloud/spring-cloud-config架構

編寫Config Server

示例

  • 加依賴app

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  • 加註解:@EnableConfigServer分佈式

  • 寫配置:微服務

    server:
      port: 8080
    spring:
      application:
        name: microservice-config-server
      cloud:
        config:
          server:
            git:
              # Git倉庫地址
              uri: https://git.oschina.net/itmuch/spring-cloud-config-repo.git
              # Git倉庫帳號
              username:
              # Git倉庫密碼
              password:

路徑規則

Spring Cloud Config Server提供了RESTful API,可用來訪問存放在Git倉庫中的配置文件。測試

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

其中的{appliation}、{profile}、{label} 都是佔位符。

TIPS

事實上,可以使用Spring Cloud Config實現配置的「繼承」與「組合」,舉個例子——

假設有一個應用:microservice-foo ,其profile是dev,那麼其實Spring Cloud Config會查找以下幾個文件:

  • microservice-foo-dev.yml
  • microservice-foo.yml
  • application-dev.yml
  • application.yml

對於相同屬性的配置,從上至下優先級逐漸遞減;最終得到的配置屬性是四個文件的組合。由此,不難分析,可以下規劃幾個配置文件:

  • microservice-foo-dev.yml 做爲指定應用在指定profile下的配置文件
  • microservice-foo.yml 做爲制定應用在任何profile下都通用的配置文件
  • application-dev.yml 做爲全部應用在指定profile下的配置文件
  • application.yml 做爲全部應用在任何profile下都通用的配置文件

測試

  • 訪問http://localhost:8080/microservice-foo-dev.yml 可訪問到Git倉庫的microservice-foo-dev.properties 並組合application.properties

集成Config Client

編碼

  • 加依賴

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  • 加配置:applicaiton.yml

    server:
      port: 8081
  • 加配置:bootstrap.yml

    spring:
      application:
        name: microservice-foo    # 對應config server所獲取的配置文件的{application}
      cloud:
        config:
          uri: http://localhost:8080/
          profile: dev            # profile對應config server所獲取的配置文件中的{profile} 
          label: master           # 指定Git倉庫的分支,對應config server所獲取的配置文件的{label}

    其中:

    spring.application.name:對應Config Server所獲取的配置文件中的{application} ;

    spring.cloud.config.uri:指定Config Server的地址,默認是http://localhost:8888

    spring.cloud.config.profile:profile對應Config Server所獲取的配置文件中的{profile} ;

    spring.cloud.config.label:指定Git倉庫的分支,對應Config Server所獲取配置文件的{label}。

    值得注意的是,以上屬性應配置在bootstrap.yml,而不是application.yml中。若是配置在application.yml中,該部分配置就不能正常工做。例如,Config Client會鏈接spring.cloud.config.uri的默認值 http://localhost:8888 ,而並不是咱們配置的 http://localhost:8080/

    Spring Cloud有一個「引導上下文」的概念,這是主應用程序上下文(Application Context)的父上下文。引導上下文負責從配置服務器加載配置屬性,以及解密外部配置文件中的屬性。和主應用程序加載application.* (yml或properties)中的屬性不一樣,引導上下文加載bootstrap.* 中的屬性。配置在bootstrap.* 中的屬性有更高的優先級,所以默認狀況下它們不能被本地配置覆蓋。

  • 寫代碼

    @RestController
    public class ConfigClientController {
      @Value("${profile}")
      private String profile;
    
      @GetMapping("/profile")
      public String hello() {
        return this.profile;
      }
    }

測試

訪問http://localhost:8081/profile 可返回Git倉庫中的配置屬性。

配套代碼

Config Server

Config Client

本文首發

http://www.itmuch.com/spring-cloud/finchley-19/

乾貨分享

全是乾貨哦!

相關文章
相關標籤/搜索