Hystrix Dashboard搭建

1,142 阅读3分钟

本文只涉及Hystrix Dashboard的搭建(踩坑日记),其他功能不在本文讨论范围之内。

我们在Feign和Ribbon整合以后,要保证服务的高可用,还需要引入Hystrix,主要使用的就是它熔断、降级、隔离等技术。

以上工作都完成以后,我们想单拉一个Hystrix Dashboard的服务出来,监控Hystrix单节点的状况。

先去start.spring.io/创建一个SpringBoot的工程,只需要引入一个hystrix dashboard的依赖即可,完整的pom文件如下:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.8.RELEASE</version>
    </parent>

    <groupId>com.numbpad</groupId>
    <artifactId>hystrix-dashboard</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR10</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
    </dependencies>
</project>

接下来添加@EnableHystrixDashboard到我们的main class

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

配置文件先简单配个port:

server:
  port: 8400

简单看下Eureka注册中心的实例列表,有3台实例,其中Eureka-Client-A两台,为Eureka-Client-B提供接口服务;Eureka-Client-B一台,集成了Feign(声明式的服务调用)、Ribbon(对服务A进行负载均衡调用)、Hystrix(高可用) Eureka集群.png 我们搭建这个Hystrix Dashboard就是对Eureka-Client-B进行监控的,要想实现对服务B的最重要的是要引入两个pom依赖,其中spring-cloud-starter-netflix-hystrix是hystrix的SpringBoot依赖,spring-boot-starter-actuator是收集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>

服务B的启动类如下:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class EurekaClientBApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientBApplication.class, args);
    }
}

接着把http://localhost:8300/actuator/hystrix.stream 链接填入输入框内,点击Monitor Stream按钮:

HystrixDashboard.png

接着我们就会发现无法实现对服务B进行监控,直接报Unable to connect to Command Metric Stream.

Untitled.png

我们去观察Hystrix-Dashboard服务的控制台会发现一行日志信息

2021-04-10 13:13:11.439  WARN 92394 --- [nio-8400-exec-7] ashboardConfiguration$ProxyStreamServlet : Origin parameter: https://localhost:8300/actuator/hystrix.stream is not in the allowed list of proxy host names.  If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.

这段日志就是提示我们应该把服务B的代理加入Hystrix-Dashboard的白名单,解决方法也很简单,只需要在Hystrix-Dashboard服务加入:

server:
  port: 8400

hystrix:
  dashboard:
    # *的意思是对所有服务生效
    proxyStreamAllowList: '*' 

重新启动后刷新页面,发现还不行。。。。。

2021-04-10 13:18:31.487  WARN 92861 --- [nio-8400-exec-8] ashboardConfiguration$ProxyStreamServlet : Failed opening connection to http://localhost:8300/actuator/hystrix.stream : 404 : HTTP/1.1 404

我去,什么鬼,404?在网上查了很久,还去Spring的官方教程看了,写的也是极其简单,查了很久也没发现,搞的我真是一头雾水。。。

Untitled.png 最后终于查到了这么一段配置,配置以后重新启动服务B

management:
  endpoints:
    web:
      exposure:
	#表示对外提供什么服务,这里我们只对外提供了hystrix.stream的监控功能,可以配置成 *
        include: 'hystrix.stream'
	#配了这段就之前输入框的URL就可以从[/actuator/hystrix.stream](https://localhost:8300/actuator/hystrix.stream)简化为/hystrix.stream
        basePath: /

再次刷新页面,然后疯狂访问下我们服务B的接口(被HystrixCommand代理): Untitled.png 解释一下这个仪表盘的意思:

  • 圆圈,那个是代表了服务的流量,如果圆圈越大,就代表了流量越大

  • 圆圈下面有一条线,是最近2分钟内的流量变化情况

  • bad request,你要发送请求的时候,你发送的请求本身都是坏的,都是有问题的,就比如说你发送的请求的请求方法(PUT),人家接口接收的是GET

  • 有两排数字,左边一排,从上到下依次是:成功的请求数量、熔断的请求数量。右边一排,从上到小,依次是:超时请求数量、线程池拒绝的请求数量、异常的请求数量

  • 百分比数字,是最近10秒钟的失败的请求所占的百分比

  • Host:这个是服务实例的每秒的请求数量,也就是所谓的QPS

  • Cluster:这个是服务的(包含所有服务实例的)的每秒的请求数量,看的是服务整体的QPS

  • Circuit:这个是断路器的状态,打开或者关闭

  • Hosts:这个是说一个服务有多少个服务实例

  • Median(请求延时的中位数,请求的中位数延时是100ms)、Mean(请求延时的平均数,150ms)、90th(TP90,90%的请求都是50ms)、99th(TP99,99%的请求,190ms)、99.5th(TP99.5,99.5%的请求,300ms)

  • Threadpool:这个是记录线程池内部的一些情况,服务B调用服务A的专用的线程池的一些配置和情况,这里就可以让我们去看到服务与服务之间的调用的很多关键的统计信息

  • 请求次数(流量大小)、QPS、请求时延、对其他服务的调用QPS

Hystrix Dashboard目前只能监控单一服务,如果要监控集群的话,Spring还提供了一个聚合Hystrix Dashboard的项目叫turbine,感兴趣可以自己搭建玩一玩。