Tomcat中的线程配置

本文详细介绍了Tomcat中线程配置的区别,包括连接器配置与线程池配置的关系。maxThreads和minSpareThreads参数在连接器和线程池中的覆盖规则,以及maxConnections和acceptCount的作用。总结了Tomcat能够同时处理的请求数量计算方式,并提供了官方配置资源链接供进一步学习。

一、概述

    tomcat中在server.xml中配置线程,可以在connector中配置,也可以配置executor,这两者之间的配置有什么区别?

    具体配置格式及内容,还请查看conf/server.xml配置文件,默认是注释掉了。


二、详解

1. 概述

    线程池和连接器的关系,在连接器配置选项中清晰的被陈述出来。

executor

A reference to the name in an Executor element. If this attribute is set, and the named executor exists, the connector will use the executor, and all the other thread attributes will be ignored. Note that if a shared executor is not specified for a connector then the connector will use a private, internal executor to provide the thread pool.


    连接器中的线程配置是私有的,连接器自己的配置只能自己使用;而线程池可以被共享,多个连接器通过executor属性关联到连接池的name完成配置(见下图);一旦连接器中配置了一个存在的executor,那么线程池的配置将会覆盖连接器的线程配置。


The Executor represents a thread pool that can be shared between components in Tomcat.
 Historically there has been a thread pool per connector created but this allows you to share a thread pool,
 between (primarily) connector but also other components when those get configured to support executors
Each incoming request requires a thread for the duration of that request. 
If more simultaneous requests are received than can be handled by the currently available request processing threads, 
additional threads will be created up to the configured maximum (the value of the maxThreads attribute). 
If still more simultaneous requests are received, they are stacked up inside the server socket created by the Connector, up to the configured maximum (the value of the acceptCount attribute).
 Any further simultaneous requests will receive "connection refused" errors, until resources are available to process them.

    线程池覆盖连接器中的线程配置,主要是覆盖maxThreads和minSpareThreads两个参数,这两个参数在线程池标签和连接器标签中都存在。

    a. 对于线程池而言:最主要的配置项是maxThreads和minSpareThreads(参看3.1表格,官网全部配置项参看4.1链接),前者代表线程池中可以创建的最大线程数,后者代表当线程池空闲的时候保留的线程数量,当线程池覆盖连接器的线程配置的时候,最主要的是覆盖这两个参数,连接器如何关联线程池,参看上文截图。

    b. 对于连接器而言:线程相关的主要参数有四个——maxThreads、minSpareThreads、maxConnections、acceptCount,如果连接器关联了线程池,那么maxThreads和minSpareThreads会被覆盖(重要的事情反复说),acceptCount和maxConnections继续生效;

    maxThreads:一个请求分配一个线程处理,其实就是serversocket.accept之后,新建了一个线程去处理这个接受的socket,然后这个连接器可以建立的最大线程数量即为maxThreads配置的值,如果是多个连接器关联了同一个线程池,那么多个连接器允许建立的线程数量之和即为maxThreads的值;

    minSpareThreads:如果请求少或者没有请求,那么tomcat就会主动关闭一些不用的线程,但是至少会保留minSpareThreads配置的线程数;

    maxConnections:表示可以同时被当前连接器处理的请求数量,这个配置项需要和maxThreads进行对比,一个请求一旦被接受处理就会分配给一个线程,这时候能够同时处理多少个请求取决于maxConnections和maxThreads两个配置项中的较小者,如果连接器是BIO则maxConnections默认等于maxThreads(连接器的maxThreads,如果关联了线程池,则为线程池中的maxThreads),NIO2默认是10000,APR默认是8192(BIO/NIO/NIO2/APR主要是根据connector的protocol属性,这个属性可以配置小表中的类);

    举个例子,连接器中maxThreads=200,maxConnections=100,这时候因为线程池最多只允许创建100个线程,因此能被同时处理的请求数量是100,取决于两者中的较小者;但是如果连接器关联了线程池maxThreads=200,连接器A的maxConnections=120,连接器B的maxConnections=100,这时候对于连接器A而言,处理请求的最大的线程数是120,不会超过120,但是能不能达到120还需要看线程池中的可用的线程数,如果这时候连接器B已经拿了100个线程,那么此时A只能拿到100个线程;


Java Blocking Connector
BIO
Java Nio Connector
NIO
Java Nio2 Connector
NIO2
APR/native Connector
APR
Classname Http11Protocol Http11NioProtocol Http11Nio2Protocol Http11AprProtocol

    acceptCount:相当于建立ServerSocket的时候构造函数中传入的backlog参数,表示serversocket中完成三次握手后(成功建立TCP连接)允许等待被accept的socket数量,已经建立好了TCP连接,但是socket还没有被accept取出来处理;


