本文基于: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