Spring Cloud Hystrix
一、什么是Hystrix
NetFlix开源的一个类库,实现了超时机制和断路器模式,用于隔离服务、远程系统或者第三方库,防止级联失败,从而提升系统的容错性和可用性。
- 1.为系统提供保护机制。在依赖的服务出现高延迟或失败时,为系统提供保护和控制。
- 2.防止雪崩。
- 3.包裹请求:使用HystrixCommand(或HystrixObservableCommand)包裹对依赖的调用逻辑,每个命令在独立线程中运行。
- 4.跳闸机制:当某服务失败率达到一定的阈值时,Hystrix可以自动跳闸,停止请求该服务一段时间(进入熔断状态)。
- 5.资源隔离:Hystrix为每个请求都的依赖都维护了一个小型线程池,如果该线程池已满,发往该依赖的请求就被立即拒绝,而不是排队等候,从而加速失败判定。防止级联失败(服务失败进行隔离,互相不会影响)。
- 6.快速失败:Fail Fast。同时能快速恢复。侧重点是:(不去真正的请求服务,发生异常再返回),而是直接失败(当hystrix线程数满/信号量无/时间段内请求失败/进入熔断状态时,请求会快速失败)。
- 7.监控:Hystrix可以实时监控运行指标和配置的变化,提供近实时的监控、报警、运维控制(需要借助hystrix-dashboard和actuator)。
- 8.回退机制:fallback,当请求失败、超时、被拒绝,或当断路器被打开时,执行回退逻辑。回退逻辑我们自定义,提供优雅的服务降级。
- 9.自我修复:断路器打开一段时间后,会自动进入“半开”状态,可以进行打开,关闭(熔断),半开状态(熔断后,每隔一段时间会去尝试请求一次,看下服务是否恢复可用)的转换。
二、配置使用
1、hystrix可以脱离springCloud独立使用
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
public class HystrixTest extends HystrixCommand {
protected HystrixTest(HystrixCommandGroupKey group) {
super(group);
}
public static void main(String[] args) {
// HystrixTest hystrixTest = new HystrixTest(HystrixCommandGroupKey.Factory.asKey("ext"));
/**
* execute():以同步阻塞方式执行run()。以demo为例,调用execute()后,
* hystrix先创建一个新线程运行run(),
* 接着调用程序要在execute()调用处一直阻塞着,直到run()运行完成
*/
// System.out.println("result:" + hystrixTest.execute());
/**
* queue():以异步非阻塞方式执行run()。以demo为例,
* 一调用queue()就直接返回一个Future对象,
* 同时hystrix创建一个新线程运行run(),
* 调用程序通过Future.get()拿到run()的返回结果,
* 而Future.get()是阻塞执行的
*/
Future<String> futureResult = new HystrixTest(HystrixCommandGroupKey.Factory.asKey("ext")).queue();
String result = "";
try {
result = futureResult.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("程序结果:"+result);
}
@Override
protected Object run() throws Exception {
System.out.println("执行逻辑");
int i = 1/0;
return "ok";
}
@Override
protected Object getFallback() {
return "getFallbackgetFallback";
}
}
2、可以整合RestTemplate使用
@HystrixCommand(fallbackMethod = "back")
public String alive() {
// 自动处理URL
RestTemplate restTemplate = new RestTemplate();
String url ="http://provider/live";
String object = restTemplate.getForObject(url, String.class);
return object;
}
public String back() {
return "请求失败";
}
//启动类需要添加@EnableCircuitBreaker注解
3、整合Feign
# 配置启用
# feign.hystrix.enabled=true
//接口
@FeignClient(name = "provider",fallback = LiveBack.class)
public interface ConsumerApi {
@RequestMapping(value = "/live",method = RequestMethod.GET)
public String alive();
}
//FallBack实现类
import java.util.Map;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
@Component
public class LiveBack implements ConsumerApi{
@Override
public String live() {
return "降级了。。。";
}
}
//也可以通过FallBackFactory实现 可以针对不同异常进行不同降级
@FeignClient(name = "provider",fallbackFactory = ProviderBackFactory.class)
public interface ConsumerUserApi extends UserApi {
}
//Factory实现
@Component
public class ProviderBackFactory implements FallbackFactory<ConsumerUserApi> {
@Override
public ConsumerUserApi create(Throwable throwable) {
return new ConsumerUserApi() {
@Override
public String live() {
throwable.printStackTrace();
if(throwable instanceof FeignException.InternalServerError) {
return "远程服务调用500==>" + throwable.getLocalizedMessage();
} else if(throwable instanceof RuntimeException){
return "运行出错,请联系管理员" + throwable.getLocalizedMessage();
} else {
return "系统异常,请稍后再试" + throwable.getLocalizedMessage();
}
}
};
}
}
4、信号量隔离与线程池隔离
线程池隔离是Hystrix会单独维护一个自己的线程池,启用自己的线程去执行调用服务。
信号量隔离是直接让tomcat的worker线程去执行调用服务,信号量隔离是一道关卡,信号量有多少,就允许多少线程通过。它不需要自己的线程池,更加轻量级。
默认情况下使用线程池控制请求隔离
hystrix.command.default.execution.isolation.strategy 隔离策略,默认是Thread, 可选Thread|Semaphore
thread 通过线程数量来限制并发请求数,可以提供额外的保护,但有一定的延迟。一般用于网络调用
semaphore 通过semaphore count来限制并发请求数,适用于无网络的高并发请求
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 命令执行超时时间,默认1000ms
hystrix.command.default.execution.timeout.enabled 执行是否启用超时,默认启用true
hystrix.command.default.execution.isolation.thread.interruptOnTimeout 发生超时是是否中断,默认true
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests 最大并发请求数,默认10,该参数当使用ExecutionIsolationStrategy.SEMAPHORE策略时才有效。如果达到最大并发请求数,请求会被拒绝。理论上选择semaphore size的原则和选择thread size一致,但选用semaphore时每次执行的单元要比较小且执行速度快(ms级别),否则的话应该用thread。semaphore应该占整个容器(tomcat)的线程池的一小部分。
5、开启dashboard
//启动类添加
@EnableHystrixDashboard
<!--引入依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
//健康上报
http://localhost:90/actuator/hystrix.stream
//图形化
http://localhost:90/hystrix
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)