1.子项目
使用hystrixdashboard时子项目需要有相映的监控依赖和hystrix路段器依赖
,并已经将provider服务注册进eureka中,在消费端访问provider端时hystrixdashboard可以监控访问数据。
- 子项目相映依赖
<!-- hystrix 服务熔断@HystrixCommand-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<!--添加Eureka和hystrixdashboard监控的完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
- 消费端访问提供端的方法上需要配置熔断机制:
@HystrixCommand(fallbackMethod = “HystrixCommand_Get”)意为:
在服务请求抛出异常时不会继续等待,而是访问fallbacMethod方法HystrixCommand_Get
@HystrixCommand(fallbackMethod = "HystrixCommand_Get")
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") Long id) {
return service.get(id);
}
public Dept HystrixCommand_Get(@PathVariable("id") Long id) {
return new Dept().setDeptno(id).setDname("null").setDbSource("NoSource");
}
- 在子项目的启动类上加入@EnableCircuitBreaker允许线路熔断机制打开
@EnableCircuitBreaker
@SpringBootApplication
public class ApplicationProvider_APP {
public static void main(String[] args) {
SpringApplication.run(ApplicationProvider_APP.class, args);
}
}
2.hystrix监控端
- 子项目Hystrixdashboard依赖
<!-- hystrix和 hystrix-dashboard相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
- 启动类加注解@EnableHystrixDashboard
@EnableHystrixDashboard
@SpringBootApplication
public class HystricDashboard_9001_APP {
public static void main(String[] args) {
SpringApplication.run(HystricDashboard_9001_APP.class, args);
}
}
- 启动测试
- 在框框中地址和数据
地址加端口号/hystrix.stream
输入刷新时间2000ms
输入监控名字
不停的刷新添加了@HystrixCommand注解的方法url映射地址,在dashboard 上会有响应的数据。
控制台有相应的连接地址: