And*_*rff 16 spring hystrix spring-cloud
有没有办法在Spring Boot应用程序中HystrixCommand使用@HystrixCommand注释时失败的原因?看起来如果您实现自己的HystrixCommand,您可以访问getFailedExecutionException但是如何在使用注释时访问它?我希望能够根据发生的异常类型在回退方法中执行不同的操作.这可能吗?
我看到一个音符有关HystrixRequestContext.initializeContext(),但HystrixRequestContext不给你访问什么,有没有使用上下文来获取访问例外不同的方式?
Mat*_*ttJ 46
只需在fallback方法中添加一个Throwable参数,它就会收到原始命令产生的异常.
来自https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica
@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");
}
Run Code Online (Sandbox Code Playgroud)
And*_*rff 13
我还没有找到一种方法来获取Annotations的异常,但创建我自己的Command对我来说是这样的:
public static class DemoCommand extends HystrixCommand<String> {
protected DemoCommand() {
super(HystrixCommandGroupKey.Factory.asKey("Demo"));
}
@Override
protected String run() throws Exception {
throw new RuntimeException("failed!");
}
@Override
protected String getFallback() {
System.out.println("Events (so far) in Fallback: " + getExecutionEvents());
return getFailedExecutionException().getMessage();
}
}
Run Code Online (Sandbox Code Playgroud)
希望这对其他人也有帮助.
| 归档时间: |
|
| 查看次数: |
21117 次 |
| 最近记录: |