有了@HystrixCommand注解,可以配置一个回退方法,该方法应该在方法失败的情况下运行。
public Link defaultDogeLink(Account account) {
return null;
}
@HystrixCommand(fallbackMethod = "defaultDogeLink")
public Link buildDogeLink(Account account) {
// some code that may throw Runtime Exceptions
} 我应该怎么做才能(在一个中心类中)记录所有使用@HystrixCommand注解的方法中抛出的运行时异常?
我使用的是spring-cloud-netflix而不是vanilla hystrix-javanica。我正在寻找一些类似于org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler类的东西(用于Spring的@Async),我需要在我的应用程序中实现。
在hystrix-core中,HystrixCommand类具有方法getFailedExecutionException(),可以在回退方法中使用该方法来记录异常。有人能告诉我在使用hystrix-javanica时如何获得这个异常吗?
发布于 2015-07-08 17:08:00
我在hystrix-javanica的单元测试中找到了获取最后执行的hystrix命令的方法:
public Link defaultDogeLink(Account account) {
LOG.warn("exception occured while building doge link for account " + account, getCommand().getFailedExecutionException());
return null;
}
@HystrixCommand(fallbackMethod = "defaultDogeLink")
public Link buildDogeLink(Account account) {
// some code that may throw Runtime Exceptions
}
private com.netflix.hystrix.HystrixInvokableInfo<?> getCommand() {
return HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
}它比预期的有点冗长,但满足了我的要求。
https://stackoverflow.com/questions/31274174
复制相似问题