十、Hystrix熔断器
10.1是什么?
Hystrix是一个处理分布式系统 延迟 和容错的开源的库 。在分布式系统系统过依赖不可避免的调用失败。 比如超时 异常 等。
Hystrix 能够保证在一个依赖出现问题情况下。 不会导致我们的服务失败。 避免一个级联的故障。 来提高我们分布式的系统的弹性。
熔断器 本身是一个开关装置。 当某个服务发生故障之后。 通过断路器的故障监控。 返回一个符合预期的可处理的备用的响应。
10.2能干什么
10.2.1 服务降级 (fllback)
例如:当服务器超时、程序运行异常、线程池爆满的时候,立即返回一个友好提示如 “服务器忙,请稍后再试”,而不是直接给客服端整个 ERROR
10.2.2 服务熔断 (break)
例如:当服务器达到最大访问量后,直接拒绝访问(例如家里的 保险丝 ),然后调用服务降级的方法并返回友好提示(过程:服务降级 —> 进而熔断 —> 恢复调用链路 )
10.2.2 服务限流 (flowlimit)
例如:淘宝、京东秒杀时高并发操作时,严禁一窝蜂的过来拥挤,大家排队,一秒N个,有序进行
10.3怎么玩
10.3.1 创建cloud-provider-hystrix-payment8001
10.3.2 pom
10.3.3 yml
10.3.4 主类
10.3.5 业务类
10.4 高并发测试
10.4.1 用jmeter进行压测, 对8001端口的超时服务进行压死操作
10.4.2 我们在请求 /payment/hystrix/ok/
结论:
2个都转圈圈
为什么会卡死?
tomcat 默认工作线程被打满了。 没有多余的线程来分解压力和处理了。
10.5.1 服务降级
配置@HystrixCommand
我们要从8001 从自身找问题 - 设置自身超时时间的峰值。 峰值内可以正常运行 。 超过了需要有一个兜底的方法进行处理。 做服务的降级fallback
1. 业务类上面加注解@HystrixCommand
2. 主启动类加上@EnableCircuitBreaker
配置 客户端 服务降级
-
pom.xml
<!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
-
主启动类开启断路器
@EnableHystrix @EnableFeignClients @SpringBootApplication @EnableEurekaClient @Slf4j public class OrderHysitxMain8000 { public static void main(String[] args) { SpringApplication.run( OrderHysitxMain8000.class,args); log.info("****************OrderHysitxMain8000 启动 ************ "); } }
-
yml 配置openFeign 对 Hystrix 支持
feign: hystrix: enabled: true
-
@EnableHystrix 开启hystrix
@EnableCircuitBreaker spring cloud hystrix
10.4 目前的问题
每个业务方法对应一个兜底的方法。 代码膨胀
- 默认兜底的方法
package com.etc.cloud.controller;
import com.etc.cloud.service.OrderHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author kalista
* @Description
* @Date 2020/8/23 11:04
**/
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentGlobal")
public class OrderHystrixController {
@Autowired
private OrderHystrixService orderHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
@HystrixCommand
public String payment_ok(@PathVariable("id") Integer id){
System.out.println(2/0);
return orderHystrixService.payment_ok(id);
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand
public String payment_timeout(@PathVariable("id") Integer id){
return orderHystrixService.payment_timeout(id);
}
public String payment_timeoutHandler( Integer id){
return "我是消费者80 对方的支付西繁忙请10秒钟后在试一试";
}
public String paymentGlobal(){
return "Global 异常 请稍后在试试";
}
}
服务的熔断
熔断器 类型
- 熔断打开 - 请求不在调用当前服务内设置一般为平均故障处理时间。 当打开长达导至所设置的时钟进入半熔断状态
- 熔断关闭 - 关闭不会对服务在进行熔断
- 熔断半开 - 部分请求根据规则调用当前服务。 如果请求成功符合规则认为党外你的服务恢复正常状态。 关闭熔断
熔断器在上面时候开始起作用的。
3个重要参数
- 快照时间窗 :@HystrixProperty(name = “circuitBreaker.sleepWindowInMilliseconds”,value = “10000”), // 时间窗口期
- 请求的总阈值:@HystrixProperty(name = “circuitBreaker.requestVolumeThreshold”,value = “10”),// 请求次数
- 错误百分比的阈值: @HystrixProperty(name = “circuitBreaker.errorThresholdPercentage”,value = “60”),// 失败率达到多少后跳闸