SpringCloud框架Hystrix Dashboard熔断监控

版权声明:技术交流群:758191639 作者支付宝18696232390喜欢的可以打钱! https://blog.csdn.net/u014131617/article/details/85991964

Hystrix监控

除了隔离依赖服务的调用以外,Hystrix还提供了近实时的监控,Hystrix会实时、累
加地记录所有关于HystrixCommand的执行信息,包括每秒执行多少请求多少成
功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的
监控。
上文提到的 microservice-consumer-movie-ribbon-with-hystrix 项目已经具
备对Hystrix监控的能力,下面我们进入测试。

测试步骤

  1. 启动:microservice-discovery-eureka
  2. 启动:microservice-provider-user
  3. 启动:microservice-consumer-movie-ribbon-with-hystrix
  4. 访问:http://localhost:8011/ribbon/1,注意:该步骤不能省略,因为如果应用
    的所有接口都未被调用,将只会看到一个ping
  5. 访问:http://localhost:8011/hystrix.stream,可以看到类似如下输出:
data: {"type":"HystrixCommand","name":"findById","group":"Ri
bbonHystrixService","currentTime":1472658867784,"isCircuitBr
eakerOpen":false,"errorPercentage":0,"errorCount":0,"request
Count":0,"rollingCountBadRequests":0....}

并且会不断刷新以获取实时的监控数据。但是纯文字的输出可读性实在是太差,运
维人员很难一眼看出系统当前的运行状态。那么是不是有可视化的工具呢?

Hystrix Dashboard

Hystrix Dashboard可以可视化查看实时监控数据。我们可以下载hystrix-dashboard
的war包部署到诸如Tomcat之类的容器就,本文不做赘述另外Spring Cloud也提供
了Hystrix Dashboard的整合,下面我们看看Spring Cloud是怎么玩转Hystrix
Dashboard的。

新建一个maven项目,在pom.xml中添加如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht
tp://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://m
aven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-hystrix-dashboard</artifactId>
<packaging>jar</packaging>
<parent>
	<groupId>com.itmuch.cloud</groupId>
	<artifactId>spring-cloud-microservice-study</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
</dependencies>
</project>
  • 编写启动类:HystrixDashboardApplication.java
/**
* 测试步骤:
* 1. 访问http://localhost:8030/hystrix.stream 可以查看Dashboard
* 2. 在上面的输入框填入: http://想监控的服务:端口/hystrix.stream进行测
试
* 注意:首先要先调用一下想监控的服务的API,否则将会显示一个空的图表.
* @author eacdy
*/
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
	public static void main(String[] args) {
		new SpringApplicationBuilder(HystrixDashboardApplication.class).web(true).run(args);
	}
}
  • 配置文件:application.yml
spring:
	application:
		name: hystrix-dashboard
server:
	port: 8030

启动后,访问http://localhost:8030/hystrix.stream将会看到如下界面:
2.4.2. Hystrix Dashboard

此时,我们在输入框中输入http://localhost:8011/hystrix.stream,并随意设置一个
Title后,点击Monitor Stream按钮,会出现如下界面:

在这里插入图片描述

此时我们会看到findById这个API的各种指标。Hystrix Dashboard Wiki上详细说明
了图上每个指标的含义,如下图:

在这里插入图片描述

此时,我们可以尝试将microservice-provider-user停止,然后重复访问多
次http://localhost:8011/ribbon/1(20次以上),会发现断路器状态会变为开启。

扫描二维码关注公众号,回复: 4842376 查看本文章

代码地址(任选其一):
http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservicehystrix-dashboard
https://github.com/eacdy/spring-cloud-study/tree/master/microservice-hystrixdashboard

TIPS

  1. Hystrix的监控数据默认是保存在每个实例的内存中的,Spring Boot提供了多种
    方式,可以导入到Redis、TSDB以供日后分析使用。
  2. 我们启动前文的 microservice-consumer-movie-feign-with-hystrix 项
    目后发现其访问localhost:8021/hystrix.stream ,是404,查看pom.xml依赖
    树,发现其没有依赖hystrix-metrics-event-stream项目。故而添加依赖:
<!-- 整合hystrix,其实feign中自带了hystrix,引入该依赖主要是为了使用
其中的hystrix-metrics-event-stream,用于dashboard -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

并在启动类上添加 @EnableCircuitBreaker 注解即可。详见项
目 microservice-consumer-movie-feign-with-hystrix-stream ,因为这
不是本文的讨论重点,故而只做扩展阅读。
microservice-consumer-movie-feign-with-hystrix-stream 代码地址
(二选一):
https://github.com/eacdy/spring-cloud-study/tree/master/microserviceconsumer-movie-feign-with-hystrix-stream
https://git.oschina.net/itmuch/spring-cloudstudy/tree/master/microservice-consumer-movie-feign-with-hystrixstream

猜你喜欢

转载自blog.csdn.net/u014131617/article/details/85991964