[英]How to add Custom response in Mono / Flux in Spring web flux?
[英]How to orginize thread pooling with spring flux and spring flux web client?
考慮一個簡單的控制器
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);
}
}
一個文檔告訴我們,在Spring Web流程中,只有一個IO線程和N個工作線程(N-CPU核心數)。 並且工作線程在Mono / Flux調用之間重用。
但是,當我運行該應用程序時,我發現除了彈簧通量之外,spring web客戶端還創建了IO線程和N個工作器。 所以我得到了:
"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
帶有reactor-http-nio
前綴的線程對應於spring應用程序,而XNIO
於web流量客戶端。
有沒有辦法在Web客戶端和應用程序之間重用工作進程? 重用那些線程是個好主意嗎?
您的服務器應用程序是在Reactor Netty上運行還是在Undertow上運行? XNIO線程通常與Undertow關聯。 您可以通過在應用程序啟動期間查看日志來確保這一點。
在這種情況下,這是預期的行為,因為Reactor Netty和Undertow不共享客戶端和服務器資源。
在最新版本的Spring Boot中, 如果可能的話,Jetty和Reactor Netty會自動共享客戶端和服務器資源 。
附帶說明一下,如果您使用的是Spring Boot,則應嘗試從WebClient.Builder
創建WebClient
實例,然后將其注入應用程序中的任何位置。 這將為您提供預期的默認值和自定義配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.