hystrix详解

hystrix详解

大家好,我是酷酷的韩~
在这里插入图片描述
一.什么是hystrix?
hystrix是Netlifx开源的一款容错框架,防雪崩利器,具备服务降级,服务熔断,依赖隔离,监控(Hystrix Dashboard)等功能。

二.功能点详解

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.服务熔断监控

1.添加pom文件

    <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

一份耕耘,份收获,努力越大,收获越多。------酷酷的韩

  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩金群

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值