SpringCloud深入学习(七)——Hystrix开启监控以及应用(hystrix-dashboard和Turbine)

一、前言

Hystrix 不仅提供了降级、限流、熔断保护等操作,他还有个最常用的技术-----监控。

我们之前SpringCloud专栏博客中,对其有详细的讲解和使用配置等,从降级、限流、熔断等操作流程中,Hystrix其实默认也做了相关的监听操作。就如下图所示:
在这里插入图片描述
每次流程的进行,都会有一个Metrics监控信息,这一节博客内容,我们就来探究Hystrix监控是什么技术和怎么来使用吧。

二、Hystrix监控

Hystrix的模块中 hystrix-metrics-event-stream,就可将这些监控的指标信息以text/event-stream的格式暴露给外部系统。

spring-cloud-starter-netflix-hystrix包含该模块,在此基础上,只须为项目添加 spring-boot-starter-actuator依赖,就可使用 /hystrix.stream端点获得Hystrix的监控信息了。

我们接下来就按照说明,来配置我们的项目,看看这个监控信息是什么。

2.1、feign实现hystrix的监控操作

我们之前配置Feign整合Hystrix时,由于Feign默认自带Hystrix,我们并没有直接的去引入Hystrix的pom依赖,就能实现降级、限流等服务。
但是OpenFeign依赖组件只是自动引入了Hystrix的核心代码而已,针对监控操作,并没有进行引入,所以此处我们如果使用Feign来实现Hystrix的监控配置就必须还要引入完整的Hystrix依赖。

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

那这个包中包含什么东西是我们此时需要的?
在这里插入图片描述

上图中我们发现:
1、openfeign中只包含了主要的hystrix依赖信息。并没有 hystrix-metrics-event-stream 依赖信息。
2、Hytrix 中包含了 hystrix-metrics-event-stream 依赖和很多相关的信息。

引入pom依赖文件后,我们还需要在启动类上新增一个注解,开启Hystrix。

@EnableCircuitBreaker

同时,也需要增加一个配置文件信息,不然会出现 http://localhost:10001/hystrix.stream 404的问题。

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;

@Configuration
public class HystrixConfiguration {
	@Bean
    public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet(){
        HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean<HystrixMetricsStreamServlet> servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(hystrixMetricsStreamServlet);
        servletRegistrationBean.addUrlMappings("/hystrix.stream");
        servletRegistrationBean.setName("HystrixMetricsStreamServlet");
        return servletRegistrationBean;
    }
}

分别启动Eureka注册中心和当前的这个应用,注册成功后,我们只需要访问下面连接,就能查看当前的文本信息

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

http://localhost:10001/hystrix.stream
在这里插入图片描述

当我们发送一个请求后,再次查看信息:

http://localhost:10001/test2?name=1
在这里插入图片描述

但是,这里全部是文本类型的数据,数据现实并不直观,我们还需要采取另外一种方式-----图像化仪表盘显示。

2.2、Feign实现Hystrix仪表盘监控显示

我们采取文本形式展示监控信息,很难一眼看出问题所在,所以我们需要采取另外一种形式,利用图像化的形式展示。
如果我们需要使用仪表盘,还需要在依赖文件中增加一个新的依赖。

也可以单独开启一个SpringBoot项目,不用注册至注册中心。

<!-- 仪表盘显示依赖引入 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

并在启动类上新增一个注解信息,开启仪表盘功能。

@EnableHystrixDashboard

重启项目,并注册至eureka注册中心上,访问请求,观察。

http://localhost:10001/hystrix
在这里插入图片描述

但是,这个界面如何玩呢?相信很多最开始接触SpringCloud的朋友一脸懵逼,接下来教大家如何去玩这个东西。(都是花时间慢慢实验出来的)

2.3、玩转仪表盘

仪表盘的作用:

将 http://localhost:10001/hystrix.stream 的文本数据 转换为可以直接查看的图像数据。

依据作用,我们只需要在下面图形处输入相关的stream即可,如图所示。
在这里插入图片描述
点击按钮后,我们会进入一个可视化的界面。
在这里插入图片描述

2.4、图像上的含义

我们上面成功调取出了图像化显示信息,但是这个图我们看着还是有点懵逼,这些又是圆形,又是线条数字的都是什么啊?
我们接下来针对这些图形化信息做一个大致的解释。

