【Spring Cloud总结】22.Hystrix Dashboard的使用(上)

接上篇《21.单个FeginClient禁用Hystrix》  Spring Cloud版本为Finchley.SR2版

上一篇我们简单介绍了存在多个FeignClient的情况下,如何禁用单个的Hystrix。
本篇我们延续之前的Hystrix系列,着重讲解一下
本部分官方文档:https://cloud.spring.io/spring-cloud-static/Finchley.SR4/single/spring-cloud.html#_circuit_breaker_hystrix_dashboard
注:好像Finchley.SR2的文档已经挂了,最新的是Finchley.SR4的文档。

还记得之前Hystrix的Metrics Stream支持吗?Metrics流是用来监控当前API的情况,引入spring-boot-start-actuator的依赖,并设置management.endpoints.web.exposure.include: hystrix.stream后,访问/actuator/hystrix.stream即可看到当前系统的API情况:

之前我们提到过,这种JSON的打印日志阅读起来十分困难,Spring Cloud给我们提供了“Hystrix Dashboard”图形化监控看板,可以让我们更轻松的获取应用的API状态信息,下面我们就来介绍它。

一、Hystrix Dashboard介绍

根据官方文档的描述,因为Hystrix会收集每一组HystrixCommand命令指标,而Hystrix Dashboard(仪表盘)就是以一种有效的方式显示每个断路器的健康状况,通过仪表盘我们可以看到Hystrix的各项指标信息,以便于快速发现系统中存在的问题进而解决它。

下面就是官方文档提供的一个Hystrix Dashboard的图形界面样例:

Hystrix Dashboard有两种使用策略,一种是对单体应用的监控,另一种是整合Turbine,对集群进行监控。我们分别来进行介绍。

一、单体应用的监控

按照我们原有的思路,应该是在需要监控的微服务上嵌入Hystrix仪表盘,但是作为微服务体系,我们没有必要为每一个微服务都嵌入Hystrix Dashboard。合适的做法是,我们需要专门创建一个工程来实现Hystrix Dashboard。

下面我们创建一个工程来实现Hystrix Dashboard。

1、创建一个名为“microserver-hystrix-dashboard”的Spring Boot工程:

2、创建好工程之后,在工程的pom.xml中,新增Spring Cloud的父工程,以及hystrix、hystrix-dashboard以及actuator的依赖:

<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>com.microserver.cloud</groupId>
  <artifactId>microserver-hystrix-dashboard</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>microserver-hystrix-dashboard</name>
  
  <parent>
    <groupId>com.microserver.cloud</groupId>
    <artifactId>microserver-spring-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <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>
  </dependencies>
</project>

注意,这里为了便于版本统一管理,该工程的parent父工程和我们User、Movie工程一样,均依赖于microserver-spring-cloud工程(此父工程统一引入了spring-cloud-dependencies的Finchley.SR2版,这个在前面的章节已经讲过)。

父工程pom.xml的modules中别忘记加入这个新工程(microserver-hystrix-dashboard):

<modules>
    <module>microserver-provider-user</module>
    <module>microserver-consumer-movie</module>
    <module>microserver-discovery-eureka</module>
    <module>microserver-discovery-eureka-high-availability</module>
    <module>microserver-hystrix-dashboard</module>
</modules>

创建完毕之后工程结构如下:

3、新建启动类,在启动类中,添加“@EnableHystrixDashboard”注解,以开启仪表盘功能:

package com.microserver.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
public class MicroserverHystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(MicroserverHystrixDashboardApplication.class, args);
    }
}

4、然后在resource文件夹下创建application.yml,配置一下应用名和应用端口:

spring:
  application:
    name: microserver-hystrix-dashboard
server:
  port: 2001

工程完整结构:

  
下面我们启动该工程,访问“http://localhost:2001/hystrix”,我们可以看到下面的界面:

该界面有几个需要输入的参数,在图中已标注。

现在我们要监控之前的Movie服务,我们将Movie、Eureka Server启动起来,保证“/hystrix.stream”接口可以正常访问,然后在仪表盘主页中输入Movie工程的hystrix.stream监控地址(http://localhost:7901/actuator/hystrix.stream),点击“Monitor Stream”按钮,就可以看到它的监控页面了:


下图是关于监控页面的详细解释:

好了,有关Hystrix Dashboard的单体应用监控就介绍到这,下一篇我们来介绍使用Turbine来实现Hystrix Dashboard的集群监控(因为篇幅过长,我们拆开成单独的一篇来讲解)。

参考:《51CTO学院Spring Cloud高级视频》
https://my.oschina.net/xiaominmin/blog/1788456
转载请注明出处:https://blog.csdn.net/acmman/article/details/102314768

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章