5. 使用 Consul 进行分布式配置

Consul 提供了一个 Key/Value Store 来存储配置和其他元数据。Spring Cloud Consul Config 是 Config Server 和 Client 的替代方案。在特殊的 “bootstrap” 阶段,将 Configuration 加载到 Spring Environment 中。配置存储在/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

5.1. 如何激活

要开始使用 Consul 配置,请使用 starter with grouporg.springframework.cloud和工件 IDspring-cloud-starter-consul-config.有关使用当前 Spring Cloud Release Train 设置构建系统的详细信息,请参阅 Spring Cloud 项目页面spring-doc.cadn.net.cn

5.2. Spring Boot Config 数据导入

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 代理。删除optional:前缀会导致 Consul Config 无法连接到 Consul 失败。要更改 Consul Config 的连接属性,请将spring.cloud.consul.hostspring.cloud.consul.port或将主机/端口对添加到spring.config.import语句,例如,spring.config.import=optional:consul:myhost:8500.import 属性中的位置优先于 host 和 port 属性。spring-doc.cadn.net.cn

Consul Config 将尝试根据spring.cloud.consul.config.name(默认为spring.application.nameproperty) 和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

一个bootstrap文件(properties 或 yaml)不需要 Spring Boot Config Data 方法的导入方式spring.config.import.

5.3. 自定义

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.

5.4. 配置监视

Consul Config Watch 利用 consul 监视键前缀的能力。Config Watch 会进行阻止 Consul HTTP API 调用,以确定当前应用程序的任何相关配置数据是否已更改。如果有新的配置数据,则会发布 Refresh Event。这相当于调用/refreshactuator 端点。spring-doc.cadn.net.cn

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

禁用 Config Watch 集spring.cloud.consul.config.watch.enabled=false.spring-doc.cadn.net.cn

手表使用弹簧TaskScheduler安排对 Consul 的呼叫。默认情况下,它是一个ThreadPoolTaskScheduler替换为poolSize的 1.要将TaskScheduler,创建一个TaskSchedulerConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME不断。spring-doc.cadn.net.cn

5.5. 使用 Config 的 YAML 或属性

以 YAML 或 Properties 格式存储 blob 属性可能比单个键/值对存储更方便。将spring.cloud.consul.config.formatproperty 设置为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键入 Consul。使用键上方的默认值将如下所示: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

5.6. 使用 Config 的 git2consul

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

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

给定/configdevelopmentprofile 和应用程序名称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 或 Properties 文件。spring-doc.cadn.net.cn

5.7. 快速失败

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

如果您已设置spring.cloud.bootstrap.enabled=truespring.config.use-legacy-processing=true或包括spring-cloud-starter-bootstrap,则需要将上述值放入bootstrap.yml而不是application.yml.