1. 程式人生 > >走進Spring Cloud之八 SpringCloud Config(配置中心)(Greenwich版本)

走進Spring Cloud之八 SpringCloud Config(配置中心)(Greenwich版本)

走進Spring Cloud之八 SpringCloud Config(配置中心)(Greenwich版本)

Spring Boot profile

在Spring Boot應用中,我們可以將配置內容寫入application.yml,設定多個profile,也可以用多個application-{profile}.properties檔案配置,並在啟動時指定--spring.profiles.active={profile}來載入不同環境下的配置。

在Spring Cloud微服務架構中,這種方式未必適用,微服務架構對配置管理有著更高的要求,如:

  • 集中管理:成百上千(可能沒這麼多)個微服務需要集中管理配置,否則維護困難、容易出錯;
  • 執行期動態調整:某些引數需要在應用執行時動態調整(如連線池大小、熔斷閾值等),並且調整時不停止服務;
  • 自動更新配置:微服務能夠在配置發生變化是自動更新配置。

以上這些要求,傳統方式是無法實現的,所以有必要藉助一個通用的配置管理機制,通常使用配置伺服器來管理配置。

Spring Cloud Config

Spring Cloud Config 為分散式系統的外部配置提供了服務端和客戶端的支援方案。在配置的服務端您可以在所有環境中為應用程式管理外部屬性的中心位置。客戶端和服務端概念上的Spring Environment

PropertySource 抽象保持同步, 它們非常適合Spring應用程式,但是可以與任何語言中執行的應用程式一起使用。當應用程式在部署管道中從一個開發到測試直至進入生產時,您可以管理這些環境之間的配置,並確保應用程式在遷移時具有它們需要執行的所有內容。伺服器儲存後端的預設實現使用git,因此它很容易支援標記版本的配置環境,並且能夠被管理內容的各種工具訪問。很容易新增替代的實現,並用Spring配置將它們插入。

Spring Cloud Config 包含了Client和Server兩個部分,server提供配置檔案的儲存、以介面的形式將配置檔案的內容提供出去,client通過介面獲取資料、並依據此資料初始化自己的應用。Spring cloud使用git或svn存放配置檔案,預設情況下使用git,我們先以git為例做一套示例。

springcloud-config-repo(遠端git配置倉庫)

我這裡先在 我的scexample GitHub倉庫建立遠端配置儲存倉庫springcloud-config-repo。並且建立了3個遠端配置檔案。

#開發使用遠端雲配置
springcloud-config-dev.yml
#生產使用遠端雲配置
springcloud-config-pro.yml
#測試使用遠端雲配置
springcloud-config-test.yml

配置資訊分別為
springcloud-config-dev.yml

writer: jason(cry)

springcloud-config-pro.yml

writer: jason(silent)

springcloud-config-test.yml

writer: jason(smile)

config-server(配置服務端)

我們新建一個子moudle->config-server

pom.xml

在pom.xml中新增spring-cloud-config-server依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>scexample</artifactId>
        <groupId>com.pubutech</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.yml

resources目錄下新建application.yml配置檔案,配置資訊如下:

server:
  port: 8888

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/Jaysong2012/scexample     # 配置git倉庫的地址
          search-paths: springcloud-config-repo                             # git倉庫地址下的相對地址,可以配置多個,用,分割。
          username:                                             # git倉庫的賬號(私有庫必填)
          password:                                             # git倉庫的密碼(私有庫必填)
      label: master                                        #配置git倉庫的分支

這裡需要注意:
Spring Cloud Config也提供本地儲存配置的方式。我們只需要設定屬性spring.profiles.active=native,Config Server會預設從應用的src/main/resource目錄下檢索配置檔案。也可以通過spring.cloud.config.server.native.searchLocations=file:/properties/屬性來指定配置檔案的位置。雖然Spring Cloud Config提供了這樣的功能,但是為了支援更好的管理內容和版本控制的功能,還是推薦使用git的方式。

ConfigServerApplication.java

新建Spring BootApplication ConfigServerApplication

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//開啟配置伺服器
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

啟動測試

訪問http://localhost:8888/springcloud-config/pro/master
8888
結果如下:

{
    "name":"springcloud-config",
    "profiles":[
        "pro"
    ],
    "label":"master",
    "version":"a669f6077492e5988bc21a3a3da2eaead7578469",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/Jaysong2012/scexample/springcloud-config-repo/springcloud-config-pro.yml",
            "source":{
                "writer":"jason(silent)"
            }
        }
    ]
}

我們成功的讀取了遠端的配置檔案

繼續訪問http://localhost:8888/master/springcloud-config-pro.yml
yml
我們可以直接拿到整個配置資訊。

http請求地址和資原始檔對映規則如下:

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

通過這裡和上面返回的json結果,我們不難看出來

  • name : 對應profile的配置名稱字首(也理解為application的配置字首)
  • profile:對應application的配置profile
  • label:對應遠端倉庫的分支

config-client(讀取遠端配置的客戶端)

我們新建一個子moudle->config-client

pom.xml

修改pom.xml新增spring-cloud-starter-config依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>scexample</artifactId>
        <groupId>com.pubutech</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

resources目錄下新建application.yml本配置

server:
  port: 8889

spring:
  application:
    name: config-client

bootstrap.yml

resources目錄下新建bootstrap.yml遠端配置

spring:
  cloud:
    config:
      name: springcloud-config             #對應{application}部分
      profile: pro                         #對應{profile}部分
      uri: http://localhost:8888/          #配置中心的具體地址
      label: master                        #對應git的分支。如果配置中心使用的是本地儲存,則該引數無用
      discovery:
        service-id: config-server      #指定配置中心的service-id,便於擴充套件為高可用配置叢集。

ConfigClientApplication.java

新建啟動SpringBoot Application

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

}

新建測試載入遠端配置的案例Controller

@RestController
public class ConfigClientController {

    @Value("${writer}")
    String writer;

    @RequestMapping(value = "/writer")
    public String writer(){
        return writer;
    }

}

啟動測試

訪問http://localhost:8889/writer
writer

GitHub原始碼