spring boot Tomcat maxConnections、maxThreads、acceptCount

本文详细解析了SpringBoot中Tomcat连接池的几个关键参数,包括maxConnections(最大连接数)、acceptCount(最大等待连接数)、maxThreads(最大处理线程数)以及connectionTimeout(连接超时时间)和maxKeepAliveRequests(最大保持连接请求数)。通过示例和图解说明了这些参数如何影响服务器的并发处理能力和连接管理,同时提供了配置示例及可能出现的问题,如实际等待连接数比acceptCount设置值多1的情况。此外,还讨论了当连接数超出限制时客户端可能遇到的连接超时问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1. maxConnections:

2. acceptCount 

3. maxThreads:

4.connectionTimeout

5.maxKeepAliveRequests

问题1:

问题2:

附录1:

附录2:

参考:

1:

1. maxConnections:

  • 官方解释
AttributeDescription
maxConnections

The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server will accept, but not process, one further connection. This additional connection be blocked until the number of connections being processed falls below maxConnections at which point the server will start accepting and processing new connections again. Note that once the limit has been reached, the operating system may still accept connections based on the acceptCount setting. The default value is 8192.

For NIO/NIO2 only, setting the value to -1, will disable the maxConnections feature and connections will not be counted.

  • 图2

  • 理解

服务程序可以在一定时间内接收并处理的连接数目如图1中queue-2,超过这个数,会根据acceptCount 这个值继续建立连接存放在queue-1中,但是该连接不会被处理,只有当queue-2中的连接数小于maxConnections值,queue-1中的连接才会进入queue-2中,该连接才有可能被执行。queue-2中的连接状态如图2标注所示。当同时请求数大于maxConnections+acceptCount 新的请求将会被拒绝连接。

  • spring boot 默认值

  • 设置

配置spring boot 项目 resources 目录下的application. properties 文件

server.tomcat.max-connections=10

2. acceptCount 

  • 官方解释
AttributeDescription
acceptCount

The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100.

  • 图3

  • 理解

超过maxConnections这个值的连接数将根据acceptCount这个值继续建立连接,如图1 queue-1,当queue-2的连接数小于maxConnections, queue-1的连接进入queue-2.

queue-1的连接状态如图3标注所示。

  • spring boot 默认值

  • 设置:

配置spring boot 项目 resources 目录下的application. properties 文件

server.tomcat.accept-count=5

3. maxThreads:

  • 官方解释
AttributeDescription
maxThreads

The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as -1 to make clear that it is not used.

  • 理解

服务程序可以同时处理的线程数如图1 ThreadPool,可以理解为通过设定 maxConnections=10 ,同时可以建立10个连接,maxThreads=3,则这10个连接中同时只有3个连接被处理,其余7个连接都在queue-2中等待被处理,等这3个连接处理完之后,其余的7个连接中的3个才可以被处理。如果处理完的3个连接关闭后,queue-1中就可以有3个连接进入queue-2。

  • spring boot 默认值

  • 设置

配置spring boot 项目 resources 目录下的application. properties 文件

server.tomcat.max-threads=3

4.connectionTimeout

  • 官方解释
AttributeDescription
connectionTimeout

The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented. Use a value of -1 to indicate no (i.e. infinite) timeout. The default value is 60000 (i.e. 60 seconds) but note that the standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20 seconds). Unless disableUploadTimeout is set to false, this timeout will also be used when reading the request body (if any).

  • 理解

连接的生存周期,当已经建立的连接,在connectionTimeout时间内,如果没有新的请求到来,服务端程序将会主动关闭该连接。

  • 设置

配置spring boot 项目 resources 目录下的application. properties 文件

server.tomcat.connection-timeout=20000

5.maxKeepAliveRequests

  • 官方解释
maxKeepAliveRequests

The maximum number of HTTP requests which can be pipelined until the connection is closed by the server. Setting this attribute to 1 will disable HTTP/1.0 keep-alive, as well as HTTP/1.1 keep-alive and pipelining. Setting this to -1 will allow an unlimited amount of pipelined or keep-alive HTTP requests. If not specified, this attribute is set to 100.

  • 理解

长连接,在发送了maxKeepAliveRequests个请求后就会被服务器端主动断开连接。

  • 设置
server.tomcat.max-connections=-1

问题1:

