Hystrix-概述
• Hystix 是 Netflix 开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败(雪崩)。
• 雪崩:一个服务失败,导致整条链路的服务都失败的情形
Hystix 主要功能
• 隔离
线程池隔离
信号量隔离
• 降级:异常,超时
• 熔断
• 限流
其中,需要我们去重点掌握的是降级,熔断和限流.
1.Hystrix的降级
降级的使用是为了解决代码的异常和一些网络抖动产生的连接超时,返回一些能让用户识别的数据,让用户有更好的体验。
使用之前需要导入依赖,由于消费方使用时需要导入fegin的依赖,在fegin的依赖中包含有Hystrix的依赖,可以不用导入,提供方需要导入
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
1.1提供方的降级
提供方的降级,直接在提供方的controller层中,书写需要降级的原方法中的降级方法,一般是原方法名+fallback
首先定义降级方法
/**
* 定义降级方法:
* 1. 方法的返回值需要和原方法一样
* 2. 方法的参数需要和原方法一样
*/
public Goods findOne_fallback(int id){
Goods goods = new Goods();
goods.setTitle("降级了~~~");
return goods;
}
使用 @HystrixCommand 注解配置降级方法:在原方法上加上@HystrixCommand注解,并指定降级方法。
/**
* 降级:
* 1. 出现异常
* 2. 服务调用超时
* * 默认1s超时
*
* @HystrixCommand(fallbackMethod = "findOne_fallback")
* fallbackMethod:指定降级后调用的方法名称
*/
@GetMapping("/findOne/{id}")
@HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {
//设置Hystrix的超时时间,默认1s
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public Goods findOne(@PathVariable("id") int id){
//1.造个异常
int i = 3/0;
try {
//2. 休眠2秒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Goods goods = goodsService.findOne(id);
goods.setTitle(goods.getTitle() + ":" + port);//将端口号,设置到了 商品标题上
return goods;
}
在启动类上开启Hystrix功能:@EnableCircuitBreaker
package com.itheima.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 启动类
*/
@EnableEurekaClient //该注解 在新版本中可以省略
@SpringBootApplication
@EnableCircuitBreaker // 开启Hystrix功能
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}
1.2服务方的降级
- 定义feign 调用接口实现类,复写方法,即 降级方法 GoodsFeignClientFallback来实现原来Feign接口
package com.itheima.consumer.feign;
import com.itheima.consumer.domain.Goods;
import org.springframework.stereotype.Component;
/**
* Feign 客户端的降级处理类
* 1. 定义类 实现 Feign 客户端接口
* 2. 使用@Component注解将该类的Bean加入SpringIOC容器
*/
@Component
public class GoodsFeignClientFallback implements GoodsFeignClient {
@Override
public Goods findGoodsById(int id) {
Goods goods = new Goods();
goods.setTitle("又被降级了~~~");
return goods;
}
}
在 @FeignClient 注解中使用 fallback 属性设置降级处理类。
GoodsFeignClient
package com.itheima.consumer.feign;
import com.itheima.consumer.domain.Goods;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "HYSTRIX-PROVIDER",fallback = GoodsFeignClientFallback.class)
public interface GoodsFeignClient {
@GetMapping("/goods/findOne/{id}")
public Goods findGoodsById(@PathVariable("id") int id);
}
配置开启 feign.hystrix.enabled = true
application.yml
# 开启feign对hystrix的支持
feign:
hystrix:
enabled: true
2.Hystrix的熔断
熔断在springCloud中是默认开启的,不需要再添加其他的配置。
熔断是指在用户访问我们的微服务时,在默认时间内失败的次数达到指定次数(默认是5s20次),此时短路器会打开,之后会拒绝所有的请求,即使这个请求是正常的。但是不会一直短路,在短路器开启一段时间后(默认5s),短路器会放开一部分请求进入到我们的服务中,如果这些服务能够正常请求,那么短路器就关闭。如果在5s内请求依然被拒绝20次以上,那么短路器还是保持关闭,循环执行之后。