首先,我们如果需要修改tomcat 7的最大连接数,我们可以去tomcat官方网站,查看Documentation 进入tomcat的官方网站http://tomcat.apache.org 我们点击左侧导航栏中"Documentation"下的Tomcat 7.0 进入到这个链接后,详细的信息我们不用都看,注意在左侧导航栏Reference下有一个链接Configuration菜单选择项 我们点进去之后,再点击其左侧导航栏中connector一项的HTTP,就进入到HTTP连接数及其他相关属性的设置页面了。在这里 http://tomcat.apache.org/tomcat-7.0-doc/config/http.html我们可以看到,在Connector的属性配置中,网上有很多文章说可以修改maxProcessors等参数 但这里的官方文档上压根就没有maxProcessors等的设置选项。其中这句话已经介绍得很清楚: 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 configuredmaximum (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). 所以我们需要设置tomcat的最大连接数就只需要修改maxThreads和acceptCount这两个值: 其中,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. 而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. 所以两者的默认值分别是200和100,要调整Tomcat的默认最大连接数,可以增加这两个属性的值,并且使acceptCount大于等于maxThreads: 对于tomcat是修改./server.xml文件,对于virgo-tomcat则是修改./configuration/tomcat-server.xml文件,配置的格式都大同小异的,这里以virgo-tomcat为例 <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxThreads="400" acceptCount="500" URIEncoding="UTF-8" compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml" />
其中上述配置的意义如下: maxThreads="400" 即最大线程数,表示最多同时处理400个连接 ,默认为200 acceptCount="500" 最大排队数,当同时连接的数量达到maxThreads时,还可以接收排队的连接,超过这个连接的则直接返回拒绝连接。 简单的说,对于maxThreads,acceptCount有如下三种情况 情况1:接受一个请求,此时tomcat起动的线程数没有到达maxThreads,tomcat会起动一个线程来处理此请求。 情况2:接受一个请求,此时tomcat起动的线程数已经到达maxThreads,tomcat会把此请求放入等待队列,等待空闲线程。 情况3:接受一个请求,此时tomcat起动的线程数已经到达maxThreads,等待队列中的请求个数也达到了acceptCount,此时tomcat会直接拒绝此次请求,返回connection refused (责任编辑:IT) |