九、Hystrix断路器
1、概述
-
分布式系统面临的问题
分布式系统面临的问题
复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败。 -
是什么
Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
-
能干嘛
- 服务降级
- 服务熔断
- 接近实时的监控
-
官网资料
https://github.com/Netflix/Hystrix/wiki/How-To-Use
-
Hystrix官宣,停更进维
https://github.com/Netflix/Hystrix
- 被动修复bugs
- 不再接受合并请求
- 不再发布新版本
2、Hystrix重要概念
-
服务降级
服务器忙,请稍后再试,不让客户端等待并立刻返回一个友好提示,fallback
哪些情况会出发降级
- 程序运行异常
- 超时
- 服务熔断触发服务降级
- 线程池/信号量打满也会导致服务降级
-
服务熔断
-
类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法并返回友好提示
-
就是保险丝
服务的降级->进而熔断->恢复调用链路
-
-
服务限流
秒杀高并发等操作,严禁一窝蜂的过来拥挤,大家排队,一秒钟N个,有序进行
3、hystrix案例
-
构建
-
新建cloud-provider-hystrix-payment8001
-
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-provider-hystrix-payment8001</artifactId> <dependencies> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--web--> <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> <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity --> <groupId>com.likun.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </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: 8001 spring: application: name: cloud-provider-hystrix-payment eureka: client: register-with-eureka: true fetch-registry: true service-url: #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka defaultZone: http://eureka7001.com:7001/eureka
-
主启动
package com.likun.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * @author likun * @create 2021-04-28 22:27 */ @SpringBootApplication @EnableEurekaClient public class PaymentHystrixMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentHystrixMain8001.class, args); } }
-
业务类
PaymentService
package com.likun.springcloud.service; /** * @author likun * @create 2021-04-28 22:29 */ public interface PaymentService { String paymentInfo_OK(Integer id); String paymentInfo_TimeOut(Integer id); }
PaymentServiceImpl
package com.likun.springcloud.service.Impl; import com.likun.springcloud.service.PaymentService; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; /** * @author likun * @create 2021-04-28 22:31 */ @Service public class PaymentServiceImpl implements PaymentService { @Override public String paymentInfo_OK(Integer id) { return "线程池:"+Thread.currentThread().getName()+"paymentInfo_OK,id:" + id+"O(∩_∩)O"; } @Override public String paymentInfo_TimeOut(Integer id) { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return "线程池:"+Thread.currentThread().getName()+"paymentInfo_OK,id:" + id+"O(∩_∩)O不好意思~睡过了3秒钟"; } }
PaymentController
package com.likun.springcloud.controller; import com.likun.springcloud.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author likun * @create 2021-04-28 22:29 */ @RestController @Slf4j public class PaymentController { @Resource PaymentService paymentService; @GetMapping("/payment/hystrix/ok/{id}") public String paymentInfo_OK(@PathVariable("id") Integer id) { String result = paymentService.paymentInfo_OK(id); log.info("*******result:"+result); return result; } @GetMapping("/payment/hystrix/timeout/{id}") public String paymentInfo_TimeOut(@PathVariable("id")Integer id) { String result = paymentService.paymentInfo_TimeOut(id); log.info("*******result:"+result); return result; } }
-
正常测试
http://localhost:8001/payment/hystrix/ok/5
http://localhost:8001/payment/hystrix/timeout/5
控制台
-
-
高并发测试
上述在非高并发情形下,还能勉强满足 but…
-
Jmeter压测测试
开启Jmeter,来20000个并发压死8001,20000个请求都去访问paymentInfo_TimeOut服务
再来一个访问:http://localhost:8001/payment/hystrix/timeout/5
看演示结果
ok请求也开始转圈,为什么会被卡死?
- tomcat的默认的工作线程数被打满 了,没有多余的线程来分解压力和处理。
-
Jmeter压测结论
上面还是服务提供者8001自己测试,假如此时外部的消费者80也来访问,
那消费者只能干等,最终导致消费端80不满意,服务端8001直接被拖死 -
看热闹不嫌弃事大,80新建加入
-
cloud-consumer-feign-hystrix-order80
-
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-feign-hystrix-order80</artifactId> <dependencies> <!--openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 引入自己定义的api通用包,可以使用Payment支付Entity --> <dependency> <groupId>com.likun.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <!--web--> <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> <!--一般基础通用配置--> <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 ```yaml server: port: 80 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka/
-
主启动类
package com.likun.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; /** * @author likun * @create 2021-04-29 0:05 */ @SpringBootApplication @EnableFeignClients public class OrderHystrixMain80 { public static void main(String[] args) { SpringApplication.run(OrderHystrixMain80.class, args); } }
-
业务类
PaymentHystrixService
package com.likun.springcloud.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @author likun * @create 2021-04-29 0:22 */ @Component @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT") public interface PaymentHystrixService { @GetMapping("/payment/hystrix/ok/{id}") String paymentInfo_OK(@PathVariable("id") Integer id); @GetMapping("/payment/hystrix/timeout/{id}") String paymentInfo_TimeOut(@PathVariable("id") Integer id); }
OrderHystirxController
package com.likun.springcloud.controrller; import com.likun.springcloud.service.PaymentHystrixService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author likun * @create 2021-04-29 0:24 */ @RestController @Slf4j public class OrderHystirxController { @Resource private PaymentHystrixService paymentHystrixService; @GetMapping("/consumer/payment/hystrix/ok/{id}") public String paymentInfo_OK(@PathVariable("id") Integer id) { String result = paymentHystrixService.paymentInfo_OK(id); return result; } @GetMapping("/consumer/payment/hystrix/timeout/{id}") public String paymentInfo_TimeOut(@PathVariable("id") Integer id) { String result = paymentHystrixService.paymentInfo_TimeOut(id); return result; } }
-
正常测试
http://localhost/consumer/payment/hystrix/ok/31
-
- 高并发测试
结果:消费者80,o(╥﹏╥)o
要么转圈圈等待
要么消费端报超时错误
-
-
故障现象和导致原因
正因为有上述故障或不佳表现才有我们的降级/容错/限流等技术诞生
-
上诉结论
正因为有上述故障或不佳表现
才有我们的降级/容错/限流等技术诞生 -
如何解决?解决的要求
-
超时导致服务器变慢(转圈)
超时不再等待
-
出错(宕机或程序运行出错)
出错要有兜底
-
解决
- 对方服务(8001)超时了,调用者(80)不能一直卡死等待,必须有服务降级
- 对方服务(8001)down机了,调用者(80)不能一直卡死等待,必须有服务降级
- 对方服务(8001)OK,调用者(80)自己出故障或有自我要求(自己的等待时间小于服务提供者),自己处理降级
-
3.1、服务降级
-
降级配置
@HystrixCommand
-
8001先从自身找问题
设置自身调用超时时间的峰值,峰值内可以正常运行,
超过了需要有兜底的方法处理,作服务降级fallback -
8001fallback
业务类启用
package com.likun.springcloud.service.Impl; import com.likun.springcloud.service.PaymentService; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; /** * @author likun * @create 2021-04-28 22:31 */ @Service public class PaymentServiceImpl implements PaymentService { @Override public String paymentInfo_OK(Integer id) { return "线程池:" + Thread.currentThread().getName() + "paymentInfo_OK,id:" + id + "O(∩_∩)O"; } @Override @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000") }) public String paymentInfo_TimeOut(Integer id) { int a = 10 / 0; try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return "线程池:" + Thread.currentThread().getName() + "paymentInfo_OK,id:" + id + "O(∩_∩)O不好意思~睡过了3秒钟"; } public String paymentInfo_TimeOutHandler(Integer id) { return "当前线程名:" + Thread.currentThread().getName() + "\t" + id + "\t" + "连接服务器超时或异常" + "o(╥﹏╥)o"; } }
@HystrixCommand报异常后如何处理
一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
上图故意制造两个异常:
1、 int age = 10/0; 计算异常
2 、 我们能接受3秒钟,它运行5秒钟,超时异常。当前服务不可用了,做服务降级,兜底的方案都是paymentInfo_TimeOutHandler
主启动类激活
添加新注解**@EnableCircuitBreaker**
小测试:
http://localhost:8001/payment/hystrix/timeout/31
-
80fallback
-
80订单微服务,也可以更好的保护自己,自己也依样画葫芦进行客户端降级保护
-
题外话,切记
我们自己配置过的热部署方式对java代码的改动明显,但对@HystrixCommand内属性的修改建议重启微服务
-
YML
feign: hystrix: enabled: true
-
主启动
加**@EnableHystrix**
-
业务类
package com.likun.springcloud.controrller; import com.likun.springcloud.service.PaymentHystrixService; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author likun * @create 2021-04-29 0:24 */ @RestController @Slf4j public class OrderHystirxController { @Resource private PaymentHystrixService paymentHystrixService; @GetMapping("/consumer/payment/hystrix/ok/{id}") public String paymentInfo_OK(@PathVariable("id") Integer id) { String result = paymentHystrixService.paymentInfo_OK(id); return result; } @GetMapping("/consumer/payment/hystrix/timeout/{id}") @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")}) public String paymentInfo_TimeOut(@PathVariable("id") Integer id) { String result = paymentHystrixService.paymentInfo_TimeOut(id); return result; } public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) { return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o"; } }
小测试
-
-
目前问题
- 每个业务方法对应一个兜底的方法,代码膨胀
- 统一和自定义的分开
-
解决问题
-
每个方法配置一个???膨胀
-
feign接口系列
-
@DefaultProperties(defaultFallback = “”)
-
说明
@DefaultProperties(defaultFallback = “”)
1:1 每个方法配置一个服务降级方法,技术上可以,实际上傻X 1:N 除了个别重要核心业务有专属,其它普通的可以通过@DefaultProperties(defaultFallback = "") 统一跳转到统一处理结果页面 通用的和独享的各自分开,避免了代码膨胀,合理减少了代码量,O(∩_∩)O哈哈~
-
和业务逻辑混一起???混乱
-
服务降级,客户端去调用服务端,碰上服务端宕机或关闭
-
本次案例服务降级处理是在客户端80实现完成的,与服务端8001没有关系只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦
-
未来我们要面对的异常
- 运行
- 超时
- 宕机
-
修改cloud-consumer-feign-hystrix-order80
-
根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现该接口,统一为接口里面的方法进行异常处理
package com.likun.springcloud.service; import org.springframework.stereotype.Component; /** * @author likun * @create 2021-04-29 3:59 */ @Component//必须加!!! public class PaymentFallbackService implements PaymentHystrixService{ @Override public String paymentInfo_OK(Integer id) { return "------PaymentFallbackService rall back-paymentInfo_OK,o(╥﹏╥)o"; } @Override public String paymentInfo_TimeOut(Integer id) { return "------PaymentFallbackService rall back-paymentInfo_TimeOut,o(╥﹏╥)o"; } }
-
YML
# 用于服务降级 在注解@FeignClient中添加fallbackFactory属性值 feign: hystrix: enabled: true #在Feign中开启Hystri
-
PaymentFallbackService接口
package com.likun.springcloud.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @author likun * @create 2021-04-29 0:22 */ @Component @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class) public interface PaymentHystrixService { @GetMapping("/payment/hystrix/ok/{id}") String paymentInfo_OK(@PathVariable("id") Integer id); @GetMapping("/payment/hystrix/timeout/{id}") String paymentInfo_TimeOut(@PathVariable("id") Integer id); }
-
测试
正常情况:
-
故意关闭微服务8001
此时服务端provider已经down了,但是我们做了服务降级处理,让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器
-