1. 程式人生 > >tomcat連線數、執行緒數關係

tomcat連線數、執行緒數關係

個人對tomcat聯結器3個屬性maxConnections、maxThreads、acceptCount的理解:
先摘取官網對這3個屬性的描述:

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.
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 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.
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.
其中maxConnections描述紅色部分說明當連線數達到最大值後,系統會繼續接收連線但不會超過acceptCount的值。
理解:

我們可以把tomcat比做一個電影院,流程是取號、買票、觀影,acceptCount比作前廳(容納取到號的人)、maxConnections比作大廳(容納買到票的人)、maxThreads比作影廳(可以理解一個影廳只容納一個人,因為一個執行緒同時只處理一個請求),以下場景是針對已達到maxConnections最大值來討論的

1)取號:如果前廳人數已達到acceptCount,則拿號失敗,會得到Connection refused connect的回覆資訊。反之則會進入前廳,等待買票。

2)買票:當大廳人數小於maxConnections時,前廳的人就可以進入大廳

3)觀影:當影廳的人離開時,大廳的部分人能進入影廳,一般來講大廳的容量要遠大於影廳的數量。

本文是針對apache-tomcat-7.0.55做的測試。

tomcat server.xml配置引數

<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" acceptCount="2" maxConnections="10" maxThreads="2"
           connectionTimeout="20000"
           redirectPort="8443" />

Servlet的程式碼:休眠20秒

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
InputStream is = request.getInputStream();
System.out.println(new Date()+”:”+is+”開始”);
Thread.sleep(20000);
System.out.println(new Date()+”:”+is+”結束”);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
一個請求或一個connection的生命週期:可以簡單的理解為從doGet或doPost開始response響應結束。
使用JMeter測試,執行緒數20,連線超時時間10s,響應超時時間30s

測試結果:

察看結果數:

第1秒:8個請求得到響應資料:Connection refused connect

第20秒:2個請求正常響應

第30秒:剩餘10個請求得到響應資料:Readtimed out

tomcat日誌:

列印12組開始結束的日誌,說明tomcat會處理acceptCount+maxConnections的請求,說明只要取到號,有足夠的耐心,就肯定能夠看到電影。

maxThread 最大併發量
maxConnections + acceptCount 最大連線數,它們與maxThread沒有一點關係。