Spring Cloud-Hystrix Dashboard与Turbine断路器监控

简介

Hystrix Dashboard是Spring Cloud中查看Hystrix实例执行情况的一种仪表盘组件,支持查看单个实例和查看集群实例,本文将对其用法进行详细介绍。
版本信息:
Spring Cloud:Hoxton.RELEASE
Spring Boot:2.2.2.RELEASE

Hystrix Dashboard可以有效的反映出每个Hystrix实例的运行情况,帮助我们快速发现系统中的问题,从而采取对应措施。

Hystrix单个实例监控

创建hystrix-hashboard模块

用来监控hystrix实例的执行情况。
pom.xml添加相关依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-hystrix-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

application.yml对应配置:

server:
  port: 8501
spring:
  application:
    name: hystrix-dashboard
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8001/eureka

在启动类上添加@EnableHystrixDashboard注解:

@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixDashboardApplication {

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

}

另外,还需注意,被监控的hystrix-service服务需要开启hystrix.stream端点,配置信息如下:

management:
  endpoints:
    web:
      exposure:
        #暴露hystrix监控端点
        include: 'hystrix.stream'

启动相关服务
eureka-service
user-service
hystrixx-service
hystrixx-dashboard

启动后,注册中心注册情况如下图示:

springcloud-hystrix-dashboard-01.pngspringcloud-hystrix-dashboard-01.png

Hystrix实例监控演示

访问Hystrix Dashboard:http://localhost:8501/hystrix
如下图示:

springcloud-hystrix-dashboard-02.pngspringcloud-hystrix-dashboard-02.png

填写单实例地址:
http://localhost:8401/actuator/hystrix.stream,点击Monitor Stream监控页面,如下图示:

springcloud-hystrix-dashboard-03.pngspringcloud-hystrix-dashboard-03.png

调用几次hystrix-service接口:http://localhost:8401/user/hystrix/testCommand/1

可以在监控页面上看到commandKey、threadPoolKey属性已经显示在上面,并且4次调用都成功了。

Hystrix Dashboard图表解读

小球代表该实例健康状态及流量情况,颜色越显眼,表示实例越不健康,小球越大,表示实例流量越大。
曲线表示Hystrix实例的实时流量变化。

如下图示:

springcloud-hystrix-dashboard-04.pngspringcloud-hystrix-dashboard-04.png

Hystrix集群实例监控

这里我们使用Turbine来聚合hystrix-service服务的监控信息,然后我们的hystrix-dashboard服务就可以从Turbine获取聚合好的监控信息。

创建turbine-service模块

pom.xml添加相关依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>

application.yml添加配置:

server:
  port: 8601
spring:
  application:
    name: turbine-service
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8001/eureka

turbine:
  # 指定需要收集信息的服务名称
  app-config: hystrix-service
  # 指定服务所属集群
  cluster-name-expression: new String('default')
  # 以主机名和端口号区分服务
  combine-host-port: true

在启动类添加@EnableTurbine来启动Turbine相关功能:

@EnableTurbine
@EnableDiscoveryClient
@SpringBootApplication
public class TurbineServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(TurbineServiceApplication.class, args);
    }

}

启动相关服务:
eureka-service
user-service
hystrix-service:8401
hystrix-service:8402
hystrix-dashboard
turbine-service

注册中心服务列表如下所示:

springcloud-hystrix-dashboard-05.pngspringcloud-hystrix-dashboard-05.png

Hystrix集群监控演示

访问Hystrix Dashboard:http://localhost:8501/hystrix

集群监控地址,输入turbine-service的地址:
http://localhost:8601/turbine.stream

如下图示:

springcloud-hystrix-dashboard-06.pngspringcloud-hystrix-dashboard-06.png

调用几次hystrix-service接口:
http://localhost:8401/user/hystrix/testCommand/1
http://localhost:8402/user/hystrix/testCommand/1

监控页面如下图示:

springcloud-hystrix-dashboard-07.pngspringcloud-hystrix-dashboard-07.png

可以看到我们的hystrix实例数量变成了两个。

Gitee项目源码地址:
https://gitee.com/whwtree/springcloud-learning.git

(完)

最后修改于:2022年12月11日 03:04

添加新评论