hystrix详解

Hystrix是Netflix的一款容错管理工具,用于处理服务间的延迟和故障,防止雪崩效应。它通过命令模式实现服务降级、熔断和依赖隔离,并提供监控功能。当服务调用超时或线程池满时,Hystrix会执行降级策略,快速返回默认或备用响应,同时通过熔断机制在一段时间内切断对该服务的调用。此外,Hystrix还提供了实时监控和报警功能,帮助开发者及时发现和处理问题。
摘要由CSDN通过智能技术生成

笔记,博客来源:
————————————————
版权声明:本文为CSDN博主「韩金群」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:
https://blog.csdn.net/hjq_ku/article/details/89520168
https://blog.csdn.net/hjq_ku/article/details/89516530
https://blog.csdn.net/qq_41557799/article/details/118566537

一.什么是hystrix?
hystrix是Netlifx开源的一款容错框架,防雪崩利器,具备服务降级,服务熔断,依赖隔离,监控(Hystrix Dashboard)等功能。

hystrix是一个库,通过延迟容忍和容错逻辑,控制分布式服务之间的交互。它通过隔离服务间的访问点、防止级联失败和提供回退选项,保证系统的整体弹性。
Hystrix作用
hystrix被设计的目标是:
1.对通过第三方客户端库访问的依赖项(通常是通过网络)的延迟和故障进行保护和控制。
2.在复杂的分布式系统汇中阻止级联故障。
3.快速失败,快速恢复。
4.回退,尽可能优雅的降级。
5.启用近实时监控、警报和操作控制。
Hystrix使用场景
1.调用超时时间比你自己定义的阈值要长。
2.线程池满了,该以来应该立即拒绝请求,而不是排队
3.在一段时间内,如果服务的错误百分比超过了一个阈值,就会触发一个断路器来停止对特定服务的所有请求,无论是手动还是自动的

Hystrix如何解决依赖隔离
1.Hystrix使用命令模式HystrixCommand(Command)包装依赖调用逻辑,每个命令在单独线程中/信号授权下执行。
2.可配置依赖调用超时时间,超时时间一般设为比99.5%平均时间略高即可.当调用超时时,直接返回或执行fallback逻辑。
3.为每个依赖提供一个小的线程池(或信号),如果线程池已满调用将被立即拒绝,默认不采用排队.加速失败判定时间。
4.依赖调用结果分:成功,失败(抛出异常),超时,线程拒绝,短路。 请求失败(异常,拒绝,超时,短路)时执行fallback(降级)逻辑。
5.提供熔断器组件,可以自动运行或手动调用,停止当前依赖一段时间(10秒),熔断器默认错误率阈值为50%,超过将自动运行。
6.提供近实时依赖的统计和监控

二.功能点详解

1.服务降级

使用场景:
比如双十一买东西出现,哎哟喂,被挤爆了。app秒杀某个商品提示网络开小差,请稍后再试。
使用方法:
(1)添加pom文件

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

(2)启动类加@EnableCircuitBreaker注解

(3)使用RestTemplate请求数据 加上@HystrixCommand(fallbackMethod=“fallback”)

(4)fallback是方法 在方法中保持传参和返回值相同。

(5)对(3)(4)的举例

  @GetMapping("/user/test")
  @HystrixCommand(fallbackMethod = "fallback2")
    public String test(@RequestParam ("number")Integer number) {
        if (number == 1) {
            return "success";
        }
        RestTemplate restTemplate = new RestTemplate();
        String str = restTemplate.getForObject("http://127.0.0.1:8081/rw/user/test", String.class);
        return str;
    }
     private String fallback2(Integer number) {
        return "太拥挤了,请稍后再试2";
    }

通过RestTemplate 调用的其他服务,其他服务为正常的接口编写。
如果number输入任意不为1时就会请求其http://127.0.0.1:8081/rw/user/test的服务,正常调用时返回正常结果,如果服务短掉或者请求时间过长,则走fallback2
(6)修改超时时间
默认超时时间是1000ms,可通过以下进行设置 单位为ms

    @HystrixCommand(fallbackMethod = "fallback2",
    commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})

通过application.yum配置文件进行修改:

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000 #请求命令执行超时时间

想对其test单独进行设置,可通过在default的同级添加方法名例如:

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000 #请求命令执行超时时间
    test:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 1000 #请求命令执行超时时间单独为某一个方法设定(default换为方法名)

2.依赖隔离:

(1)为每一个hystrixCommand提供一个线程池,自动实现了依赖隔离,即使线程池满了也不会影响其他进程。

3.服务熔断:

(1)服务在高并发的情况下出现进程阻塞,导致当前线程不可用,慢慢的全部线程阻塞 ,导致服务器雪崩。这时直接熔断整个服务,而不是一直等到服务器超时。

(2)断路器全开时:一段时间内 达到一定的次数无法调用 并且多次监测没有恢复的迹象 断路器完全打开 那么下次请求就不会请求到该服务
半开:短时间内 有恢复迹象 断路器会将部分请求发给该服务,正常调用时 断路器关闭
关闭:当服务一直处于正常状态 能正常调用

(3)服务熔断使用:

   @HystrixCommand(fallbackMethod = "fallback2", commandProperties = {
            /* @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"),*/
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
    })
    @GetMapping("/user/test")
    public String test(@RequestParam ("number")Integer number) {
        if (number == 1) {
            return "success";
        }
        RestTemplate restTemplate = new RestTemplate();
        String str = restTemplate.getForObject("http://127.0.0.1:8081/rw/user/test", String.class);
        return str;
    }

circuitBreaker.enabled :true 打开熔断 默认开启
circuitBreaker.requestVolumeThreshold: 当在配置时间窗口内达到此数量的失败后,进行短路。默认20个
circuitBreaker.sleepWindowInMilliseconds:短路多久以后开始尝试是否恢复,默认5s
circuitBreaker.errorThresholdPercentage:出错百分比阈值,当达到此阈值后,开始短路。默认50%

4.服务熔断监控

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

2.启动类添加注解 @EnableHystrixDashboard //断路器可视化
比如我这用的本地
localhost:8082/hystrix访问

点击Monitor Stream进入
在这里插入图片描述
Circuit为熔断器的开启状态 默认是Closed 关闭,当出现多个fallback时会出现Open打开熔断器 再次访问则直接fallback,当服务慢慢的恢复再变为closed。

下面这一篇是解决访问出问题的博客:https://blog.csdn.net/hjq_ku/article/details/89516530
hystrix dashboard Unable to connect to Command Metric Stream解决办法

1.首先查看依赖是否配置全

 <!--熔断器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</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>

2.检查启动类是否有如下注解

@EnableCircuitBreaker //断路器
@EnableHystrixDashboard //断路器可视化

3.如果都没问题那么检查下springboot 版本如果是2.0则需要添加 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;
    }

4.启动熔断器可视化界面
输入地址http:ip:port/hystrix ,如下界面:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值