SpringClound-分散式配置中心及其高可用
Spring Cloud Config為服務端和客戶端提供了分散式系統的外部化配置支援。配置伺服器為各應用的所有環境提供了一箇中心化的外部配置。它實現了對服務端和客戶端對Spring Environment和PropertySource抽象的對映,所以它除了適用於Spring構建的應用程式,也可以在任何其他語言執行的應用程式中使用。作為一個應用可以通過部署管道來進行測試或者投入生產,我們可以分別為這些環境建立配置,並且在需要遷移環境的時候獲取對應環境的配置來執行。
配置伺服器預設採用git來儲存配置資訊,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容。當然他也提供本地化檔案系統的儲存方式,下面從這兩方面介紹如何使用分散式配置來儲存微服務應用多環境的配置內容。
構建Config Server
建立configserver專案
我們首先建立程式主程式,只需要多加上@EnableConfigServer
註解即可
/**
* 分散式配置中心服務端
* 2018年6月15日14:13:01
* yangzhap
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
配置檔案中配置GIt資訊
spring.application.name=config-server
server.port=8766
# git管理配置
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
#配置git倉庫位置
spring.cloud.config.server.git.searchPaths=respo
#配置倉庫路徑下的相對搜尋位置,可以配置多個
spring.cloud.config.label=master
spring.cloud.config.server .git.username=your username
#訪問git倉庫的使用者名稱
spring.cloud.config.server.git.password=your password
#訪問git倉庫的使用者密碼
URL與配置檔案的對映關係如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
另外Spring Cloud Config也提供本地儲存配置的方式。我們只需要設定屬性spring.profiles.active=native
,Config Server會預設從應用的src/main/resource
目錄下檢索配置檔案。也可以通過spring.cloud.config.server.native.searchLocations=file:F:/properties/
屬性來指定配置檔案的位置。雖然Spring Cloud Config提供了這樣的功能,但是為了支援更好的管理內容和版本控制的功能,還是推薦使用git的方式。
我們現在configserver專案已經可以抓取到git中配置檔案的資訊,我們現在建立configclient專案
構建Config Client
建立程式主類
/**
* 分散式配置中心客戶端
* 2018年6月15日14:21:55
* yangzhao
*/
@SpringBootApplication
@RestController
public class ConfigclientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigclientApplication.class, args);
}
@Value("${democonfigclient.message}")
private String from ;
@RequestMapping("/from")
public String from() {
return this.from ;
}
}
建立bootstrap.properties
配置
spring.application.name=config-client
#配置zull就不行 不知道為什麼 抓取不上
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:8766/
server.port=8767
#只和git中檔案路徑有關 和server(除了地址)中沒關係
#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
。
啟動configclient專案,訪問http://localhost:8767/from
高可用問題
我們來說一下配置中心高可用問題,傳統的做法就是我們將多個configserver專案訪問同一個Git,接著在client和server專案之間加上負載均衡實現高可用叢集。
另外一種更簡單的方法就是我們將我們的專案註冊為微服務,然後訪問同一個Git,我們就可以通過微服務叢集實現高可用問題了。接下來我們改造我們的server以及client專案
configServer 專案我們只需要在程式主類中加上@EnableDiscoveryClient
註解,並在配置檔案中指定註冊中心地址即可eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
configClient 專案我們只需要在程式主類中加上@EnableDiscoveryClient
註解,並修改我們的bootstrap.properties
檔案
spring.application.name=config-client
server.port=8767
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.profile=dev
#通過eureka.client.serviceUrl.defaultZone引數指定服務註冊中心,用於服務的註冊與發現,
#再將spring.cloud.config.discovery.enabled引數設定為true,開啟通過服務來訪問Config Server的功能,\
#最後利用spring.cloud.config.discovery.serviceId引數來指定Config Server註冊的服務名。\
#這裡的spring.application.name和spring.cloud.config.profile如之前通過URI的方式訪問時候一樣,用來定位Git中的資源。
總結
- 簡單來說高可用的實現就是通過註冊為微服務
- 整個配置中心的實現:server專案通過配置Git專案url(
spring.cloud.config.server.git.uri
)以及配置檔案確定 唯一資料夾(spring.cloud.config.server.git.searchPaths
),client專案則通過application,profile,label等確定唯一的檔案(pring.application.name
,spring.cloud.config.profile
) 最後在介面中通過
@Value("${democonfigclient.message}")
private String from ;
確定配置檔案中的具體資訊,從而獲得value