解决 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、提供者客户端和消费者客户端服务,发送几次请求之后刷新熔断监控图形化页面即可,最终结果如下: