简体   繁体   English

如何使用弹簧助焊剂和弹簧助焊剂网络客户端来创建线程池?

[英]How to orginize thread pooling with spring flux and spring flux web client?

Consider a simple controller 考虑一个简单的控制器

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;

@RestController
public class DtoController {

    private final WebClient client = WebClient.create("http://localhost:8081");

    @GetMapping("/booking")
    public Mono<MyDto> getDto() {
        return carsClient.get().uri("/myUrl")
                .retrieve()
                .bodyToMono(MyDto.class);
    }

}

A documentation tells that in spring web flux there are single IO thread and N workers (N - CPU core count). 一个文档告诉我们,在Spring Web流程中,只有一个IO线程和N个工作线程(N-CPU核心数)。 And worker threads are reused between Mono/Flux calls. 并且工作线程在Mono / Flux调用之间重用。

But when I run this application I see that in addition to spring flux spring web client also creates IO thread and N workers. 但是,当我运行该应用程序时,我发现除了弹簧通量之外,spring web客户端还创建了IO线程和N个工作器。 So I got: 所以我得到了:

"Attach Listener"@11 046: RUNNING
"DestroyJavaVM"@9 089 in group "main": RUNNING
"Finalizer"@11 048: WAIT
"ObjectCleanerThread"@6 732 in group "main": WAIT
"reactor-http-nio-1"@6 711 in group "main": RUNNING
"reactor-http-nio-2"@6 713 in group "main": RUNNING
"reactor-http-nio-3"@6 715 in group "main": RUNNING
"reactor-http-nio-4"@6 714 in group "main": RUNNING
"Reference Handler"@11 049: WAIT
"RMI Scheduler(0)"@1 798: WAIT
"RMI TCP Accept-0"@1 288: RUNNING
"RMI TCP Accept-0"@1 445: RUNNING
"RMI TCP Accept-59098"@1 355: RUNNING
"Signal Dispatcher"@11 047: RUNNING
"SimplePauseDetectorThread_0"@6 329: SLEEPING
"Thread-15"@6 328: WAIT
"XNIO-1 Accept"@8 573 in group "main": RUNNING
"XNIO-1 I/O-1"@8 518 in group "main": RUNNING
"XNIO-1 I/O-2"@8 537 in group "main": RUNNING
"XNIO-1 I/O-3"@8 547 in group "main": RUNNING
"XNIO-1 I/O-4"@8 552 in group "main": RUNNING

Threads with reactor-http-nio prefix correspond to spring application, while XNIO to web flux client. 带有reactor-http-nio前缀的线程对应于spring应用程序,而XNIO于web流量客户端。

Is there a way to reuse workers between web client and application? 有没有办法在Web客户端和应用程序之间重用工作进程? And is it a good idea to reuse those threads? 重用那些线程是个好主意吗?

Is your server application running on top of Reactor Netty or maybe Undertow? 您的服务器应用程序是在Reactor Netty上运行还是在Undertow上运行? XNIO threads are usually associated with Undertow. XNIO线程通常与Undertow关联。 You can make sure of that by looking at the logs during application startup. 您可以通过在应用程序启动期间查看日志来确保这一点。

If that's the case, then this is the expected behavior since Reactor Netty and Undertow don't share client and server resources. 在这种情况下,这是预期的行为,因为Reactor Netty和Undertow不共享客户端和服务器资源。

In recent versions of Spring Boot, client and server resources are automatically shared, when possible for Jetty and Reactor Netty . 在最新版本的Spring Boot中, 如果可能的话,Jetty和Reactor Netty会自动共享客户端和服务器资源

As a side note, if you're using Spring Boot, you should try and create WebClient instances from a WebClient.Builder that you can get injected anywhere in your app. 附带说明一下,如果您使用的是Spring Boot,则应尝试从WebClient.Builder创建WebClient实例,然后将其注入应用程序中的任何位置。 This will provide you with the expected defaults and custom configuration. 这将为您提供预期的默认值和自定义配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM