gpt4 book ai didi

spring-webflux - Spring Reactor WebClient 是如何实现非阻塞的?

转载 作者:行者123 更新时间:2023-12-01 03:11:26 25 4
gpt4 key购买 nike

基本问题:与 RestTemplate 相比,Spring Reactors WebClient 如何实现非阻塞?在将请求发送到外部服务(例如)之后,它是否不必阻塞某处? HTTP本质上是同步的,对吗?所以调用应用程序必须等待响应?
线程如何知道上下文以对服务的响应使用react?

最佳答案

这里有几个单独的问题。

  • 如何管理 I/O 操作?
  • 这个运行时背后的线程模型是什么?
  • 应用程序如何处理 HTTP 背后的请求/响应模型?

  • WebClient 的情况下和项目 Reactor 一样,Netty 事件循环用于排队/调度/处理事件。每个读/写操作都是以非阻塞方式完成的,这意味着没有线程等待 I/O 操作完成。在这个模型中,并发不是通过线程池完成的,但是有少量线程处理不应阻塞的工作单元。

    从纯 HTTP 的角度来看(即,如果您正在捕获网络上的 HTTP 数据包),您会发现 RestTemplate 之间没有太大区别。和 WebClient称呼。 HTTP 传输本身不支持背压概念。所以客户端仍然需要等待响应——这里的区别是应用程序使用了 WebClient不会浪费资源等待该操作完成 - 它将使用它们来处理其他事件。

    有关更多信息,请查看 the reactive programming introduction in the Reactor reference documentationthis talk given by Rossen Stoyanchev that explains things well if you're used to the typical Servlet container model .

    关于spring-webflux - Spring Reactor WebClient 是如何实现非阻塞的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51852629/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com