简介
Hystrix Dashboard
在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard
是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
开启 HD
修改RibbonConsumerApplication.java
类
在程序的入口RibbonConsumerApplication
类,加上@EnableHystrix
注解开启断路器,这个是必须的,并且需要在程序中声明断路点@HystrixCommand
;加上@EnableHystrixDashboard注解
,开启HystrixDashboard。
@EnableHystrix
@EnableDiscoveryClient
@EnableHystrixDashboard
@SpringBootApplication
public class RibbonConsumerApplication {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
声明断路点
声明断路点 @HystrixCommand(fallbackMethod = "defaultStores")
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "defaultStores")
@GetMapping(value = "/hello")
public String hello() {
return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody();
}
public String defaultStores() {
return "feign + hystrix Dashboard ,提供者服务挂了";
}
}
@HystrixCommand
表明该方法为hystrix
包裹,可以对依赖服务进行隔离、降级、快速失败、快速重试等等hystrix
相关功能 该注解属性较多,下面讲解其中几个
- fallbackMethod 降级方法
- commandProperties 普通配置属性,可以配置HystrixCommand对应属性,例如采用线程池还是信号量隔离、熔断器熔断规则等等
- ignoreExceptions 忽略的异常,默认HystrixBadRequestException不计入失败
- groupKey() 组名称,默认使用类名称
- commandKey 命令名称,默认使用方法名
测试服务
依次启动项目:
spring-cloud-eureka-service
spring-cloud-eureka-provider-1
spring-cloud-eureka-provider-2
spring-cloud-eureka-provider-3
spring-cloud-ribbon-consumer-hystrix-dashboard
启动该工程后,访问服务注册中心,查看服务是否都已注册成功:http://localhost:8761/
Hystrix Dashboard 监控
可以访问 http://127.0.0.1:9090/hystrix
,获取Hystrix Dashboard信息,默认最大打开5个终端获取监控信息,可以增加delay参数指定获取监控数据间隔时间
在界面依次输入:http://127.0.0.1:9000/hystrix.stream
、2000 、hello 点确定。可以访问以下,图形化监控页面。