Hystrix Dashboard简介
在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是一款针对Hystrix进行实时监控的图形化界面工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。
准备工作
我们在第一篇文章的工程的基础上更改,重新命名为:springcloud-hystrix-dashboard。
添加依赖
在pom的工程文件引入相应的依赖:
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
其中,这三个依赖是必须的,缺一不可。
在程序的入口ProducerApplication类,加上@EnableHystrix注解开启断路器,这个是必须的,并且需要在程序中声明断路点HystrixCommand;加上@EnableHystrixDashboard注解,开启HystrixDashboard
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
@Value("${server.port}")
String port;
@HystrixCommand(fallbackMethod = "helloError")
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello " + name + ",i am from port:" + port;
}
public String helloError(String name) {
return "hello," + name + ",sorry,error!";
}
}
运行程序: 依次开启sso-server 和sso-service-A.
Hystrix Dashboard图形展示
3、测试
启动工程后访问 http://localhost:8089/hystrix,将会看到如下界面:
图中会有一些提示:
Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream
Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
Single Hystrix App: http://hystrix-app:port/hystrix.stream
大概意思就是如果查看默认集群使用第一个url,查看指定集群使用第二个url,单个应用的监控使用最后一个,我们暂时只演示单个应用的所以在输入框中输入: http://localhost:8089/hystrix.stream ,输入之后点击 monitor,进入页面。如果没有请求会先显示Loading …,访问http://localhost:8089/hystrix.stream 也会不断的显示ping。
请求服务http://localhost:8089/hello?name=chanjay,就可以看到监控的效果了,首先访问http://localhost:8089/hystrix.stream,显示如下:
说明已经返回了监控的各项结果
到监控页面就会显示如下图:
其实就是http://localhost:8089/hystrix.stream返回结果的图形化显示,Hystrix Dashboard Wiki上详细说明了图上每个指标的含义,如下图:
到此单个应用的熔断监控已经完成。
源码下载:https://github.com/chenjary/SpringCloud/tree/master/springcloud-hystrix-dashboard