使用 Consul 进行分布式配置

Consul 提供了一个键/值存储,用于存储配置和其他元数据。Spring Cloud Consul Config 是 Config Server 和 Client 的替代方案。在特殊的“引导”阶段将配置加载到 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

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

如何激活

要开始使用 Consul 配置,请使用带有组的Startersorg.springframework.cloud和工件 IDspring-cloud-starter-consul-config.有关使用当前 Spring Cloud Release Train 设置构建系统的详细信息,请参阅 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

应用程序属性
spring.config.import=optional:consul:

这将连接到默认位置“http://localhost:8500”的 Consul 代理。删除optional:prefix 将导致 Consul Config 失败,如果它无法连接到 Consul。要更改 Consul Config 的连接属性,请将spring.cloud.consul.hostspring.cloud.consul.port或将主机/端口对添加到spring.config.import语句,例如,spring.config.import=optional:consul:myhost:8500.import 属性中的位置优先于主机和端口属性。spring-doc.cadn.net.cn

Consul Config 将尝试从四个自动上下文中加载值,具体取决于spring.cloud.consul.config.name(默认为spring.application.name属性)和spring.cloud.consul.config.default-context(默认为application).如果要指定上下文而不是使用计算上下文,可以将该信息添加到spring.config.import陈述。spring-doc.cadn.net.cn

应用程序属性
spring.config.import=optional:consul:myhost:8500/contextone;/context/two

这将选择仅从/contextone/context/two.spring-doc.cadn.net.cn

一个bootstrap文件(属性或 yaml)对于通过导入的 Spring Boot Config Data 方法不需要spring.config.import.

定制

Consul Config 可以使用以下属性进行自定义: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执行器端点。spring-doc.cadn.net.cn

更改调用配置监视的频率 更改spring.cloud.consul.config.watch.delay.默认值为 1000,以毫秒为单位。延迟是上一个调用结束和下一个调用开始后的时间量。spring-doc.cadn.net.cn

禁用配置监视集spring.cloud.consul.config.watch.enabled=false.spring-doc.cadn.net.cn

手表使用弹簧TaskScheduler安排给领事的电话。默认情况下,它是ThreadPoolTaskScheduler使用poolSize的 1.要更改TaskScheduler,创建一个类型为TaskScheduler命名为ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME不断。spring-doc.cadn.net.cn

YAML 或带有配置的属性

以 YAML 或属性格式存储属性 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 必须在适当的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 与 Config

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

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

给定以下键/configdevelopment配置文件,应用程序名称为foo:spring-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.