環境倉庫
Config Server 的配置資料應該儲存在哪裡?管理這種行為的策略是 EnvironmentRepository
,它提供 Environment
物件。這個 Environment
是 Spring Environment
領域(包括 propertySources
作為主要特性)的淺層副本。Environment
資源透過三個變數進行引數化:
-
{application}
,它對映客戶端的spring.application.name
。 -
{profile}
,它對映客戶端的spring.profiles.active
(逗號分隔列表)。 -
{label}
,這是伺服器端的一個功能,用於標記“版本化”的配置檔案集。
倉庫實現通常像一個 Spring Boot 應用一樣執行,從與 {application}
引數同名的 spring.config.name
以及與 {profiles}
引數同名的 spring.profiles.active
中載入配置檔案。Profile 的優先順序規則也與常規 Spring Boot 應用中的規則相同:啟用的 profile 優先於預設設定,如果存在多個 profile,最後一個生效(類似於向 Map
中新增條目)。
以下示例客戶端應用具有此 bootstrap 配置
spring:
application:
name: foo
profiles:
active: dev,mysql
(與常規 Spring Boot 應用一樣,這些屬性也可以透過環境變數或命令列引數設定)。
如果倉庫是基於檔案的,伺服器會從 application.yml
(所有客戶端共享)和 foo.yml
(foo.yml
優先順序更高)建立 Environment
。如果 YAML 檔案內部包含指向 Spring profile 的文件,則這些 profile 會以更高的優先順序應用(按照列出的 profile 順序)。如果存在特定於 profile 的 YAML(或屬性)檔案,它們的優先順序也高於預設檔案。優先順序更高意味著在 Environment
中更早列出 PropertySource
。(這些規則也適用於獨立的 Spring Boot 應用。)
你可以將 spring.cloud.config.server.accept-empty
設定為 false
,這樣如果找不到應用,伺服器將返回 HTTP 404 狀態。預設情況下,此標誌設定為 true
。
你不能將 spring.main.* 屬性放在遠端 EnvironmentRepository 中。這些屬性在應用初始化時使用。 |