4

使用 @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 时如何得到这个异常吗?

4

1 回答 1

3

我在 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();
}

它比预期的要详细一些,但满足我的要求。

于 2015-07-08T09:08:00.347 回答