使用 Consul 進行分散式配置

Consul提供了一個鍵/值儲存,用於儲存配置和其他元資料。Spring Cloud Consul Config是配置伺服器和客戶端的替代方案。配置在特殊的“bootstrap”階段載入到Spring環境中。預設情況下,配置儲存在/config資料夾中。根據應用程式的名稱和活動配置檔案建立多個PropertySource例項,這模擬了Spring Cloud Config解析屬性的順序。例如,一個名為“testApp”且帶有“dev”配置檔案的應用程式將建立以下屬性源:

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

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

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

如何啟用

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

Spring Boot配置資料匯入

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。匯入屬性中的位置優先於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配置資料匯入方法不需要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設定用於在帶有配置檔案的屬性源中分隔配置檔名稱的分隔符的值

配置觀察

Consul配置觀察利用了Consul觀察鍵字首的能力。配置觀察會進行阻塞的Consul HTTP API呼叫,以確定當前應用程式的任何相關配置資料是否已更改。如果有新的配置資料,將釋出一個重新整理事件。這等同於呼叫/refresh執行器端點。

要更改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中相應的data鍵中設定。使用上述預設值,鍵將如下所示:

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配置檔案和應用程式名稱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檔案。

快速失敗

在某些情況下(如本地開發或某些測試場景),如果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中。
© . This site is unofficial and not affiliated with VMware.