gpt4 book ai didi

java - 当hystrix超时时,如何打印所花费的时间?

转载 作者:行者123 更新时间:2023-12-01 09:40:13 26 4
gpt4 key购买 nike

每当我们的代码抛出com.netflix.hystrix.exception.HystrixRuntimeException: MyClass timed-out andfallback failed.的异常时,我们总是必须重新猜测自己是否问题是 Hystrix 配置正确。我们可以回答这个问题的一个简单方法是,日志是否显示 Hystrix 线程在出错之前运行了多长时间。这样,如果它说 1000ms,我们就知道 Hystrix 没有正确配置(因为这是默认值),如果它说 5000ms,我们就知道它是按照我们想要的方式配置的。

最佳答案

你可以这样做:

command.getExecutionTimeInMilliseconds()

此方法告诉您 run 方法花费了多少秒,要捕获超时,您可以将其包装在命令类中的 try catch 中

 @Override
protected HttpResponse run() throws Exception {
try {
return executeSomeCall();
} catch (Exception e) {
System.out.println(getExecutionTimeInMilliseconds());
throw e;
}
}

或在调用时在其外部

try {
HttpResponse response = command.execute();
return response;
}
catch (HystrixRuntimeException e) {
command.getExecutionTimeInMilliseconds() // do something with the info
}

只有一点,如果你使用Threads策略是可以的,但是如果你使用Semaphor,只有在得到响应后才会发生超时。即使你的超时是 1000 毫秒,而请求花费了 5000 毫秒。 hystryx 将等待 5000 毫秒给您一个超时错误,并且 getExecutionTimeInMilliseconds 将返回 1000 毫秒给您。

关于java - 当hystrix超时时,如何打印所花费的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38515439/

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