在@HystrixCommand 回退方法中获取失败异常
Get failure exception in @HystrixCommand fallback method
在 Spring 引导应用程序中使用 @HystrixCommand
注释时,有没有办法找出 HystrixCommand
失败的原因?看起来如果您实现自己的 HystrixCommand
,您可以访问 getFailedExecutionException
,但是在使用注释时如何访问它?我希望能够根据发生的异常类型在回退方法中做不同的事情。这可能吗?
我看到了一个关于 HystrixRequestContext.initializeContext()
的 但是 HystrixRequestContext
没有让你访问任何东西,有没有不同的方法来使用那个上下文来访问异常?
我找不到通过注释获取异常的方法,但我找到了 HystrixPlugins
,你可以注册一个 HystrixCommandExecutionHook
并且你可以获得确切的异常这:
HystrixPlugins.getInstance().registerCommandExecutionHook(new HystrixCommandExecutionHook() {
@Override
public <T> void onFallbackStart(final HystrixInvokable<T> commandInstance) {
}
});
命令实例是GenericCommand
。
我也没有找到一种方法来获取注释的异常,但是创建我自己的命令对我来说是这样的:
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();
}
}
希望这对其他人也有帮助。
只需在回退方法中添加一个 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");
}
大多数时候只使用 getFailedExecutionException().getMessage() 就给我空值。
Exception errorFromThrowable = getExceptionFromThrowable(getExecutionException());
String errMessage = (errorFromThrowable != null) ? errorFromThrowable.getMessage()
这总是让我获得更好的结果。
如文档中所述 Hystrix-documentation getFallback()
方法将在以下情况下抛出:
- 每当命令执行失败时:当 construct() 或 运行() 抛出异常时
- 当指令因开路而短路时
- 当命令的线程池和队列或信号量达到容量时
- 当命令超过其超时长度时。
因此,您可以通过将执行异常分配给 Throwable 对象.
来轻松获取调用后备方法的原因
假设你的 HystrixCommand returns 一个字符串
public class ExampleTask extends HystrixCommand<String> {
//Your class body
}
进行如下操作:
@Override
protected ErrorCodes getFallback() {
Throwable t = getExecutionException();
if (circuitBreaker.isOpen()) {
// Log or something
} else if (t instanceof RejectedExecutionException) {
// Log and get the threadpool name, could be useful
} else {
// Maybe something else happened
}
return "A default String"; // Avoid using any HTTP request or ypu will need to wrap it also in HystrixCommand
}
更多信息here
在 Spring 引导应用程序中使用 @HystrixCommand
注释时,有没有办法找出 HystrixCommand
失败的原因?看起来如果您实现自己的 HystrixCommand
,您可以访问 getFailedExecutionException
,但是在使用注释时如何访问它?我希望能够根据发生的异常类型在回退方法中做不同的事情。这可能吗?
我看到了一个关于 HystrixRequestContext.initializeContext()
的 HystrixRequestContext
没有让你访问任何东西,有没有不同的方法来使用那个上下文来访问异常?
我找不到通过注释获取异常的方法,但我找到了 HystrixPlugins
,你可以注册一个 HystrixCommandExecutionHook
并且你可以获得确切的异常这:
HystrixPlugins.getInstance().registerCommandExecutionHook(new HystrixCommandExecutionHook() {
@Override
public <T> void onFallbackStart(final HystrixInvokable<T> commandInstance) {
}
});
命令实例是GenericCommand
。
我也没有找到一种方法来获取注释的异常,但是创建我自己的命令对我来说是这样的:
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();
}
}
希望这对其他人也有帮助。
只需在回退方法中添加一个 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");
}
大多数时候只使用 getFailedExecutionException().getMessage() 就给我空值。
Exception errorFromThrowable = getExceptionFromThrowable(getExecutionException());
String errMessage = (errorFromThrowable != null) ? errorFromThrowable.getMessage()
这总是让我获得更好的结果。
如文档中所述 Hystrix-documentation getFallback()
方法将在以下情况下抛出:
- 每当命令执行失败时:当 construct() 或 运行() 抛出异常时
- 当指令因开路而短路时
- 当命令的线程池和队列或信号量达到容量时
- 当命令超过其超时长度时。
因此,您可以通过将执行异常分配给 Throwable 对象.
来轻松获取调用后备方法的原因假设你的 HystrixCommand returns 一个字符串
public class ExampleTask extends HystrixCommand<String> {
//Your class body
}
进行如下操作:
@Override
protected ErrorCodes getFallback() {
Throwable t = getExecutionException();
if (circuitBreaker.isOpen()) {
// Log or something
} else if (t instanceof RejectedExecutionException) {
// Log and get the threadpool name, could be useful
} else {
// Maybe something else happened
}
return "A default String"; // Avoid using any HTTP request or ypu will need to wrap it also in HystrixCommand
}
更多信息here