由于我们是针对 http://localhost:10001/hystrix.stream 做图像检测显示。

此时我们只需要调用 10001端口的项目做请求测试操作。

http://localhost:10001/test2?name=1

在这里插入图片描述
我们不断地请求带有降级操作地接口,触发降级,发现:

1、圆球由小变大、由绿变红。
2、线条由平滑变为曲线。
3、开始带有数字信息。

那么这些信息都表示什么呢?
在这里插入图片描述
实心圆:

1、通过颜色的变化代表了实例的健康程度,健康程度从绿色、黄色、橙色、红色递减。
2、通过大小表示请求流量发生变化,流量越大该实心圆就越大。所以可以在大量的实例中快速发现故障实例和高压实例。

曲线:

用来记录2分钟内流浪的相对变化,可以通过它来观察流量的上升和下降趋势。

三、使用Turbine监控

3.1、Turbine技术使用的由来

在上面的介绍中,我们知道 Hystrix 默认的图像化监测,是只能监测一个微服务,如果要监控更多的微服务信息,则需要开启更多的窗口去进行。
这样显而易见不够方便,那我们能否将我们需要监测的微服务信息放置在一个页面中呢?

使用 Turbine技术!

3.2、Turbine技术的简介

Turbine是一个聚合 Hystrix监控数据的工具,它可将所有相关/hystrix.stream端点的数据聚合到一个组合的/turbine.stream中,从而让集群的监控更加方便。

3.3、Turbine的使用

我们知道这个技术是做什么的往往不够,我们还需要知道如何去玩这个技术。

此处我们采取新建一个项目做配置。(也可以不新建)

创建项目,并引入依赖。

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>

完整的项目依赖为:

<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>

	<groupId>cn.linkpower</groupId>
	<artifactId>Hystrix-jiankong-Turbine</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>Hystrix-jiankong-Turbine</name>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	</properties>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>

			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>

		</dependencies>
	</dependencyManagement>

	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
		</dependency>

		<!-- 添加hystrix依赖组件 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-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-netflix-hystrix-dashboard</artifactId>
		</dependency>
	</dependencies>

	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

再启动类上,新增一个注解信息。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
//@EnableEurekaClient
@EnableDiscoveryClient
@EnableTurbine //开启turbine
@EnableHystrixDashboard   // 开启Hystrix仪表盘功能
public class HystrixTurbine8031 {
	public static void main(String[] args) {
		SpringApplication.run(HystrixTurbine8031.class, args);
	}
}

当然,这还不够,我们还需要增加相关的配置文件编写。

###服务启动端口号
server:
  port: 8031

###服务名称
spring:
    application:
        name: app-hystrix-turbine
                
###服务注册到eureka地址
eureka:
  client:
    service-url:
       defaultZone: http://localhost:10000/eureka/
  instance:
    prefer-ip-address: true
    
### 配置Turbine从rureka上拉取的服务信息
turbine:
  app-config: app-bunana-consumer-hystrix-feign  ##如果需要聚合多个服务的监测信息,使用","拼接
  cluster-name-expression: "'default'"  ##集群名称
    
    

启动项目,查看eureka,我们可以发现:
在这里插入图片描述

3.4、Turbine的测试

我们访问 http://192.168.1.89:8031/turbine.stream,可以发现:
在这里插入图片描述
访问仪表盘 http://192.168.1.89:8031/hystrix,如下所示
在这里插入图片描述
在这里插入图片描述

3.5、Turbine遇到的问题

[问1:]org.apache.http.conn.ConnectTimeoutException

1、如果启动Turbine项目时,控制台报错出现连接某个微服务超时,记得一定要去修改心跳时间和心跳超时踢出时间!!!
2、本地测试不要随便变动网络。网络变动会影响ip地址的变更,导致缓存的ip+port无法访问到指定的服务上,从而导致连接失败!!

[问2:]多个监控信息如何配置?

只需要修改对应的yml配置文件即可!
在这里插入图片描述

四、demo下载连接

feign实现Hystrix监控text文本显示
feign实现Hystrix监控dashboard仪表盘显示
feign实现Hystrix监控dashboard+turbine仪表盘聚合显示

猜你喜欢

转载自blog.csdn.net/qq_38322527/article/details/104841806