摘要: 原创出处 http://www.iocoder.cn/Hystrix/command-execute-third-timeout/ 「芋道源码」欢迎转载,保留摘要,谢谢!
本文主要基于 Hystrix 1.5.X 版本
������关注微信公众号:【芋道源码】有福利:
1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
3. 您对于源码的疑问每条留言都将得到认真回复。甚至不知道如何读源码也可以请教噢。
4. 新的源码解析文章实时收到通知。每周更新一篇左右。
5. 认真的源码交流微信群。
1. 概述
本文主要分享 Hystrix 命令执行(三)之执行超时。
建议 :对 RxJava 已经有一定的了解的基础上阅读本文。
开启执行超时功能,需要配置 :
HystrixCommandProperties.executionTimeoutEnabled
:执行命令超时功能开关。
- 值 :Boolean
- 默认值 :
true
HystrixCommandProperties.executionTimeoutInMilliseconds
:执行命令超时时长。
- 值 :Integer
- 单位 :毫秒
- 默认值 :1000 毫秒
在 《Hystrix 源码解析 —— 命令执行(一)之正常执行逻辑》「4. #executeCommandAndObserve(…)」 中,#executeCommandAndObserve(...)
方法的第 75 行 lift(new HystrixObservableTimeoutOperator<R>(_cmd))
,实现了对执行命令超时的监控。
- 对
Observable#lift(Operator)
方法不熟悉的同学,在 《RxJava 源码解析 —— Observable#lift(Operator)》 有详细解析。
推荐 Spring Cloud 书籍:
- 请支持正版。下载盗版,等于主动编写低级 BUG 。
- 程序猿DD —— 《Spring Cloud微服务实战》
- 周立 —— 《Spring Cloud与Docker微服务架构实战》
- 两书齐买,京东包邮。
2. HystrixObservableTimeoutOperator
HystrixObservableTimeoutOperator 类,代码如下 :
1: private static class HystrixObservableTimeoutOperator<R> implements Operator<R, R> {
2:
3: final AbstractCommand<R> originalCommand;
4:
5: public HystrixObservableTimeoutOperator(final AbstractCommand<R> originalCommand) {
6: this.originalCommand = originalCommand;
7: }
8:
9: @Override
10: public Subscriber<? super R> call(final Subscriber<? super R> child) {
11: // 创建 订阅
12: final CompositeSubscription s = new CompositeSubscription();
13: // 添加 订阅
14: // if the child unsubscribes we unsubscribe our parent as well
15: child.add(s);
16:
17: //capture the HystrixRequestContext upfront so that we can use it in the timeout thread later
18: final HystrixRequestContext hystrixRequestContext = HystrixRequestContext.getContextForCurrentThread();
19:
20: TimerListener listener = new TimerListener() {
21:
22: @Override
23: public void tick() {
24: // if we can go from NOT_EXECUTED to TIMED_OUT then we do the timeout codepath
25: