Hystrix 调用 fallbackMethod 即使后端 API 被执行

Hystrix Calls the fallbackMethod even when backend API gets executed

我正在尝试将 Hystrix 实施到我们的微服务项目中。为了让问题简洁明了,我将描述以下场景:

(a) 调用有时执行缓慢的后端服务(例如支付服务)

(b) 我已经用@Hystrix 注释了该方法(从我调用支付服务的地方)。另外,我已经实现了相应的回退方法。

(c) 下面是相同的代码片段。

@HystrixCommand(fallbackMethod = "fallbackProcessPayment")
public String processPayment(User user) throws Exception
{  
    // Call the payment service (which is slow in nature) to process the payment for the user....
}

 public String fallbackProcessPayment(User user){       
    // This is the fallback method for processPayment....
    // Gracefully handle the processPayment 
}

In the config.properties file timeout is configured as 
hystrix.command.getUseCase1.execution.isolation.thread.timeoutInMilliseconds=2000

当前行为 - 从 processPayment(..) 方法调用后端支付服务后,它比我在 hystrix.command 中设置的时间长(~ 4000 毫秒)。getUseCase1.execution.isolation.thread.timeoutInMilliseconds(2000 毫秒) 因此 Hystrix 调用 fallbackProcessPayment (...) 但我还看到后端支付服务也得到执行,尽管速度很慢。 这是不受欢迎的行为,因为付款正在后台处理,因为我还通知用户(通过回退方法)我们无法处理付款(因为调用超时,因为 paymentService 需要 4 秒才能响应,而 Hystrix 预期2 秒内响应(基于 timeoutInMilliseconds 配置)。

是否缺少任何配置以使其正常工作???

对此的任何指示都会有很大帮助。

感谢您的宝贵时间

嗯。这是 hystrix 的预期行为。你有几个选择。
1.要么增加超时
2. 在你的回退方法中检查方法失败的原因。即在哪个异常上。 (您可以通过向 Throwable 类型的回退方法添加一个参数来了解这一点,该方法将触发触发回退方法的异常)。如果失败是由于超时,那么您可以编写一段代码来检查前一个过程是否完成,然后再返回响应。

但是第二种方法不可行,因为,如果你将阈值设置为5,如果连续5次请求超时失败,那么第6次请求将直接进入你的回退方法。检查之前的过程是否完成没有意义。