SpringCloud(五)GateWay閘道器

Odousang發表於2021-05-03

Config 分散式配置中心

概述

微服務意味著要將單體應用中的業務拆分成個個子服務,每個服務的粒度相對較小因此係統中會出現大量的服務
由於每個服務都需要必要的配置資訊才能執行,所以一套集中式的、動態的配置管理設施是必不可少的
Spring Cloud提供了 ConfigServer 來解決這個問題,我們每個微服務自己帶著一個 application.yml,上百個配置檔案的管理會導致膨脹
官方地址:https://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 倉庫,來配置環境

  1. 新建一個module
  2. 修改 pom 依賴
<!-- config-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  1. 配置 yml
server:
  port: 3344

spring:
  application:
    name: Config-center
  cloud:
    config:
      server:
        git:
          # Github 上倉庫的名字
          uri: https://github.com/odousnag/SpringCloudStudy.git
          # 搜尋目錄
          search-paths:
            - springcloud-config
      # 讀取分支
      label: master

# Eureka
eureka:
  client:
    service-url:
      # 單機版
      # defaultZone: http://localhost:7001/eureka/
      # 叢集版
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
  1. 主啟動類開啟註解
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain.class,args);
    }
}
  1. windows 下更改 hosts 增加對映
  2. 測試是否能從 Github 上獲取配置內容

    啟動配置中心:http://config3344.com:3344/master/config-dev.yml

    7.配置讀寫規則
    官網上的配置讀寫規則
常用的三種:
/{label}/{application}-{profile}.properties
  master 分支:http://config3344.com:3344/master/config-xxx.yml
  dev 分支:http://config3344.com:3344/dev/config-xxx.yml

/{application}/{profile}[/{label}]
  http://config3344.com:3344/config-xxx.yml

/{application}-{profile}.yml
  http://config3344.com:3344/config/xxx/master

客戶端配置

  1. 新建一個module
  2. 修改 pom 依賴
<!-- spring-cloud-starter-config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  1. 新建一個 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: http://localhost:3344

# Eureka
eureka:
  client:
    service-url:
      # 單機版
      # defaultZone: http://localhost:7001/eureka/
      # 叢集版
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
  1. 修改 application-dev.yml 配置並提交到 Github 中,比如加個變數 age 或者版本號 version
  2. 業務類測試
@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客戶端沒有任何響應
修改客戶端

  1. 引入 actuator 監控依賴
<!-- spring-boot-starter-actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. 修改 yml 配置,暴露監控埠
# 暴露監控埠
management:
  endpoints:
    web:
      exposure:
        include: "*"
  1. 業務類修改,增加註解 @RefreshScope
  2. 傳送Post請求重新整理 客戶端
curl -X POST "http://localhost:3355/actuator/refresh"

出現的問題:
現在每次更改,都要手動發動請求,這不合適,實現廣播,一次通知,處處修改
下一章到訊息匯流排會講到

相關文章