SpringCloud第九章Hystrix断路器(服务监控hystrixDashboard)

5、服务监控hystrixDashboard

5.1、概述

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

5.2、仪表盘9001

  • 新建cloud-consumer-hystrix-dashboard9001

  • 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">
        <parent>
            <artifactId>cloud2020</artifactId>
            <groupId>com.likun.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>
    
        <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>
    
    
    </project>
    
  • YML

    server:
      port: 9001
    
  • HystrixDashboardMain9001+新注解@EnableHystrixDashboard

    package com.likun.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    
    /**
     * @author likun
     * @create 2021-04-29 6:46
     */
    @SpringBootApplication
    @EnableHystrixDashboard
    public class HystrixDashboardMain9001 {
        public static void main(String[] args) {
            SpringApplication.run(HystrixDashboardMain9001.class, args);
        }
    }
    
  • 所有Provider微服务提供类(8001/8002/8003)都需要监控依赖配置

    <!-- actuator监控信息完善 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
  • 启动cloud-consumer-hystrix-dashboard9001该微服务后续将监控微服务8001

    http://localhost:9001/hystrix (豪猪哥!!!出来)

    在这里插入图片描述

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

  • 修改cloud-provider-hystrix-payment8001

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

    /**
    *此配置是为了服务监控而配置,与服务容错本身无关,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;
    }
    

    Unable to connect to Command Metric Stream.

    404

  • 监控测试

    • 启动1个eureka或者3个eureka集群均可

    • 观察监控窗口

      • 9001监控8001

        在这里插入图片描述

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

    • 测试地址

      • http://localhost:8001/payment/circuit/31

      • http://localhost:8001/payment/circuit/-31

      • 上述测试通过(ok)

      • 先访问正确地址,再访问错误地址,再正确地址,会发现图示断路器都是慢慢放开的。

        监控结果,成功

        在这里插入图片描述

    监控结果,失败

    在这里插入图片描述

    • 如何看?

      • 7色

      • 1圈

        在这里插入图片描述

    • 1线

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

    • 整图说明

      在这里插入图片描述

    • 整图说明2

      在这里插入图片描述

    • 搞懂一个才能看懂复杂的

      在这里插入图片描述

(完)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值