java - HystrixCommand 和 HystrixObservableCommand 的区别

标签 java hystrix

我想了解 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 类可能会更简洁。

如果您只使用非阻塞调用,您应该使用 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/

相关文章:

performance - Hystrix 性能开销

java - 如何对 javanica @HystrixCommand 注释方法进行单元测试?

spring-boot - com.netflix.zuul.exception.ZuulException : Hystrix Readed time out

spring-cloud - 没有 Eureka 的涡轮仪表板指标

java - 如果找不到元素,我该如何继续我的脚本?

java - Spring MVC 拦截器只拦截特定的映射吗?

java - hibernate 。保留对最后一个 child 的引用

java - 为什么 Spring MVC 4 和 Hystrix 集成在没有 Spring boot API 的情况下无法工作?

java - 如何在svn中查找文件夹内的文件数

java - 类型级别的 Spring Security @PreAuthorize 不能在方法级别上覆盖