L42*_*L42 13 java spring hystrix feign
这个外部呼叫我有一个假装客户端:
@RequestMapping(method = RequestMethod.GET, value = "GetResourceA", consumes = "application/json")
@Cacheable("ResourceA")
List<Stop> getResourceA() throws MyOwnException;
Run Code Online (Sandbox Code Playgroud)
在我的application.yml我有这个设置:
hystrix:
command:
default:
execution.isolation.thread.timeoutInMilliseconds: 1000
fallback.enabled: false
Run Code Online (Sandbox Code Playgroud)
现在,如果getResourceA超时,即完成时间超过一秒,我得到这个:
com.netflix.hystrix.exception.HystrixRuntimeException: getResourceA timed-out and no fallback available
Run Code Online (Sandbox Code Playgroud)
或者,如果我定义了一个我自己抛出异常的后备,我得到这个:
com.netflix.hystrix.exception.HystrixRuntimeException: getResourceA timed-out and fallback failed.
Run Code Online (Sandbox Code Playgroud)
我不能从后备中抛出自己的异常吗?
如果我希望在服务停止时抛出自己的异常怎么办?我想没有后备(因为我没有合理的价值从回退中返回),而是抛出我自己的错误,我可以抓住并让程序恢复.有人可以帮我解决这个问题吗?
Ben回答后更新:
所以我尝试了捕获HysterixRuntimeException并检查导致它的原因的方法,但最终得到了这个丑陋的代码:
try {
getResourceA();
} catch (HystrixRuntimeException e) {
if (e.getFailureType().name().equals("TIMEOUT")) {
throw new MyOwnException("Service timed out");
}
throw e;
}
Run Code Online (Sandbox Code Playgroud)
所有能够在超时时抛出MyOwnException的东西.肯定还有另一种方式吗?
您应该能够通过获取原因来获得从回退中抛出的异常HystrixRuntimeException
因此,要处理自定义异常,您可以这样做:
try {
getResourceA();
} catch (HystrixRuntimeException e) {
if (e.getCause() instanceof MyException) {
handleException((MyException)e.getCause());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2029 次 |
| 最近记录: |