使用Consul作为注册中心和配置中心
什么是Consul?
Consul是HashiCorp公司推出的一款开源工具,用于实现分布式系统的服务发现、配置和网络自动化。它提供以下核心功能:
- 服务发现:服务可注册到Consul,其他服务可通过Consul发现依赖服务
- 健康检查:自动监控服务健康状态
- KV存储:键值存储用于动态配置
- 多数据中心支持:开箱即用的多数据中心支持
- 访问控制:ACL和令牌系统保证安全
Consul与其他分布式组件的比较
组件 | 服务发现 | 配置中心 | 健康检查 | 多数据中心 | KV存储 | ACP(一致性协议) | 特点 |
---|---|---|---|---|---|---|---|
Consul | ✅ | ✅ | ✅ | ✅ | ✅ | CP | 一体化解决方案,多DC支持 |
Eureka | ✅ | ❌ | ✅ | ❌ | ❌ | AP | AP系统,纯服务发现 |
Zookeeper | ✅ | ✅ | ✅ | ❌ | ✅ | CP | CP系统,强一致性,配置复杂 |
Nacos | ✅ | ✅ | ✅ | ✅ | ✅ | AP/CP | 阿里系,AP/CP可切换 |
为什么选择Consul?
- 一致性平衡:基于Raft协议实现CP系统,同时通过健康检查保证可用性
- 一体化解决方案:同时提供注册中心和配置中心功能
- 多数据中心支持:原生支持多数据中心部署
- 健康检查丰富:支持HTTP/TCP/Shell等多种健康检查方式
- 轻量级:相比Zookeeper等更轻量
- 社区活跃:由HashiCorp维护,社区支持良好
作为注册中心使用
1. 启动Consul服务
# 开发模式启动
consul agent -dev
# 查看集群成员
consul members
2. Spring Cloud集成
添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>3.1.1</version>
</dependency>
配置application.yml:
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
instance-id: ${spring.application.name}:${server.port}
health-check-path: /actuator/health
health-check-interval: 15s
作为配置中心使用
从官方参考文档查看Consul支持两种配置加载方式:
方式一:bootstrap.yml(传统方式)
- 添加依赖:
版本要高于3.0.4,否则配置中心可能会失效
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
- 创建bootstrap.yml:
spring:
application:
name: userservice
profiles:
active: consul # 环境
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
heartbeat:
enabled: true
prefer-ip-address: true
health-check-interval: 15s
register: true
config:
profile-separator: "-" #分隔符,默认是,类似创建keyvalue时是userservice-consul
format: yaml # 格式
watch:
wait-time: 1 # 热更新的时间,配合注解@RefreshScope使用
方式二:spring.config.import(推荐)
- 在application.yml中添加:
spring:
application:
name: userservice
config:
import: optional:consul:127.0.0.1:8500
cloud:
consul:
config:
server-addr: ${NACOS_SERVER_ADDR:127.0.0.1:8500}
profile-separator: "-"
format: yaml
watch:
wait-time: 1
host: 127.0.0.1
port: 8500
discovery:
heartbeat:
enabled: true
prefer-ip-address: true
health-check-interval: 15s
register: true
-
去掉bootstrap依赖引入,否则会报错
Parameter 0 of method configWatch in org.springframework.cloud.consul.config.ConsulConfigAutoConfiguration$ConsulRefreshConfiguration required a single bean, but 2 were found: - configDataConsulConfigProperties: a programmatically registered singleton - consulConfigProperties: defined by method 'consulConfigProperties' in class path resource [org/springframework/cloud/consul/config/ConsulConfigBootstrapConfiguration$ConsulPropertySourceConfiguration.class] Action: Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
创建key-value
默认地址 config/${spring.application.name}-${spring.profiles.active}/data
这里的-是profile-separator配置的,也可以自定义配置
开始创建
配置存储示例
通过Consul UI或API存储配置:
curl --request PUT --data \
'{
"app":
"name": "demo-service",
"version": "1.0.0"
}' \
http://localhost:8500/v1/kv/config/demo-service/data
官方资源
总结
Consul作为注册中心和配置中心的优势在于:
- 开箱即用的多数据中心支持
- 基于Raft协议的强一致性保证(CP)
- 与Spring Cloud生态良好集成
- 同时提供KV存储和服务发现能力
- 丰富的健康检查机制
适合需要统一服务发现和配置管理的分布式系统,特别是多数据中心场景。