Spring Cloud Config 用於爲分佈式系統中的基礎設施和微服務應用提供集中化的外部配置支持,分爲服務端和客戶端。java
服務端爲分佈式配置中心,是一個獨立的微服務應用;客戶端爲分佈式系統中的基礎設置或微服務應用,經過指定配置中心來管理相關的配置。git
Spring Cloud Config 構建的配置中心,除了適用於 Spring 構建的應用外,也能夠在任何其餘語言構建的應用中使用。github
Spring Cloud Config 默認採用 Git 存儲配置信息,自然支持對配置信息的版本管理。web
建立 Spring Boot 工程 config-server,而後按如下步驟完成配置中心的構建。spring
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
複製代碼
package com.ulyssesss.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
複製代碼
spring.application.name=config-server
server.port=7001
# Git 倉庫位置
spring.cloud.config.server.git.uri=https://github.com/Ulyssesss/spring-cloud-config-example.git
# 倉庫路徑下相對搜索位置,可配置多個
spring.cloud.config.server.git.search-paths=config
# 訪問 Git 倉庫的用戶名
spring.cloud.config.server.git.username=
# 訪問 Git 倉庫的密碼
spring.cloud.config.server.git.password=
複製代碼
建立 Git 倉庫及 config 目錄,添加 ulyssesss.properties
、ulyssesss-dev.properties
,在配置中分別添加 from=default-1.0
和 from=dev-1.0
。bootstrap
建立 config-label-test
分支,將配置文件中的版本號 1.0
修改成 2.0
。安全
提交修改並推送至遠程倉庫後啓動 config-server,可按照如下規則訪問配置信息:app
建立Spring Boot 工程 config-client ,按如下步驟編寫客戶端:負載均衡
<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>
複製代碼
建立 bootstrap.properties,並增長配置中心的相關配置。分佈式
spring.application.name=ulyssesss
spring.cloud.config.profile=dev
spring.cloud.config.label=config-label-test
spring.cloud.config.uri=http://localhost:7001
複製代碼
注意,以上屬性必須配置在 bootstrap.properties 中。
因爲 Spring Boot 應用會優先加載應用 jar 包之外的配置,而經過 bootstrap.properties 對 config-server 的配置會使應用從 config-server 中獲取外部配置,優先級比本地配置高。
建立 ConfigClientController,經過訪問 http://localhost:8080/from 獲取配置。
package com.ulyssesss.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestConfigController {
@Value("${from}")
private String from;
@GetMapping("from")
public String from() {
return from;
}
}
複製代碼
訪問 http://localhost:8080/from ,獲得返回結果 from=dev-2.0
。修改配置中的 spring.cloud.config.profile
和 spring.cloud.config.label
,重啓應用再次訪問連接會獲得相應的配置信息。
傳統模式的高可用不須要額外的配置,只需將全部的 config-server 實例所有指向同一個 Git 倉庫,客戶端指定 config-server 時指向上層負載均衡設備地址。
服務模式經過將 config-server 歸入 Eureka 服務治理體系,將 config-server 註冊成爲一個微服務應用,客戶端經過服務名從服務註冊中心獲取配置中心的實例信息。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
複製代碼
package com.ulyssesss.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;
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
複製代碼
spring.application.name=config-server
server.port=7001
# Git 倉庫位置
spring.cloud.config.server.git.uri=https://github.com/Ulyssesss/spring-cloud-config-example.git
# 倉庫路徑下相對搜索位置,可配置多個
spring.cloud.config.server.git.search-paths=config
# 訪問 Git 倉庫的用戶名
spring.cloud.config.server.git.username=
# 訪問 Git 倉庫的密碼
spring.cloud.config.server.git.password=
# 服務註冊中心地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
複製代碼
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
複製代碼
package com.ulyssesss.configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
複製代碼
spring.application.name=ulyssesss
spring.cloud.config.profile=test
spring.cloud.config.label=config-label-test
#spring.cloud.config.uri=http://localhost:7001
# 服務註冊中心地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
# 啓用配置客戶端的服務發現功能
spring.cloud.config.discovery.enabled=true
# 指定配置中心的服務名
spring.cloud.config.discovery.service-id=config-server
複製代碼
有時須要對配置內容進行實時更新,Spring Cloud Config 經過 actuator 可實現此功能。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
複製代碼
package com.ulyssesss.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class TestConfigController {
@Value("${from}")
private String from;
@GetMapping("from")
public String from() {
return from;
}
}
複製代碼
修改配置中 from 的值,提交到遠程倉庫,經過 post 方法訪問 http://localhost:8080/refresh 便可刷新配置項的值。
注意,spring boot 1.5 以上會默認開啓安全認證,可經過一下配置關閉安全認證。
# 關閉安全認證
management.security.enabled=false
複製代碼
示例代碼 歡迎 Star