目录
Spring Cloud Consul Config
Consul 通过 Key/Value 功能集中管理存储配置信息, 通过 Spring Cloud Consul Config 可以实现 Config Server 和 Client 的关联. 在 Spring 启动的 bootstrap 阶段, 配置会被载入环境上下文.
配置前缀, 路径和优先级
默认情况下, 配置的路径前缀是 /config , 不同的 application 和 profile 对应不同的配置路径, 例如对应应用 "testApp" 和 "dev" profile 的配置, 会涉及以下路径- config/testApp,dev/
- config/testApp/
- config/application,dev/
- config/application/
复制代码 这个列表从上往下分别对应的配置优先级从高到低, 优先级高的同样配置项会覆盖优先级低的配置项.
- config/application/ 全局公共配置, 对应使用 config 前缀的所有应用
- config/application,dev/ 全局dev公共配置, 对应使用 config 前缀的所有, 且启用 dev profile 的应用
- config/testApp/ 对应使用 config 前缀的, 名称为 testApp 的应用
- config/testApp,dev/ 对应使用 config 前缀的, 名称为 testApp, 且启用 dev profile 的应用
注意: 配置对应的 profile 和节点应用名是平级的, config/service-name,dev/data 这样, data 是配置的子项, 不要把 profile 加到 data 去了
在项目中启用 Consul Config
如果要使用 Consul 的分布式配置(Distributed Configuration), 需要添加 spring-cloud-starter-consul-config 的依赖
spring-cloud-starter-consul-discovery 不带 spring-cloud-starter-consul-config, 如果需要用 Consul Config, 需要单独添加依赖- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-consul-config</artifactId>
- </dependency>
复制代码 也可以直接用 spring-cloud-starter-consul-all, 包含了 spring-cloud-starter-consul-discovery 和 spring-cloud-starter-consul-config- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-consul-all</artifactId>
- </dependency>
复制代码 配置文件 application.yml
添加了 consul-config 依赖之后, 在 application.yml 就要增加对应的设置 spring.config.import = consul: 否则启动会报错,
Spring Boot 在 2.4 版本之后新增了这个项(spring.config.import property)用于导入配置, 并且是默认的配置方式.- # properties
- spring.config.import=optional:consul:
- # yaml
- spring:
- config:
- import: 'consul:'
复制代码 上面的配置, 如果启动时import失败会导致启动失败, 如果不强制 import, 可以加上 optional:- # properties
- spring.config.import=optional:consul:
- # yaml
- spring:
- config:
- import: 'optional:consul:'
复制代码 上面的这两个配置, 都会用默认的地址 http://localhost:8500 去请求 Consul 服务, 如果需要自定义地址, 可以通过配置spring.cloud.consul.host和spring.cloud.consul.port,- spring:
- cloud:
- consul:
- host: 10.123.123.123
- port: 8501
复制代码 或者使用- spring.config.import=optional:consul:myhost:8500
复制代码 对应以上配置, 在 Spring 启动的 bootstrap 阶段会通过 Consul 去获取 key = config/dummy-service/data 对应的 value (假定这个模块的 application.name = dummy-service),
对应value格式为 yaml, 需要增加配置- # yaml
- spring:
- cloud:
- consul:
- config:
- format: YAML
- prefix: config
- data-key: data
复制代码 其中
- format: YAML 设置配置格式
- prefix: config修改 config/dummy-service/data 的前缀
- data-key: data修改 config/dummy-service/data 的后缀
默认的请求路径生成基于
- spring.cloud.consul.config.name , 值默认等于 spring.application.name
- spring.cloud.consul.config.default-context , 这个值默认等于 application
- spring.profiles.active , 可以在启动时通过 VM Option -Dspring.profiles.active=xxx 指定
如果不想用默认的, 想自己指定, 可以用如下的方式- # properties
- spring.config.import=optional:consul:myhost:8500/config/custom/context/one;/config/custom/context/two
复制代码 上面的设置将只从这两个Key/Value路径读取配置, 注意路径的对应关系, 在import中体现前缀 config, 但是不体现后缀 data
- /config/custom/context/one/data
- /config/custom/context/two/data
配置自动更新, Config Watch
Consul Config Watch 使用 consul 的路径前缀对配置更新进行检查, 当配置变化时会产生一个 Refresh Event, 等价于请求 /refresh actuator endpoint.
默认的检查频率为 1000 单位毫秒, 可以通过 spring.cloud.consul.config.watch.delay 配置
如果要禁用配置自动更新, 需要设置 spring.cloud.consul.config.watch.enabled=false
Consul 配置管理
通过 WEB 界面
默认为 http://127.0.0.1:8500 可以在 Key/Value 中直接添加, 记得格式要改为 YAML
通过命令行
读取- $ ./consul kv get foo
- bar
- $ ./consul kv get config/application/data
- cassandra:
- host: 127.0.0.1:9042,127.0.0.2:9042
- user: my_user
- password: my_pass
复制代码 使用文件data.yml中的内容, 直接写入- $ ./consul kv put config/application/data @data.yml
- Success! Data written to: config/application/data
- The data can be retrieved the same way,
复制代码 使用配置
经过以上配置, 在项目中就可以通过 @Configuration 获取 Consul 中配置的信息- @Slf4j
- @Configuration
- public class CommonConfig {
- @Value("${common.name}")
- private String name = "Dummy";
- @Value("${common.code}")
- private String code = "001";
- public String getName() {return name;}
- public void setName(String name) {this.name = name;}
- public String getCode() {return code;}
- public void setCode(String code) {this.code = code;}
- @PostConstruct
- public void postConstruct() {
- log.info("name: {}", name);
- log.info("code: {}", code);
- }
- }
复制代码 参考
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |