SpringCloud(六)Hystrix仪表盘Dashboard

SpringCloud(六)Hystrix仪表盘Dashboard

SpringCloud提供了Hystrix-Dashboard,可以对每一个HystrixCommand进行监控,直观的显示每一个熔断器的健康状态。

引入Hystrix Dashboard

在此前项目的基础上我们新建一个服务dashboard service用作监控服务。
Hystrix Dashboard需要在pom.xml文件中引入

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

在我们的启动类上增加@EnableHystrixDashboard注解

/**
 * 监控服务启动类
 */
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
public class DashboardApplicationStarter {

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

这样一个Hystrix Dashboard服务就已经可以使用了。我们通过浏览器直接访问仪表盘的首页(http://localhost:8102/hystrix)。
Hystrix Dashboard首页
我们可以使用仪表盘监控单个服务,指定集群或者默认集群的状态。

监控单个服务

在此处我们只监控一个服务的Hystrix Command,即Single Hystrix App,我们输入web-app-service服务的hystrix流信息路径http://localhost:8101/hystrix.stream。点击监控按钮跳转到监控页面。
监控首页
此时由于web-app-service的接口还没有调用过,没有Hystrix Command的统计信息,所以监控页面是空白的,我们多次调用web-app-service/user/command/exception接口,随机触发异常。
调用接口后仪表盘显示数据

集成Turbine监控多个服务

上面的方法只能监控到单个服务的接口调用健康状态,但是在一个分布式系统中,我们更关心的是全部或者一系列系统的状况。Turbine可以将我们关心的应用通过Eureka和自身的/hystrix.stream接口组合在一起成为/turbine.stream,通过仪表盘展示出来。
下面展示如何集成和配置Turbine。
在pom.xml文件中引入Turbine依赖

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

application.yaml中进行Turbine设置

turbine:
  app-config: web-app-service,sms-service
  cluster-name-expression: "'default'"

trubine.app-config指定的是Eureka中注册的服务名,Turbine会把这些服务的hystrix.stream聚合起来。
现在我们把监控的流路径改为http://localhost:8102/turbine.stream?cluster=default(clusterName为default时,可以省略该参数即http://localhost:8102/turbine.stream)。

Hystrix仪表盘图表说明

左侧圈的颜色和大小分别表示健康状态和流量大小,其颜色状态从健康到熔断状态依次变化为绿色->黄色->橙色->红色。里面的折线表示流量变化曲线。
右侧的彩色数字在左上角有图例,分别对应Success | Short-Circuited | Bad Request | Timeout | Rejected | Failure | Error %
图例
详细说明贴一张官方Wiki的图片:
在这里插入图片描述


相关代码

SpringCloudDemo-HystrixDashboard


参考
hystrix-dashboard wiki参考

猜你喜欢

转载自blog.csdn.net/chenxyz707/article/details/81567647