HystrixCommand 和 HystrixObservableCommand 的区别

Difference between HystrixCommand and HystrixObservableCommand

我正在尝试了解 HystrixCommand 和 HystrixObservableCommand 之间的区别。我感到困惑的原因是 HysterixCommand 也有一个 observe() 或 toObservable() 方法,它们分别发出热和冷的 observable。那么创建 HystrixObservableCommand 的必要性是什么。如果我将完全致力于非阻塞调用,我应该使用哪一个?为什么?

来自 Javadocs:

HystrixCommand

This command is essentially a blocking command but provides an Observable facade if used with observe()

HystrixObservableCommand

This command should be used for a purely non-blocking call pattern. The caller of this command will be subscribed to the Observable returned by the run() method.

区别在于 HystrixCommand 默认支持阻塞范式,但也通过外观通过 Observable 提供非阻塞行为,而 HystrixObservableCommand 是专门为非阻塞设置实现的。我不完全确定为什么它被分成两个实现,但我猜原因是因为最初 HystrixCommand 不支持非阻塞。它是在 a year or so after the original implementation 左右添加的。编写一个纯粹的非阻塞 hystrix class.

可能会更干净

如果您只处理非阻塞调用,您可能应该使用 HystrixObservableCommand。 Hystrix 开发者之一 Ben Christensen 在 this post:

中对此进行了很好的总结

However, if you are wrapping blocking calls, you should just stick with using HystrixCommand as that’s what it’s built for and it defaults to running everything in a separate thread. Using HystrixCommand.observe() will give you the concurrent, async composition you’re looking for.

HystrixObservableCommand is intended for wrapping around async, non-blocking Observables that don’t need extra threads.

除了 Nick Defazio 的回答之外,HystrixObservableCommand 包装 Observables 的实现可以发射多个项目,而 HystrixCommand 永远不会发射超过一个项目,即使在调用 observe().toObservable() 仅包装由 run() 方法 .

返回的单个值