Hystrix使用Commond的三种方式
目录(?)[-]
- 1 依赖引入
- 2 使用
- 21 Hystrix command
- 211 同步执行
- 212 异步执行
- 213 反应执行
- 214 三种模式使用区别
- 22 Fallback
- 23 Error Propagation
- 24 Configuration
- 21 Hystrix command
1. 依赖引入
<properties>
<hystrix-version> 1.4 . 22 </hystrix-version>
</properties>
<dependencies>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>${hystrix-version}</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>
<version>${hystrix-version}</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>${hystrix-version}</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-servo-metrics-publisher</artifactId>
<version>${hystrix-version}</version>
</dependency>
</dependencies>
|
applicationContext.xml:
<aop:aspectj-autoproxy/>
<bean id= "hystrixAspect" class = "com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />
|
web.xml:
<servlet>
<display-name>HystrixMetricsStreamServlet</display-name>
<servlet-name>HystrixMetricsStreamServlet</servlet-name>
<servlet- class >com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet- class >
</servlet>
<servlet-mapping>
<servlet-name>HystrixMetricsStreamServlet</servlet-name>
<url-pattern>/hystrix.stream</url-pattern>
</servlet-mapping>
|
jmonitor:
collector=jvm,appdata,http
|
2. 使用
这里只讲注解的使用方式以及比较重要的部分,如果需要了解全部查看:https://github.com/Netflix/Hystrix/wiki/How-To-Use
2.1 Hystrix command
2.1.1 同步执行
public class UserService {
...
@HystrixCommand
public User getUserById(String id) {
return userResource.getUserById(id);
}
}
|
2.1.2 异步执行
public class UserService {
...
@HystrixCommand
public Future<User> getUserByIdAsync( final String id) {
return new AsyncResult<User>() {
@Override
public User invoke() {
return userResource.getUserById(id);
}
};
}
}
|
2.1.3 反应执行()
@HystrixCommand
public Observable<User> getUserById( final String id) {
return Observable.create( new Observable.OnSubscribe<User>() {
@Override
public void call(Subscriber<? super User> observer) {
try {
if (!observer.isUnsubscribed()) {
observer.onNext( userResource.getUserById(id));
observer.onCompleted();
}
} catch (Exception e) {
observer.onError(e);
}
}
});
}
}
|
2.1.4 三种模式使用区别
- 同步执行:当执行到注解方法时,程序会顺序执行。
- 异步执行:当执行到注解方法时,会并发异步执行,返回一个Future对象,后面使用.get()方法来阻塞拿到结果。如果有多个方法时,执行时间就是其中最长的一个服务的执行时间。
- 反应执行:当执行到注解方法时,返回一个观察者。支持EAGER和LAZY模式。和同步异步执行的区别是,当对多个方法之间的返回结果不需要做合并而是希望当多个方法返回时触发一些事件时比较适合使用该模式。
反应执行没太明白,如果需要了解可以先参考下这个https://mcxiaoke.gitbooks.io/rxdocs/content/Intro.html
2.2 Fallback
@HystrixCommand (fallbackMethod = "fallback1" )
User getUserById(String id) {
throw new RuntimeException( "getUserById command failed" );
}
@HystrixCommand (fallbackMethod = "fallback2" )
User fallback1(String id, Throwable e) {
assert "getUserById command failed" .equals(e.getMessage());
throw new RuntimeException( "fallback1 failed" );
}
@HystrixCommand (fallbackMethod = "fallback3" )
User fallback2(String id) {
throw new RuntimeException( "fallback2 failed" );
}
|
注意点:
- fallback应该和注解方法在同一类下
- fallback的返回值和参数列表应该和注解方法一致,如果需要异常,则在末尾添加Throwable参数,对访问修饰符无要求
- fallback方法上可以继续添加fallback
command和fallback只支持以下几种组合:
- sync command, sync fallback
- async command, sync fallback
- async command, async fallback
2.3 Error Propagation
@HystrixCommand (ignoreExceptions = {BadRequestException. class })
public User getUserById(String id) {
return userResource.getUserById(id);
}
|
当遇到BadRequestException时不会进入fallback,而是直接抛出异常
2.4 Configuration
@HystrixCommand (groupKey= "UserGroup" , commandKey = "GetUserByIdCommand" ,
commandProperties = {
@HystrixProperty (name = "execution.isolation.thread.timeoutInMilliseconds" , value = "500" )
},
threadPoolProperties = {
@HystrixProperty (name = "coreSize" , value = "30" ),
@HystrixProperty (name = "maxQueueSize" , value = "101" ),
@HystrixProperty (name = "keepAliveTimeMinutes" , value = "2" ),
@HystrixProperty (name = "queueSizeRejectionThreshold" , value = "15" ),
@HystrixProperty (name = "metrics.rollingStats.numBuckets" , value = "12" ),
@HystrixProperty (name = "metrics.rollingStats.timeInMilliseconds" , value = "1440" )
})
|
参数
|
作用
|
备注
|
---|---|---|
groupKey |
表示所属的group,一个group共用线程池 |
默认值:getClass().getSimpleName(); |
commandKey |
默认值:当前执行方法名 | |
execution.isolation.strategy |
隔离策略,有THREAD和SEMAPHORE |
默认使用THREAD模式,以下几种可以使用SEMAPHORE模式:
|
execution.isolation.thread.timeoutInMilliseconds |
超时时间 |
默认值:1000 在THREAD模式下,达到超时时间,可以中断 在SEMAPHORE模式下,会等待执行完成后,再去判断是否超时 设置标准: 有retry,99meantime+avg meantime 没有retry,99.5meantime |
execution.timeout.enabled |
是否打开超时 | |
execution.isolation.thread.interruptOnTimeout |
是否打开超时线程中断 | THREAD模式有效 |
execution.isolation.semaphore.maxConcurrentRequests |
信号量最大并发度 | SEMAPHORE模式有效,默认值:10 |
fallback.isolation.semaphore.maxConcurrentRequests |
fallback最大并发度 | 默认值:10 |
circuitBreaker.requestVolumeThreshold |
熔断触发的最小个数/10s | 默认值:20 |
circuitBreaker.sleepWindowInMilliseconds |
熔断多少秒后去尝试请求 | 默认值:5000 |
circuitBreaker.errorThresholdPercentage |
失败率达到多少百分比后熔断 |
默认值:50 主要根据依赖重要性进行调整 |
circuitBreaker.forceClosed |
是否强制关闭熔断 | 如果是强依赖,应该设置为true |
coreSize |
线程池coreSize |
默认值:10 设置标准:qps*99meantime+breathing room |
maxQueueSize |
请求等待队列 |
默认值:-1 如果使用正数,队列将从SynchronizeQueue改为LinkedBlockingQueue |
Hystrix使用Commond的三种方式相关推荐
- 远程控制virtual box虚拟机系统的三种方式
远程控制virtual box虚拟机系统的三种方式[阅读时间:5分钟] 1.使用现有的远程控制软件 2.使用SSH连接 3.使用远程桌面控制虚拟机系统 对于把虚拟机安装在寝室笔记本的人来说,能够远程控 ...
- 命令行运行Python脚本时传入参数的三种方式
三种常用的方式 如果在运行python脚本时需要传入一些参数,例如gpus与batch_size,可以使用如下三种方式. python script.py 0,1,2 10 python script ...
- sass文件编译的三种方式【舒】
[舒:]sass文件编译的三种方式 方式一: 1.webstorm打开evtGulp项目或者mcake-activity项目 2.中,开启gulp->default/develop,启动watc ...
- java多线程w3c_Java创建多线程的三种方式
前言 这篇文章主要讲述线程的概念.组成.Java创建多线程的三种方式以及线程的类型. 线程概念 线程和进程的区别 **进程:**正在运行的程序,例如:你打开 的qq音乐.exe程序,其由PCB(进程控 ...
- vue 函数 路由跳转_vue中通过路由跳转的三种方式
router-view 实现路由内容的地方,引入组件时写到需要引入的地方 需要注意的是,使用vue-router控制路由则必须router-view作为容器. 通过路由跳转的三种方式 1.router ...
- linux+Qt 下利用D-Bus进行进程间高效通信的三种方式
linux+Qt 下利用D-Bus进行进程间高效通信的三种方式 原文链接: https://www.cnblogs.com/wwang/archive/2010/10/27/1862552.html ...
- ios 应用和电脑共享文件夹_堪比AirDrop,苹果 iPhone与Windows电脑互传文件的三种方式...
如果你是苹果全家桶用户,一定会对 「AirDrop(隔空投送)」 功能赞誉有加,使用 AirDrop 可以在 iPhone 与 MacBook.iPad 等设备之间快速传递照片.视频或文件. 遗憾的是 ...
- discard connection丢失数据_python kafka 生产者发送数据的三种方式
python kafka 生产者发送数据的三种方式 发送方式 同步发送 发送数据耗时最长 有发送数据的状态,不会丢失数据,数据可靠性高 以同步的方式发送消息时,一条一条的发送,对每条消息返回的结果判断 ...
- 时序预测的三种方式:统计学模型、机器学习、循环神经网络
作者 | luanhz 来源 | 小数志 导读 时序预测是一类经典的问题,在学术界和工业界都有着广泛的研究和应用.甚至说,世间万物加上时间维度后都可抽象为时间序列问题,例如股票价格.天气变化等等.关于 ...
最新文章
- ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!
- w7设置双显示器_win7怎么用双显示器,如何设置???
- Python的Crypto模块使用:自动输入Shell中的密码
- OpenCV平滑图像Smoothing Images
- AIX系统Memory性能评估
- 为什么要有周考?周考是用来干什么的?
- leetcode78. 子集(回溯)
- php5.1.4,apache 2.2.2 + PHP5.1.4 不能运行的解_php
- 互联网日报 | 5月3日 星期一 | 京东物流通过港交所上市聆讯;理想汽车累计交付破5万辆;拼多多年活跃商户达860万
- asp.net 类似 webbrowser_一款类似经典的FC的像素风忍者游戏
- [浪风JQuery开发]jquery最有意思的IFrame类似应用--值得深入研究
- modelandview为null的原因_一千个不用 Null 的理由!
- 23. 线程如何退出结束
- html div 画半圆,css画变形的半圆
- java 包装类详解_解析包装类
- 100个必会的python脚本-python100例,python经典例题
- mysql超市管理系统怎么写_求php+mysql的小型超市管理系统
- jclasslib 查看类信息
- DDNS 通过阿里云API接口实现 DDNS 动态解析
- 简单的路由器设置下一条(小白像)
热门文章
- 走进 Facebook POP 的世界
- C# WinForm 弹出模式窗口操作滚动条
- 无标题窗体的移动及其简单美化
- CCF CSP 201612-1中位数(满分代码)
- 定义结构体的时候,里面的变量可以同时初始化吗?(不可以)
- 阿里云Kubernetes服务 - Service Broker快速入门指南
- 数据绑定(十)Binding的数据转换
- UITableView中selectRowAtIndexPath: animated:scrollPosition滚动到指定Row
- Distributed Systems-leader based分布式一致性协议
- 推断股票强弱最有效的一个方法