1. 前言
一个IP发请求过来,是一个IP对应一个线程吗?这里主要探讨以下这个问题。
2. 正文
Springboot默认的内嵌容器是Tomcat,也就是我们的程序实际是运行在Tomcat里的。所以与其说Springboot可以处理多少请求,倒不如说Tomcat可以处理可以处理多少请求。
关于Tomcat的默认配置,都在spring-configuration-metadata.json
文件中, 对应的配置类则是org.springframework.boot.autoconfigure.web.ServletProperties
。
和处理请求数量相关的参数有四个:
1 | { |
-【server.tomcat.accept-count
】:等待队列的长度,默认大小是100
-【server.tomcat.threads.max
】:最大的工作线程数,默认大小是200,该参数相当于临时工,如果并发请求的数量在10~200之间,就会使用这些临时工线程进行处理。
-【server.tomcat.threads.min-spare
】:最少的工作线程数,默认大小是10。该参数相当于长期工,如果并发请求的数量达不到10,就会一次使用这几个线程去处理请求。
-【server.tomcat.max-connections
】:最大连接数,默认大小是8192。表示Tomcat可以处理的最大请求数量,超过了8192的请求就会被放到等待队列。
所以Springboot同时能处理最大请求数量是max-connections + accept-count
,超出这个数量的请求就会被丢弃
3. 实战代码
- 创建项目,添加依赖
1
2
3
4
5<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
在
application.yml
里面配置如下参数(默认配置太大不好测试):1
2
3
4
5
6
7server:
tomcat:
accept-count: 10 #最大等待数量
max-connections: 30 #最大连接数
threads:
max: 15 #最大的工作线程数
min-spare: 10 #最少线程数添加测试接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19/**
* @author xiaoyuge
*/
public class ExampleController {
Logger logger = LoggerFactory.getLogger(ExampleController.class);
public AjaxResult test() {
logger.info("线程:{}", Thread.currentThread().getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return AjaxResult.success("操作成功");
}
}使用Apifox创建测试用例,模拟并发场景
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
332023-07-25 21:43:15.952 INFO 94834 --- [nio-8080-exec-3] org.example.ExampleController : 线程:http-nio-8080-exec-3
2023-07-25 21:43:15.952 INFO 94834 --- [nio-8080-exec-1] org.example.ExampleController : 线程:http-nio-8080-exec-1
2023-07-25 21:43:15.952 INFO 94834 --- [nio-8080-exec-2] org.example.ExampleController : 线程:http-nio-8080-exec-2
2023-07-25 21:43:18.109 INFO 94834 --- [nio-8080-exec-8] org.example.ExampleController : 线程:http-nio-8080-exec-8
2023-07-25 21:43:18.123 INFO 94834 --- [io-8080-exec-10] org.example.ExampleController : 线程:http-nio-8080-exec-10
2023-07-25 21:43:18.135 INFO 94834 --- [nio-8080-exec-7] org.example.ExampleController : 线程:http-nio-8080-exec-7
2023-07-25 21:43:21.203 INFO 94834 --- [io-8080-exec-10] org.example.ExampleController : 线程:http-nio-8080-exec-10
2023-07-25 21:43:21.203 INFO 94834 --- [nio-8080-exec-9] org.example.ExampleController : 线程:http-nio-8080-exec-9
2023-07-25 21:43:21.203 INFO 94834 --- [nio-8080-exec-6] org.example.ExampleController : 线程:http-nio-8080-exec-6
2023-07-25 21:43:21.203 INFO 94834 --- [nio-8080-exec-8] org.example.ExampleController : 线程:http-nio-8080-exec-8
2023-07-25 21:43:21.203 INFO 94834 --- [nio-8080-exec-7] org.example.ExampleController : 线程:http-nio-8080-exec-7
2023-07-25 21:43:21.207 INFO 94834 --- [nio-8080-exec-2] org.example.ExampleController : 线程:http-nio-8080-exec-2
2023-07-25 21:43:21.208 INFO 94834 --- [nio-8080-exec-4] org.example.ExampleController : 线程:http-nio-8080-exec-4
2023-07-25 21:43:21.208 INFO 94834 --- [nio-8080-exec-5] org.example.ExampleController : 线程:http-nio-8080-exec-5
2023-07-25 21:43:21.241 INFO 94834 --- [io-8080-exec-12] org.example.ExampleController : 线程:http-nio-8080-exec-12
2023-07-25 21:43:21.243 INFO 94834 --- [io-8080-exec-13] org.example.ExampleController : 线程:http-nio-8080-exec-13
2023-07-25 21:43:21.243 INFO 94834 --- [io-8080-exec-14] org.example.ExampleController : 线程:http-nio-8080-exec-14
2023-07-25 21:43:21.244 INFO 94834 --- [nio-8080-exec-3] org.example.ExampleController : 线程:http-nio-8080-exec-3
2023-07-25 21:43:21.245 INFO 94834 --- [io-8080-exec-15] org.example.ExampleController : 线程:http-nio-8080-exec-15
2023-07-25 21:43:21.246 INFO 94834 --- [nio-8080-exec-1] org.example.ExampleController : 线程:http-nio-8080-exec-1
2023-07-25 21:43:21.246 INFO 94834 --- [io-8080-exec-11] org.example.ExampleController : 线程:http-nio-8080-exec-11
2023-07-25 21:43:21.707 INFO 94834 --- [nio-8080-exec-9] org.example.ExampleController : 线程:http-nio-8080-exec-9
2023-07-25 21:43:21.708 INFO 94834 --- [nio-8080-exec-7] org.example.ExampleController : 线程:http-nio-8080-exec-7
2023-07-25 21:43:21.709 INFO 94834 --- [io-8080-exec-10] org.example.ExampleController : 线程:http-nio-8080-exec-10
2023-07-25 21:43:21.709 INFO 94834 --- [nio-8080-exec-6] org.example.ExampleController : 线程:http-nio-8080-exec-6
2023-07-25 21:43:21.711 INFO 94834 --- [nio-8080-exec-4] org.example.ExampleController : 线程:http-nio-8080-exec-4
2023-07-25 21:43:21.712 INFO 94834 --- [nio-8080-exec-8] org.example.ExampleController : 线程:http-nio-8080-exec-8
2023-07-25 21:43:21.712 INFO 94834 --- [nio-8080-exec-2] org.example.ExampleController : 线程:http-nio-8080-exec-2
2023-07-25 21:43:21.713 INFO 94834 --- [nio-8080-exec-5] org.example.ExampleController : 线程:http-nio-8080-exec-5
2023-07-25 21:43:21.745 INFO 94834 --- [io-8080-exec-12] org.example.ExampleController : 线程:http-nio-8080-exec-12
2023-07-25 21:44:17.310 INFO 94834 --- [nio-8080-exec-9] org.example.ExampleController : 线程:http-nio-8080-exec-9
2023-07-25 21:44:17.311 INFO 94834 --- [nio-8080-exec-2] org.example.ExampleController : 线程:http-nio-8080-exec-2
2023-07-25 21:44:17.312 INFO 94834 --- [nio-8080-exec-5] org.example.ExampleController : 线程:http-nio-8080-exec-5控制台的打印日志可以看到,线程的最大编号是15
总结
如果并发请求数量低于「server.tomcat.threads.max」,则会被立即处理,超过的部分会先进行等待,如果数量超过max-connections与accept-count之和,则多余的部分则会被直接丢弃