spring cloud consul服务注册后无法完成心跳检测

这里记录下,搭建consul时,服务可以注册发现但是无法完成心跳检测的坑

环境为spring-cloud 20222.0.0 ,springboot 3.0.2,jdk17 这是环境,微服务basic-service,使用consul完成注册发现,运行在windows11环境下

1.安装consul

  1. consul 服务端下载:官方下载地址

  2. 下载后解压是一个exe 执行文件,运行的方式,在此文件夹下右键运行cmd,然后执行命令:./consul.exe agent -dev
    如下图,成功运行
    在这里插入图片描述

  3. 默认访问端口为8500 ,如下图
    在这里插入图片描述

2.搭建微服务 basic-service

  1. pom.xml 文件引入 spring-cloud-consul 依赖
      // spring boot 依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        // 这个是配置,这里没用到,可以不用
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
  1. 配置文件:
server:
  port: 20500
spring:
  application:
    name: basic-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
      enabled: true
      service-name: ${spring.application.name}    
  1. 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class BasicApplication {
    public static void main(String[] args) {
        SpringApplication.run(BasicApplication.class, args);
    }
}

3.启动consul 后 然后启动微服务

  1. 查看consul面板 在这里插入图片描述
    发现微服务无法显示状态是异常的
    点进去查看如图,在这里插入图片描述
    没发现问题出在哪里,由于心跳检测是 consul 服务主动发出去,所以去看consul服务的打印信息,在这里插入图片描述
    发现,检测时调的是这个接口,这里分析了好久,如果微服务提供的这个接口要存在,那应该是consul的依赖包中给出的接口来用于心跳检测,但是查了下没有,这里就发现事情不对了
    然后在回去看consul面板,发现了重要信息
    在这里插入图片描述
    这里异常信息就很明显了,这里调用的是 spring-actuator 这个检测工具提供的接口!!!
    所以 只需要在依赖中引入此工具包即可,修改pom.xml 添加如下依赖
// 如果没引入 spring-cloud-dependencies 记得自己添加对应的版本号
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

然后重启启动 微服务,ok了
在这里插入图片描述

4. 微服务中consul的配置信息

如果在微服务框架中使用bootstrap.yml ;那么consul 的配置要放到bootstrap中

以下是注意点


spring:
  cloud:
    consul:
      host: 124.222.73.88
      port: 18090
      discovery:
        enabled: true # 开启服务
        service-name: ${spring.application.name} #自定义名称
        hostname: 127.0.0.1 # 配置hostname,默认为电脑的名称,如果不是consul和 微服务不是一台机器,一定要更改,否则无法就要配置consul所在服务器的 hosts了;例如:这里不配置,我的电脑名称为 xk;那么要在 consul中的hosts配置: 172.10.10.56(我的内网地址) xk。
        ip-address: 127.0.0.1 # 注册时的ip地址,prefer-ip-address 不开启,默认使用 hostname
        prefer-ip-address: true #开启:注册时使用ip地址,不使用hostname,如果没配置 ip-address,则使用所在的内网地址;强烈建议开启,测试时配置ip-address为 127.0.0.1,生产中 无需配置ip-address,使用服务器的内网地址即可
        health-check-path: /heartCheck #自定义 检测路径,上面写了,默认为 http://xx:8080/actuator/health,如果自定义了路径,就不需要引入 spring-actuator的依赖了。
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值