【问题标题】:Spring webclient, how many instances?Spring webclient,有多少个实例?
【发布时间】:2019-04-11 07:59:01
【问题描述】:

我在 spring 文档https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-concurrency-model 中读到,当为客户端和服务器使用 reactor netty 时,事件循环资源是共享的,这意味着当我创建多个 webclient 实例时,事件循环资源也是共享的? 如果我想与我的代码中的不同 API 进行通信,我应该创建多个 Webclient 实例还是应该共享同一个实例?

【问题讨论】:

    标签: java spring spring-webflux reactive


    【解决方案1】:

    由于 WebClient 实例是不可变的,因此您别无选择,只能使用其内置构建器为不同的 API 创建不同的 WebClient。

    您可以使用 mutate() 方法复制现有的 WebClient 配置或创建一个裸露的新实例。

    另请参阅https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/web-reactive.html#webflux-clientRight way to use Spring WebClient in multi-thread environment

    【讨论】:

    • 谢谢塞利姆,只是一个简单的问题,如果为不同的“主机”创建不同的网络客户端,这将自动共享底层网络资源?我的意思是,事件循环和线程,或者这将为这个网络客户端创建一个新的?
    • 根据文档,如果您使用 WebClient.Builder 创建 WebClient 实例,spring boot 对每个实例使用相同的 ClientHttpConnector。 docs.spring.io/spring-boot/docs/current/reference/html/… "Spring Boot 为您创建并预配置了一个 WebClient.Builder;强烈建议将其注入您的组件中并使用它来创建 WebClient 实例。Spring Boot 正在配置该构建器以共享 HTTP 资源,"跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    相关资源
    最近更新 更多