Spring中的并发请求数量与http-nio-8080-exec线程数量之间的关系

5

我想测试我的控制器中的并行请求。我知道默认情况下Spring可以处理200个并行请求,我们可以通过修改这个属性server.tomcat.max-threads来更改它。

我尝试了一下那个值,并发现了有趣的事情:当我将它设置为3时,启动应用程序时会创建3个线程:http-nio-8080-exec-1,2,3。当我将它设置为5时,我会看到5个这样的线程。它一直持续到10,停止在10。当我将它设置为15时,仍然有10个名称为http-nio-8080-exec的线程。有人能解释一下为什么它从未超过10吗?

如果我像这样创建控制器

@GetMapping("test")
public String test(@RequestParam("skip") boolean skip) throws InterruptedException {
    if(!skip) {
        System.out.println("I'm starting waiting");
        Thread.sleep(10000);
        System.out.println("I stopped waiting");
    }
    return "dd";
}

在设置server.tomcat.max-threads=200的情况下,发起10个如下请求:http://localhost:8080/test?skip=false

以及同时发起第11个如下请求:http://localhost:8080/test?skip=true

这前10个请求应该等待10秒钟才能返回响应,但第11个请求应该立即返回。问题是第11个请求也会等待,所以被阻塞了。有人能解释一下它是如何工作的吗?如果我将server.tomcat.max-threads设置为200,那么我希望我可以处理200个独立的请求,对吗?

1个回答

2

我对这种行为很好奇,所以自己进行测试。不幸的是,我无法确认您正在经历的行为。为了能够同时测试对该服务的请求,我编写了一个简单的Go程序来证明响应时间。因此,我启动了一个带有单个REST端点的Spring Boot服务,并使用您上面发布的代码作为客户端跟随Go程序:

package main

import (
    "log"
    "os/exec"
    "time"
)

func main() {
    for i := 0; i< 15; i++ {
        go runCmd(i)
    }
    time.Sleep(time.Second * 50)
}

func runCmd(i int) {
    param := "localhost:8080/test?skip=false"
    if i > 10 {
        param = "localhost:8080/test?skip=true"
    }
    cmd := exec.Command("curl", param)
    log.Printf("Running command %d and waiting for it to finish...", i)
    start := time.Now()
    err := cmd.Run()
    end := time.Now()
    duration := end.Sub(start)
    log.Printf("Command  %d finished within of second %f error: %v", i, duration.Seconds(), err)
}

这个程序使用curl发送了前10个请求,参数为skip=false,另外5个请求的参数为skip=true。输出结果如下:

2020/03/08 19:21:18 Running command 14 and waiting for it to finish...
2020/03/08 19:21:18 Running command 9 and waiting for it to finish...
2020/03/08 19:21:18 Running command 3 and waiting for it to finish...
2020/03/08 19:21:18 Running command 10 and waiting for it to finish...
2020/03/08 19:21:18 Running command 7 and waiting for it to finish...
2020/03/08 19:21:18 Running command 0 and waiting for it to finish...
2020/03/08 19:21:18 Running command 12 and waiting for it to finish...
2020/03/08 19:21:18 Running command 6 and waiting for it to finish...
2020/03/08 19:21:18 Running command 5 and waiting for it to finish...
2020/03/08 19:21:18 Running command 13 and waiting for it to finish...
2020/03/08 19:21:18 Running command 11 and waiting for it to finish...
2020/03/08 19:21:18 Running command 2 and waiting for it to finish...
2020/03/08 19:21:18 Running command 1 and waiting for it to finish...
2020/03/08 19:21:18 Running command 4 and waiting for it to finish...
2020/03/08 19:21:18 Running command 8 and waiting for it to finish...
2020/03/08 19:21:18 Command  12 finished within of second 0.035109 error: <nil>
2020/03/08 19:21:18 Command  14 finished within of second 0.035290 error: <nil>
2020/03/08 19:21:18 Command  11 finished within of second 0.040090 error: <nil>
2020/03/08 19:21:18 Command  13 finished within of second 0.041358 error: <nil>
2020/03/08 19:21:28 Command  9 finished within of second 10.034510 error: <nil>
2020/03/08 19:21:28 Command  0 finished within of second 10.034436 error: <nil>
2020/03/08 19:21:28 Command  10 finished within of second 10.037470 error: <nil>
2020/03/08 19:21:28 Command  6 finished within of second 10.042294 error: <nil>
2020/03/08 19:21:28 Command  5 finished within of second 10.042328 error: <nil>
2020/03/08 19:21:28 Command  7 finished within of second 10.045510 error: <nil>
2020/03/08 19:21:28 Command  3 finished within of second 10.045638 error: <nil>
2020/03/08 19:21:28 Command  1 finished within of second 10.049024 error: <nil>
2020/03/08 19:21:28 Command  2 finished within of second 10.053824 error: <nil>
2020/03/08 19:21:28 Command  8 finished within of second 10.053102 error: <nil>
2020/03/08 19:21:28 Command  4 finished within of second 10.053306 error: <nil>

正如您在输出中看到的,请求11-14没有延迟完成,因此它们没有被阻塞。因此,您可能需要检查您的测试,问题可能出现在测试中而不是tomcat配置中。
同时,处理200个独立请求的期望是正确的,应该表现出这种行为,并且确实表现出了这种行为。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接