config--學習筆記(6)
目錄
一、參考Spring Cloud官方文檔
--1、Spring Cloud Config
--2、快速開始
--3、客戶端使用
--4、Spring Cloud Config服務器
--5、環境庫
--6、Spring Cloud Config客戶端
----6.1、配置第一引導
----6.2、發現第一個引導
----6.3、查找遠程配置資源
二、實操
--1、工程準備
--2、config服務器端的配置
--3、config客戶端的配置
--4、github上的配置
--5、運行結果
一、參考Spring Cloud官方文檔
1、Spring Cloud Config
Spring Cloud Config為分布式系統中的外部配置提供服務器和客戶端支持。使用Config Server,您可以在所有環境中管理應用程序的外部屬性。客戶端和服務器上的概念映射與Spring Environment和PropertySource抽象相同,因此它們與Spring應用程序非常契合,但可以與任何以任何語言運行的應用程序一起使用。
2、快速開始
定位資源的默認策略是克隆一個git倉庫(在spring.cloud.config.server.git.uri),并使用它來初始化一個迷你SpringApplication。
HTTP服務具有以下格式的資源:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中“應用程序”作為SpringApplication中的spring.config.name注入(即常規的Spring Boot應用程序中通常是“應用程序”),“配置文件”是活動配置文件(或逗號分隔列表的屬性),“label”是可選的git標簽(默認為“master”)。
Spring Cloud Config服務器從git存儲庫(必須提供)為遠程客戶端提供配置:
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
3、客戶端使用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
程序運行的時候會抓取外部配置,默認從本地8888端口抓取本地配置。可以在bootstrap.properties文件中改變這種配置。bootstrap.properties是程序啟動階段的上下文,簡單的說就是bootstrap.properties會先于application.properties文件在啟動時被加載。
spring.cloud.config.uri: http://myconfigserver.com
4、Spring Cloud Config服務器
服務器為外部配置(名稱值對或等效的YAML內容)提供了基于資源的HTTP。服務器可以使用@EnableConfigServer注釋輕松嵌入到Spring Boot應用程序中。
ConfigServer.java
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
5、環境庫
您要在哪里存儲配置服務器的配置數據?管理此行為的策略是EnvironmentRepository,服務于Environment對象。此Environment是Spring Environment(包括propertySources作為主要功能)的域的淺層副本。Environment資源由三個變量參數化:
{application}映射到客戶端的“spring.application.name”;
{profile}映射到客戶端上的“spring.profiles.active”(逗號分隔列表); 和
{label}這是一個服務器端功能,標記“版本”的配置文件集
Spring Boot加載配置的時候,"spring.cloud.name"等效于{application},"spring.profiles.active"等效于{profile}
示例:客戶端應用程序具有此引導配置:
bootstrap.yml
spring:
application:
name: foo
profiles:
active: dev,mysql
(通常使用Spring Boot應用程序,這些屬性也可以設置為環境變量或命令行參數)。
倉庫中,可以把配置文件存到子目錄下,并且通過模式匹配來檢索
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
searchPaths: foo,bar*
在此示例中,服務器搜索頂級和“foo /”子目錄以及名稱以“bar”開頭的任何子目錄中的配置文件。
另外,在Git的URL路徑中可以使用占位符,在搜索路徑中也可以使用占位符。
6、Spring Cloud Config客戶端
Spring Boot應用程序可以立即利用Spring配置服務器(或應用程序開發人員提供的其他外部屬性源),并且還將獲取與Environment更改事件相關的一些其他有用功能。
6.1、配置第一引導
這是在類路徑上具有Spring Cloud Config Client的任何應用程序的默認行為。配置客戶端啟動時,它將通過配置服務器(通過引導配置屬性spring.cloud.config.uri)綁定,并使用遠程屬性源初始化Spring Environment。
這樣做的最終結果是所有想要使用Config Server的客戶端應用程序需要bootstrap.yml(或環境變量),服務器地址位于spring.cloud.config.uri(默認為“http:// localhost:8888” )。
6.2、發現第一個引導
如果您正在使用DiscoveryClient實現,例如Spring Cloud Netflix和Eureka服務發現或Spring Cloud Consul(Spring Cloud Zookeeper不支持此功能),那么您可以使用Config Server如果您想要發現服務注冊,但在默認的“配置優先”模式下,客戶端將無法利用注冊。
如果您希望使用DiscoveryClient找到配置服務器,可以通過設置spring.cloud.config.discovery.enabled=true(默認為“false”)來實現。最終的結果是,客戶端應用程序都需要具有適當發現配置的bootstrap.yml(或環境變量)。例如,使用Spring Cloud Netflix,您需要定義Eureka服務器地址,例如eureka.client.serviceUrl.defaultZone。使用此選項的價格是啟動時額外的網絡往返,以定位服務注冊。好處是配置服務器可以更改其坐標,只要發現服務是一個固定點。默認的服務標識是“configserver”,但您可以使用spring.cloud.config.discovery.serviceId在客戶端進行更改(在服務器上以服務的通常方式更改,例如設置spring.application.name)。
6.3、查找遠程配置資源
配置服務從/{name}/{profile}/{label}提供屬性源,客戶端應用程序中的默認綁定
“name”= ${spring.application.name}
“profile”= ${spring.profiles.active}(實際上是Environment.getActiveProfiles())
“label”=“master”
所有這些都可以通過設置spring.cloud.config.* (其中*是“name”,“profile”或“label”)來覆蓋。“標簽”可用于回滾到以前版本的配置; 使用默認的Config Server實現,它可以是git標簽,分支名稱或提交ID。標簽也可以以逗號分隔的列表形式提供,在這種情況下,列表中的項目會逐個嘗試,直到成功。例如,當您可能希望將配置標簽與您的分支對齊,但使其成為可選(例如spring.cloud.config.label=myfeature,develop)時,這對于在特征分支上工作時可能很有用。
二、實操
1、工程準備
此處需要三個工程eureka-server-demo、config-server-demo、config-client-demo
eureka-server-demo
這個工程參考參考Eureka--學習筆記(1)
config-server-demo
服務器端 本文后面會講到配置
config-client-demo
客戶端 本文后面會講到配置
2、config服務器端的配置
pom.xml
<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>
application.yml
spring:
cloud:
config:
server:
git:
uri: https://github.com/guozimao/config
application:
name: config-server-demo
server:
port: 8100
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
啟動類
@SpringBootApplication
@EnableConfigServer
public class ConfigServerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerDemoApplication.class, args);
}
}
3、config客戶端的配置
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
bootstrap.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
cloud:
config:
discovery:
enable: true
serviceId: config-server-demo
failFast: false
uri: http://localhost:8100
lable: master
application:
name: config-client-demo
profiles:
active: dev
application.yml
server:
port: 8094
被調用類
@RequestMapping("/")
@RestController
public class HelloController {
@Value("${foo}")
private String name;
@GetMapping("/home")
private String home(){
return "Hello! " + name;
}
}
4、github上的配置
https://github.com/guozimao/config
config-client-demo-dev.yml
5、運行結果
UI界面的監控
在瀏覽器中,輸入http://localhost:8094/home