使用 Consul 的分散式組態
Consul 提供了 鍵/值儲存,用於儲存組態和其他中繼資料。Spring Cloud Consul Config 是 Config Server 和 Client 的替代方案。組態會在特殊的「啟動」階段載入到 Spring 環境中。組態預設儲存在 /config
資料夾中。會根據應用程式的名稱和作用中的設定檔建立多個 PropertySource
實例,以模擬 Spring Cloud Config 解析屬性的順序。例如,名稱為「testApp」且具有「dev」設定檔的應用程式將會建立下列屬性來源
config/testApp,dev/ config/testApp/ config/application,dev/ config/application/
最特定的屬性來源在最上方,最不特定的在最下方。config/application
資料夾中的屬性適用於所有使用 Consul 進行組態的應用程式。config/testApp
資料夾中的屬性僅適用於名為「testApp」的服務實例。
組態目前在應用程式啟動時讀取。將 HTTP POST 傳送至 /refresh
將導致重新載入組態。組態監看 也會自動偵測變更並重新載入應用程式上下文。
如何啟用
若要開始使用 Consul 組態,請使用群組為 org.springframework.cloud
且 Artifact ID 為 spring-cloud-starter-consul-config
的 Starter。請參閱 Spring Cloud 專案頁面,以取得使用目前 Spring Cloud Release Train 設定建置系統的詳細資訊。
Spring Boot 組態資料匯入
Spring Boot 2.4 引入了一種透過 spring.config.import
屬性匯入組態資料的新方法。這現在是從 Consul 取得組態的預設方式。
若要選擇性地連線至 Consul,請在 application.properties 中設定下列項目
spring.config.import=optional:consul:
這將連線至預設位置「https://#:8500」的 Consul Agent。移除 optional:
前綴將導致 Consul Config 在無法連線至 Consul 時失敗。若要變更 Consul Config 的連線屬性,請設定 spring.cloud.consul.host
和 spring.cloud.consul.port
,或將主機/埠配對新增至 spring.config.import
陳述式,例如 spring.config.import=optional:consul:myhost:8500
。匯入屬性中的位置優先於主機和埠屬性。
Consul Config 將嘗試從四個自動上下文載入值,這些上下文基於 spring.cloud.consul.config.name
(預設為 spring.application.name
屬性的值)和 spring.cloud.consul.config.default-context
(預設為 application
)。如果您想要指定上下文而不是使用計算出的上下文,您可以將該資訊新增至 spring.config.import
陳述式。
spring.config.import=optional:consul:myhost:8500/contextone;/context/two
這將選擇性地僅從 /contextone
和 /context/two
載入組態。
Spring Boot 組態資料匯入方法透過 spring.config.import 匯入,不需要 bootstrap 檔案 (properties 或 yaml)。 |
自訂
可以使用下列屬性自訂 Consul Config
spring:
cloud:
consul:
config:
enabled: true
prefix: configuration
defaultContext: apps
profileSeparator: '::'
如果您已設定 spring.cloud.bootstrap.enabled=true 或 spring.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
Actuator 端點。
若要變更呼叫組態監看的頻率,請變更 spring.cloud.consul.config.watch.delay
。預設值為 1000,單位為毫秒。延遲是前一次調用結束後到下一次調用開始之間的時間量。
若要停用組態監看,請設定 spring.cloud.consul.config.watch.enabled=false
。
監看使用 Spring TaskScheduler
來排程對 Consul 的呼叫。預設情況下,它是 ThreadPoolTaskScheduler
,poolSize
為 1。若要變更 TaskScheduler
,請建立類型為 TaskScheduler
的 Bean,並以 ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME
常數命名。
使用組態的 YAML 或 Properties
以 YAML 或 Properties 格式儲存屬性 Blob 可能比個別的鍵/值組更方便。將 spring.cloud.consul.config.format
屬性設定為 YAML
或 PROPERTIES
。例如,若要使用 YAML
spring:
cloud:
consul:
config:
format: YAML
如果您已設定 spring.cloud.bootstrap.enabled=true 或 spring.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 中的個別金鑰。預設情況下,金鑰的名稱是檔案的名稱。YAML 和 Properties 檔案支援 .yml
和 .properties
的副檔名。將 spring.cloud.consul.config.format
屬性設定為 FILES
。例如
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=true 或 spring.config.use-legacy-processing=true ,或包含 spring-cloud-starter-bootstrap ,則上述值需要放置在 bootstrap.yml 而不是 application.yml 中。 |