Distributed Configuration with Consul
Consul 提供一个 Key/Value Store 用于存储配置和其他元数据。Spring Cloud Consul Config 是一种替代品,即 Config Server and Client。配置在特殊的“bootstrap”阶段加载到 Spring Environment 中。默认情况下,配置存储在 /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 将导致重新加载配置。Config Watch 还将自动检测更改并重新加载应用程序上下文。
How to activate
如需通过Consul配置开始,请使用组为`org.springframework.cloud`且工件ID为`spring-cloud-starter-consul-config`的starter。请参阅 Spring Cloud Project page了解有关使用当前Spring Cloud Release Train设置构建系统 的详细信息。
Spring Boot Config Data Import
Spring Boot 2.4 引入了一种通过 spring.config.import 属性导入配置数据的新方法。这是从 Consul 获取配置的默认方式。
要选择连接到 Consul,请在 application.properties 中设置以下内容:
spring.config.import=optional:consul:
这将连接到“http://localhost:8500”默认位置的 Consul Agent。删除 optional: 前缀将导致如果无法连接到 Consul,则 Consul Config 失败。要更改 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 加载配置(可选)。
|
对于通过 |
Customizing
консу尔配置可借助以下属性进行自定义:
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`设置用于在带有配置文件的属性源中分隔配置文件名的分隔符的值
Config Watch
Consul Config Watch 利用了 consul 能够 watch a key prefix的能力。Config Watch 发出一个阻止 Consul HTTP API 调用,以确定是否已经为当前应用程序更改了任何相关配置数据。如果存在新的配置数据,则会发布刷新事件。这相当于调用 `/refresh`执行器端点。
若要更改配置监听调用的频率,请更改 spring.cloud.consul.config.watch.delay。默认值为 1000(毫秒)。延迟是在上一次调用结束与下次调用开始之间的时间量。
若要禁用配置监听,请设置 spring.cloud.consul.config.watch.enabled=false。
监听使用 Spring TaskScheduler 安排对 консу尔的调用。默认情况下,它是一个“线程池任务计划程序”,poolSize 为 1。若要更改 TaskScheduler,请创建一个名为 ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME 常量的类型为 TaskScheduler 的 bean。
YAML or Properties with Config
与将属性存储为各个键/值对相比,以 YAML 或属性格式存储属性 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。
必须在 консу尔中相应的 data 密钥中设置 YAML。使用上述默认值,秘钥将如下所示:
config/testApp,dev/data config/testApp/data config/application,dev/data config/application/data
您可以在上面列出任何一个秘钥中存储 YAML 文档。
您可以使用 spring.cloud.consul.config.data-key 更改数据秘钥。
git2consul with Config
git2consul 是一个 Consul 社区项目,它将 git 存储库中的文件加载到 Consul 中各个单独的秘钥中。默认情况下,秘钥名称为文件名称。YAML 和属性文件受支持,它们的文件扩展名分别为 .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 或属性文件。
Fail Fast
在某些情况下(例如本地开发或某些测试场景),如果 консу尔不可用于配置,则不出现故障可能很方便。设置 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。