主题
内嵌Web容器
Spring Boot 内置的 Tomcat 服务器的最大连接数和最大并发数是可以通过配置进行调整的关键参数,它们影响着应用程序能够处理的请求数量。
参数配置详见: org.springframework.boot.autoconfigure.web.ServerProperties
java
public ServerProperties() {
this.shutdown = Shutdown.IMMEDIATE;
this.compression = new Compression();
this.mimeMappings = new MimeMappings();
this.http2 = new Http2();
this.servlet = new ServerProperties.Servlet();
this.reactive = new ServerProperties.Reactive();
this.tomcat = new ServerProperties.Tomcat();
this.jetty = new ServerProperties.Jetty();
this.netty = new ServerProperties.Netty();
this.undertow = new ServerProperties.Undertow();
}
最大连接数(max-connections
)
指服务器能建立的最大 TCP 连接数,默认为 8192。这包括正在处理的连接和排队等待处理的连接
java
public Tomcat() {
this.uriEncoding = StandardCharsets.UTF_8;
this.maxConnections = 8192;
this.acceptCount = 100;
this.processorCache = 200;
this.maxKeepAliveRequests = 100;
this.additionalTldSkipPatterns = new ArrayList();
this.relaxedPathChars = new ArrayList();
this.relaxedQueryChars = new ArrayList();
this.rejectIllegalHeader = true;
this.resource = new ServerProperties.Tomcat.Resource();
this.mbeanregistry = new ServerProperties.Tomcat.Mbeanregistry();
this.remoteip = new ServerProperties.Tomcat.Remoteip();
this.maxHttpResponseHeaderSize = DataSize.ofKilobytes(8L);
}
- 定义:指 Tomcat 能够同时处理的最大 TCP 连接数量。
- 默认值:8192
- 配置项:
server.tomcat.max-connections
- 说明:当连接数达到这个限制时,操作系统仍然可以接受基于
accept-count
属性的额外连接,但这些额外连接会被放入队列中等待处理。
最大并发数
指服务器同时处理的最大请求数,默认情况下由线程池的最大线程数(200)决定。
java
public static class Threads {
private int max = 200;
private int minSpare = 10;
private int maxQueueCapacity = 2147483647;
}
最大并发数实际上指的是 Tomcat 同时处理请求的能力,这与线程池配置有关,而不是直接由单一配置项决定。它取决于以下配置项:
最大线程数(
threads.max
或server.tomcat.threads.max
):- 定义:Tomcat 线程池中可以创建的最大线程数量,用于并发处理 HTTP 请求。
- 默认值:200
- 说明:这是指能够同时处理的最大请求数量。每个线程负责处理一个请求,因此最大并发数通常不会超过最大线程数。
最小空闲线程数(
server.tomcat.threads.min-spare
):- 定义:线程池中保持的最小空闲线程数,以便快速响应新请求。
- 默认值:10
接受队列长度(
accept-count
或server.tomcat.accept-count
):- 定义:当所有可能的请求处理线程都在使用中时,传入连接请求的最大队列长度。
- 默认值:100
- 说明:一旦所有的线程都被占用,新的连接将被放入此队列中等待处理。如果队列已满,则客户端可能会收到连接超时错误。