63. 使用 Consul 分发 Configuration
Consul 提供Key/Value Store用于存储 configuration 和其他元数据。 Spring Cloud Consul Config 是配置服务器和 Client的替代品。 Configuration 在特殊的“bootstrap”阶段加载到 Spring 环境中。 Configuration 默认存储在/config
文件夹中。基于 application 的 name 和 active profiles 创建了多个PropertySource
实例,这些实例模仿解析 properties 的 Spring Cloud Config order。对于 example,name“testApp”和“dev”profile 的 application 将创建以下 property 源:
config/testApp,dev/
config/testApp/
config/application,dev/
config/application/
最具体的 property 源位于顶部,底部最不具体。 config/application
文件夹中的 Properties 适用于使用 consul 进行 configuration 的所有 applications。 config/testApp
文件夹中的 Properties 仅可用于名为“testApp”的服务实例。
Configuration 目前在启动 application 时读取。将 HTTP POST 发送到/refresh
将导致重新加载 configuration。 第 63.3 节,“Config Watch”还将自动检测更改并重新加载 application context。
63.1 如何激活
要开始使用 Consul Configuration,请将 starter 与 group org.springframework.cloud
和 artifact id spring-cloud-starter-consul-config
一起使用。有关使用当前 Spring Cloud Release Train 设置 build 系统的详细信息,请参阅Spring Cloud 项目页面。
这将启用 auto-configuration,它将设置 Spring Cloud Consul Config。
63.2 自定义
可以使用以下 properties 自定义 Consul 配置:
bootstrap.yml.
spring:
cloud:
consul:
config:
enabled: true
prefix: configuration
defaultContext: apps
profileSeparator: '::'
-
enabled
将此 value 设置为“false”会禁用 Consul 配置 -
prefix
_setconfiguration 值的基本文件夹 -
defaultContext
sets 设置所有 applications 使用的文件夹 name -
profileSeparator
sets 用于将 property 源中的 profile name 与 profiles 分隔开的分隔符的 value
63.3 Config Watch
Consul Config Watch 利用 consul watch 一个 key 前缀的能力。 Config Watch 进行阻塞 Consul HTTP API 调用,以确定当前 application 是否有任何相关的 configuration 数据已更改。如果有新的 configuration 数据,则发布 Refresh Event。这相当于调用/refresh
actuator 端点。
要更改调用 Config Watch 的频率,请更改spring.cloud.consul.config.watch.delay
。默认的 value 是 1000,以毫秒为单位。
要禁用 Config Watch set spring.cloud.consul.config.watch.enabled=false
。
63.4 YAML 或 Properties with Config
以 YAML 或 Properties 格式存储 properties blob 可能更方便,而不是单个 key/value 对。将spring.cloud.consul.config.format
property 设置为YAML
或PROPERTIES
。对于 example 使用 YAML:
bootstrap.yml.
spring:
cloud:
consul:
config:
format: YAML
必须在 consul 中的相应data
key 中设置 YAML。使用上面的默认值看起来像:
config/testApp,dev/data
config/testApp/data
config/application,dev/data
config/application/data
您可以在上面列出的任何键中存储 YAML 文档。
您可以使用spring.cloud.consul.config.data-key
更改数据 key。
使用 Config 63.5 git2consul
git2consul 是一个 Consul 社区项目,它将 git repository 中的 files 加载到 Consul 中。默认情况下,键的名称是 files 的名称。分别使用.yml
和.properties
的文件 extensions 支持 YAML 和 Properties files。将spring.cloud.consul.config.format
property 设置为FILES
。例如:
bootstrap.yml.
spring:
cloud:
consul:
config:
format: FILES
给定/config
中的以下键,development
profile 和foo
的 application name:
.gitignore
application.yml
bar.properties
foo-development.properties
foo-production.yml
foo.properties
master.ref
将创建以下 property 源:
config/foo-development.properties
config/foo.properties
config/application.yml
每个 key 的 value 必须是格式正确的 YAML 或 Properties 文件。
63.6 快速失败
如果 consul 不可用于 configuration,在某些情况下(如本地开发或某些测试场景)可能会很方便。在bootstrap.yml
中设置spring.cloud.consul.config.failFast=false
将导致 configuration 模块 log 警告而不是抛出 exception。这将允许 application 正常继续启动。