我想了解 HystrixCommand 和 HystrixObservableCommand 之间的区别。我感到困惑的原因是 HysterixCommand 也有一个 observe() 或 toObservable() 方法,它们分别发出热和冷的 observable。那么创建 HystrixObservableCommand 的必要性是什么?如果我将完全致力于非阻塞调用,我应该使用哪一个?为什么?
最佳答案
来自 Javadocs:
This command is essentially a blocking command but provides an Observable facade if used with observe()
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 类可能会更简洁。
如果您只使用非阻塞调用,您应该使用 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.
关于java - HystrixCommand 和 HystrixObservableCommand 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569560/