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 で注釈が付けられたすべてのメソッドでスローされたランタイム例外を (中央クラスで) ログに記録するにはどうすればよいですか?

バニラ hystrix-javanica ではなく、spring-cloud-netflix を使用しています。アプリケーションに実装する必要がある 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 に答える