Spring Cloud 集成 Hystrix Dashboard 仪表盘问题汇总

【注】出现问题的版本在 SpringBoot 2.x 以上,1.x 的仅供参考,一般出问题都是第三种类型

1. 使用 feign调用的服务,需要设置 hystrisfeign:hystrix:enabled: true 即可

2. 网关服务 zuul 本来就默认有 Hystrix Dashboard,不用额外配置

3. 非 feign 的 SpringBoot 项目,需要以下配置:

在解决问题之前首先要保证正确引入以下三个依赖,并且启动类有以下三个注解,并且服务接口一定要有 @HystrixCommand注解,否则一切都没有意义。

需要的依赖:

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

启动类需要添加以下三个注解:

@EnableHystrix
@EnableCircuitBreaker
@EnableHystrixDashboard

服务接口有 @HystrixCommand 注解,例如:

@RequestMapping("/hi")
@HystrixCommand(fallbackMethod = "hiError")
public String home(@RequestParam(value = "name", defaultValue = "roc") String name) {
    return "hi " + name + " ,i am from port:" + port;
}

public String hiError(String name) {
    return "hi,"+name+",sorry,error! The system is under repair.";
}

问题汇总:

Q:访问 hystrix.stream 时 404 Not Found?

A:访问的时候不要访问 http://localhost:8762/hystrix.stream,要访问 http://localhost:8762/actuator/hystrix.stream

Q:访问 /actuator/hystrix.stream 时 404 Not Found?

A:请在启动类中添加以下代码:

@Bean
public ServletRegistrationBean getServlet() {
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/actuator/hystrix.stream");
    registrationBean.setName("HystrixMetricsStreamServlet");
    return registrationBean;
}

重新启动即可访问。

Q:访问 /actuator/hystrix.stream 一直 ping ?

A:需要调用一个 hysrix 服务接口,该接口必须添加 @HystrixCommand 注解,如果没有调用,访问 hystrix.stream 会一直ping,hystrix 监控界面一直 loading,这个时候查看 hystrix.stream 是没数据的。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注