带Consul的分布式配置

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

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

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

配置目前在应用程序启动时读取。发送HTTP POST地址/刷新会导致配置重新加载。配置观察还会自动检测更改并重新加载应用上下文。spring-doc.cadn.net.cn

如何激活

要开始使用Consul配置,可以使用带组的起始程序org.springframework.cloud以及工件IDSpring-cloud-starter-consul-config.请参阅 Spring Cloud Project 页面,了解如何使用当前的 Spring Cloud 发布列车来设置你的构建系统。spring-doc.cadn.net.cn

Spring Boot Config Data Import

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:

这会连接到默认位置“http://localhost:8500”的领事代理。移除自选:如果 Consul 配置无法连接,前缀会导致 Consul 配置失败。要更改Consul Config的连接属性,可以设置任一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(默认为应用).如果你想指定上下文而不是使用计算出来的上下文,你可以把这些信息添加到spring.config.import陈述。spring-doc.cadn.net.cn

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

这将选择性地加载从以下的配置。/contextone/上下文/二.spring-doc.cadn.net.cn

一个启动文件(属性或 yaml)对于 Spring Boot 配置数据方法的导入方式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,以确定当前应用是否有相关配置数据发生变化。如果有新的配置数据,会发布刷新事件。这等价于调用/刷新执行器端点。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

这款手表使用了发条任务调度器安排与领事通话。默认情况下,它是ThreadPoolTaskScheduler其中池大小1。要更改任务调度器,创建 的 bean任务调度器ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME不断。spring-doc.cadn.net.cn

YAML 或 Properties with Config

存储一大堆属性以YAML或属性格式存储可能比单独的键值对更方便。设置Spring.cloud.consul.config.format属性到YAML性能.例如,使用 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 必须设置在相应的数据领事钥匙。使用上述默认键,按键看起来会是: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 with Config

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

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

给定以下密钥/config发展配置文件及应用名称: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 或 Properties 文件。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.