服务监控HystrixDashboard

代码仓库地址:gitee仓库链接

1、HystrixDashboard概述

  除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard),Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面。

2、新建DashBoard模块

2.1 新建cloud-consumer-hystrix-dashboard9001模块

image-20220407214312547

2.2 pom.xml

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.3 application.yml

 
server:
  port: 9001

2.4 HystrixDashboardMain9001+新注解@EnableHystrixDashboard

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

2.5 所有Provider微服务提供类(8001/8002/8003)都需要监控依赖配置

 <!-- actuator监控信息完善 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

  服务提供者都要添加这个依赖

2.6 启动cloud-consumer-hystrix-dashboard9001该微服务后续将监控微服务8001

  访问:http://localhost:9001/hystrix

image-20220407215125856

3、断路器演示(服务监控hystrixDashboard)

3.1 修改cloud-provider-hystrix-payment8001

  注意:新版本Hystrix需要在主启动类MainAppHystrix8001中指定监控路径

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
@EnableCircuitBreaker//对hystrixR熔断机制的支持
public class PaymentHystrixMain8001
{
    public static void main(String[] args)
    {
        SpringApplication.run(PaymentHystrixMain8001.class,args);
    }

    /**
     *此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
     *ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
     *只要在自己的项目里配置上下面的servlet就可以了
     */
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

image-20220407215618451

3.2 监控测试

3.2.1 启动eureka服务注册中心

  本来应该启动eureka集群,这里为了方便就启动一个eureka就行。

image-20220407215804841

3.2.2 9001监控8001

  填写监控地址:http://localhost:8001/hystrix.stream

image-20220407215843219

3.2.2 测试地址

  正常访问:http://localhost:8001/payment/circuit/31

image-20220407220039017

http://localhost:8001/payment/circuit/-31,这个访问会直接调用fallback

image-20220407220046637

  一直刷新http://localhost:8001/payment/circuit/31地址之后,

  查看HystrixDashboard:

image-20220407220228715

  测试服务熔断,我们一直访问错误地址:http://localhost:8001/payment/circuit/-31,不断刷新,直到达到服务熔断的条件,再观察HystrixDashboard会发现断路器打开了。

image-20220407220355482

  可以看到,断路器变成Open状态,断路器已经打开了。

  过一会我们给一条正确的请求:http://localhost:8001/payment/circuit/31,再观察Dashboard

image-20220407220546946

  可以看到,此时断路器由Open变成Closed,这是因为,过一会之后,断路器其实是半开状态,这时候如果给一条正确的请求,断路器检测到该请求返回结果正常之后,就会关闭。

  过程就是服务的降级->进而熔断->恢复调用链路

3.2.3 如何看这个Dashboard?

  七种颜色代表七种状态

image-20220407220836817

  实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,它的健康度从绿色<黄色<橙色<红色递减。该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。

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

  整图说明:

image-20220407220906749

image-20220407220919863

上面只是几个微服务,在生产环境中会碰到个几百个微服务,大致效果如下:

  到此,HystrixDashboard服务监控就介绍完了,老项目用没问题,新项目的话后面我另写一篇阿里巴巴的Sentinel微服务限流组件。

posted @   别团等shy哥发育  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-04-08 summernote富文本编辑器基本使用
2020-04-08 循环双向链表(带头节点)
2020-04-08 JSP内置对象
点击右上角即可分享
微信分享提示