Config配置中心
學習地址:https://www.bilibili.com/video/BV18E411x7eT?t=11&p=74
分散式系統面臨的配置問題
微服務意味著要將單體應用中地業務分成一個個子服務,每個服務地粒度相對較小,因此係統中會出現大量的服務。由於每個服務都需要必要的配置資訊才能執行,所以一套集中式的、動態的配置管理設施事必不可少的。
SpringCloud Config
官網地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/
SpringCloud Config為微報務架構中的微服務提供集中化的外部配置支援,配置務器為各個不同服務應的所有環境提供了一箇中心化的外部配置。
SpringCloud Config分為服務端和客戶端兩部分
-
服務端也稱為分散式配置中心,它是一個獨立的微服務應,用來連線配置伺服器併為客戶端提供獲取配置資訊,加密/解密資訊等訪問介面
-
客戶端則是通過指定配置中心來管理應用資源,以與業務相關的配置內容,並在啟動的時候從配置中獲取和載入配置資訊,配置伺服器預設採用git來儲存配置資訊,這樣就有助於對環境配置進行版本管理,並且可通過git客戶端工具來方便的管理和訪問配置內容。
用途
-
集中管理配置檔案
-
不同環境不同配置,動態化的配置更新,分環境部署比如dev/test/prod/beta/release
-
執行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置檔案,服務會向配置中心統一拉取配置自己的資訊
-
當配置發生變動時,服務不需要重啟即可感知到配置的變化並應用新的配置
-
將配置資訊以REST介面的形式暴露。post、curl訪問重新整理均可....
Config服務端配置與測試
由於SpringCloud Config預設使用Git來儲存配置檔案(也有其它方式,比如支援svn和本地檔案,但最推薦的還是Git,而且使用的是http/https訪問的形式)
- 用你自己的賬號在Github上新建一個名為Sprincloud-Config的新Repository
- 克隆倉庫
- 獲取配置檔案(開發時自己寫
儲存格式必須為UTF-8
$ git clone https://github.com/zzyybs/springcloud-config.git
- 上傳配置檔案
add
commit
push
檢視
cloud-config-center-3344
-
建module
-
寫POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud2020</artifactId>
<groupId>com.nuc.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-center-3344</artifactId>
<dependencies>
<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>
<dependency>
<groupId>com.nuc.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- 寫YML
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
# Github的git倉庫
uri: https://github.com/mobiwusihuan288/SpringCloud-Config.git
# 搜尋目錄
search-paths:
- SpringCloud-Config
# 讀取分支
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
- 主啟動類
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
- windows下修改hosts檔案,增加對映
127.0.0.1 config-3344.com
- 測試
7001、3344
http://config-3344.com:3344/master/config-dev.yml
配置讀取規則
label:分支(branch)
name:服務名
profiles:環境(dev/test/prod)
1. /{label}/{application}-{profile}.yml(最推薦使用這種方式)
master
http://config-3344.com:3344/master/config-dev.yml
http://config-3344.com:3344/master/config-test.yml
http://config-3344.com:3344/master/config-prod.yml
dev
http://config-3344.com:3344/dev/config-dev.yml
http://config-3344.com:3344/dev/config-test.yml
http://config-3344.com:3344/dev/config-prod.yml
2. /{application}-{profile}.yml
http://config-3344.com:3344/config-dev.yml
http://config-3344.com:3344/config-test.yml
http://config-3344.com:3344/config-prod.yml
http://config-3344.com:3344/config-xxxx.yml(不存在的配置)-->空json串
3. /{application}-{profile}[/{label}]
http://config-3344.com:3344/config/dev/master
http://config-3344.com:3344/config/test/master
http://config-3344.com:3344/config/prod/master
Config客戶端配置與測試
cloud-config-client-3355
-
建module
-
寫POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud2020</artifactId>
<groupId>com.nuc.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-client-3355</artifactId>
<dependencies>
<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>
<dependency>
<groupId>com.nuc.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- 寫YML(bootstap.yml)
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客戶端配置
config:
label: master #分支名稱
name: config #配置檔名稱
profile: dev #讀取字尾名稱
# 以上配置 master分支上config-dev.yml 相當於http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
- 主啟動
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
- 業務類
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
- 測試
7001、3344、3355
http://localhost:3355/configInfo
關於application.yml和bootstrap.yml
-
application.yml是使用者級的資源配置項
-
bootstrap.yml是系統級的,優先順序更加高
-
Spring Cloud會建立一個"Bootstrap Context",作為Spring應用的"Application Context"的父上下文。初始化的時候,"Bootstrap Context"負責從外部源載入配置屬性並解析配置。這兩個上下文共享一個從外部獲取的"Environment"。
-
Bootstrap屬性有高優先順序,預設情況下,它們不會被本地配置覆蓋。"Bootstrap context"和"ApplicationContext"有不同的約定所以新增了一省bootstrap.yml檔案,保證"BootstrapContext"和"ApplicationContext"配置的分離。
- 要將Client模組下的application.yml檔案改為bootstrap.yml,這是很關鍵的,因為bootstrap.yml是比application.yml先載入的。bootstrap.yml優先順序高於application.yml
分散式配置的動態重新整理
問題描述
-
Linux運維修改GitHub上的配置檔案內容做調整
-
重新整理3344,發現ConfigServer配置中心立刻響應
-
重新整理3355,發現ConfigServer客戶端沒有任何響應
-
3355沒有變化除非自己重啟或者重新載入
解決
- 修改3355YML
management:
endpoints:
web:
exposure:
include: "*"
- 修改業務類
@RestController
@RefreshScope
public class ConfigClientController {}
- 需要運維人員傳送Post請求重新整理3355
curl -X POST "http://localhost:3355/actuator/refresh"
- 測試
修改git配置檔案
重新整理3344
重新整理3355
傳送Post請求重新整理3355
再次重新整理3355
問題:
假如有多個微服務客戶端3355/3366/3377。。。。
每個微服務都要執行一次post請求,手動重新整理?
可否廣播,一次通知,處處生效?
我們想大範圍的自動重新整理,求方法
解決:
下一期SpringCloud Bus 訊息匯流排