java 如何使用 hystrix 重试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46847930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:22:49  来源:igfitidea点击:

How to retry with hystrix

javahystrixretry-logic

提问by evgeniy44

I have a hystrix command that encapsulates a REST call. In case of failure(e.g. timeout) I want to make a single retry and in case of failure return an appropriate error.

我有一个封装了 REST 调用的 hystrix 命令。如果失败(例如超时),我想进行一次重试,并在失败的情况下返回适当的错误。

As I can see Hystrix doesn't support retries. The only way to do it with Hystrix is to put the main logic into getFallback() method. But it doesn't look to be correct.

正如我所见,Hystrix 不支持重试。使用 Hystrix 做到这一点的唯一方法是将主要逻辑放入 getFallback() 方法中。但它看起来并不正确。

So, what is a proper way to implement timeout with hystrix?

那么,使用 hystrix 实现超时的正确方法是什么?

回答by meistermeier

Hystrix itself does not care what kind of command gets wrapped by it and it does not support the idea of retries. Example behind the idea: If your command (that wraps a REST request) is parametrised it could be that some resource endpoints should be retried while others not. It won't be nice to have either two commands that do more or less the same nor a technical parameter to activate a retry. Additionally this will add some extra complexity to the project.

Hystrix 本身并不关心它包装了什么样的命令,也不支持重试的想法。想法背后的示例:如果您的命令(包装 REST 请求)已参数化,则可能是某些资源端点应该重试,而其他资源端点则不应该重试。如果有两个执行或多或少相同的命令或激活重试的技术参数,这将不会很好。此外,这会给项目增加一些额外的复杂性。

To get around this problem and stick with Hystrix you might want to take a look into SpringRetryif you are working on a Spring application.

为了解决这个问题并坚持使用 Hystrix,如果您正在处理 Spring 应用程序,您可能需要查看SpringRetry

Another possible solution is resilience4jwhich could be seen as a combination of Hystrix and SpringRetry in this context.

另一种可能的解决方案是resilience4j,在这种情况下,它可以被视为Hystrix 和SpringRetry 的组合。