SpringCloud搭建分布式配置中心(基於git)


 

1.簡介

Spring Cloud Config.它用來為分布式系統中的基礎設施和微服務提供集中化的外部配置支持,分為服務端和客戶端兩個部分。

其中服務端也稱為分布式配置中心,他是獨立的微服務應用,用來連接配置倉庫並為客戶端提供獲取接口(這些接口返回配置信息、加密、解密信息等);

客戶端是微服務架構中的各個微服務應用或基礎設施,它們通過制定的配置中心來管理應用資源與業務相關的配置內容,並在啟動的時候從配置中心獲取和加載配置信息。
由於配置中心默認采用Git來存儲配置信息.
另外,我自己用的Git遠程倉庫是碼雲。

 

2.git倉庫數據准備

  1. 在Gitee上新建一個項目https://gitee.com/colozhu/spring-cloud-learning.git
  2. 在項目下新建子目錄spring-cloud-config-file,然后新建三個文件
  3. 內容分別是 from=git-dev-1.0、from=git-test-1.0、from=git-1.0
  4. 新建一個分支feature,新分支里面新建三個同名的文件,不過內容分別是from=git-dev-2.0、from=git-test-2.0、from=git-2.0

   

3.Demo結構

       

這里使用maven父項目,兩個子項目.

 

4.父項目

<?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.colo</groupId>
    <artifactId>colo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>


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

    </modules>

    <!-- 使用dependencyManagement進行版本管理 -->
    <dependencyManagement>
        <dependencies>
            <!--Greenwich版本-支持Spring Boot 2.1.X  -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <!--測試依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

 

5.配置中心(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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.colo</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <!-- 引入config server依賴 -->
        <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>

啟動類:

package com.colo.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


/**
 * @EnableConfigServer 開啟Spring Cloud Config 的服務端功能
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    // 分別測試master分支和feature分支
    //http://localhost:7002/colozhu/dev/feature
    //http://localhost:7002/colozhu/test/master
    //http://localhost:7002/colozhu/dev/master
    //http://localhost:7002/colozhu/test   //默認master分支
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

配置文件:

server.port=7002
spring.application.name=config-server

#--------指定遠程倉庫信息----
# clientApplication客戶端啟動時候獲取分支上的配置參數${from}時候,配置中心會從git倉庫拉取colo-dev.properties,colo.properties等文件到本地
# 例如:Adding property source: file:/C:/Users/600336/AppData/Local/Temp/config-repo-1543229677936769440/spring-cloud-config-file/colo-dev.properties

#配置Git倉庫的地址
spring.cloud.config.server.git.uri=https://gitee.com/colozhu/spring-cloud-learning
#配置倉庫路徑下的相對搜索位置,可以配置多個
spring.cloud.config.server.git.search-paths=spring-cloud-config-file
#這里配置你的Git倉庫的用戶名
spring.cloud.config.server.git.username=xxxxxx@qq.com
#這里配置你的Git倉庫的密碼
spring.cloud.config.server.git.password=xxxxx

啟動並驗證:

    訪問配置信息的URL與配置文件的映射關系如下:

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

    上面的url會映射{application}-{profile}.properties對應的配置文件,其中{label}對應Git上不同的分支,默認是master

    通過瀏覽器訪問 http://localhost:7002/colozhu/dev/feature ,結果如下:

     

6.客戶端(config-client)

pom.xml (springboot)

<?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.colo</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- 引入config依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>

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

</project>

啟動類:

@SpringBootApplication
public class ConfigClientApplication {

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

}

配置bootstrap.properties文件,指定config-server位置

server.port=7003
#{application}  應用名
spring.application.name=colo
#{profile}  環境名
spring.cloud.config.profile=dev
#{label}  分支名
spring.cloud.config.label=master

#config server uri 
#指定config-server位置 spring.cloud.config.uri
=http://localhost:7002/ # gitee上面的文件colo-dev.properties里面有 from=git-dev-1.0

創建controller:

package com.colo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class TestController {


    /**
     * 通過@Value 來講配置文件中的值寫入到代碼中,
     * clientApplication客戶端啟動時候獲取分支上的配置參數${from}時候,配置中心會從git倉庫拉取colo-dev.properties,colo.properties等文件到本地
     * 例如:Adding property source: file:/C:/Users/600336/AppData/Local/Temp/config-repo-1543229677936769440/spring-cloud-config-file/colo-dev.properties
     */
    @Value("${from}")
    private String from;

    //http://localhost:7003/from
    @RequestMapping("/from")
    public String from() {
        return from;
    }
}

 

啟動並測試

localhost:7002/from

7.工作原理

  1. 客戶端啟動時,根據bootstrap.properties中配置的應用名{application}、環境名{profile}、分支名{label},向Config Server請求獲取配置信息。
  2. Config Server根據自己維護的Git倉庫信息和客戶傳遞過來的配置定位信息去查找配置信息。
  3. 通過git clone命令將找到的配置信息下載到本地(Config Server的文件系統中)。在通過頁面訪問或啟動客戶端的時候,我們在服務端能看到如下下載的log(mac上):
2019-07-09 10:57:47.552  INFO 594 --- [nio-7001-exec-2] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/var/folders/rv/97y9wj7j3_d759f5tymjvb080000gn/T/config-repo-6459346864682184876/spring-cloud-config-file/colo-dev.properties
2019-07-09 10:57:47.552  INFO 594 --- [nio-7001-exec-2] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/var/folders/rv/97y9wj7j3_d759f5tymjvb080000gn/T/config-repo-6459346864682184876/spring-cloud-config-file/colo.properties

  4.Config Server創建Spring 的ApplicationContext實例,並從Git本地倉庫中加載配置文件,最后將這些配置內容讀取出來返回給客戶端。

  5.客戶端在獲取外部配置信息后加載到客戶端的applicationContext實例。

 

 

 

 

 

 參考:https://www.cnblogs.com/sam-uncle/p/9036053.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM