我正在尝试构建一个需要一些并发性的应用程序,因为吞吐量很重要。
步骤可以总结如下:
AccountCollector1 -> (AccountRestService1, AccountRestService2) -> 返回列表
AccountCollector2 -> (AccountRestService3, AccountRestService4) -> 返回列表
理想情况下,AccountCollector 中的调用应该是并发的。它应该发送请求并等待直到两者都返回,然后对结果进行一些处理并通知等待结果的人
理想情况下,AccountCollector 也应该并行运行,它们不相互依赖。
因此有两个级别的并发,AccountCollector 并行运行,AccountRestServices 在每个 AccountCollector 中并行运行。
我正在为此探索最佳实现。
我开始使用 Spring Webflux,以便 AccountRestService 返回 Mono。
我认为 RxJava 将是理想的,但我未能找到一种方法来合并结果 合并一直等到所有 REST 客户端都返回 Mono 或至少超时/失败
所以我继续使用 ExecutorService(下面的伪代码)实现了并行性。 我也使用 ExecutorService 来实现 AccountCollector 之间的并行
我的问题如下:
抱歉问了这么冗长的问题,我很乐意提供更多详细信息。困扰我的主要事情是我从 WebFlux 开始,现在我觉得我失去了它给我的任何优势。
谢谢!
public interface AccountRestService {
Mono<UserAccount> fetchUserAccount();
}
public class AccountCollector {
public List<UserAccount> collect() {
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
CompletionService<List<UserAccount> pool = new ExecutorCompletionService<>(executor);
///submit to pool two rest clients
// get from pool, collect
}
}