環境:
jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3
摘要說明:
Spring Cloud Config:Spring Cloud Config為分布式系統中的外部化配置提供伺服器和用戶端支援。通過配置伺服器,您可以在所有環境中管理應用程式的外部屬性。用戶端和伺服器映射上的概念與Spring環境和PropertySource抽象完全一緻,是以它們非常适合Spring應用程式,但可以用于任何語言中運作的任何應用程式。當應用程式通過從dev到測試和生産的部署管道時,您可以管理這些環境之間的配置,并確定應用程式擁有在遷移時需要運作的所有内容。伺服器存儲後端的預設實作使用git,是以它可以輕松地支援帶标簽的配置環境版本,并且可以通路各種工具來管理内容。很容易添加替代實作并将它們與Spring配置插入。可參考;
特征
Spring Cloud Config Server功能:
- 用于外部配置的HTTP,基于資源的API(名稱 - 值對或等效的YAML内容)
- 加密和解密屬性值(對稱或不對稱)
- 使用可輕松嵌入Spring Boot應用程式
@EnableConfigServer
Config Client功能(适用于Spring應用程式):
- 綁定到配置伺服器并
使用遠端屬性來初始化SpringEnvironment
- 加密和解密屬性值(對稱或不對稱)
總結:配置中心為每個服務統一配置;各個服務可直接從配置中心擷取資訊;
步驟:
1.建立configServer(configServer配置中心)子項目
通過
SPRING INITIALIZR
工具選擇Cloud Config:Config Server子產品建構configServer子項目引入依賴(pom.xnl):
<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>pers.cc</groupId>
<artifactId>springCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>configServer</artifactId>
<name>configServer</name>
<description>配置中心-服務端</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2.配置configServer
使用@EnableConfigServer注解開啟configServer的服務中心配置:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
配置application.properties:
這裡面需要先配置一個git倉庫,當然這裡也可以使用其他倉庫如svn;如有分支,使用者,密碼及目錄都可以配置;此次示例倉庫為public且為更路徑下故不設定;
#配置服務名稱及端口
spring.application.name=config-server
server.port=9001
#将服務注冊到注冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#服務的git倉庫位址
spring.cloud.config.server.git.uri=https://gitee.com/yepiaoling/spring-could-config
#配置檔案所在的目錄
#spring.cloud.config.server.git.search-paths=/**
#配置檔案所在的分支
spring.cloud.config.label=master
#git倉庫的使用者名
#spring.cloud.config.username=********
#git倉庫的密碼
#spring.cloud.config.password=********
倉庫指定目錄下的配置檔案格式如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中label為分支預設為master;application為服務名稱;profile為環境;
故在倉庫中添加配置檔案config-client.properties,指的是給config-client服務預設環境的配置檔案:
test=config-client-test
啟動服務:
服務注冊中心(eurekaServer):eureka-server(1001)
配置中心(configServer):config-server(9001)
通過通路http://localhost:9001/config-client/master;獲得配置資訊:
{"name":"config-client","profiles":["master"],"label":null,"version":"e68941966cfb7f870643074ade0b27a9f752510d","state":null,"propertySources":[{"name":"https://gitee.com/yepiaoling/spring-could-config/config-client.properties","source":{"test":"config-client-test"}}]}
或者通路http://localhost:9001/master/config-client.properties(http://localhost:9001/config-client.properties)直接通路配置檔案的資訊:
test=config-client-test
3.建立configClient(configClient配置用戶端)子項目
通過
SPRING INITIALIZR
工具選擇Cloud Config:Config Client子產品建構configClient子項目引入依賴(pom.xnl):
<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>pers.cc</groupId>
<artifactId>springCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>configClient</artifactId>
<name>configClient</name>
<description>服務配置中心-用戶端</description>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
4.配置configClient
配置application.properties:
#配置服務名稱及端口
spring.application.name=config-client
server.port=10001
但想擷取配置中心的資訊必須配置到bootstrap.properties中:
此次使用的是服務配置來進行與服務中心的互動;也可以直接使用spring.cloud.config.uri來進行配置;
#将服務注冊到注冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#開啟配置服務發現
spring.cloud.config.discovery.enabled=true
#配置服務執行個體名稱
spring.cloud.config.discovery.service-id=config-server
#配置服務中心
#spring.cloud.config.uri=http://localhost:9001/
#配置檔案所在分支
spring.cloud.config.label=master
#配置檔案所指環境
spring.cloud.config.profile=default
開發一個測試類(TestController)進行測試:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${test}")
private String test;
@GetMapping("/test")
public String test() {
return test;
}
}
啟動服務:
服務注冊中心(eurekaServer):eureka-server(1001)
配置中心(configServer):config-server(9001)
配置用戶端(configClient):config-client(10001)
通過通路http://localhost:10001/test來進行驗證可直接擷取配置的值:
config-client-test
5.配置手動重新整理
我們接着上面将git倉庫中添加config-client-dev.properties:
test=config-dev
更新bootstrap.properties中的:
#配置檔案所指環境
spring.cloud.config.profile=dev
重新啟動服務:
配置用戶端(configClient):config-client(10001)
通過通路http://localhost:10001/test來進行驗證可直接擷取配置的值:
config-dev
接着我們修改config-client-dev.properties中的test值為config-client-dev;重新通路http://localhost:10001/test;但值未變,這說明配置修改但用戶端未進行變更;
我們在pom.xml中添加spring-boot-starter-actuator依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
更新bootstrap.properties:
management.endpoints.web.exposure.include=*
#springboot 1.5.X 以上預設開通了安全認證,是以需要在配置檔案application.properties添加以下配置,以post請求的方式來通路http://localhost:8081/refresh 就會更新修改後的配置檔案
management.security.enabled=false
TestController中添加注解@RefreshScope;
重新啟動服務:
配置用戶端(configClient):config-client(10001)
通過通路http://localhost:10001/test來進行驗證可直接擷取配置的值:
config-client-dev
再修改config-client-dev.properties中的test值為config-client-dev1;
用post方式:http://localhost:10001/actuator/refresh進行任務更新;再通路會獲得最新值;
6.源碼位址
github位址:https://github.com/cc6688211/springCloud.git