1. 程式人生 > >Spring Cloud Config構建配置中心

Spring Cloud Config構建配置中心

  1. Spring Cloud Config是什麼,能做什麼?

    它是通過SpringBoot構造的,用來為分散式系統提供配置中心。由中心來管理所有環境應用程式的屬性,便於在整個專案週期統一調配配置。支援將資訊放在github中,在服務端做簡單的配置即可從遠端倉庫中獲取配置資訊,而所有的客戶端只需要指定去獲取資訊的服務連線,然後就可以直接使用各種變數資訊。
  2. Spring Cloud Config構建

    1. Server端構建

      先去github建立個repository(當然可以使用預設的,需要配置spring.config.name=configserver,因為Spring-cloud-config-server-*.jar包中已經給出了預設的伺服器配置資訊和測試地址,Spring-cloud-config-server-2.0.2.jar的檔案中叫configserver.yml),然後建立一個建立一個目錄和一個檔案,用來做伺服器同步資料測試。比如我已經建立好的https://github.com/poetLoo/gitRepository

      建立一個SpringBoot專案,然後配置Spring Cloud config依賴,啟動類中加入註解:@EnableConfigServer

      。我這裡使用maven來構建專案,具體的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.example</groupId>
      	<artifactId>springCloud</artifactId>
      	<version>0.0.1-SNAPSHOT</version>
      	<packaging>jar</packaging>
      
      	<name>springCloudConfig</name>
      	<description>Demo project for Spring Boot</description>
      
      	<parent>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-parent</artifactId>
      		<version>2.1.0.RELEASE</version>
      		<relativePath/> <!-- lookup parent from repository -->
      	</parent>
      
      	<properties>
      		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      		<java.version>1.8</java.version>
      	</properties>
      
      	<dependencyManagement>
      	    <dependencies>
      	        <dependency>
      	            <groupId>org.springframework.cloud</groupId>
      	            <artifactId>spring-cloud-dependencies</artifactId>
      	            <version>Finchley.SR2</version>
      	            <type>pom</type>
      	            <scope>import</scope>
      	        </dependency>
      	    </dependencies>
      	</dependencyManagement>
      	
      	<dependencies>
      		
      		<dependency>
      	        <groupId>org.springframework.cloud</groupId>
      	        <artifactId>spring-cloud-starter-config</artifactId>
      	    </dependency>
      	    <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-config-server</artifactId>
              </dependency>
              
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-test</artifactId>
      			<scope>test</scope>
      		</dependency>
      		
      	</dependencies>
      
      	<build>
      		<plugins>
      			<plugin>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-maven-plugin</artifactId>
      			</plugin>
      		</plugins>
      	</build>
      
      
      </project>
      

      需要注意的是,Spring boot和Spring cloud的版本要能對應起來,否則專案啟動會報錯.版本對應關係:

      Spring Cloud Spring Boot
      Finchley 相容Spring Boot 2.0.x,不相容Spring Boot 1.5.x
      Dalston和Edgware 相容Spring Boot 1.5.x,不相容Spring Boot 2.0.x
      Camden 相容Spring Boot 1.4.x,也相容Spring Boot 1.5.x
      Brixton 相容Spring Boot 1.3.x,也相容Spring Boot 1.4.x
      Angel 相容Spring Boot 1.2.x

      如果使用我們自己的repository,那麼application.yml配置如下:

      server:
        port: 8888
      
      spring:
        cloud:
          config:
            server:
              git:
                #指定配置中心地址
                uri: https://github.com/poetLoo/cloudConfig
                #設定搜尋目錄,如果不設定,預設只搜尋根目錄
                search-paths:
                - myConfig
        name: server
        application:
          name: configServer

      返回資訊如下:

      {
          "name": "config-sample",
          "profiles": [
              "master"
          ],
          "label": null,
          "version": "168139978ce7b76e646f222cc6fb1ff8f2203366",
          "state": null,
          "propertySources": [
              {
                  "name": "https://github.com/poetLoo/cloudConfig/myConfig/config-sample.yml",
                  "source": {
                      "appName": "poetLav",
                      "version": "1.0.0"
                  }
              }
          ]
      }
      /{application}/{profile}[/{label}]
      /{application}-{profile}.yml
      /{label}/{application}-{profile}.yml
      /{application}-{profile}.properties
      /{label}/{application}-{profile}.properties
    2. Client端構建

      新建一個專案,作為客戶端構建和服務端基本一致,在pom中多加一個依賴,表示是一個web服務

      <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-web</artifactId>

      </dependency>

      主要是在resource目錄下新增一個bootstrap.yml檔案,基本配置如下:
      spring:
        application:
          name: config-sample
        cloud:
          config:
            uri:
            - http://localhost:8888
            

      新增一個簡單的controller:  

      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      public class HelloConfigController {
      
      	@Value("${appName}") String name;
      	@Value("${version}") String version;
      
      	@RequestMapping("/helloConfig")
      	public Object hello() {
      		return "hello " + name
      				+" version:"+ version;
      	}
      }
  3. 總結

    Spring cloud Config Server端的repository可以由多種方式,預設是使用遠端git倉庫、還可以是本地檔案系統、自己搭建配置中心的方式。由於git本身是一個分散式版本控制系統,可以方便的檢視和追蹤資源變更記錄,便於管理,所以預設推薦。 server端還由其他一些配置策略,比如同步遠端倉庫的版本、是否啟動時克隆配置資訊到本地、連線不上遠端配置時處理等,在使用到的時候可以去探索。