Tomcat 7最大併發鏈接數的正確修改方法

這是個很簡單的問題,可是搜了一圈,發現你們都寫錯了。因此這裏總結一下:html

幾乎全部的中文網頁都介紹,要修改Tomcat的默認最大併發鏈接數,應該進行以下設置(實際上這些步驟是錯誤的):web

--------------------------------------------apache

在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代碼tomcat

 

  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等的設置選項。其中這句話已經介紹得很清楚:socket

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).ide

因此咱們須要設置的是maxThreads和acceptCount這兩個值:性能

其中,maxThreads的介紹以下:this

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"   
  2.            connectionTimeout="20000"   
  3.            redirectPort="8443" acceptCount="500" maxThreads="400" />  

Html代碼  

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


今天就記錄這麼多,但願你們之後在轉載別人的經驗時更用心些,不要老出現上面那些以訛傳訛的狀況。也但願能對有些朋友起到幫助。

maxThreads 是處理請求的線程數,acceptCount 是等待隊列,acceptCount並非必定要大於等於maxThreads。 maxThreads 滿了,進入acceptCount ,acceptCount 也滿了,則 拒絕請求

相關文章
相關標籤/搜索