gpt4 book ai didi

spring-cloud - 记录来自 hystrix 命令的错误

转载 作者:行者123 更新时间:2023-12-04 04:09:42 25 4
gpt4 key购买 nike

使用@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 时如何获得此异常吗?

最佳答案

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

它比预期的要冗长一些,但满足我的要求。

关于spring-cloud - 记录来自 hystrix 命令的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31274174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com