Hystrix概述
服务雪崩
多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其它的微服务,这就是所谓的"扇出”。
如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的“雪崩效应”,对于高流量的应用来说,单一的后端依赖可能会导致所有服务器上的所有资源都在几秒钟内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障。这些都表示需要对故障和延迟进行隔离和管理,以便单个依赖关系的失败,不能取消整个应用程序或系统。
Hystix的作用
Hystrix是一个用于处理分布式系统的延迟和容错的开源库, 在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,
Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
- 服务降级(fallback): 服务降级是从整个系统的负荷情况出发和考虑的,对某些负荷会比较高的情况,为了预防某些功能(业务场景)出现负荷过载或者响应慢的情况,在其内部暂时舍弃对一些非核心的接口和数据的请求,而直接返回一个提前准备好的fallback(退路)错误处理信息。这样,虽然提供的是一个有损的服务,但却保证了整个系统的稳定性和可用性。
- 服务熔断:当下游的服务因为某种原因突然变得不可用或响应过慢,上游服务为了保证自己整体服务的可用性,通常先服务降级,并不再继续调用目标服务,直接返回,快速释放资源。如果目标服务情况好转则恢复调用。
- 服务限流:限流的目的是通过对并发访问/请求进行限速或者一个时间窗口内的的请求进行限速来保护系统,一旦达到限制速率则可以拒绝服务、排队或等待、降级。
哪些情况会发生服务降级:
- 程序运行异常
- 超时
- 服务熔断触发服务降级
- 线程池/信号量也会导致服务降级
Hystrix服务降级
模拟一个高并发访问缓慢的服务
继续新建模块cloud-provider-hystrix-payment8001
,在模块cloud-provider-payment8001
的基础上添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
yaml:
server:
port: 8001
spring:
application:
name: cloud-hystrix-payment-service
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
# 将自己注册进eureka server
register-with-eureka: true
# 是否从eureka server抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
instance:
instance-id: payment-hystrix-8001
启动类:
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
}
service:
@Service
public class PaymentService {
public String paymentInfo_OK(Integer id){
return "线程池:"+Thread.currentThread().getName()+"\t paymentInfo_OK,id:"+id+"\t ok";
}
public String paymentInfo_Timeout(Integer id){
int timeNumber = 5;
try {
TimeUnit.SECONDS.sleep(timeNumber);
}catch (Exception e){
e.printStackTrace();
}
return "线程池:"+Thread.currentThread().getName()+"\t paymentInfo_Timeout,id:"+id+"\t ok,耗时"+timeNumber+"秒钟";
}
}
controller:
@Slf4j
@RestController
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@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;
}
}
我们使用Jmeter通过200个线程循环访问 http://localhost:8001/payment/hystrix/ok/30 ,并中途手动访问 http://localhost:8001/payment/hystrix/ok/2 ,会发现耗时短的服务的响应时间也被拖慢了,主要原因是大量的请求耗尽了tomcat的线程池资源。
提供方配置服务降级
hystrix可以设置超时时间,超时时间内正常服务,超过超时时间做服务降级fallback。
主配置类添加注解@EnableCircuitBreaker
使支持hystrix注解:
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
}
修改service,在需要做服务降级的方法上添加@HystrixCommand
并配置相关属性,再添加一个用于超时处理的方法:
@Service
public class PaymentService {
public String paymentInfo_OK(Integer id){
return "线程池:"+Thread.currentThread().getName()+"\t paymentInfo_OK,id:"+id+"\t ok";
}
//fallbackMethod指定超时处理的方法,在commandProperties中配置超时时间
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public String paymentInfo_Timeout(Integer id){
int timeNumber = 5;
try {
TimeUnit.SECONDS.sleep(timeNumber);
}catch (Exception e){
e.printStackTrace();
}
return "线程池:"+Thread.currentThread().getName()+"\t paymentInfo_Timeout,id:"+id+"\t ok,耗时"+timeNumber+"秒钟";
}
public String paymentInfo_TimeOutHandler(Integer id){
return "线程池:"+Thread.currentThread().getName()+"\t paymentInfo_TimeOutHandler,id:"+id+"\t redirect";
}
}
消费方配置服务降级
服务提供方可以配置服务降级,消费方也可以,并且通常Hystrix是配置在消费者方的,这里新建一个消费端模块cloud-consumer-feign-hystrix-order80
,依赖:
<dependencies>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>${project.version}</version>
</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>
<!--热部署-->
<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>
启动类:
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
feign service接口:
@Component
@FeignClient("CLOUD-HYSTRIX-PAYMENT-SERVICE")
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_Timeout(@PathVariable("id") Integer id);
}
controller:
@RestController
@Slf4j
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
return paymentHystrixService.paymentInfo_OK(id);
};
//超时1.5s则fallback至paymentTimeOutFallbackMethod方法
@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){
return paymentHystrixService.paymentInfo_Timeout(id);
};
public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己/(ㄒoㄒ)/~~";
}
}
默认降级方法
可以将一个类中的方法配置一个默认的降级方法,避免对每个方法都配置一个降级方法,造成代码臃肿:
@RestController
@Slf4j
//指定默认降级方法
@DefaultProperties(defaultFallback = "paymentGlobalFallbackMethod")
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
return paymentHystrixService.paymentInfo_OK(id);
};
@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){
return paymentHystrixService.paymentInfo_Timeout(id);
};
public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己/(ㄒoㄒ)/~~";
}
//下面是全局fallback
public String paymentGlobalFallbackMethod(){
return "Global异常信息,请稍后再试┭┮﹏┭┮";
}
}
降级方法解耦
降级方法属于异常处理方法,不应该放入正常业务类中,可以为Feign Client使用的接口注册一个对应的服务降级处理类
在@FeignClient
中指定fallback为一个降级处理类
@Component
@FeignClient(value = "CLOUD-HYSTRIX-PAYMENT-SERVICE",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_Timeout(@PathVariable("id") Integer id);
}
编写这个类,它一定要实现PaymentHystrixService
接口
@Component
public class PaymentFallbackService implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "-----PaymentFallbackService fall back paymentInfo_OK";
}
@Override
public String paymentInfo_Timeout(Integer id) {
return "-----PaymentFallbackService fall back paymentInfo_Timeout";
}
}
自定义配置
参考:
原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/hystrix%e6%a6%82%e8%bf%b0%e4%b8%8e%e6%9c%8d%e5%8a%a1%e9%99%8d%e7%ba%a7/