在项目中直接使用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
猜你喜欢
- 1.以G71列车为例,首先对车次站台进行占位编码(从1开始到最后一站递加)对以上占位简单描述以下:G71总共18个站点那么我们的单个座位的座
- 1.说明如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。该插件目前支持Oracle,Mysql,MariaD
- 简介在 Java 开发领域,热部署一直是一个难以解决的问题,目前的 Java 虚拟机只能实现方法体的修改热部署,对于整个类的结构修改,仍然需
- MD5,全称为 Message Digest Algorithm 5(消息摘要算法第五版).详情请参考 * :MD5MD5加密后是一个字
- 使用SwingWorker线程模式谨慎地使用并发机制对Swing开发人员来说非常重要。一个好的Swing程序使用并发机制来创建不会失去响应的
- 在Android Studio中对一个自己库进行生成操作时将会同时生成*.jar与*.aar文件。分别存储位置: &n
- 单例模式动机有时候只有一个类的实例是很重要的。比如,一个系统应该只有一个窗口管理实例。单例模式是最简单设计模式:类负责实例化自己,确保只有一
- 首先写一个测试文件然后点击IDEA右侧的maven,然后选择package,之后点击上面运行或者直接双击即可,等下方控制台构建成功即可:然后
- 【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1
- 题目题目要求思路:模拟用一个哈希表记录可出现的字母,然后逐一遍历每个单词每个字母,符合条件则结果加一。Javaclass Solution
- 前言Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在
- 1.搜索树的概念二叉搜索树是一种特殊的二叉树,又称二叉查找树,二叉排序树,它有几个特点:如果左子树存在,则左子树每个结点的值均小于根结点的值
- 线程可以划分优先级,优先级高的线程得到的CPU资源比较多,也就是CPU优先执行优先级高的线程对象中的任务。设置线程优先级有助于帮助线程规划器
- DeferredResult的超时处理,采用委托机制,也就是在实例DeferredResult时给予一个超时时长(毫秒),同时在onTime
- 参考:How to catch an Exception from a threadIs there a way to make Runna
- 我们首先看下BASEJDBC的写法实例:package com.dao;import java.sql.Connection;import
- 概述背景函数式编程的理论基础是阿隆佐·丘奇(Alonzo Church)于 1930 年代提出的 &lambd
- Oracle的jdbc驱动三种主要分类:1、JDBC OCI: oci是oracle call interface的缩写,此驱动类似于传统的
- 本文实例讲述了Java使用synchronized实现互斥锁功能。分享给大家供大家参考,具体如下:代码package per.thread;
- 本文主要给大家介绍java的InputStream 流的使用。(1)FileInputstream: 子类,读取数据的通道使用步骤:1.获取