Part II. Spring Cloud Config
Finchley.SR2
Spring Cloud Config為分布式系統中的外部化配置提供伺服器端和用戶端支援。使用Config Server,您有一個中心位置來管理跨所有環境的應用程式的外部屬性。客戶機和伺服器上的概念與Spring環境和PropertySource抽象完全相同,是以它們非常适合Spring應用程式,但是可以與任何語言中運作的應用程式一起使用。随着應用程式在部署管道中從開發到測試并進入生産,您可以管理這些環境之間的配置,并確定應用程式在遷移時具有它們需要運作的所有内容。伺服器存儲後端的預設實作使用git,是以它很容易支援配置環境的标記版本,并且可以被用于管理内容的各種工具通路。很容易添加替代的實作,并用Spring配置将它們插入。
4. 快速開始
快速開始使用Spring Cloud Config Server 的伺服器和用戶端。
首先,啟動伺服器,如下:
$ cd spring-cloud-config-server
$ ../mvnw spring-boot:run
該服務是一個Spring Boot 應用程式,如果你願意,可以從你的IDE運作它(主類是 ConfigServerApplication)。
接着嘗試一下用戶端,如下:
$ curl localhost:8888/foo/development
{"name":"foo","label":"master","propertySources":[
{"name":"https://github.com/scratches/config-repo/foo-development.properties","source":{"bar":"spam"}},
{"name":"https://github.com/scratches/config-repo/foo.properties","source":{"foo":"bar"}}
]}
定位屬性源的預設政策是克隆git倉庫(在spring.cloud.config.server.git.uri)并使用它來初始化迷你SpringApplication.迷你應用程式的環境用于枚舉屬性源并在JSON端點釋出它們。
HTTP服務具有以下形式的資源:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中,application 作為spring.config.name 被注入到SpringApplication(正常的Spring Boot 應用程式裡通常是application),概要檔案是活動的概要檔案(或逗号分隔的屬性清單),而是一個可選的git标簽(預設為master)。
Spring Cloud Config 從git 倉庫(必須提供)中提取遠端用戶端的配置,如下面的例子所示:
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
4.1 用戶端使用
要在應用程式中使用這些特性,你可以建一個依賴sprin-cloud-config-client(例如,參考配置用戶端或示例應用程式的測試用例)的Spring Boot 應用程式。添加依賴最便捷的方式是使用Spring Boot starter org.springframework.cloud:spring-cloud-starter-config。Maven使用者還有一個父pom和BOM(spring-cloud-starter-parent),以及Gradle和Spring CLI使用者的Spring IO版本管理屬性檔案。
以下示例顯示了典型的Maven配置:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{spring-boot-docs-version}</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
<!-- repositories also needed for snapshots and milestones -->
現在你可以建立一個标準的Spring Boot 應用程式,如以下的HTTP 伺服器:
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
當這個HTTP伺服器運作時,它從端口8888上的預設本地配置伺服器(如果它正在運作)擷取外部配置。要修改啟動行為,可以使用bootstrap.properties(類似于application.properties,但是對于應用程式上下文的引導階段)更改配置伺服器的位置,如下例所示:
spring.cloud.config.uri: http://myconfigserver.com
引導屬性在/env端點中顯示為高優先級屬性源,如下面的示例所示。
$ curl localhost:8080/env
{
"profiles":[],
"configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"},
"servletContextInitParams":{},
"systemProperties":{...},
...
}
名為configService:<遠端倉庫的URL>/<檔案名>的屬性源包含具有bar值的foo屬性并且是最高優先級。
【注意】屬性源名稱中的URL是Git倉庫,而不是配置伺服器URL。