1. 程式人生 > >spring-cloud(六)分散式配置中心(Spring Cloud Config)(Finchley版本)

spring-cloud(六)分散式配置中心(Spring Cloud Config)(Finchley版本)

隨著線上專案變的日益龐大,每個專案都散落著各種配置檔案,如果採用分散式的開發模式,需要的配置檔案隨著服務增加而不斷增多。某一個基礎服務資訊變更,都會引起一系列的更新和重啟,運維苦不堪言也容易出錯。配置中心便是解決此類問題的靈丹妙藥。

Spring Cloud Config專案是一個解決分散式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置檔案的儲存、以介面的形式將配置檔案的內容提供出去,client通過介面獲取資料、並依據此資料初始化自己的應用。Spring cloud使用git或svn存放配置檔案,預設情況下使用git

首先在github上面建立了一個資料夾config-repo用來存放配置檔案,為了模擬生產環境,我們建立以下三個配置檔案

每個配置檔案中都寫一個屬性config=i am dev/pro/test
建立maven父工程 springcloudconfig

pom檔案如下

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xuxu</groupId>
    <artifactId>springcloudconfig</artifactId>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>config-server</module>
        <module>config-client</module>
    </modules>

    <name>springcloudconfig</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

config服務端

建立module config-server

pom檔案

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xuxu</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.xuxu</groupId>
        <artifactId>springcloudconfig</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

啟動類上加上@EnableConfigServer註解

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

配置檔案application.yml

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xukai19891114/springCloudConfig
          search-paths: config-repo
          username:
          password:
      label: master
  application:
    name: config-server
server:
  port: 8888

上面是連線github的配置檔案 

  • uri:github地址
  • search-paths:對應搜尋配置檔案時的倉庫相對路徑 可以是多個用  ,  號隔開(本例中對應配置檔案存放目錄路徑)
  • username,password:對應git倉庫使用者名稱密碼,如果是公共倉庫則不需要填寫
  • lable:分支名

啟動程式

瀏覽器直接訪問http://localhost:8888/config/dev 瀏覽器返回

{
  "name": "config",
  "profiles": [
    "dev"
  ],
  "label": null,
  "version": "20f8f344a1246ce27252e98341f11d2fa0f3f22f",
  "state": null,
  "propertySources": [
    {
      "name": "https://github.com/xukai19891114/springCloudConfig/config-repo/config-dev.properties",
      "source": {
        "config": "i am dev"
      }
    }
  ]
}

上述的返回的資訊包含了配置檔案的位置、版本、配置檔案的名稱以及配置檔案中的具體內容,說明server端已經成功獲取了git倉庫的配置資訊。

如果直接檢視配置檔案中的配置資訊可訪問:http://localhost:8888/config-dev.properties
返回:

修改

修改github上config-dev.properties檔案中為

config: i am dev updated

再次訪問:http://localhost:8888/config-dev.properties

說明server端會自動讀取最新提交的內容

倉庫中的配置檔案會被轉換成web介面,訪問可以參照以下的規則:

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

以config-dev.properties為例子,它的application是config,profile是dev。client會根據填寫的引數來選擇讀取對應的配置。