2. 小结

    tomcat中能够同时存在并被处理的请求数是maxThreads和maxConnections中的较小者(不管是连接器私有配置还是关联一个线程池);当正在被处理的请求数量达到这个值的时候,再过来的请求会被接受(TCP连接建立成功),但是没有被处理,被阻塞到serversocket上(对客户端浏览器而言,就是响应慢,页面标签一直在刷新等待状态,直到请求被处理),接受并被阻塞的socket请求数的最大值为acceptCount;当阻塞达到acceptCount的最大数目时,在发送过来的请求会建立连接refuse,对客户端而言,返回连接被拒绝的错误。

    所以,tomcat能接受的最大请求数量为maxThreads or maxConnections中的较小者,加上acceptCount的数量,其中前者中的较小者是正在被处理的请求数量,acceptCount为等待被处理的请求数量,超过这两者之和的请求会被拒绝。


三、官网相关配置详解

1. 线程池的主要配置

Attribute Description
maxThreads

(int) The max number of active threads in this pool, default is 200

minSpareThreads

(int) The minimum number of threads always kept alive, default is 25

maxIdleTime

(int) The number of milliseconds before an idle thread shutsdown, unless the number of active threads are less or equal to minSpareThreads. Default value is 60000(1 minute)

maxQueueSize

(int) The maximum number of runnable tasks that can queue up awaiting execution before we reject them. Default value is Integer.MAX_VALUE


2. 连接器的主要配置

Attribute Description
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.

executor A reference to the name in an Executor element. If this attribute is set, and the named executor exists, the connector will use the executor, and all the other thread attributes will be ignored. Note that if a shared executor is not specified for a connector then the connector will use a private, internal executor to provide the thread pool.
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 varies by connector type. For BIO the default is the value of maxThreads unless an Executor is used in which case the default will be the value of maxThreads from the executor. For NIO and NIO2 the default is 10000. For APR/native, the default is 8192.

Note that for APR/native on Windows, the configured value will be reduced to the highest multiple of 1024 that is less than or equal to maxConnections. This is done for performance reasons.
If set to a value of -1, the maxConnections feature is disabled and connections are not counted.

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.
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.
minSpareThreads The minimum number of threads always kept running. If not specified, the default of 10 is used. 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. executor:http://tomcat.apache.org/tomcat-8.0-doc/config/executor.html

    2. connector:http://tomcat.apache.org/tomcat-8.0-doc/config/http.html


附注:

  本文如有错漏,烦请不吝指正,谢谢!

Tomcat 9.0.11线程配置不生效时,可以尝试以下解决办法: ### 检查配置文件 - **server.xml文件**:Tomcat线程配置通常在`server.xml`文件中进行,主要涉及`Connector`元素。确保配置正确,没有语法错误。例如,以下是一个配置线程池的示例: ```xml <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" executor="tomcatThreadPool"/> <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> ``` 要确保`maxThreads`、`minSpareThreads`等参数设置符合预期,并且`Connector`元素正确引用了`Executor`。 - **配置文件路径**:确认`server.xml`文件的路径是否正确,Tomcat是否加载了该文件。一般来说,`server.xml`位于Tomcat的`conf`目录下。 ### 检查Tomcat版本兼容性 - 确保所做的线程配置Tomcat 9.0.11版本兼容。不同版本的Tomcat可能在配置参数和语法上存在差异,查阅Tomcat 9.0.11的官方文档,确保使用的配置参数是该版本支持的。 ### 检查Java环境 - **Java版本**:确保使用的Java版本与Tomcat 9.0.11兼容。Tomcat 9.x通常要求Java 8或更高版本。可以通过以下命令检查Java版本: ```bash java -version ``` - **Java环境变量**:确保`JAVA_HOME`环境变量正确设置,Tomcat能够找到正确的Java运行环境。 ### 检查日志文件 - **catalina.out日志**:查看Tomcat的`logs/catalina.out`日志文件,查找是否有与线程配置相关的错误信息。日志中可能会提示配置文件加载失败、参数解析错误等问题,根据错误信息进行相应的处理。 - **调试日志**:可以将Tomcat的日志级别设置为调试级别,以便获取更详细的信息。在`logging.properties`文件中,将`org.apache.catalina.core`的日志级别设置为`FINE`或`FINER`: ```properties org.apache.catalina.core.level = FINE ``` ### 检查应用程序 - **应用程序配置**:某些应用程序可能会覆盖Tomcat线程配置。检查应用程序的配置文件,确保没有对线程池进行额外的配置。 - **应用程序代码**:检查应用程序代码中是否有手动创建线程池或修改线程配置的代码,这些代码可能会影响Tomcat线程配置。 ### 重启Tomcat - 在对配置文件进行修改后,确保彻底重启Tomcat,使配置生效。可以通过以下命令停止和启动Tomcat: ```bash ./shutdown.sh ./startup.sh ``` ### 检查权限问题 - 确保Tomcat进程有足够的权限读取和加载配置文件。检查`server.xml`文件的权限,确保Tomcat进程可以访问该文件。 ### 检查服务器资源 - 确保服务器有足够的资源来支持所配置线程数。如果服务器的内存、CPU等资源不足,可能会导致线程配置不生效。可以通过系统监控工具查看服务器的资源使用情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值