【发布时间】:2021-11-12 05:06:54
【问题描述】:
我不明白为什么 webclient 会阻塞 netty 主线程 我在这里使用 gradle 是它的依赖项:
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR11"
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
...
}
此 gradle 脚本用于两个应用程序。 在第一个应用程序中,我执行:
@GetMapping
open fun otherApp(): Mono<String> {
return WebClient.create("http://localhost:8081")
.get()
.uri("/test")
.retrieve()
.bodyToMono(String::class.java)
// I tried to use an additional scheduler but the main stream is blocked with it too
// .publishOn(Schedulers.boundedElastic())
}
第二个应用程序模拟长响应处理:
@GetMapping("/test")
open fun test(): Mono<String> {
Thread.sleep(15000L)
return Mono.just("Hello");
}
并且我希望调用服务不会阻塞主线程,而是会继续处理传入的连接,但是直到我从第一个调用得到响应(睡眠会起作用),我的下一个连接都处于等待状态。
结果: 第一个应用程序像 tomcat 一样使用单线程
我的问题: 我怎么了?
【问题讨论】:
-
你怎么知道第一个应用程序被阻止了?是否再次调用应用程序 2?
-
我在浏览器中直接调用第一个应用,挂在8080端口,请求挂了,没关系。在 15 秒内,当第二个应用程序模拟第一个请求的处理时,我在另一个浏览器窗口中再次调用第一个应用程序,并且对第一个应用程序的第二个请求没有进入控制器
标签: spring kotlin netty webclient blocking