在项目中直接使用hystrix的流程分析
作者:编号94530 发布时间:2021-10-22 01:55:21
什么是Hystrix
Hystrix是Netflix针对微服务分布式系统的熔断保护中间件,当我们的客户端连接远程的微服务时,有两种情况需要考虑:首先,如果远程系统当机了我们怎么办?
其次,我们如何管理对远程微服务的调用性能,以保证每个微服务以最小延迟最快性能响应?
Hystrix是一个有关延迟和失败容错的开源库包,用来设计隔离访问远程系统端点或微服务等,防止级联 * 式的失败,也就是由一个小问题引起接二连三扩大的
疯狂的错误 * 直至整个系统瘫痪,能够让复杂的分布式系统更加灵活具有弹性。这篇文章给大家介绍下在项目中如何直接使用hystrix?
一、背景
最近由于一些背景原因,需要在项目中需要对接口进行限流。所以就考虑到了直接使用Hystrix。但是呢,又不想直接使用SpringCloud,而是直接引入原生,现在发现挺好用的,所以记录下来,分享出来。
二、使用方式
2.1 Jar包引入
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.18</version>
</dependency>
引入两个包,分别是Hystrix核心包,以及直接原生的Java包
2.2 配置文件
在Resources目录下面,放上hystrix.properties文件。配置如下。
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=1000
hystrix.command.default.circuitBreaker.requestVolumeThreshold=20
hystrix.command.default.metrics.rollingStats.numBuckets=10
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
hystrix.command.default.circuitBreaker.forceOpen=false
hystrix.command.default.circuitBreaker.forceClosed=false
hystrix.command.default.requestCache.enabled=false
hystrix.threadpool.default.coreSize=10
hystrix.threadpool.default.maximumSize=10
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true
hystrix.threadpool.default.keepAliveTimeMinutes=1
hystrix.threadpool.default.maxQueueSize=100
hystrix.threadpool.default.queueSizeRejectionThreshold=101
hystrix.threadpool.default.metrics.rollingStats.numBuckets=10
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=10000
#hystrix.timer.threadpool.default.coreSize = 10
这个是一部分配置,如果需要知道更多,可以Click-Github Hystrix Wiki
2.3 设置配置
设置Hystrix的配置
/**
* <p>熔断器配置</p>
*
* @author fattycal@qq.com
* @since 2022/6/4
*/
@Configuration
public 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包带的切面,通过切面通知,找去需要熔断的方法,然后进行处理。
@Aspect
public 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 实现代码
/**
* <p>熔断器测试</p>
*
* @author fattycal@qq.com
* @since 2022/6/4
*/
@RestController
public 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。
commandKey
和threadPoolKey
是自己设置的,可以为这个方法定制线程数、核心线程等配置(在hystrix.properties中添加)。给出示例如下。
#-------------------------------------------------------------------
hystrix.threadpool.hystrixTestController-getHello.coreSize=1
hystrix.threadpool.hystrixTestController-getHello.maximumSize=2
hystrix.threadpool.hystrixTestController-getHello.maxQueueSize=1
hystrix.threadpool.hystrixTestController-getHello.queueSizeRejectionThreshold=2
#-------------------------------------------------------------------
至此,完成了所有的配置和准备,接下来直接测试
三、测试试验
直接从Jmeter官网下载jmeter,拿到跑测试, 具体下载过程就不一样展示了,直接贴出测试结果。
由于为这个方法设置的核心线程数、线程数、队列数都不大,很容易测试出结果。我们可以从console中很明显的看到熔断器打开,说明方法被执行到。
在从Jmeter中查看一下结果,也是可以佐证我们的效果。测试图如下:
四、总结
自此,整个流程是走完了,可以看到效果着实起来了。 Hystrix知识限流熔断中的一种方案,大家可以结合实际情况做出更多的选择。
来源:https://www.cnblogs.com/lifacheng/p/16341993.html


猜你喜欢
- 本文实例为大家分享了Java多文件以ZIP压缩包导出的具体代码,供大家参考,具体内容如下1、使用java实现吧服务器的图片打包成一个zip格
- EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。ehcach
- 最近出现一个问题是这样的:我们的系统在国外打印的日志时间由于时差关系和国内不一致,看起来不方便,希望国外的日志和国内保持一致,即:需要对不同
- 前言作者:京东科技 张天赐JDK 8 是一次重大的版本升级,新增了非常多的特性,其中之一便是 CompletableFuture。
- 本文实例为大家分享了Chronometer实现倒计时功能,Android提供了实现按照秒计时的API,供大家参考,具体内容如下一、自定义Ch
- 1、功能需求本实例将通过c# winform实现简单的分页功能,需要的基础知识有SQL语句,c#语言基础以及c# winform的一些简单知
- Nacos获取不到配置的值namespace设计真实一个奇特的东西。用spring-cloud-starter-alibaba-nacos-
- Android手机震动抖动效果的实现(1)布局文件如下<RelativeLayout xmlns:android="http
- 排序算法有很多,所以在特定情景中使用哪一种算法很重要。为了选择合适的算法,可以按照建议的顺序考虑以下标准: (1)执行时间 (2)存储空间
- kafka作为一个使用广泛的消息队列,很多人都不会陌生,但当你在网上搜索“kafka 延迟队列”,出
- 我们知道,在java中,将一个非原型类型类型的对象引用,赋值给另一个对象的引用之后,这两个引用就指向了同一个对象,如:public clas
- 1.Lombok是什么Lombok是使用java编写的一款开源类库。其主作用是使用注解来代替一些具有格式固定,没有过多技术含量的编码工作。使
- 本文介绍了springboot前后台数据交互的示例代码,分享给大家,具体如下:1.在路径中传递数据,比如对某个数据的id:123前台发送:格
- 1、mybatis-plus @DS实现动态切换数据源原理首先mybatis-plus使用com.baomidou.dynamic.data
- 前言:理论和代码必须结合起来才能真正的掌握一、this概念:this代表着当前对象的引用,也是当前函数所属对象的引用。直白的说,哪个对象调用
- 什么是队列结构一种线性结构,具有特殊的运算法则【只能在一端(队头)删除,在另一端(队尾)插入】。分类:顺序队列结构链式队列结构基本操作:入队
- 初学spring,我在dao层初始化c3p0的时候,使用@Resource注解新建对象是发现注入为null,告诉我 java.la
- 本文实例讲述了Android编程实现为ListView创建上下文菜单(ContextMenu)的方法。分享给大家供大家参考,具体如下:Con
- 本文简单介绍如何动态创建接口interface的实现实例对象,包含两个知识点:1.如何获取接口interface的所有实现实例对象?2.如何
- 标识符和关键字标识符读音 biao zhi fu什么是标识符包、类、变量、方法…等等,只要是起名的地方,那个名字就是标