这里测试过程中设置的 server.tomcat.accept-count=5,但是图3中的 连接数是6,而且通过改变server.tomcat.accept-count这个值,测出的等待连接数都会比这个值多1。

问题2:

当连接数大于maxConnections+acceptCount时,新来的请求没有收到服务器拒绝连接响应,而是不会和新的请求建立连接,如果客户端设置了请求连接超时,就会出现请求连接超时。

附录1:

Jmeter测试设置:

附录2:

TCP 3次握手4次挥手流程:

参考:

Common Application Properties

Apache Tomcat 9 Configuration Reference (9.0.68) - The HTTP Connector

Linux netstat命令详解 - 小a玖拾柒 - 博客园

[Spring Boot]用Jmeter测试maxConnections、maxThreads、acceptCount的关系_seabreezesuper的博客-优快云博客

<think>好的,我需要回答用户关于若依框架支持的最大并发数的问题。首先,我得仔细看看用户提供的引用内容,找到相关的信息。 根据引用[1],里面提到了SpringBoot的最大连接数由maxConnectionsacceptCount控制,这两个参数影响Tomcat的处理能力。而若依框架是基于SpringBoot的,所以Tomcat的配置应该适用于若依。那用户问的是若依的支持的最大并发数,可能和Tomcat的配置有关。 然后引用[4]提到若依在Shiro配置中设置了用户的最大会话数,比如maxSession参数,但这可能更多是关于用户登录会话的限制,而不是系统整体的并发处理能力。所以需要区分开会话数和并发连接数的不同。 接下来,引用[3]讲的是JavaScript中处理Promise的最大并发数,这可能和前端有关,但用户的问题集中在若依框架的后端,所以这部分可能不太相关,暂时可以忽略。 再看引用[2],提到若依能生成Spring Boot和MyBatis的代码,说明其底层依赖SpringBoot,因此最大并发数的设置应该和SpringBoot的配置一致,也就是引用[1]提到的Tomcat参数。 所以结论应该是,若依框架的最大并发数主要由其使用的Web服务器(如Tomcat)的maxConnectionsacceptCount决定。用户需要在application.yml中配置Tomcat的相关参数来调整最大连接数和等待队列长度。同时,若依还通过Shiro限制了单个用户的并发会话数,但这属于应用层面的限制,和系统整体的并发数不同。 最后,我需要整理这些信息,分点说明,并生成相关问题。确保使用正确的LaTeX格式,比如变量用$...$,没有数学公式的话可能不需要,但如果有数值的话需要注意。同时检查引用标注是否正确,比如[^1]、[^4]等。</think>若依框架的最大并发数主要取决于其底层使用的Web服务器(如TomBoot内置的Tomcat)配置,具体通过以下两类参数实现控制: ### 一、Web服务器级并发控制 1. **Tomcat连接参数** - **maxConnections**:设置Tomcat能同时处理的最大连接数,默认值根据环境不同(如NIO模式默认为$10000$) - **acceptCount**:当连接数达到`maxConnections`时,允许进入等待队列的请求数量(默认$100$) - 配置示例(在`application.yml`中添加): ```yaml server: tomcat: max-connections: 2000 accept-count: 500 ``` 2. **线程池配置** - **maxThreads**:Tomcat处理请求的最大线程数(默认$200$),直接影响并发处理能力: ```yaml server: tomcat: threads: max: 500 ``` ### 二、应用级会话控制 若依通过Shiro模块限制**单个用户的并发会话数**(如登录设备数限制): ```java // ShiroConfig.java中配置 @Value("${shiro.session.maxSession}") private int maxSession; // 用户最大会话数 @Value("${shiro.session.kickoutAfter}") private boolean kickoutAfter; // 是否踢出后续登录 ``` 需在`application.yml`设置: ```yaml shiro: session: maxSession: 1 # 同一账号仅允许1个会话 kickoutAfter: false # 优先踢出旧会话 ``` ### 三、优化建议 1. **硬件与中间件调优**:结合服务器CPU核心数调整`maxThreads`,通常建议设为$2 \times CPU核心数 + 1$ 2. **数据库连接池**:需同步调整如Druid的`maxActive`参数,避免成为瓶颈 3. **分布式扩展**:通过Nginx负载均衡横向扩展实例,突破单节点并发限制
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值