首先看官網(wǎng)對(duì)這個(gè)三個(gè)參數(shù)的說(shuō)明:
http://tomcat.apache.org/tomcat-7.0-doc/config/http.html
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. |
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. |
用httpClient作為客戶端對(duì)不同配置分別發(fā)10個(gè)請(qǐng)求鹃唯,服務(wù)端死循環(huán)讓請(qǐng)求一直沒(méi)有返回铭污,然后查看連接情況。
注意饮焦,在用httpClient發(fā)送請(qǐng)求的時(shí)候一定要設(shè)置這個(gè)參數(shù)伏嗜,否則默認(rèn)最多只能發(fā)送兩個(gè)并發(fā)請(qǐng)求府喳。
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
一##
tomcat配置:
<Connector port="8090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" maxConnections="3" maxThreads="2" acceptCount="4" />
連接情況7個(gè)為ESTABLISHED落塑,3個(gè)為SYN_SENT:
netstat -an | grep 8090##
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61515 ESTABLISHED
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61514 ESTABLISHED
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61513 ESTABLISHED
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61512 ESTABLISHED
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61511 ESTABLISHED
tcp4 0 0 127.0.0.1.8090 127.0.0.1.61510 ESTABLISHED
tcp4 0 0 127.0.0.1.8090 127.0.0.1.61509 ESTABLISHED
tcp4 0 0 127.0.0.1.61510 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61511 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61512 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61513 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61514 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61515 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61517 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61509 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61518 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61516 127.0.0.1.8090 SYN_SENT
tcp46 0 0 *.8090 . LISTEN
二##
tomcat配置:
<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxConnections="5" maxThreads="2" acceptCount="1" />
連接情況6個(gè)為ESTABLISHED蛤克,4個(gè)為SYN_SENT:
netstat -an | grep 8090#
tcp4 0 0 127.0.0.1.61672 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61671 127.0.0.1.8090 SYN_SENT
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61663 ESTABLISHED
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61670 ESTABLISHED
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61669 ESTABLISHED
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61668 ESTABLISHED
tcp4 0 0 127.0.0.1.8090 127.0.0.1.61662 ESTABLISHED
tcp4 0 0 127.0.0.1.8090 127.0.0.1.61661 ESTABLISHED
tcp4 0 0 127.0.0.1.61669 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61668 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61661 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61667 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61662 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61663 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61664 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61670 127.0.0.1.8090 ESTABLISHED
tcp46 0 0 *.8090 . LISTEN
三##
tomcat配置:
<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxConnections="0" maxThreads="2" acceptCount="1" />
連接情況3個(gè)為ESTABLISHED,7個(gè)為SYN_SENT:
netstat -an | grep 8090#
tcp4 158 0 127.0.0.1.8090 127.0.0.1.61748 ESTABLISHED
tcp4 0 0 127.0.0.1.8090 127.0.0.1.61743 ESTABLISHED
tcp4 0 0 127.0.0.1.8090 127.0.0.1.61742 ESTABLISHED
tcp4 0 0 127.0.0.1.61744 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61748 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61743 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61742 127.0.0.1.8090 ESTABLISHED
tcp4 0 0 127.0.0.1.61749 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61747 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61750 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61751 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61745 127.0.0.1.8090 SYN_SENT
tcp4 0 0 127.0.0.1.61746 127.0.0.1.8090 SYN_SENT
tcp46 0 0 *.8090 . LISTEN
四##
tomcat配置:
<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxConnections="0" maxThreads="2" acceptCount="0" />
這次改為發(fā)了101個(gè)請(qǐng)求闷祥,結(jié)果連接狀態(tài)都為ESTABLISHED娱颊。
做這幾個(gè)實(shí)驗(yàn),主要是讀文檔的時(shí)候沒(méi)有弄清楚acceptCount是當(dāng)maxThreads達(dá)到最大值時(shí)起作用還是當(dāng)maxConnections達(dá)到最大值時(shí)起作用凯砍。
最后通過(guò)以上得出結(jié)論:##
1)tomcat可接受最大連接數(shù)為maxConnections+acceptCount箱硕;
2)當(dāng)maxConnections="0",maxConnections的值等于maxThreads悟衩;
3)當(dāng)acceptCount="0"剧罩,acceptCount的值不是默認(rèn)的100,具體多少取決操作系統(tǒng)的配置(后面看下tomcat的源碼)座泳。
4)maxThreads就是真正同時(shí)在處理請(qǐng)求的業(yè)務(wù)線程(可以通過(guò)看stack日志確定)惠昔。
其實(shí)上面這些通過(guò)看tomcat的源碼最為準(zhǔn)確了,做這個(gè)測(cè)試主要是為了打發(fā)下時(shí)間挑势。另外對(duì)tcp的連接狀態(tài)不太清楚的可以看下tcp/ip詳解的第18章镇防。