13 consul的基本使用

Consul 支持健康检查,并提供了 HTTP 和 DNS 调用的API接口完成服务注册,服务发现,以及K/V存储 这些功能。接下来通过发送HTTP请求的形式来了解一下Consul

服务注册与发现

(1)注册服务

通过postman发送put请求到http://192.168.74.101:8500/v1/catalog/register地址可以完成服务注册

{
 "Datacenter": "dc1", 
 "Node": "node01", 
 "Address": "192.168.74.102",
 "Service": {
 "ID":"mysql-01",
 "Service": "mysql", 
 "tags": ["master","v1"], 
 "Address": "192.168.74.102",
 "Port": 3306
 }
}

(2)服务查询

通过postman发送get请求到http://192.168.74.101:8500/v1/catalog/services查看所有的服务列表

image-20211224221925349.png

通过postman发送get请求到http://192.168.74.101:8500/v1/catalog/service/服务名查看具体的服务详情

image-20211224221955414.png

(3)服务删

通过postman发送put请求到http://192.168.74.101:8500/v1/catalog/deregister删除服务

{
  "Datacenter": "dc1",
  "Node": "node01",
  "ServiceID": "mysql-01"
}

Consul的KV存储

可以参照Consul提供的KV存储的API完成基于Consul的数据存储

image-20211224222108467.png
  1. key值中可以带/, 可以看做是不同的目录结构。
  2. value的值经过了base64_encode,获取到数据后base64_decode才能获取到原始值。数据不能大 于512Kb
  3. 不同数据中心的kv存储系统是独立的,使用dc=?参数指定。

基于consul的服务注册

案例准备

(1)复制一份新的工程进行配置

拷贝一份新的工程,起名为 shop_consul_parent ,并导入相关的子模块


image-20211224222306109.png

(2)修改微服务的相关pom文件

修改每个微服务的pom文件,添加SpringCloud提供的基于Consul的依赖

<!--SpringCloud提供的基于Consul的服务发现-->
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
 <!--actuator用于心跳检查-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

其中 spring-cloud-starter-consul-discovery 是SpringCloud提供的对consul支持的相关依赖。

spring-boot-starter-actuator 适用于完成心跳检测响应的相关依赖。

配置服务注册

修改每个微服务的application.yml配置文件,添加consul服务发现的相关配置信息

spring:
 ...省略
 cloud:
   consul: #consul相关配置
     host: 192.168.74.101 #ConsulServer请求地址
     port: 8500 #ConsulServer端口
     discovery:
        #是否注册
       register: true
        #实例ID
       instance-id: ${spring.application.name}-1
        #服务实例名称
       service-name: ${spring.application.name}
        #服务实例端口
       port: ${server.port}
        #健康检查路径
       healthCheckPath: /actuator/health
        #健康检查时间间隔
       healthCheckInterval: 15s
        #开启ip地址注册
       prefer-ip-address: true
        #实例的请求ip
       ip-address: ${spring.cloud.client.ip-address}

其中 spring.cloud.consul 中添加consul的相关配置

  1. host:表示Consul的Server的请求地址

  2. port:表示Consul的Server的端口

  3. discovery:服务注册与发现的相关配置

    instance-id : 实例的唯一id(推荐必填),spring cloud官网文档的推荐,为了保证生成一 个唯一的id ,也可以换成 {spring.application.name}:{spring.cloud.client.ipAddress} prefer-ip-address:开启ip地址注册

    ip-address:当前微服务的请求ip

在控制台中查看服务列表

打开ConsulServer的管理控制台,可以发现三个微服务已经全部注册到Consul中了。

image-20211224222712952.png

基于consul的服务发现

由于SpringCloud对Consul进行了封装。对于在消费者端获取服务提供者信息和Eureka是一致的。同样 使用 DiscoveryClient 完成调用获取微服务实例信息

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是 Consul​ Consul是一种服务网格解决方案,提供具有服务发现,配置和分段功能的全功能控制平面。这些...
    P_ursuit阅读 2,721评论 0 0
  • 背景 consul使用来做服务发现,提供了开箱即用的功能,这里记录下遇到的问题和使用总结 介绍 常用API age...
    tony_0c73阅读 7,441评论 0 3
  • 相关源码: spring cloud demo 什么是服务发现 微服务的框架体系中,服务发现是不能不提的一个模块。...
    不小下阅读 48,038评论 11 56
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • Consul是实现服务治理的杰出项目,它可以和Docker完美结合在一起。Consul是HashiCrop公司推出...
    奋斗_登阅读 299评论 0 0