spring cloud 入門系列七:基於Git存儲的分佈式配置中心--Spring Cloud Config

咱們前面接觸到的spring cloud組件都是基於Netflix的組件進行實現的,此次咱們來看下spring cloud 團隊本身建立的一個全新項目:Spring Cloud Config.
它用來爲分佈式系統中的基礎設施和微服務提供集中化的外部配置支持,分爲服務端和客戶端兩個部分。java

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

客戶端是微服務架構中的各個微服務應用或基礎設施,它們經過制定的配置中心來管理應用資源與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息。
因爲配置中心默認採用Git來存儲配置信息,所以咱們會用到Git相關的內容,若是沒有用過Git或者忘記怎麼用了,能夠參考下廖雪峯老師的Git教程
另外,我本身用的Git遠程倉庫是碼雲。
====================華麗的分割線===================
接下來看下代碼怎麼實現。web

1、準備遠程Git倉庫spring

  1. 在Gitee上新建一個項目https://gitee.com/sam-uncle/spring-cloud-learning
  2. 在項目下新建子目錄spring-cloud-config-file,而後新建三個文件
    1.   

       

    2. 內容分別是from=git-dev-1.0、from=git-test-1.0、from=git-1.0
  3. 新建一個分支config-lable-test,新分支裏面新建三個同名的文件,不過內容分別是from=git-dev-2.0、from=git-test-2.0、from=git-2.0

 

2、構建配置中心apache

  先給出最終代碼結構:bootstrap

  

  搭建過程以下:瀏覽器

  1. 新建maven工程config-server
  2. 修改POM文件
    <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.sam</groupId>
      <artifactId>config-server</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
    
        <properties>
            <javaVersion>1.8</javaVersion>
        </properties>
        <!-- 使用dependencyManagement進行版本管理 -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR6</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    
        </dependencyManagement>
    
        <dependencies>
           <!-- 引入config server依賴 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
    
        </dependencies>
        
    </project>
  3. 建立啓動類
    /**
     * @EnableConfigServer
     * 
     * 開啓Spring Cloud Config 的服務端功能
     *
     */
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApp {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApp.class, args);
        }
    }
  4. 配置application.properties文件,指定遠程倉庫信息
    server.port=7001
    spring.application.name=config-server
    
    #配置Git倉庫的地址
    spring.cloud.config.server.git.uri=https://gitee.com/sam-uncle/spring-cloud-learning/
    #配置倉庫路徑下的相對搜索位置,能夠配置多個
    spring.cloud.config.server.git.search-paths=spring-cloud-config-file
    #這裏配置你的Git倉庫的用戶名
    spring.cloud.config.server.git.username=用戶名
    #這裏配置你的Git倉庫的密碼
    spring.cloud.config.server.git.password=密碼
  5. 啓動並驗證

    訪問配置信息的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。app

    經過瀏覽器訪問http://localhost:7001/sam/dev/config-label-test,結果以下:maven

    


3、實現客戶端

    最終代碼結構:

  

 

   搭建過程以下:

  1. 新建maven工程config-client
  2. 修改POM文件
    <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.sam</groupId>
        <artifactId>config-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
    
        <properties>
            <javaVersion>1.8</javaVersion>
        </properties>
        <!-- 使用dependencyManagement進行版本管理 -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR6</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    
        </dependencyManagement>
    
        <dependencies>
            <!-- 引入config依賴 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
        </dependencies>
    
    </project>
  3. 建立啓動類
    @SpringBootApplication
    public class ConfigClientApp {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientApp.class, args);
        }
    
    }
  4. 配置bootstrap.properties文件,指定config-server位置
    server.port=7002
    #{application}
    spring.application.name=sam
    #{profile}
    spring.cloud.config.profile=dev
    #{label}
    spring.cloud.config.label=master
    
    #config server uri
    spring.cloud.config.uri=http://localhost:7001/
  5. 建立controller
    @RefreshScope
    @RestController
    public class TestController {
    
        
        /**
         * 經過@Value 來說配置文件中的值寫入到代碼中
         */
        @Value("${from}")
        private String from;
    
        @RequestMapping("/from")
        public String from() {
            return from;
        }
    }
  6. 啓動並測試

    

 

4、工做原理
Spring Cloud Config配置中心的工做原理以下:

  1. 客戶端啓動時,根據bootstrap.properties中配置的應用名{application}、環境名{profile}、分支名{label},向Config Server請求獲取配置信息。
  2. Config Server根據本身維護的Git倉庫信息和客戶傳遞過來的配置定位信息去查找配置信息。
  3. 經過git clone命令將找到的配置信息下載到本地(Config Server的文件系統中)。在經過頁面訪問或啓動客戶端的時候,咱們在服務端能看到以下下載的log:
    2018-05-14 22:51:58.055  INFO 3084 --- [nio-7001-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-8627749771720918793/spring-cloud-config-file/sam-dev.properties
    2018-05-14 22:51:58.055  INFO 3084 --- [nio-7001-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-8627749771720918793/spring-cloud-config-file/sam.properties
  4. Config Server建立Spring 的ApplicationContext實例,並從Git本地倉庫中加載配置文件,最後將這些配置內容讀取出來返回給客戶端。
  5. 客戶端在獲取外部配置信息後加載到客戶端的applicationContext實例。
相關文章
相關標籤/搜索