spring-mvc - SpringBoot WebFlux - 发出并行 WebClient 请求

标签 spring-mvc spring-boot spring-webflux

我正在尝试使用新的 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的通量连接)?
  • 另外,当我这样做时,我使用了旧的已弃用的 AsyncRestTemplate
    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/

    相关文章:

    spring - 如何在 Spring-Web 中使用 RestTemplate 解析 gzip 编码的响应

    使用 jdbc 模板 Autowiring 数据源问题的 Spring Boot 自动配置

    带有 WebFlux 的 Spring Boot 总是在测试中抛出 403 状态

    java - Spring 5 - 如何提供静态资源

    java - Spring MVC/Thymeleaf - 如何使用一个输入字段插入两个不同的表?

    java - 部署到 Heroku 时无法找到并加载主类 HomeController?

    java - Spring MVC 文件上传将 null 传递给 Controller

    spring-boot - gradle - spring boot 无法识别 - 无法为任务 'archiveFileName' 设置未知属性 ':bootJar'

    java - 在 Spring Boot 2 中使用实体管理器时,没有类型 'javax.persistence.EntityManager' 的合格 bean

    spring-boot - 在内存数据库中使用时出现 R2dbc H2 问题