

Spring Cloud开发实践(七): 集成Consul配置中心 - Milton
source link: https://www.cnblogs.com/milton/p/17448461.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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)用于导入配置, 并且是默认的配置方式.
# propertiesspring.config.import=optional:consul:# yamlspring: config: import: 'consul:'
上面的配置, 如果启动时import失败会导致启动失败, 如果不强制 import, 可以加上 optional:
# propertiesspring.config.import=optional:consul:# yamlspring: 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, 需要增加配置
# yamlspring: 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
指定
如果不想用默认的, 想自己指定, 可以用如下的方式
# propertiesspring.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 foobar $ ./consul kv get config/application/datacassandra: 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.ymlSuccess! Data written to: config/application/dataThe data can be retrieved the same way,
经过以上配置, 在项目中就可以通过 @Configuration 获取 Consul 中配置的信息
@Slf4j@Configurationpublic 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); }}
Recommend
-
56
-
58
同事dnsmasq + consul 整服务自动发现,但是不是tls且consul server是单点,而且进程启动用的nohub而不是systemd,这里查了下资料实践了下 开了五台云主机,总体规划为下面,角色那有条件的可以分开,例如client单独一台 IP...
-
29
前面我们已经学习过 Spring Cloud Config 了: Spring Cloud 系列之 Config 配置中心(一)
-
22
前言 默认情况下,用nacos-sdk-csharp集成ASP.NET Core的配置系统,是基于JSON格式的数据。 随着业务系统的多样化,可能用的配置格式也是各有千秋的。有的会用yaml/yml,有的会用ini,有的会用xml,等...
-
8
Api网关Kong集成Consul做服务发现及在Asp.Net Core中的使用
-
6
上一次我们介绍了 Ocelot 网关的基本用法。这次我们开始介绍服务注册发现组件 Consul 的简单使用方法。 服务注册发现 首先先让我们回顾下服务注册发现的...
-
17
使用consul作为istio的注册中心(intree or by service entry) by 伊布 默认istio使用k8s作为注册中心,k8s的service、endpoint对应于服务...
-
9
-
14
Consul 服务 启动Consul服务, 在Win10下可以执行以下命令, 或者存成bat文件运行, 保持窗口打开 consul agent -dev -client=0.0.0.0 -data-dir .\ -advertise 127.0.0.1 -ui -config-dir .\...
-
16
关于 Consul HashiCorp Consul 是微服务网络解决方案之一, 用于管理跨网络和多云环境服务之间的安全网络连接, 提供服务发现, 服务网格, 流量管理和自动更新. 可以单独部署, 也可以分布式部署. Consul 的特点
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK