我正在尝试使用新的 SpringBoot 2 Reactive WebClient 类(它没有批处理端点)对相同的休息服务进行并行(批处理)调用。例如,我需要 100 个“评论”对象(ID 为 1...100),并且我正在执行以下并行调用:
List<Mono<Comment>> monos = ids.stream()
.map(id -> webClient.get()
.uri("/comments/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Comment.class))
.collect(Collectors.toList());
return Flux.merge(monos);
我是 Spring WebFlux 的新手,我不确定这是使用 WebClient 进行并行调用的正确方法
Monos的通量连接)?
ThreadPoolExecutor ...我应该使用类似的概念吗?
网络客户端? ...是否有类似的东西 reactive ?
问候
完整源码可绑定(bind):https://github.com/fdlessard/SpringBootReactiveComment
最佳答案
Flux.fromIterable(ids)
.flatMap(id -> webClient.get()
.uri("/comments/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Comment.class))
.subscribeOn(Schedulers.parallel());
关于spring-mvc - SpringBoot WebFlux - 发出并行 WebClient 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51118848/