【发布时间】:2020-05-16 22:10:53
【问题描述】:
我正在学习 Spring webflux,遇到了一个我不理解的情况。
春季版 = 2.2.4.RELEASE 我的电脑有 4 个核心。
我的班级:
@RestController
@RequestMapping("rest/v1/")
public class RootController {
private final WebClient webClient =
WebClient.create("http://localhost:8081");
@GetMapping("/booking")
public Mono book() {
return Mono.just("A");
}
@GetMapping("/sleep")
public Mono<String> sleep(@RequestParam("time") long time) throws InterruptedException {
System.out.printf("Thread name is %s, date is %s. \n", Thread.currentThread().getName(), new Date());
String uri = "/rest/v1/sleep?time=" + time;
String uuid = UUID.randomUUID().toString();
System.out.println(">>>>>>>>>>>>" + uuid);
Mono<String> stringMono = webClient.get().uri(uri)
.retrieve()
.bodyToMono(String.class);
System.out.printf("Finish -> Thread name is %s, date is %s. \n", Thread.currentThread().getName(), new Date());
return stringMono;
}
}
我的机器上有另一个应用程序,在端口 8081 上运行。 这个 endoint 只休眠 param 接收到的一段时间。
目标是测试调用慢速端点时 webflux 的行为如何。
我正在执行这样的请求:
http://localhost:8082/rest/v1/sleep?time=20000
在这种情况下,响应需要 20 秒。
当我在此期间执行另一个请求时,控制器不会处理该请求,它会一直等待该请求完成(20s)。
Spring 在等待网络调用时是否应该处理另一个请求?
这是日志:
Thread name is reactor-http-epoll-3, date is Sat May 16 19:06:54 BRT 2020.
>>>>>>>>>>>>22b36a46-e36b-4104-981a-e132483a334e
Finish -> Thread name is reactor-http-epoll-3, date is Sat May 16 19:06:54 BRT 2020.
Thread name is reactor-http-epoll-3, date is Sat May 16 19:07:14 BRT 2020.
>>>>>>>>>>>>076a0eec-3bb1-485a-ab88-9b11c6b9a5bd
Finish -> Thread name is reactor-http-epoll-3, date is Sat May 16 19:07:14 BRT 2020.
【问题讨论】:
-
是的,它应该处理另一个请求
-
您是否从 Chrome 浏览器访问了您的端点?我遇到了类似的问题,但不确定原因。不过,我记得它在 Firefox 或 Edge 中运行良好。您也可以从另一个应用程序以编程方式调用它。编辑:stackoverflow.com/a/53236741/6051176
-
另外,看看睡眠是如何在你的“慢”服务中实现的可能会很有趣。是否有可能阻止其他并发请求?
标签: spring spring-webflux