轉自: http://www.jianshu.com/p/8445645b3aff
引言
這段時間折騰了哈java web應用的壓力測試,部署容器是tomcat 7。期間學到了蠻多散碎的知識點,及時梳理總結,構建良好且易理解的知識架構把它們組織起來,以備忘。
對web應用開發者來說,我們很關心應用可同時處理的請求數,以及響應時間。應用本身和它運行在其中的web容器是兩個很重要的影響因素。
對tomcat來說,每一個進來的請求(request)都需要一個線程,直到該請求結束。如果同時進來的請求多于當前可用的請求處理線程數,額外的線程就會被創建,直到到達配置的最大線程數(maxThreads屬性值)。如果仍就同時接收到更多請求,這些來不及處理的請求就會在Connector創建的Server Socket中堆積起來,直到到達最大的配置值(acceptCount屬性值)。至此,任何再來的請求將會收到connection refused錯誤,直到有可用的資源來處理它們。
分析、梳理、組織
這里我們關心的是tomcat能同時處理的請求數和請求響應時間,顯然Connector元素的maxThreads和acceptCount屬性對其有直接的影響。無論acceptCount值為多少,maxThreads直接決定了實際可同時處理的請求數。而不管maxThreads如何,acceptCount則決定了有多少請求可等待處理。然而,不管是可立即處理請求還是需要放入等待區,都需要tomcat先接受該請求(即接受client的連接請求,建立socket channel),那么tomcat同時可建立的連接數(maxConnections屬性值)也會影響可同時處理的請求數。
我們可把tomcat想象成一家醫院,你來到醫院大廳掛號看病,如果人家接受了,就相當于client和server建立socket連接了。接著你來到相應的科室,科室里每位醫生都有一間診室,這就相當于處理請求的線程;如果所有診室都有病人,科室的調度護士會讓你在科室小廳中耐心等待,直到他們通知你去幾號診室就診;如果有空閑醫生,你就可以立即就診。
有的病人到醫院很倉促,結果輪到他掛號或者就診了,他還在包里翻找病例本和醫保卡,如果超過了護士或醫生心里可承受的等待時間,他們就會讓病人到旁邊找去,先服務下位。這種情形跟Connector元素的connectionTimeout屬性所起的作用很相像。如果當前連接器(Connector)在接受連接后,等待了指定的時間但仍未接收到request URI line,就會拋出超時異常。
知識點收集
tomcat 7 的配置參考文檔對相關屬性已經描述的很詳細了,這里把它們收集到一起:
protocol
Sets the protocol to handle incoming traffic. The default value is HTTP/1.1 which uses an auto-switching mechanism to select either a blocking Java based connector or an APR/native based connector. If the PATH (Windows) or LD_LIBRARY_PATH (on most unix systems) environment variables contain the Tomcat native library, the APR/native connector will be used. If the native library cannot be found, the blocking Java based connector will be used. Note that the APR/native connector has different settings for HTTPS than the Java connectors.
To use an explicit protocol rather than rely on the auto-switching mechanism described above, the following values may be used:
org.apache.coyote.http11.Http11Protocol - blocking Java connector
org.apache.coyote.http11.Http11NioProtocol - non blocking Java connector
org.apache.coyote.http11.Http11AprProtocol - the APR/native connector.
Custom implementations may also be used.
Take a look at our Connector Comparison chart. The configuration for both Java connectors is identical, for http and https.
For more information on the APR connector and APR specific SSL settings please visit the APR documentation
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.
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.
connectionTimeout
The number of milliseconds this? Connector ?will? wait , after accepting a connection,? for the request URI line to be presented . Use a value of -1 to indicate no (i.e. infinite) timeout. The default value is 60000 (i.e. 60 seconds) but note that the standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20 seconds). Unless? disableUploadTimeout ?is set to false, this timeout will also be used when reading the request body (if any).
進一步分析
tomcat的http connector有三種:bio、nio、apr。從上面的屬性描述中可以看出對于不同的connector實現,相同的屬性可能會有不同的默認值和不同的處理策略,所以在調整配置前,要先弄清楚各種實現之間的不同,以及當前部署容器使用的是哪種connector。
查閱
Tomcat 7 http connector 配置文檔
Connector Comparison部分便可獲知各種connector實現間的差異。
怎樣才能知道容器使用的是何種connector實現?啟動tomcat后,訪問
Server Status Page
,看到如下信息即可知道使用的是何種connector:
我的OS是windows,所以tomcat默認使用的是apr connector。在linux上,默認使用的是bio connector。與nio相比,bio性能較低。將<TOMCAT_HOME>/conf/server.xml中的如下配置片段:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改為:
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" />
就可將http connector切換至nio了。更多細節請參考 修改Tomcat Connector運行模式,優化Tomcat運行性能
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
