SpringCloud(五)GateWay網關
- 2021 年 5 月 3 日
- 筆記
- SpringCloud
Config 分散式配置中心
概述
微服務意味著要將單體應用中的業務拆分成個個子服務,每個服務的粒度相對較小因此系統中會出現大量的服務
由於每個服務都需要必要的配置資訊才能運行,所以一套集中式的、動態的配置管理設施是必不可少的
Spring Cloud提供了 ConfigServer 來解決這個問題,我們每個微服務自己帶著一個 application.yml,上百個配置文件的管理會導致膨脹
官方地址://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/reference/html/
Spring Cloud Config為微服務架構中的微服務提供集中化的外部配置攴持,配置伺服器為各個不同微服務應用的所有環境提供了一個中心化的配置
Spring Cloud Config分為服務端和客戶端兩部分
- 服務端也稱為分散式配置中心,它是獨立的微服務應用,用來連接配置伺服器並為客戶端提供獲取配置資訊,加密解密資訊等訪問介面
- 客戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啟動的時候從配置中心獲取和載入配置資訊,配置伺服器默認採用 git 來存儲配置資訊,這樣就有助於對環境配置進行版本管理,並且可以通過 git 客戶端工具來方便的管理和訪問配置內容
主要分支
- 集中管理配置文件
- 不同環境不同配置,動態化的配置更新,分環境部署比如 dev/test/prod/beta/release
- 運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置自己的資訊
- 當配置發生變動時,服務不需要重啟即可感知到配置的變化並應用新的配置
- 將配置資訊以REST介面的形式暴露
- Github整合配置:由於 Spring Cloud Config默認使用Git來存儲配置文件(也有其它方式比如支援SVN和本地文件),但最推薦的還是Git,而且使用的是http/https訪問的形式
服務端配置
首先要在 Github 上創建一個 Config 倉庫,來配置環境
- 新建一個module
- 修改 pom 依賴
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- 配置 yml
server:
port: 3344
spring:
application:
name: Config-center
cloud:
config:
server:
git:
# Github 上倉庫的名字
uri: //github.com/odousnag/SpringCloudStudy.git
# 搜索目錄
search-paths:
- springcloud-config
# 讀取分支
label: master
# Eureka
eureka:
client:
service-url:
# 單機版
# defaultZone: //localhost:7001/eureka/
# 集群版
defaultZone: //eureka7001.com:7001/eureka, //eureka7002.com:7002/eureka
- 主啟動類開啟註解
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain.class,args);
}
}
- windows 下更改 hosts 增加映射
- 測試是否能從 Github 上獲取配置內容
啟動配置中心://config3344.com:3344/master/config-dev.yml
7.配置讀寫規則
官網上的配置讀寫規則
常用的三種:
/{label}/{application}-{profile}.properties
master 分支://config3344.com:3344/master/config-xxx.yml
dev 分支://config3344.com:3344/dev/config-xxx.yml
/{application}/{profile}[/{label}]
//config3344.com:3344/config-xxx.yml
/{application}-{profile}.yml
//config3344.com:3344/config/xxx/master
客戶端配置
- 新建一個module
- 修改 pom 依賴
<!-- spring-cloud-starter-config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 新建一個 bootstrap.yml
applicaiton.yml 是用戶級的資源配置項,bootstrap.yml是系統級的,優先順序更加高
Spring Cloud會創建一個 “Bootstrap Context”,作為 Spring應用的 Application Context的父上下文
初始化的時候, Bootstrap Context 負責從外部源載入配置屬性並解析配置,這兩個上下文共享—個從外部獲取的 Environment
將 Client 模組下的 application.yml 文件改為 bootstrap.yml 這是很關鍵的,因為 bootstrap.yml是比 application.yml 先載入的
bootstrap.yml 優先順序高於 application.yml
server:
port: 3355
spring:
application:
name: Config-client
cloud:
# Config 客戶端配置
config:
# 讀取分支
label: master
# 配置文件名稱
name: config
# 讀取名稱後綴
profile: dev
# 配置中心地址
uri: //localhost:3344
# Eureka
eureka:
client:
service-url:
# 單機版
# defaultZone: //localhost:7001/eureka/
# 集群版
defaultZone: //eureka7001.com:7001/eureka, //eureka7002.com:7002/eureka
- 修改 application-dev.yml 配置並提交到 Github 中,比如加個變數 age 或者版本號 version
- 業務類測試
@RestController
public class ClientController {
/**
* 獲取application-dev的資訊
*/
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
實現了客戶端訪問SpringCloudConfig通過GitHub獲取配置資訊
客戶端動態刷新
動態刷新問題
Linux運維修改GitHub上的配置文件內容做調整,刷新服務端,發現ConfigServer配置中心立刻響應,刷新客戶端,發現ConfigClient客戶端沒有任何響應
修改客戶端
- 引入 actuator 監控依賴
<!-- spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 修改 yml 配置,暴露監控埠
# 暴露監控埠
management:
endpoints:
web:
exposure:
include: "*"
- 業務類修改,增加註解 @RefreshScope
- 發送Post請求刷新 客戶端
curl -X POST "//localhost:3355/actuator/refresh"
出現的問題:
現在每次更改,都要手動發動請求,這不合適,實現廣播,一次通知,處處修改
下一章到消息匯流排會講到