Spring Cloud微服架構之分佈式配置中心

Spring Cloud Config爲服務端和客戶端提供了分佈式系統的外部化配置支持。配置服務器爲各應用的所有環境提供了一箇中心化的外部配置。它實現了對服務端和客戶端對Spring Environment和PropertySource抽象的映射,所以它除了適用於Spring構建的應用程序,也可以在任何其他語言運行的應用程序中使用。作爲一個應用可以通過部署管道來進行測試或者投入生產,我們可以分別爲這些環境創建配置,並且在需要遷移環境的時候獲取對應環境的配置來運行。

配置服務器默認採用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容。當然他也提供本地化文件系統的存儲方式,下面從這兩方面介紹如何使用分佈式配置來存儲微服務應用多環境的配置內容。

構建Config Server

創建一個Maven工程,在pom.xml中添加 依賴:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.3.5.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<java.version>1.7</java.version>
</properties>

<dependencies>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
</dependencies>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Brixton.RELEASE</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>
創建Spring Boot的程序主類,並添加@EnableConfigServer註解,開啓Config Server
package com.wys;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigServer
public class Application {
	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}
}
application.properties中配置服務信息以及git信息
spring.application.name=config-server
server.port=7001
# 配置服務註冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
# git管理配置
spring.cloud.config.server.git.uri=https://github.com/Mr-wys/SpringCloud/
spring.cloud.config.server.git.searchPaths=config-repo
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password

說明:

  • spring.cloud.config.server.git.uri:配置git倉庫位置
  • spring.cloud.config.server.git.searchPaths:配置倉庫路徑下的相對搜索位置,可以配置多個
  • spring.cloud.config.server.git.username:訪問git倉庫的用戶名
  • spring.cloud.config.server.git.password:訪問git倉庫的用戶密碼
到這裏,使用一個通過Spring Cloud Config實現,並使用git管理內容的配置中心已經完成了,啓動該應用,成功後開始下面的內容。
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的方式。

爲了驗證上面完成的配置服務器,在https://github.com/Mr-wys/SpringCloud/config-repo/ 下創建了一個config-repo目錄作爲配置倉庫,並根據不同環境新建了下面四個配置文件:

  • didispace.properties
  • didispace-dev.properties
  • didispace-test.properties
  • didispace-prod.properties

其中設置了一個from屬性,爲每個配置文件分別設置了不同的值,如:

  • from=git-default-1.0
  • from=git-dev-1.0
  • from=git-test-1.0
  • from=git-prod-1.0

爲了測試版本控制,在master中,我們都加入1.0的後綴,同時創建一個config-label-test分支,並將各配置文件中的值用2.0作爲後綴。

完成了這些準備工作之後,我們就可以通過瀏覽器或POSTMAN等工具直接來訪問到我們的配置內容了。

URL與配置文件的映射關係如下:

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

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

我們可以嘗試構造不同的url來訪問不同的配置內容,比如:要訪問config-label-test分支,didispace應用的dev環境,可以通過這個url:http://localhost:7001/config/dev/config-label-test


微服務端映射配置

在完成並驗證了配置服務中心之後,下面看看我們如何在微服務應用中獲取配置信息。

  • 創建一個Maven工程,在pom.xml中引入相關依賴,完整依賴關係如下:
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.3.5.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<java.version>1.7</java.version>
</properties>

<dependencies>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

	<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.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
</dependencies>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Brixton.RELEASE</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>
  • 創建最基本的Spring Boot啓動主類
package com.wys;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}
}
  • 創建bootstrap.properties配置,來指定config server,例如:
spring.application.name=config
spring.cloud.config.profile=dev
spring.cloud.config.label=config-label-test
spring.cloud.config.uri=http://localhost:7001/
server.port=7002
  • spring.application.name:對應前配置文件中的{application}部分
  • spring.cloud.config.profile:對應前配置文件中的{profile}部分
  • spring.cloud.config.label:對應前配置文件的git分支
  • spring.cloud.config.uri:配置中心的地址
這裏需要格外注意:上面這些屬性必須配置在bootstrap.properties中,config部分內容才能被正確加載。因爲config的相關配置會先於application.properties,而bootstrap.properties的加載也是先於application.properties
  • 創建一個Rest Api來返回配置中心的from屬性,具體如下:
通過@Value("${from}")綁定配置服務中配置的from屬性。
啓動該應用(啓動之前先啓動之前的服務註冊中心eureka-server 以及config-server),然後訪問:http://localhost:7002/from ,我們就可以根據配置內容輸出對應環境的from內容了。


示例下載:spring_cloud

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章