Spring Cloud之HystrixDashboard里监控一直是Unable to connect to Command Metric Stream

本文基于:spring-boot-2.3.12-RELEASE、spring-cloud-Hoxton.SR12(即2.2.9-RELEASE)

这个是一个巨坑,可以从以下几个方面来排查问题
  • 现象如下:

1、pom依赖是否加完整

  • Hystrix dashboard端
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
    </dependencies>
  • 被监控的应用端(以下是必须加的)
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

2、dashboard域名过滤配置

  • 在dashboard的配置文件里需要加上如下(根据是情况修改)
hystrix:
  dashboard:
    proxy-stream-allow-list: localhost

3、检查新增的servlet是否能正常访问

  • 在被监控的服务端代码里新增servlet代码如下(按照dashboard的推荐,url 以 /actuator/hystrix.stream结尾,但也可以写成其他的):
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        return registrationBean;
    }
  • 启动被监控的服务,然后通过链接访问看是否有页面,如果有类似界面则表示配置成功了:

4、是否加了降级、熔断相关的注解(比如 @HystrixCommand(fallbackMethod = “infoHystrix”),亲测如果没加是不会有效果的)

  • 代码里需要有降级、熔断相关的注解,否则不会生效
package com.xx.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class P1Controller {


    @HystrixCommand(fallbackMethod = "infoHystrix") // 该注解必须在被监控端使用
    @RequestMapping("/info")
    public String info() {
        return "provider01-" + System.currentTimeMillis();
    }

    public String infoHystrix() {
        return "infoHystrix-" + System.currentTimeMillis();
    }
}

5、是否有对相应的服务做访问,这一点很坑

  • 如果不先访问被监控的服务,则dashboard里就会一直显示hystrix dashboard Unable to connect to Command Metric Stream,而且单独打开页面访问 http://localhost:8003/actuator/hystrix.stream,也会一直显示ping 没有data


5、附一张配置图:
在这里插入图片描述

  • 成功图:
    在这里插入图片描述
示例代码
  • 示例代码上传到了【码云】
    https://gitee.com/aqu415/demo/tree/master/springcloud

在这里插入图片描述

good luck
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值