解决hystrix dashboard中报错Unable to connect to Command Metric Stream及相关问题

1 篇文章 0 订阅

解决 hystrix dashboard 中报错 Unable to connect to Command Metric Stream及相关问题

在练习 spring cloud hystrix dashboard 时,总结遇到的三个问题。
前提:
在提供者客户端(端口号为8001)要有依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在消费者客户端(端口号为9001)要有依赖:

<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>

1. 访问 http://localhost:8001/actuator/hystrix.stream,页面空白。

情况如下:
在这里插入图片描述
这个问题解决:我们要在提供者客户端的主启动类添加 ServletRegistrationBean 组件即可解决空白页问题。

@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
    registrationBean.addUrlMappings("/actuator/hystrix.stream");
    return registrationBean;
}

2. 访问 http://localhost:8001/actuator/hystrix.stream,页面一直ping。

情况如下:
在这里插入图片描述
这个问题解决:首先我们要在提供者客户端的控制类中添加熔断机制

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "processHystrix_Get")
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = deptService.get(id);
        if (dept == null) {
            throw new RuntimeException("该ID: " + id + " 没有对应的信息");
        }
        return dept;
    }

    public Dept processHystrix_Get(@PathVariable("id") Long id) {
        return new Dept().setDeptno(id)
                .setDname("该ID: " + id + " 没有对应的信息,null--@HystrixCommand")
                .setDb_source("no this database in MySQL");
    }
}

其次还要在提供者客户端主启动类添加对 Hystrix 熔断机制的支持

@EnableCircuitBreaker

重启eureka、提供者客户端和消费者客户端服务,最后请求几次控制类中的方法之后再去访问http://localhost:8001/actuator/hystrix.stream,就会ping出结果,如下:
在这里插入图片描述

3. 熔断监控图形化界面报错Unable to connect to Command Metric Stream

情况如下:
在这里插入图片描述
问题解决:在消费者客户端的 yml 文件中添加如下代码即可解决

hystrix:
  dashboard:
    proxy-stream-allow-list: "*"

重启eureka、提供者客户端和消费者客户端服务,发送几次请求之后刷新熔断监控图形化页面即可,最终结果如下:
在这里插入图片描述

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值