spring cloud config簡介
為什麼要使用配置中心
簡單來說,就是為了友善所有服務的配置統一管理,實時更新。
在分布式的微服務架構中,服務數量會越來越多,而每個服務執行個體都會有一個或幾個配置檔案(yml,properties,json…)。而這些檔案,分布在系統的各個角落,管理起來特别麻煩,是以出現了一些可以集中管理配置的元件。這裡的
spring cloud config
就是其中之一。
為什麼要使用spring cloud config
類似這種分布式配置中心元件,分以下兩種角色:
1. config server
2. config client
而spring cloud config的優點則是和Spring的內建更好,接口規範一緻,同時已有的Spring項目遷移也比較友善。同時和SpringBoot的标準統一,可以讓項目的版本依賴變得容易管理,減少可能出現的依賴沖突。
spring cloud config使用
服務端ConfigServer
建立一個簡單的SpringBoot的項目cloud-config。
添加依賴
在項目的pom.xml檔案中添加如下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
添加配置檔案
在項目的resources目錄下,建立application.yml(也可以使用application.properties),在其中添加如下内容:
spring:
application:
name: config-server
# 配置中心服務配置
cloud:
config:
server:
git:
# git的uri
uri: https://gitee.com/lieh_666/SpringCloudConfig/
# 搜尋的檔案夾
search-paths: clear-cloud
# 若git為公共項目的話,賬号密碼可以為空
username: xxxxxxxx
password: xxxxxxxx
#分支
label: master
server:
port:
- spring.cloud.config.server.git.uri:配置git倉庫位址
- spring.cloud.config.server.git.searchPaths:配置倉庫路徑
- spring.cloud.config.label:配置倉庫的分支
- spring.cloud.config.server.git.username:通路git倉庫的使用者名
- spring.cloud.config.server.git.password:通路git倉庫的使用者密碼
開啟服務配置
建立項目啟動類
ConfigServer
,通過
@EnableConfigServer
開啟配置中心服務。
/**
* 在分布式系統中,由于服務數量巨多,為了友善服務配置檔案統一管理,實時更新,是以需要分布式配置中心元件。
* 在Spring Cloud中,有分布式配置中心元件spring cloud config
* 它支援配置服務放在配置服務的記憶體中(即本地)
* 也支援放在遠端Git倉庫中
* 在spring cloud config 元件中,分兩個角色
* 一是config server
* 二是config client
* 這裡是ConfigServer,
* 隻需要開啟@SpringBootApplication和@EnableConfigServer
*
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
http請求位址和資源檔案映射如下(其中application為服務名):
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
用戶端ConfigClient
建立一個SpringBoot的項目
config-client
添加依賴
在其pom檔案中添加如下依賴:
<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>
添加服務端的配置
在項目的resources目錄下,建立application.yml,在其中添加如下内容:
server:
port:
spring:
application:
name: service-hello
#配置中心的配置資訊
cloud:
config:
label: master
profile: dev
uri: http://localhost:8888/
配置啟動類,并在Controller中擷取Git檔案中的屬性
建立啟動類HelloApplication,并使用
@RestController
注冊為Controller接口。使用
@Value
擷取屬性資訊:
/**
* 一個簡單的服務
*
*/
@EnableEurekaClient
@SpringBootApplication
@RestController
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
@Value("${msg}")
String msg;
@RequestMapping(value = "/hello")
public String hi(){
return msg;
}
}
因為配置服務名為
service-hello
,
profile
為
dev
,是以在git的對應目錄(這裡為https://gitee.com/lieh_666/SpringCloudConfig/clear-cloud)下,建立一個
service-hello-dev
的檔案,并在檔案中添加msg屬性如下:
msg = hello,this is git config
啟動服務端和用戶端
啟動ConfigServer
啟動ConfigClient
通路http://localhost:8889/hello
運作結果如下: