依赖
父版本
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
子模块
1.hystrix-dashboard模块 不用注册到eureka,
<!--web 模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring:
application:
name: cloud-hystrix-dashboard
server:
port: 52001
hystrix:
dashboard:
proxy-stream-allow-list: "localhost"
#management.endpoints.web.exposure.include=hystrix.stream
#降级熔断hystrix
management:
endpoints:
web:
exposure:
include: "*" #暴露所有
@EnableHystrixDashboard
@SpringBootApplication
public class DashboardApplication {
public static void main(String[] args) {
SpringApplication.run(DashboardApplication.class, args);
}
}
消费者模块:
其他业务不说,只要是controller请求就行
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
# 使用Hystrix
management:
endpoints:
web:
exposure:
include: "*"
#开启feign支持hystrix,默认关闭
#feign:
# hystrix:
# enabled: true
启动类
@EnableFeignClients
@EnableEurekaClient
//@SpringBootApplication
@SpringCloudApplication
@MapperScan("com.josion.seckill.mapper")
public class SeckillApplication {
public static void main(String[] args) {
SpringApplication.run(SeckillApplication.class, args);
}
@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;
}
}
Controller类
@RestController
@RequestMapping("/user")
//@DefaultProperties(defaultFallback = "fallBack") // 统一写整个类的回退方法
public class UserController {
@HystrixCommand(fallbackMethod = "findByIdFallback") //单独写回退方法
@GetMapping("/feign-wares/list")
//这个方法含有什么参,回退的方法就含什么参
public String feignWares() {
String waresFeign = userService.waresFeign();
return waresFeign;
}
//回退时执行的方法
public String findByIdFallback(){
return "失败了。。。";
}
}
测试:
#hystrix-dashborad测试
第一步: http://localhost:52001/hystrix
第二步: 地址框中输入
http://localhost:58080/seckill/actuator/hystrix.stream
第三步: 请求这个地址
http://localhost:9527/seckill/user/feign-wares/list
错误:
浏览器F12 没有任何报错的信息
如果直接访问http://localhost:58082/hystrix.stream 则界面一直会打印ping,但没有结果返回:
这个Loading…是一直在等待负载均衡的提供方要去消费服务,即访问负载均衡服务器,去调用客户端,如果有数据响应则监控界面就会有图形数据展示:
如果想让图中的数据发生变化,则需要循环多次的去访问负载均衡的提供方,让其消费服务,以至于达到监控的目的。
hystrix-dashborad服务请求 http://localhost:52001/actuator 没问题
http://localhost:52001/hystrix.stream没问题
http://localhost:52001/actuator/hystrix.stream报错404
解决:spring cloud 2.x 中hystrix没有/actuator/hystrix.stream路径而报错Unable to connect to Command Metric Stream
1.启动类上添加以下注解
@SpringCloudApplication
@EnableHystrixDashboard
因为@SpringCloudApplication包括@SpringBootApplication、@EnableDiscoveryClient、@EnableCircuitBreaker
启动类中添加以下代码
@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;
}