首页 热点 业界 科技快讯 数码 电子消费 通信 前沿动态 电商

在项目中直接使用hystrix的流程分析

2022-06-06 12:23:32 来源 : 软件开发网

目录

什么是Hystrix

一、背景

二、使用方式

2.1 Jar包引入

2.2 配置文件

2.3 设置配置

2.4 实现代码

三、测试试验

四、总结

什么是Hystrix

Hystrix是Netflix针对微服务分布式系统的熔断保护中间件,当我们的客户端连接远程的微服务时,有两种情况需要考虑:首先,如果远程系统当机了我们怎么办?

其次,我们如何管理对远程微服务的调用性能,以保证每个微服务以最小延迟最快性能响应?

Hystrix是一个有关延迟和失败容错的开源库包,用来设计隔离访问远程系统端点或微服务等,防止级联爆炸式的失败,也就是由一个小问题引起接二连三扩大的

疯狂的错误爆炸直至整个系统瘫痪,能够让复杂的分布式系统更加灵活具有弹性。这篇文章给大家介绍下在项目中如何直接使用hystrix?

一、背景

最近由于一些背景原因,需要在项目中需要对接口进行限流。所以就考虑到了直接使用Hystrix。但是呢,又不想直接使用SpringCloud,而是直接引入原生,现在发现挺好用的,所以记录下来,分享出来。

二、使用方式2.1 Jar包引入 com.netflix.hystrix hystrix-javanica 1.5.18 com.netflix.hystrix hystrix-core 1.5.18

引入两个包,分别是Hystrix核心包,以及直接原生的Java包

2.2 配置文件

在Resources目录下面,放上hystrix.properties文件。配置如下。

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=1000hystrix.command.default.circuitBreaker.requestVolumeThreshold=20hystrix.command.default.metrics.rollingStats.numBuckets=10hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000hystrix.command.default.circuitBreaker.errorThresholdPercentage=50hystrix.command.default.circuitBreaker.forceOpen=falsehystrix.command.default.circuitBreaker.forceClosed=falsehystrix.command.default.requestCache.enabled=falsehystrix.threadpool.default.coreSize=10hystrix.threadpool.default.maximumSize=10hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=truehystrix.threadpool.default.keepAliveTimeMinutes=1hystrix.threadpool.default.maxQueueSize=100hystrix.threadpool.default.queueSizeRejectionThreshold=101hystrix.threadpool.default.metrics.rollingStats.numBuckets=10hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=10000#hystrix.timer.threadpool.default.coreSize = 10

这个是一部分配置,如果需要知道更多,可以Click-Github Hystrix Wiki

2.3 设置配置

设置Hystrix的配置

/** *

熔断器配置

* * @author fattycal@qq.com * @since 2022/6/4 */@Configurationpublic class HystrixConfig implements InitializingBean { @Bean public HystrixCommandAspect hystrixCommandAspect(){ // 初始化切面 return new HystrixCommandAspect(); } @Override public void afterPropertiesSet() throws Exception { // 初始化熔断器配置 // 清除配置 ConfigurationManager.getConfigInstance().clear(); // 加载配置文件 ConfigurationManager.loadCascadedPropertiesFromResources("hystrix"); }}

HystrixCommandAspect是jar包带的切面,通过切面通知,找去需要熔断的方法,然后进行处理。

@Aspectpublic class HystrixCommandAspect { //...略 @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)") public void hystrixCommandAnnotationPointcut() { } @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)") public void hystrixCollapserAnnotationPointcut() { } @Around("hystrixCommandAnnotationPointcut() || hystrixCollapserAnnotationPointcut()") public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable { // ... 略 }}

ConfigurationManager看看这名字,就知道是配置管理的,也不负众望,的确是用来加载配置的。

2.4 实现代码/** *

熔断器测试

* * @author fattycal@qq.com * @since 2022/6/4 */@RestControllerpublic class HystrixTestController { @GetMapping("/hystrix") @HystrixCommand(commandKey = "hystrixTestController-getHello", threadPoolKey = "hystrixTestController-getHello", fallbackMethod = "getHelloFallback") public String getHello(){ try { // 执行太快不便于测试 Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } return "TEST Hystrix"; } public String getHelloFallback(Throwable error){ // 打印日志 System.out.println("TEST Hystrix: " + error.getMessage()); return "TEST Hystrix: " + error.getMessage(); }}

代码没有啥花里胡哨的,直接在需要熔断的方法上面加上HystrixCommond。

commandKeythreadPoolKey是自己设置的,可以为这个方法定制线程数、核心线程等配置(在hystrix.properties中添加)。给出示例如下。

#-------------------------------------------------------------------hystrix.threadpool.hystrixTestController-getHello.coreSize=1hystrix.threadpool.hystrixTestController-getHello.maximumSize=2hystrix.threadpool.hystrixTestController-getHello.maxQueueSize=1hystrix.threadpool.hystrixTestController-getHello.queueSizeRejectionThreshold=2#-------------------------------------------------------------------

至此,完成了所有的配置和准备,接下来直接测试

三、测试试验

直接从Jmeter官网下载jmeter,拿到跑测试, 具体下载过程就不一样展示了,直接贴出测试结果。

由于为这个方法设置的核心线程数、线程数、队列数都不大,很容易测试出结果。我们可以从console中很明显的看到熔断器打开,说明方法被执行到。

在从Jmeter中查看一下结果,也是可以佐证我们的效果。测试图如下:

四、总结

自此,整个流程是走完了,可以看到效果着实起来了。 Hystrix知识限流熔断中的一种方案,大家可以结合实际情况做出更多的选择。

标签: 配置文件 测试试验 分布式系统

相关文章

最近更新
观焦点:超萌相机 2023-03-01 12:29:37
海南百货网 2023-03-01 12:13:44
焦点热讯:宜点充 2023-02-28 10:10:16
天天关注:小铺CEO 2023-02-28 10:07:13
【世界聚看点】KaFit 2023-02-28 09:31:37
葱天下 2023-02-28 09:17:03
渔界竞钓 2023-02-28 08:15:29
焦点快看:鲸奇视频 2023-02-28 06:30:37
环球热议:萝小逗 2023-02-27 23:25:49
简讯:小码公交 2023-02-27 23:16:12
彼岸花 2023-02-27 22:32:52
时时夺宝 2023-02-27 21:37:50
天天动态:袜之源 2023-02-27 21:29:50
天天资讯:AI空气 2023-02-27 20:19:46
世界时讯:绘读 2023-02-27 20:19:41
看点:一元得购 2023-02-27 19:26:28