使用 Consul 的分散式配置

Consul 提供了一個用於儲存配置和其他元資料的 Key/Value Store(鍵/值儲存)。Spring Cloud Consul Config 是 Config Server 和 Client 的替代方案。配置在特殊的“bootstrap”階段被載入到 Spring Environment 中。預設情況下,配置儲存在 /config 資料夾中。會根據應用程式的名稱和活動 profiles 建立多個 PropertySource 例項,這模仿了 Spring Cloud Config 解析屬性的順序。例如,名稱為 "testApp" 且 profile 為 "dev" 的應用程式將建立以下屬性源:

config/testApp,dev/
config/testApp/
config/application,dev/
config/application/

最具體的屬性源在頂部,最不具體的在底部。config/application 資料夾中的屬性適用於所有使用 Consul 進行配置的應用程式。config/testApp 資料夾中的屬性僅適用於名為 "testApp" 的服務例項。

當前配置在應用程式啟動時讀取。向 /refresh 傳送 HTTP POST 請求將導致配置重新載入。Config Watch 也會自動檢測更改並重新載入應用上下文。

如何啟用

要開始使用 Consul 配置,請使用 group 為 org.springframework.cloud 且 artifact id 為 spring-cloud-starter-consul-config 的 starter。有關如何使用當前的 Spring Cloud Release Train 設定構建系統的詳細資訊,請參閱 Spring Cloud 專案頁面

Spring Boot Config Data Import

Spring Boot 2.4 引入了一種透過 spring.config.import 屬性匯入配置資料的新方法。現在這是從 Consul 獲取配置的預設方式。

要選擇性地連線到 Consul,請在 application.properties 中進行以下設定

application.properties
spring.config.import=optional:consul:

這將連線到預設位置 "https://:8500" 的 Consul Agent。刪除 optional: 字首會導致 Consul Config 在無法連線到 Consul 時失敗。要更改 Consul Config 的連線屬性,可以設定 spring.cloud.consul.hostspring.cloud.consul.port,或者將 host/port 對新增到 spring.config.import 語句中,例如,spring.config.import=optional:consul:myhost:8500。import 屬性中的位置優先於 host 和 port 屬性。

Consul Config 將根據 spring.cloud.consul.config.name(預設為 spring.application.name 屬性的值)和 spring.cloud.consul.config.default-context(預設為 application)從四個自動生成的上下文載入值。如果你想指定上下文而不是使用計算得出的上下文,可以將該資訊新增到 spring.config.import 語句中。

application.properties
spring.config.import=optional:consul:myhost:8500/contextone;/context/two

這將僅從 /contextone/context/two 選擇性地載入配置。

對於透過 spring.config.import 匯入的 Spring Boot Config Data 方法,**不需要** bootstrap 檔案(properties 或 yaml 格式)。

定製

可以使用以下屬性定製 Consul Config

spring:
  cloud:
    consul:
      config:
        enabled: true
        prefix: configuration
        defaultContext: apps
        profileSeparator: '::'
如果你設定了 spring.cloud.bootstrap.enabled=truespring.config.use-legacy-processing=true,或者包含了 spring-cloud-starter-bootstrap,那麼上述值需要放在 bootstrap.yml 中而不是 application.yml 中。
  • enabled 將此值設定為 "false" 將停用 Consul Config

  • prefix 設定配置值的基本資料夾

  • defaultContext 設定所有應用程式使用的資料夾名稱

  • profileSeparator 設定在帶 profile 的屬性源中用於分隔 profile 名稱的分隔符的值

配置監視 (Config Watch)

Consul Config Watch 利用了 Consul 監視 key 字首 的能力。Config Watch 會發起一個阻塞的 Consul HTTP API 呼叫,以確定當前應用程式是否有任何相關的配置資料發生變化。如果有新的配置資料,就會發佈一個 Refresh Event(重新整理事件)。這相當於呼叫 /refresh actuator 端點。

要更改 Config Watch 的呼叫頻率,請更改 spring.cloud.consul.config.watch.delay。預設值為 1000,單位是毫秒。此延遲是指前一次呼叫結束後到下一次呼叫開始之間的時間量。

要停用 Config Watch,請設定 spring.cloud.consul.config.watch.enabled=false

監視器使用 Spring 的 TaskScheduler 來排程對 Consul 的呼叫。預設情況下,它是一個 ThreadPoolTaskSchedulerpoolSize 為 1。要更改 TaskScheduler,請建立一個型別為 TaskScheduler 的 bean,其名稱使用 ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME 常量。

配置中的 YAML 或 Properties 格式

將一整塊屬性儲存為 YAML 或 Properties 格式可能比儲存單個鍵/值對更方便。將 spring.cloud.consul.config.format 屬性設定為 YAMLPROPERTIES。例如,要使用 YAML

spring:
  cloud:
    consul:
      config:
        format: YAML
如果你設定了 spring.cloud.bootstrap.enabled=truespring.config.use-legacy-processing=true,或者包含了 spring-cloud-starter-bootstrap,那麼上述值需要放在 bootstrap.yml 中而不是 application.yml 中。

YAML 必須設定在 Consul 中相應的資料鍵下。使用上述預設設定,鍵看起來像這樣

config/testApp,dev/data
config/testApp/data
config/application,dev/data
config/application/data

你可以將 YAML 文件儲存在上面列出的任何鍵中。

你可以使用 spring.cloud.consul.config.data-key 更改資料鍵。

配置中使用 git2consul

git2consul 是一個 Consul 社群專案,它將檔案從 git 倉庫載入到 Consul 中的各個鍵中。預設情況下,鍵的名稱就是檔名。支援副檔名為 .yml.properties 的 YAML 和 Properties 檔案。將 spring.cloud.consul.config.format 屬性設定為 FILES。例如

bootstrap.yml
spring:
  cloud:
    consul:
      config:
        format: FILES

假設在 /config 中有以下鍵,使用 development profile 和應用程式名稱 foo

.gitignore
application.yml
bar.properties
foo-development.properties
foo-production.yml
foo.properties
master.ref

將建立以下屬性源

config/foo-development.properties
config/foo.properties
config/application.yml

每個鍵的值必須是格式正確的 YAML 或 Properties 檔案。

快速失敗 (Fail Fast)

在某些情況下(例如本地開發或特定測試場景),如果 Consul 不可用作配置源時,不讓應用程式失敗可能會更方便。設定 spring.cloud.consul.config.fail-fast=false 將使配置模組記錄警告而不是丟擲異常。這將允許應用程式正常繼續啟動。

如果你設定了 spring.cloud.bootstrap.enabled=truespring.config.use-legacy-processing=true,或者包含了 spring-cloud-starter-bootstrap,那麼上述值需要放在 bootstrap.yml 中而不是 application.yml 中。