当前位置 博文首页 > 文章内容

    layman的博客:Spring Cloud入门系列(十二)- 服务熔断与降级之Hystrix(已停更,建议切换到Sentinel)

    作者:shunshunshun18 栏目:未分类 时间:2021-10-27 20:23:08

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    Hystrix仪表盘

    在这里插入图片描述

    代码演示

    新建模块:cloud-consumer-hystrix-dashboard9001
    在这里插入图片描述
    打开Hystrix仪表盘,需要引入相关的jar包

    spring-cloud-starter-netflix-hystrix-dashboard

    pom.xml

    <?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>cloud201230</artifactId>
            <groupId>com.banana</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>
            <!-- 引入自定义的api通用包,可以使用Payment支付Entity -->
            <dependency>
                <groupId>com.banana</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>1.0-SNAPSHOT</version>
            </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>
        </dependencies>
    </project>
    

    application.yml

    server:
      port: 9001
    

    HystrixDashboardMain9001 : 在主启动类上加上注解 ·@EnableHystrixDashboard

    package com.banana.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    
    /**
     * 豪猪哥 仪表盘
     * @author layman
     */
    @SpringBootApplication
    @EnableHystrixDashboard
    public class HystrixDashboardMain9001 {
        public static void main(String[] args) {
                SpringApplication.run(HystrixDashboardMain9001.class,args);
            }
    }
    

    主启动类启动后,如果一切顺利,再浏览器的地址栏输入:http://localhost:9001/hystrix

    应当可以看到以下画面:
    在这里插入图片描述

    Hystrix仪表盘如何使用

    接下来,我们通过一些配置,让9001模块 去监控8001 模块

    准备工作(全是在8001模块操作)

    1. pom.xml增加dependency
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
    1. PaymentHystrixMain8001添加以下代码
    /**
         * 此配置是为了服务监控而配置,与服务器容错本身无关,springCloud升级后的坑
         * ServletRegistrationBean因为springBoot的默认路径不是/hystrix.stream
         * 只要在自己的项目里配置上下文的servlet就可以了
         */
        @Bean
        public ServletRegistrationBean getServlet() {
            HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
            ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
            registrationBean.setLoadOnStartup(1);
            registrationBean.addUrlMappings("/hystrix.stream");
            registrationBean.setName("HystrixMetricsStreamServlet");
            return registrationBean;
        }
    

    先启动7001 Eureka,在启动8001服务端

    正常启动后,可以在7001Eureka看到8001服务端已经注册成功

    在这里插入图片描述
    在9001 Hystrix Dashboad中输入需要监控的地址

    http://localhost:8001/hystrix.stream
    

    在这里插入图片描述
    点击Monitor Stream后,界面应当是空空如也。

    在这里插入图片描述

    测试数据

    数据分为两种情况,一种是成功访问,不触发熔断,一种是错误访问,触发熔断

    测试正确数据

    http://localhost:8001/payment//hystrix/circuit/3
    

    在这里插入图片描述
    狂点数次,观看仪表盘情况:
    在这里插入图片描述

    仪表盘如何解读

    七色一圈一线
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    测试失败数据

    http://localhost:8001/payment//hystrix/circuit/-3
    

    在这里插入图片描述

    狂点数次,观看仪表盘情况:

    在这里插入图片描述
    可以看到,在单位时间内的错误率达到阈值,立刻触发熔断,后续的访问也无法通过。

    当服务处于熔断状态时,即便是正确的访问,也会被拒绝。如下图所示:
    在这里插入图片描述

    集群监测

    在这里插入图片描述

    写在最后

    Hystrix需要我们自己搭建监控平台,最重要的一点是,它现在已经停止更新了

    如果是在公司的项目中,加上服务降级与熔断,不建议使用Hystrix

    而是采用Alibaba的sentinel(重点!)

    cs