【學習背景】
上篇博客學習的是Spring Cloud的Hystrix組件,從而保證在高併發情況下,在使用服務熔斷、服務降級、服務隔離等方法,避免服務雪崩效應。本篇博客學習的組件是Config,分佈式配置中心。
【學習內容】
一. 爲什麼要使用分佈式配置中心?
在微服務中,如果使用傳統方式管理配置文件,配置文件管理器非常複雜。
現在的應用大多是分佈式系統,部署在N臺服務器上,一臺臺重啓機器不靠譜,維護成本很高,所以配置中心應運而生。
二. 什麼是分佈式配置中心?
配置中心被用作集中管理不同環境(Dev、Pre、Pro)和不同集羣配置,以及在修改配置後將實時動態推送到應用上進行刷新。
三. 配置中心應具備的功能?
四. Spring Cloud Config
1. 概述
Spring Cloud Config是一個集中化外部配置的分佈式系統,由服務端和客戶端組成。它不依賴於註冊中心,是一個獨立的配置中心。
Spring Cloud Config支持多種存儲配置信息的形式,目前主要有jdbc、Vault、Native、svn、git,默認爲git。
2. git版工作原理
配置客戶端啓動時會向服務端發起請求,服務端接收到客戶端的請求後,根據配置的倉庫地址,將git上的文件克隆到本地的一個臨時目錄中,這個目錄是一個git的本地倉庫目錄,然後服務端再讀取本地文件返回給客戶端。這樣做的好處是,當git服務器故障或者網絡請求異常時,保證服務端仍然可以正常工作。
3. Config Server配置
Config Server即配置服務器,爲配置客戶端提供其對應的配置信息,配置信息的來源爲配置倉庫,啓動時需要拉取配置倉庫的信息,緩存到本地倉庫中。
- 引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
- 啓動註解
/**
* @EnableConfigServer註解開啓Spring Cloud Config服務功能
*/
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
- 配置信息
### 配置文件git地址
spring.cloud.config.server.git.uri=https://github.com/huzhiting/spring-cloud-config.git
### git用戶名和密碼(公開訪問權限可不需要設置,私有訪問權限需要設置)
spring.cloud.config.server.git.username=****
spring.cloud.config.server.git.password=****
### 查找配置文件的路徑(因爲直接在根目錄下新建了配置文件,所以不需要單獨設置)
### spring.cloud.config.server.git.search-paths=config
### 代碼分支(默認master)
spring.cloud.config.label=master
### 應用名稱
spring.application.name=config-server
### 應用端口號
server.port=8888
- 讀取驗證
訪問本地配置服務器,http://127.0.0.1:8888/config-server-dev.properties
可讀取到對應的配置文件信息:env: is dev
4. Config Client配置
Config Client即配置客戶端,只會在本地配置必要的信息,如指定獲取配置的Config Server地址,啓動時從配置服務器獲取配置信息,並支持動態刷新配置倉庫中的屬性值。
- 引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
- 配置信息
### Config Server地址
spring.cloud.config.uri=http://localhost:8888
### 代碼分支
spring.cloud.config.label=master
### 配置Config Server中對應的配置文件名
spring.cloud.config.name=config-server
### 指定環境
spring.cloud.config.profile=dev
### Config Server中對應的配置文件名
spring.application.name=config-client
### 應用端口
server.port=9091
- 接口訪問
@RestController
public class ConfigClientController {
@Value("${env}") // git配置文件裏的key
String configEnv;
@RequestMapping("/getConfigInfo")
public String getConfigInfo(){
return "獲取環境配置:" + configEnv;
}
}
訪問本項目中對應的接口,http://localhost:9091/getConfigInfo
可讀取到Config Server中對應的配置信息
‘獲取環境配置:hello dev’
五. 遇到的問題
1. Config Server 配置文件位置
Config Server中我們會配置一個git地址,而對應的配置文件是從根目錄下開始搜索的,而我自認爲從resources文件夾下讀取,所以將配置文件信息放在了resources文件夾下,一直讀取不到。
如下圖,如果訪問配置信息接口,可以得到配置文件中的信息,則表明是沒有問題的。
2. Config Client 啓動報錯
- 啓動Spring-Cloud-Config-Client時,控制檯報錯:Attempted to load applicationConfig: [classpath:/bootstrap.yml] but snakeyaml was not found on the classpath
原因缺少snakeyaml jar,添加依賴即可:
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
</dependency>
- 啓動Spring-Cloud-Config-Client時,控制檯報錯:
原因就是未讀取到Config Server項目中的配置文件,首先修改了Config Server中配置文件的位置,其次讀取配置文件的規則是根據Config Client中的配置來的。
兩種情況如下:
1)在Config Client中,若配置了spring.cloud.config.name=config-server屬性及spring.cloud.config.profile=dev,則會去Config Server項目中去對應查找config-server-dev.yml/config-server-dev.properties文件,文件不存在的話則會啓動失敗。
2)在Config Client中,若沒有配置spring.cloud.config.name=config-server屬性,配置了spring.application.name=config-client及spring.cloud.config.profile=dev,則會去Config Server項目中去對應查找config-client-dev.yml/config-client-dev.properties文件,文件不存在的話則會啓動失敗。
六. demo地址
自己搭建的demo已上傳到github,地址:
https://github.com/huzhiting/spring-cloud-config
【學習總結】
學習的過程中,遇到問題是收穫最多的。看了很多書中的理論,不去自己搭建demo或在項目中用,是發現不了什麼問題的。