使用Consul作为注册中心和配置中心

使用Consul作为注册中心和配置中心

什么是Consul?

Consul是HashiCorp公司推出的一款开源工具,用于实现分布式系统的服务发现、配置和网络自动化。它提供以下核心功能:

  • 服务发现:服务可注册到Consul,其他服务可通过Consul发现依赖服务
  • 健康检查:自动监控服务健康状态
  • KV存储:键值存储用于动态配置
  • 多数据中心支持:开箱即用的多数据中心支持
  • 访问控制:ACL和令牌系统保证安全

Consul与其他分布式组件的比较

组件服务发现配置中心健康检查多数据中心KV存储ACP(一致性协议)特点
ConsulCP一体化解决方案,多DC支持
EurekaAPAP系统,纯服务发现
ZookeeperCPCP系统,强一致性,配置复杂
NacosAP/CP阿里系,AP/CP可切换

为什么选择Consul?

  1. 一致性平衡:基于Raft协议实现CP系统,同时通过健康检查保证可用性
  2. 一体化解决方案:同时提供注册中心和配置中心功能
  3. 多数据中心支持:原生支持多数据中心部署
  4. 健康检查丰富:支持HTTP/TCP/Shell等多种健康检查方式
  5. 轻量级:相比Zookeeper等更轻量
  6. 社区活跃:由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(传统方式)

在这里插入图片描述

  1. 添加依赖:

版本要高于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>
  1. 创建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(推荐)

在这里插入图片描述

  1. 在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
  1. 去掉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作为注册中心和配置中心的优势在于:

  1. 开箱即用的多数据中心支持
  2. 基于Raft协议的强一致性保证(CP)
  3. 与Spring Cloud生态良好集成
  4. 同时提供KV存储和服务发现能力
  5. 丰富的健康检查机制

适合需要统一服务发现和配置管理的分布式系统,特别是多数据中心场景。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值