­

微服務SpringCloud之Spring Cloud Config配置中心Git

  • 2019 年 10 月 3 日
  • 筆記

 微服務以單個介面為顆粒度,一個介面可能就是一個項目,如果每個項目都包含一個配置文件,一個系統可能有幾十或上百個小項目組成,那配置文件也會有好多,對後續修改維護也是比較麻煩,就和前面的服務註冊一樣,服務註冊與發現是將服務從分散到中心化,而今天的配置中心是將配置文件從分散到中心化,這樣便於後續維護。本篇主要以git為例學習使用Spring Cloud Config配置中心。

一、配置中心介紹

在我們了解spring cloud config之前,我可以想想一個配置中心提供的核心功能應該有什麼

  • 提供服務端和客戶端支援
  • 集中管理各環境的配置文件
  • 配置文件修改之後,可以快速的生效
  • 可以進行版本管理
  • 支援大的並發查詢
  • 支援各種語言

Spring Cloud Config可以完美的支援以上所有的需求。

Spring Cloud Config項目是一個解決分散式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以介面的形式將配置文件的內容提供出去,client通過介面獲取數據、並依據此數據初始化自己的應用。Spring cloud使用git或svn存放配置文件,默認情況下使用git,我們先以git為例做一套示例。

二、Server 端

1.準備配置文件

為了演示spring cloud config的使用,這裡在github()上創建了config-repo(https://github.com/ywcui/config-repo)倉庫.然後在該倉庫下創建了3個配置文件neo-config-dev.properties、neo-config-pro.properties、neo-config-test.properties,每個配置文件都設置了屬性neo.hello,value分別對應i am dev,i am pro,i am test。

2.創建Spring Cloud Config Server

創建Spring Boot項目並引入Spring Cloud Config Server,具體pom.xml如下:

<?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>      <parent>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-parent</artifactId>          <version>2.1.6.RELEASE</version>          <relativePath/> <!-- lookup parent from repository -->      </parent>      <groupId>com.example</groupId>      <artifactId>SpringCloudConfigServer</artifactId>      <version>0.0.1-SNAPSHOT</version>      <packaging>war</packaging>      <name>SpringCloudConfigServer</name>      <description>Demo project for Spring Boot</description>        <properties>          <java.version>1.8</java.version>          <spring-cloud.version>Greenwich.SR2</spring-cloud.version>      </properties>        <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>            <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-tomcat</artifactId>              <scope>provided</scope>          </dependency>          <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>

View Code

3.設置配置文件

在配置文件中做如下配置:

server.port=8001  spring.application.name=spring-cloud-config-server  spring.cloud.config.server.git.uri=https://github.com/ywcui/config-repo  spring.cloud.config.server.git.search-paths=  spring.cloud.config.server.git.username=用戶名  spring.cloud.config.server.git.password=密碼

View Code

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

4.啟動類設置

只需在啟動類中添加@EnableConfigServer即可。

5.測試

首先我們先要測試server端是否可以讀取到github上面的配置資訊,直接訪問:http://localhost:8001/neo-config/test

返回資訊如下:

如果直接查看配置文件中的配置資訊可訪問:http://localhost:8001/neo-config-test.properties,返回:neo.hello: i am test

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

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

三、Client端

1.創建創建Spring Cloud Config Client

創建Spring Boot項目並引入Spring Cloud Config Client,具體pom.xml如下:

<?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>      <parent>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-parent</artifactId>          <version>2.1.6.RELEASE</version>          <relativePath/> <!-- lookup parent from repository -->      </parent>      <groupId>com.example</groupId>      <artifactId>SpringCloudConfigClient</artifactId>      <version>0.0.1-SNAPSHOT</version>      <packaging>war</packaging>      <name>SpringCloudConfigClient</name>      <description>Demo project for Spring Boot</description>        <properties>          <java.version>1.8</java.version>          <spring-cloud.version>Greenwich.SR2</spring-cloud.version>      </properties>        <dependencies>          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-web</artifactId>          </dependency>          <dependency>              <groupId>org.springframework.cloud</groupId>              <artifactId>spring-cloud-starter-config</artifactId>          </dependency>            <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-tomcat</artifactId>              <scope>provided</scope>          </dependency>          <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>

View Code

2.設置配置文件

準備application.properties、bootstrap.properties兩個配置文件

application.properties:

spring.application.name=spring-cloud-config-client  server.port=8002

bootstrap.properties:

spring.cloud.config.name=neo-config  spring.cloud.config.profile=test  spring.cloud.config.uri=http://localhost:8001/  spring.cloud.config.label=master

spring.application.name:對應{application}部分
spring.cloud.config.profile:對應{profile}部分
spring.cloud.config.label:對應git的分支。如果配置中心使用的是本地存儲,則該參數無用
spring.cloud.config.uri:配置中心的具體地址
spring.cloud.config.discovery.service-id:指定配置中心的service-id,便於擴展為高可用配置集群。

上面這些與spring-cloud相關的屬性必須配置在bootstrap.properties中,config部分內容才能被正確載入。因為config的相關配置會先於application.properties,而bootstrap.properties的載入也是先於application.properties。

3.創建HelloController

在HelloController中使用@Value注入屬性neo.hello。

package com.example.demo;    import org.springframework.beans.factory.annotation.Value;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.bind.annotation.RestController;    @RestController  public class HelloController {      @Value("${neo.hello}")      private String hello;        @RequestMapping("/hello")      public String from() {          return this.hello;      }  }

View Code

4.測試

在瀏覽器中輸入http://localhost:8002/hello,則顯示下圖所示。

參考:http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html