使用 Consul 的分布式配置

Consul 提供了一个用于存储配置和其他元数据的 Key/Value Store(键/值存储)。Spring Cloud Consul Config 是 Config Server 和 Client 的替代方案。配置在特殊的“bootstrap”阶段被加载到 Spring Environment 中。默认情况下,配置存储在 /config 文件夹中。会根据应用程序的名称和活动 profiles 创建多个 PropertySource 实例,这模仿了 Spring Cloud Config 解析属性的顺序。例如,名称为 "testApp" 且 profile 为 "dev" 的应用程序将创建以下属性源:

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

最具体的属性源在顶部,最不具体的在底部。config/application 文件夹中的属性适用于所有使用 Consul 进行配置的应用程序。config/testApp 文件夹中的属性仅适用于名为 "testApp" 的服务实例。

当前配置在应用程序启动时读取。向 /refresh 发送 HTTP POST 请求将导致配置重新加载。Config Watch 也会自动检测更改并重新加载应用上下文。

如何激活

要开始使用 Consul 配置,请使用 group 为 org.springframework.cloud 且 artifact id 为 spring-cloud-starter-consul-config 的 starter。有关如何使用当前的 Spring Cloud Release Train 设置构建系统的详细信息,请参阅 Spring Cloud 项目页面

Spring Boot Config Data Import

Spring Boot 2.4 引入了一种通过 spring.config.import 属性导入配置数据的新方法。现在这是从 Consul 获取配置的默认方式。

要选择性地连接到 Consul,请在 application.properties 中进行以下设置

application.properties
spring.config.import=optional:consul:

这将连接到默认位置 "http://localhost: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。import 属性中的位置优先于 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 Config Data 方法,**不需要** 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 设置在带 profile 的属性源中用于分隔 profile 名称的分隔符的值

配置监视 (Config Watch)

Consul Config Watch 利用了 Consul 监视 key 前缀 的能力。Config Watch 会发起一个阻塞的 Consul HTTP API 调用,以确定当前应用程序是否有任何相关的配置数据发生变化。如果有新的配置数据,就会发布一个 Refresh Event(刷新事件)。这相当于调用 /refresh actuator 端点。

要更改 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 中相应的数据键下。使用上述默认设置,键看起来像这样

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 profile 和应用程序名称 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 文件。

快速失败 (Fail Fast)

在某些情况下(例如本地开发或特定测试场景),如果 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 中。