【问题标题】:Java: consuming a Spring WebFlux application from a Play Framework applicationJava:从 Play Framework 应用程序中使用 Spring WebFlux 应用程序
【发布时间】:2019-09-29 09:04:58
【问题描述】:

我有一些关于通过 HTTP 从 Play Framework 应用程序使用 Spring WebFlux 应用程序的问题,您能否提供一些帮助?

微服务 A 是一个响应式 Spring WebFlux,用 Java 8、SpringBoot 2.1.4 编写,它暴露了这个 API:

@Autowired private ReactiveCustomerRepository customerRepository;

@GetMapping("/customers")
public Flux<Customer> getAllCustomers() {
    Flux<Customer> c = customerRepository.findAll().delayElements(Duration.ofMillis(5000));
    return c;
}

我想通过 HTTP 以响应方式从 Play 框架微服务 B 使用它。

您能否就如何实现这一目标提供一些建议或小sn-p?

感谢您的帮助。

【问题讨论】:

    标签: java playframework microservices reactive-programming spring-webflux


    【解决方案1】:

    您可以尝试使用支持流式传输的不同内容类型 - application/stream+json。 Spring WebFlux 将序列化单个 Flux 元素并通过网络一一发送。查看以下关于它的 SO 线程:Spring WebFlux Flux behavior with non streaming application/json

    在 play-ws 方面,您应该能够以Source[T] 的形式接收此数据。

    【讨论】:

    • 我在这里找到了有趣的资源Serving large datasets with Spring WebFlux。然而,区别在于分块服务与一次性服务,而与 HTTP/2 或 GRPC 相比,HTTP 的缺陷更为普遍。似乎很适合 WebFlux,因为它以异步方式提供数据,允许逐块轻松流式传输。如果 Play WS 以流方式处理输出,这肯定是优化,
    • 毫无疑问,HTTP/2、gRPC 或 WebSockets 实际上更适合响应式应用程序。但是,即使使用这些传输,也应该始终验证它们是否适合特定用例。
    • 你好伊利亚,这很有趣。您能否推荐一个 Source[T] 使用 Spring Flux 的示例?
    • @PatPatPat 在 play-ws 中消费其他流应该没有什么不同。查看以下文档:playframework.com/documentation/2.6.x/…
    • 您好,Ilya,Play 2.4 怎么样?使用 Promise 变得有点困难。
    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 2015-11-23
    • 2018-12-14
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多