Hystix,是Netflix开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败(雪崩)。
那么如何预防雪崩的出现呢?
隔离、降级、熔断、限流等方法防止雪崩的发生。资源隔离要解决的核心的问题:就是将多个依赖服务的调用分别隔离到各自的资源池内。
导致服务所有的线程资源全部耗费在这个服务的接口调用上。
线程池隔离:适合绝大多数的场景,Hystix默认采用线程池隔离
A 服务将Threadpool中拆分成Threadpool-B、Threadpool-C、Threadpool-D3个单独对B\C\D进行服务。[C]挂掉并不会引起整条链路都失败的情况发生。
线程池隔离优缺点
优点∶
完全隔离依赖的服务(例如图中的 B、C、D服务),请求线程可以快速放回线程池隔离是独立的,不会影响其他服务和接口。线程池将清理并可立即恢复,而不需要一个长时间的恢复。缺点∶
每个命令的执行涉及到排队、调度和上下文切换都是在一个单独的线程上运行的。信号量隔离:适合与对内部的一些比较复杂的业务逻辑的访问,而不是对外部依赖的访问。
阀门(访问的次数)。超过该数量,就拒绝后续的服务。
| 是否有线程切换 | 是否支持异步 | 是否支持超时 | 是否支持熔断 | 是否支持限流 | 开销 | |
|---|---|---|---|---|---|---|
| 线程池 | √ | √ | √ | √ | √ | 大 |
| 信号量 | × | × | × | √ | √ | 小 |
什么情况下,用线程池隔离?
什么情况下,用信号量隔离?
Hystix降级:当服务发生异常或调用超时,返回默认处理方案(默认数据)。

Hystrix初始代码和Fiegn初始代码一样,这里就不再赘述。
![]() | ![]() |
|---|
(hystrix-provider),引入 hystrix依赖@HystrixCommand 注解配置降级方法@EnableCircuitBreaker
实例操作
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
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);
}
}
消费方调用提供方中controller中提供的方法接口,那么如果该接口出异常,我们就需要为其提供一个降级方法。package com.itheima.provider.controller;
import com.itheima.provider.domain.Goods;
import com.itheima.provider.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* Goods Controller 服务提供方
*/
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@Value("${server.port}")
private int port;
/**
* 降级:
* 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;
//降级情况2.服务调用超时
try {
//2. 休眠2秒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Goods goods = goodsService.findOne(id);
goods.setTitle(goods.getTitle() + ":" + port);//将端口号,设置到了 商品标题上
return goods;
}
/**
* 定义降级方法:
* 1. 方法的返回值需要和原方法一样
* 2. 方法的参数需要和原方法一样
*/
public Goods findOne_fallback(int id){//findOne的降级方法。
Goods goods = new Goods();
goods.setTitle("降级了~~~");
return goods;
}
}

复写方法,即降级方法
@FeignClient注解中使用fallback 属性设置降级处理类。feign.hystrix.enabled = true
实例操作
# 开启feign对hystrix的支持
feign:
hystrix:
enabled: true
@FeignClient注解中使用fallback 属性设置降级处理类。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);
}
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;
}
}

注意:
当服务提供方和服务的消费方都配置了降级时,当失败后(出现异常、服务调用超时)、只有服务提供方的降级会生效。
原因:当出现异常,服务提供方已经降过级,降过级后,返回的数据是正常的数据。那么这时服务消费方就不用再次被降级。
Hystrix 熔断机制,用于监控微服务调用情况,当失败的情况达到预定的阈值(5秒失败20次),会打开断路器,拒绝所有请求,不管请求降级与否,直到服务恢复正常为止。
sleepWindowlnMilliseconds:监控时间requestVolumeThreshold:失败次数errorThresholdPercentage:失败率
在服务提供方(hystrix-provider)controlloer包下的GoodsControlloer中写(默认该机制已开启)。
package com.itheima.provider.controller;
import com.itheima.provider.domain.Goods;
import com.itheima.provider.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* Goods Controller 服务提供方
*/
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@Value("${server.port}")
private int port;
@GetMapping("/findOne/{id}")
@HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {
//设置Hystrix的超时时间,默认1s
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
//监控时间 默认5000 毫秒
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "5000"),
//失败次数。默认20次
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"),
//失败率 默认50%
@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50")
})
public Goods findOne(@PathVariable("id") int id){
//如果id == 1 ,则出现异常,id != 1 则正常访问
if(id == 1){
//1.造个异常
int i = 3/0;
}
Goods goods = goodsService.findOne(id);
goods.setTitle(goods.getTitle() + ":" + port);//将端口号,设置到了 商品标题上
return goods;
}
/**
* 定义降级方法:
* 1. 方法的返回值需要和原方法一样
* 2. 方法的参数需要和原方法一样
*/
public Goods findOne_fallback(int id){
Goods goods = new Goods();
goods.setTitle("降级了~~~");
return goods;
}
}
Hystrix-dashboard 功能,用于实时监控微服务运行状态。监控一个微服务。Turbine ,进行聚合监控。
聚合监控
在高并发访问下,由于系统资源有限,必须对访问量进行控制。
网关启用Hystrix,进行限流处理,每个微服务也可以各自启用Hystrix进行限流。hystrix:
threadpool:
default:
coreSize: 200 #并发执行的最大线程数,默认10
maxQueueSize: 1000 #BlockingQueue的最大队列数,默认值-1
queueSizeRejectionThreshold: 800 #即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝,默认值5