【问题标题】:Spring 5's Reactive WebClient not so asyncronous?Spring 5 的 Reactive WebClient 不那么异步?
【发布时间】:2020-01-31 10:07:48
【问题描述】:

我遇到了奇怪的 Spring WebClient 行为。我有两个网址,慢的和快的。机器人什么都不做,但慢一点,只需等待十秒钟即可响应。当我使用 WebClient 同时调用它们时,我希望快速 URL 会比慢速 URL 更早完成,但实际上它们都是同时完成的。最糟糕的是 - 有时它会按预期工作。有没有人想过,为什么它会以这种方式运行,以及如何使它正常工作?这是我的例子

fun main() {
    val webClient = WebClient.create()
    println("${LocalDateTime.now()} [${Thread.currentThread().name}] Start")

    webClient.get().uri("http://callback-mock/slow-url").exchange()
       .subscribe { response -> 
            println("${LocalDateTime.now()} [${Thread.currentThread().name}] Executed callback slow URL with result ${response.statusCode()}") 
       }
    webClient.get().uri("http://callback-mock/fast-url").exchange()
       .subscribe { response -> 
            println("${LocalDateTime.now()} [${Thread.currentThread().name}] Executed callback fast URL with result ${response.statusCode()}")
       }
    println("${LocalDateTime.now()} [${Thread.currentThread().name}] Waiting for exit")
    Thread.sleep(15_000)
    println("${LocalDateTime.now()} [${Thread.currentThread().name}] Exit")
}

结果(大多数情况下)

2019-10-02T13:04:34.536 [main] Start
2019-10-02T13:04:35.173 [main] Waiting for exit
2019-10-02T13:04:44.791 [reactor-http-nio-4] Executed callback slow URL with result 200 OK
2019-10-02T13:04:44.791 [reactor-http-nio-2] Executed callback fast URL with result 200 OK
2019-10-02T13:04:50.193 [main] Exit

Process finished with exit code 0

在极少数情况下,它会按预期工作

2019-10-02T13:23:35.619 [main] Start
2019-10-02T13:23:36.300 [main] Waiting for exit
2019-10-02T13:23:36.802 [reactor-http-nio-2] Executed callback fast URL with result 200 OK
2019-10-02T13:23:45.810 [reactor-http-nio-4] Executed callback slow URL with result 200 OK
2019-10-02T13:23:51.308 [main] Exit

Process finished with exit code 0

【问题讨论】:

    标签: reactor-netty spring-webclient


    【解决方案1】:

    以下非常简单的测试表明fast 总是首先返回。 (Reactor Netty 用作 HTTP 服务器)

        @Test
        public void test() throws InterruptedException {
            DisposableServer server =
                    HttpServer.create()
                            .port(0)
                            .route(r -> r.get("/fast", (req,res) -> res.sendString(Mono.just("test")))
                                        .get("/slow", (req,res) -> res.sendString(Mono.just("test").delayElement(Duration.ofSeconds(10)))))
                            .bindNow();
    
            WebClient webClient = WebClient.create();
            System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() + " Start");
    
            webClient.get().uri("http://localhost:" + server.port() + "/slow").exchange()
                    .subscribe(response ->
                            System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() +
                                    " Executed callback slow URL with result " + response.statusCode()));
    
            webClient.get().uri("http://localhost:" + server.port() + "/fast").exchange()
                    .subscribe(response ->
                            System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() +
                                    " Executed callback fast URL with result " + response.statusCode()));
    
            System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() + " Waiting for exit");
            Thread.sleep(15_000);
            System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() + " Exit");
    
            server.disposeNow();
        }
    

    【讨论】:

    • 嗯...可能你是对的,也许模拟服务器有问题...我会检查它并标记你的答案,如果它是
    • 你完全正确,非常感谢。这是python服务器的问题,它是在单线程模式下运行的。谢谢你的正确方向!
    猜你喜欢
    • 1970-01-01
    • 2020-07-25
    • 2019-01-09
    • 2019-06-24
    • 2018-09-16
    • 2020-03-26
    • 2019-12-04
    • 2018-09-13
    • 1970-01-01
    相关资源
    最近更新 更多