前文演示了Hystrix断路器与服务降级:https://blog.csdn.net/flycp/article/details/108568785
一、概念
除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard),Hystrix会持续的记录所有通过Hystrix发起的请求的执行信息,并一统计报表和图形的形式展示给用户,包括每秒执行多少请求,多少成功,多少失败等。NetFlix通过hystrix-mertrics-event-stream 项目实现了对以上指标的监控。Spring Cloud 也提供了Hystrix Dashboard 的整合,对监控内容转化成可视化界面。
二、实现
新建cloud-consumer-hystrix-dashboard9001项目
pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
此处要注意,我们需要监控的项目也需要 添加 spring-boot-starter-actuator
包,否则无法监控。
application.yml:
server:
port: 9001
主启动:
package com.cpown.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboard9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboard9001.class,args);
}
}
此外,针对被监控的8001项目主启动也需要做修改:添加ServletRegistrationBean
Bean到容器。
package com.cpown.springcloud;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableHystrix //开启Hystrix
@EnableDiscoveryClient
@EnableEurekaClient
public class HystrixPaymentServiceMain8001 {
public static void main(String[] args) {
SpringApplication.run(HystrixPaymentServiceMain8001.class,args);
}
/**
* Hystrix DashBoard 监控
* @return
*/
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(streamServlet);
servletRegistrationBean.setLoadOnStartup(1);
servletRegistrationBean.addUrlMappings("/hystrix.stream");
servletRegistrationBean.setName("HystrixMetricsStreamServlet");
return servletRegistrationBean;
}
}
三、测试
启动 8001,9001:
访问:http://localhost:9001/hystrix
会出现如上页面,在input框输入 之前配置的8001项目地址:
进入:
可以看到监控项目以及端口:
此时如果我们疯狂发起错误请求:会发现熔断器被打开,请求错误率达到100%
当我们执行一次正确的请求,断路器会关闭:
四、总结
我们如何去观看监控页面呢?
- 实心圆:共有两种含义。涛通过颜色的变化代表了实例的健康程度,他的健康从绿色<黄色<橙色<红色递减。
该实心圆除了颜色变化之外,他的大小也会根据实例的请求流量发生变化,流量越大实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压实例。 - 曲线用来记录两分钟内流量的相对变化,可以通过它来观察流量的下降和上升趋势。