请使用 Spring Cloud Consul 5.0.1(最新稳定版本)!spring-doc.cadn.net.cn

分布式配置与 Consul

Consul 提供了一个 键/值存储,用于存储配置和其他元数据。Spring Cloud Consul Config 是 配置服务器和客户端 的替代方案。配置在特殊的“引导”阶段加载到 Spring 环境中。默认情况下,配置存储在 /config 文件夹中。根据应用程序名称和激活的配置文件,会创建多个 PropertySource 实例,其顺序模仿了 Spring Cloud Config 解析属性的优先级。例如,一个名为 "testApp" 且带有 "dev" 配置文件的应用程序将生成以下属性源:spring-doc.cadn.net.cn

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

最具体的属性源位于顶部,最不具体的位于底部。位于 config/application 文件夹中的属性适用于所有使用 Consul 进行配置的应用程序。位于 config/testApp 文件夹中的属性仅对服务名称为 "testApp" 的实例可用。spring-doc.cadn.net.cn

配置当前在应用程序启动时读取。向 /refresh 发送 HTTP POST 请求将导致配置重新加载。配置监控 也会自动检测到变更并重新加载应用程序上下文。spring-doc.cadn.net.cn

如何激活

要开始使用 Consul 配置,请使用组 org.springframework.cloud 和构件 ID spring-cloud-starter-consul-config 的Starters。有关如何使用当前 Spring Cloud 发行版列车配置构建系统的详细信息,请参阅 Spring Cloud 项目页面spring-doc.cadn.net.cn

Spring Boot 配置数据导入

Spring Boot 2.4 引入了一种通过 spring.config.import 属性导入配置数据的新方式。如今,这是从 Consul 获取配置的默认方式。spring-doc.cadn.net.cn

可选地连接到 Consul,请在 application.properties 中设置以下内容:spring-doc.cadn.net.cn

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

这将连接到 Consul 代理,其默认位置为 "http://localhost:8500"。移除 optional: 前缀会导致如果无法连接到 Consul,则 Consul 配置失败。要更改 Consul 配置的连接属性,请设置 spring.cloud.consul.hostspring.cloud.consul.port,或在 spring.config.import 语句中添加主机/端口对,例如 spring.config.import=optional:consul:myhost:8500。导入属性中的位置优先于主机和端口属性。spring-doc.cadn.net.cn

Consul 配置将尝试根据 spring.cloud.consul.config.name(其默认值为 spring.application.name 属性的值)和 spring.cloud.consul.config.default-context(其默认值为 application)这四个自动上下文加载值。如果您希望显式指定上下文而非使用计算出的上下文,可将相关信息添加到 spring.config.import 语句中。spring-doc.cadn.net.cn

application.properties
spring.config.import=optional:consul:myhost:8500/contextone;/context/two

这将可选地仅从 /contextone/context/two 加载配置。spring-doc.cadn.net.cn

使用 Spring Boot 配置数据导入方法时,无需 bootstrap 文件(属性文件或 YAML 文件)即可通过 spring.config.import 进行导入。

自定义

Consul 配置可使用以下属性进行自定义:spring-doc.cadn.net.cn

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

配置监视

Consul 配置观察程序利用了 Consul 的功能,可以监视密钥前缀。配置观察程序会发出一个阻止的 Consul HTTP API 调用,以确定当前应用程序是否有任何相关的配置数据已更改。如果存在新的配置数据,将发布 Refresh 事件。这相当于调用/refreshactuator端点。spring-doc.cadn.net.cn

要更改配置监视程序被调用的频率,请更改 0 。默认值为 1000,以毫秒为单位。延迟是上次调用结束和下次调用开始之间的延时。 spring-doc.cadn.net.cn

若要禁用 Config Watch,请将 spring.cloud.consul.config.watch.enabled=false 设置。spring-doc.cadn.net.cn

此监视器使用 Spring TaskScheduler 来计划对 Consul 的调用。默认情况下,其值为 ThreadPoolTaskSchedulerpoolSize 为 1。若要更改此 TaskScheduler,请创建一个名为 TaskSchedulerConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME 常量。spring-doc.cadn.net.cn

YAML 或者属性文件配置

它可能更方便地以 YAML 或 Properties 格式存储 blob 属性,而不是单独的键/值对。 将spring.cloud.consul.config.format属性设置为YAMLPROPERTIES。 例如,要使用 YAML:spring-doc.cadn.net.cn

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 的适当 data 键中设置。以上述默认值为例,密钥看起来像这样:spring-doc.cadn.net.cn

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

你可以将YAML文档存储在以上所有键中列出的位置。spring-doc.cadn.net.cn

你可以通过 spring.cloud.consul.config.data-key 更改数据键。spring-doc.cadn.net.cn

使用配置进行git2consul

git2consul 是一个 Consul 社区项目,它从 Git 存储库加载文件到 Consul 中的单个密钥。默认情况下,密钥名称是文件名。支持带有扩展名.yml.properties的 YAML 和属性文件。设置spring.cloud.consul.config.format属性为FILES。例如:spring-doc.cadn.net.cn

bootstrap.yml
spring:
  cloud:
    consul:
      config:
        format: FILES

给定以下键在/configdevelopment配置文件和应用程序名是foospring-doc.cadn.net.cn

.gitignore
application.yml
bar.properties
foo-development.properties
foo-production.yml
foo.properties
master.ref

这个属性源将会被创建:spring-doc.cadn.net.cn

config/foo-development.properties
config/foo.properties
config/application.yml

每个键的值都需要是格式正确的YAML或属性文件。spring-doc.cadn.net.cn

失败快速

在某些情况下(如本地开发或某些测试场景)方便,如果配置不可用,Consul 不会失败。将spring.cloud.consul.config.fail-fast=false设置为导致配置模块记录警告而不是抛出异常。这将允许应用程序正常启动。spring-doc.cadn.net.cn

如果您已设置 spring.cloud.bootstrap.enabled=truespring.config.use-legacy-processing=true,或包含 spring-cloud-starter-bootstrap,则上述值需放置在 bootstrap.yml 中,而非 application.yml