天天看點

Tomcat 7最大并發連接配接數的正确修改方法

本文轉自 http://blog.csdn.net/qysh123/article/details/11678903

這是個很簡單的問題,但是搜了一圈,發現大家都寫錯了。是以這裡總結一下:

幾乎所有的中文網頁都介紹,要修改Tomcat的預設最大并發連接配接數,應該進行如下設定(實際上這些步驟是錯誤的):

--------------------------------------------

在tomcat配置檔案server.xml中的<Connector ... />配置中,和連接配接數相關的參數有:

  

minProcessors:最小空閑連接配接線程數,用于提高系統處理性能,預設值為10

maxProcessors:最大連接配接線程數,即:并發處理的最大請求數,預設值為75

acceptCount:允許的最大連接配接數,應大于等于maxProcessors,預設值為100

enableLookups:是否反查域名,取值為:true或false。為了提高處理能力,應設定為false

connectionTimeout:網絡連接配接逾時,機關:毫秒。設定為0表示永不逾時,這樣設定有隐患的。通常可設定為30000毫秒。

其中和最大連接配接數相關的參數為maxProcessors和acceptCount。如果要加大并發連接配接數,應同時加大這兩個參數。

web server允許的最大連接配接數還受制于作業系統的核心參數設定,通常Windows是2000個左右,Linux是1000個左右。Unix中如何設定這些參數,請參閱Unix常用監控和管理指令

具體的配置資訊:

Java代碼

  1. <Connector className="org.apache.coyote.tomcat4.CoyoteConnector" port="8080"
  2. minProcessors="5" maxProcessors="75" enableLookups="true" redirectPort="8443"
  3. acceptCount="100" debug="0" connectionTimeout="20000 " useURIValidationHack="false"
  4. protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler"/>

--------------------------------------------

但是我仔細查了一圈,發現這個說法隻是以訛傳訛,并不适用于Tomcat 5.5以上的版本。這裡先教大家怎麼去查Tomcat的官網:

首先,在這裡:http://tomcat.apache.org/ 我們點選左側導航欄中“Documentation”下的Tomcat 7.0,進入到這個連結中:http://tomcat.apache.org/tomcat-7.0-doc/index.html ,詳細的資訊我們不用都看,在左側導航欄中有一個連結Configuration,我們點進去之後,再點選其左側導航欄中connector一項的HTTP,就進入到HTTP連接配接數及其他相關屬性的設定頁面了。在這裡(http://tomcat.apache.org/tomcat-7.0-doc/config/http.html)我們可以看到,在Connector的屬性配置中,壓根就沒有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 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).

是以我們需要設定的是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:

  1. <Connector port="8080" protocol="HTTP/1.1"   
               connectionTimeout="20000"   
               redirectPort="8443" acceptCount="500" maxThreads="400" />  
               

今天就記錄這麼多,希望大家以後在轉載别人的經驗時更用心些,不要老出現上面那些以訛傳訛的情況。也希望能對有些朋友起到幫助。