Hystrix监控
Hystrix除了实现容错之外,还提供了近乎实时的监控。Hystrix Command和HystrixObservableCommand在执行时,会会生成执行结果和运行指标,比如每秒的请求数和成功数等,这些监控数据对于分析系统请求的调用情况很有用。
我们以之前项目介绍过的micro-service-consumer-ribbon-hystrix为例,因为之前的项目中已经包含了spring-boot-starter-actuator和spring-cloud-starter-hystrix,因此可以直接通过http://localhost:7908/hystrix.stream来访问,但是页面展示的并不直观。
Hystrix实现微服务的容错处理
那么为什么之前介绍的micro-service-consumer-movie-feign-hystrix-fallback-factory打开http://localhost:8028/hystrix.stream却是404呢?
因为在引入feign的时候带了hystrix,因此只需要再单独引入hystrix即可,同时还需要在启动类上加@EnableCircuitBreaker。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
使用Hystrix Dashboard 数据可视化监控面板
创建micro-service-hystrix-dashboard项目,首先来看一下这个项目的pom文件,我们引入了spring-cloud-starter-hystrix-dashboard的依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>micro-service-hystrix-dashboard</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.fanfan.cloud</groupId>
<artifactId>micro-service-spring-cloud-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>micro-service-hystrix-dashboard</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork