使用 Consul 进行分布式配置
Consul提供了一个键/值存储,用于存储配置和其他元数据。Spring Cloud Consul Config是配置服务器和客户端的替代方案。配置在特殊的“bootstrap”阶段加载到Spring环境中。默认情况下,配置存储在/config文件夹中。根据应用程序的名称和活动配置文件创建多个PropertySource实例,这模拟了Spring Cloud Config解析属性的顺序。例如,一个名为“testApp”且带有“dev”配置文件的应用程序将创建以下属性源:
config/testApp,dev/ config/testApp/ config/application,dev/ config/application/
最具体的属性源在顶部,最不具体的在底部。config/application文件夹中的属性适用于所有使用Consul进行配置的应用程序。config/testApp文件夹中的属性仅适用于名为“testApp”的服务实例。
配置目前在应用程序启动时读取。向/refresh发送HTTP POST请求将导致配置重新加载。配置观察也将自动检测更改并重新加载应用程序上下文。
如何激活
要开始使用Consul配置,请使用group为org.springframework.cloud,artifact id为spring-cloud-starter-consul-config的starter。有关使用当前Spring Cloud发布列车设置构建系统的详细信息,请参阅Spring Cloud项目页面。
Spring Boot配置数据导入
Spring Boot 2.4引入了一种通过spring.config.import属性导入配置数据的新方式。这现在是从Consul获取配置的默认方式。
要选择性地连接到Consul,请在application.properties中设置以下内容:
spring.config.import=optional:consul:
这将连接到默认位置“https://:8500”的Consul Agent。删除optional:前缀将导致Consul Config在无法连接到Consul时失败。要更改Consul Config的连接属性,可以设置spring.cloud.consul.host和spring.cloud.consul.port,或者将host/port对添加到spring.config.import语句中,例如,spring.config.import=optional:consul:myhost:8500。导入属性中的位置优先于host和port属性。
Consul Config将尝试根据spring.cloud.consul.config.name(默认为spring.application.name属性的值)和spring.cloud.consul.config.default-context(默认为application)从四个自动上下文加载值。如果您想指定上下文而不是使用计算出的上下文,您可以将该信息添加到spring.config.import语句中。
spring.config.import=optional:consul:myhost:8500/contextone;/context/two
这将选择性地仅从/contextone和/context/two加载配置。
通过spring.config.import的Spring Boot配置数据导入方法不需要bootstrap文件(properties或yaml)。 |
自定义
Consul Config可以使用以下属性进行自定义:
spring:
cloud:
consul:
config:
enabled: true
prefix: configuration
defaultContext: apps
profileSeparator: '::'
如果您已设置spring.cloud.bootstrap.enabled=true或spring.config.use-legacy-processing=true,或者包含spring-cloud-starter-bootstrap,则上述值需要放在bootstrap.yml中而不是application.yml中。 |
-
enabled将此值设置为“false”将禁用Consul Config -
prefix设置配置值的基本文件夹 -
defaultContext设置所有应用程序使用的文件夹名称 -
profileSeparator设置用于在带有配置文件的属性源中分隔配置文件名称的分隔符的值
配置观察
Consul配置观察利用了Consul观察键前缀的能力。配置观察会进行阻塞的Consul HTTP API调用,以确定当前应用程序的任何相关配置数据是否已更改。如果有新的配置数据,将发布一个刷新事件。这等同于调用/refresh执行器端点。
要更改Config Watch被调用的频率,请更改spring.cloud.consul.config.watch.delay。默认值为1000,单位是毫秒。延迟是上一次调用结束到下一次调用开始之间的时间量。
要禁用Config Watch,请设置spring.cloud.consul.config.watch.enabled=false。
观察使用Spring TaskScheduler来调度对Consul的调用。默认情况下,它是一个ThreadPoolTaskScheduler,poolSize为1。要更改TaskScheduler,请创建一个类型为TaskScheduler的bean,并使用ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME常量命名。
使用配置的YAML或Properties
将一整块属性以YAML或Properties格式存储可能比存储单独的键/值对更方便。将spring.cloud.consul.config.format属性设置为YAML或PROPERTIES。例如,要使用YAML:
spring:
cloud:
consul:
config:
format: YAML
如果您已设置spring.cloud.bootstrap.enabled=true或spring.config.use-legacy-processing=true,或者包含spring-cloud-starter-bootstrap,则上述值需要放在bootstrap.yml中而不是application.yml中。 |
YAML必须在Consul中相应的data键中设置。使用上述默认值,键将如下所示:
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。例如:
spring:
cloud:
consul:
config:
format: FILES
给定/config中的以下键、development配置文件和应用程序名称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文件。