博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hystrix源码小贴士之调用异常处理
阅读量:5287 次
发布时间:2019-06-14

本文共 5669 字,大约阅读时间需要 18 分钟。

  executeCommandAndObserve方法处理onerror异常。

return execution.doOnNext(markEmits)                .doOnCompleted(markOnCompleted)                .onErrorResumeNext(handleFallback)                .doOnEach(setRequestContext);

  handleFallback方法处理执行过程中的各种异常

final Func1
> handleFallback = new Func1
>() { @Override public Observable
call(Throwable t) { Exception e = getExceptionFromThrowable(t); executionResult = executionResult.setExecutionException(e); if (e instanceof RejectedExecutionException) { return handleThreadPoolRejectionViaFallback(e); } else if (t instanceof HystrixTimeoutException) { return handleTimeoutViaFallback(); } else if (t instanceof HystrixBadRequestException) { return handleBadRequestByEmittingError(e); } else { /* * Treat HystrixBadRequestException from ExecutionHook like a plain HystrixBadRequestException. */ if (e instanceof HystrixBadRequestException) { eventNotifier.markEvent(HystrixEventType.BAD_REQUEST, commandKey); return Observable.error(e); } return handleFailureViaFallback(e); } } };

   handleThreadPoolRejectionViaFallback、handleTimeoutViaFallback、handleBadRequestByEmittingError、handleFailureViaFallback最终都会调用getFallbackOrThrowException来处理各种异常。

  getFallbackOrThrowException方法执行fallback方法并返回结果,如果执行过程中异常,返回异常信息。

private Observable
getFallbackOrThrowException(final AbstractCommand
_cmd, final HystrixEventType eventType, final FailureType failureType, final String message, final Exception originalException) { ... Observable
fallbackExecutionChain; // acquire a permit if (fallbackSemaphore.tryAcquire()) { try { if (isFallbackUserDefined()) { executionHook.onFallbackStart(this); fallbackExecutionChain = getFallbackObservable(); } else { //same logic as above without the hook invocation fallbackExecutionChain = getFallbackObservable(); } } catch (Throwable ex) { //If hook or user-fallback throws, then use that as the result of the fallback lookup fallbackExecutionChain = Observable.error(ex); } return fallbackExecutionChain .doOnEach(setRequestContext) .lift(new FallbackHookApplication(_cmd)) .doOnNext(markFallbackEmit) .doOnCompleted(markFallbackCompleted) .onErrorResumeNext(handleFallbackError) .doOnTerminate(singleSemaphoreRelease) .doOnUnsubscribe(singleSemaphoreRelease); } else { return handleFallbackRejectionByEmittingError(); } } else { return handleFallbackDisabledByEmittingError(originalException, failureType, message); } } }

  handleFallbackError方法,返回异常

final Func1
> handleFallbackError = new Func1
>() { @Override public Observable
call(Throwable t) { Exception e = originalException; Exception fe = getExceptionFromThrowable(t); if (fe instanceof UnsupportedOperationException) { long latency = System.currentTimeMillis() - executionResult.getStartTimestamp(); logger.debug("No fallback for HystrixCommand. ", fe); // debug only since we're throwing the exception and someone higher will do something with it eventNotifier.markEvent(HystrixEventType.FALLBACK_MISSING, commandKey); executionResult = executionResult.addEvent((int) latency, HystrixEventType.FALLBACK_MISSING); /* executionHook for all errors */ e = wrapWithOnErrorHook(failureType, e); return Observable.error(new HystrixRuntimeException(failureType, _cmd.getClass(), getLogMessagePrefix() + " " + message + " and no fallback available.", e, fe)); } else { long latency = System.currentTimeMillis() - executionResult.getStartTimestamp(); logger.debug("HystrixCommand execution " + failureType.name() + " and fallback failed.", fe); eventNotifier.markEvent(HystrixEventType.FALLBACK_FAILURE, commandKey); executionResult = executionResult.addEvent((int) latency, HystrixEventType.FALLBACK_FAILURE); /* executionHook for all errors */ e = wrapWithOnErrorHook(failureType, e); return Observable.error(new HystrixRuntimeException(failureType, _cmd.getClass(), getLogMessagePrefix() + " " + message + " and fallback failed.", e, fe)); } } };

 

转载于:https://www.cnblogs.com/zhangwanhua/p/8056956.html

你可能感兴趣的文章
JavaScript 输出
查看>>
python 函数(2)
查看>>
Python学习笔记1:python简介、输入输出、循环条件
查看>>
python学习笔记5:装饰器
查看>>
Android 开发环境配置
查看>>
skiing
查看>>
wxwidgets demo
查看>>
dubbo 实战总结
查看>>
bzoj1230 [Usaco2008 Nov]lites 开关灯
查看>>
Modulation of Lipid Metabolism by Celastrol (文献分享一组-赵倩倩)
查看>>
HDU 1044 Collect More Jewels(BFS+DFS)
查看>>
TrackbarCallback 回调函数必须为 void(int,void*)
查看>>
【BZOJ1857】[Scoi2010]传送带 三分法
查看>>
JPA与Spring2.5整合时发生不能创建entityManagerFactory的问题解决方法
查看>>
FastDFS 初始
查看>>
选项卡
查看>>
14-----定时器
查看>>
XidianOJ 1028 数字工程
查看>>
派遣函数
查看>>
教程6--配置ssh
查看>>