對tomcat來說,每一個進來的請求(request)都需要一個線程,直到該請求結束。


這段時間折騰了哈java web應用的壓力測試,部署容器是tomcat 7。期間學到了蠻多散碎的知識點,及時梳理總結,構建良好且易理解的知識架構把它們組織起來,以備忘。
對web應用開發者來說,我們很關心應用可同時處理的請求數,以及響應時間。應用本身和它運行在其中的web容器是兩個很重要的影響因素。
對tomcat來說,每一個進來的請求(request)都需要一個線程,直到該請求結束。如果同時進來的請求多於當前可用的請求處理線程數,額外的線程就會被創建,直到到達配置的最大線程數(maxThreads屬性值)。如果仍就同時接收到更多請求,這些來不及處理的請求就會在Connector創建的ServerSocket中堆積起來,直到到達最大的配置值(acceptCount屬性值)。至此,任何再來的請求將會收到connection refused錯誤,直到有可用的資源來處理它們。

分析、梳理、組織

這里我們關心的是tomcat能同時處理的請求數和請求響應時間,顯然Connector元素的maxThreads和acceptCount屬性對其有直接的影響。無論acceptCount值為多少,maxThreads直接決定了實際可同時處理的請求數。而不管maxThreads如何,acceptCount則決定了有多少請求可等待處理。然而,不管是可立即處理請求還是需要放入等待區,都需要tomcat先接受該請求(即接受client的連接請求,建立socketchannel),那么tomcat同時可建立的連接數(maxConnections屬性值)也會影響可同時處理的請求數。
我們可把tomcat想象成一家醫院,你來到醫院大廳掛號看病,如果人家接受了,就相當於client和server建立socket連接了。接着你來到相應的科室,科室里每位醫生都有一間診室,這就相當於處理請求的線程;如果所有診室都有病人,科室的調度護士會讓你在科室小廳中耐心等待,直到他們通知你去幾號診室就診;如果有空閑醫生,你就可以立即就診。
有的病人到醫院很倉促,結果輪到他掛號或者就診了,他還在包里翻找病例本和醫保卡,如果超過了護士或醫生心里可承受的等待時間,他們就會讓病人到旁邊找去,先服務下位。這種情形跟Connector元素的connectionTimeout屬性所起的作用很相像。如果當前連接器(Connector)在接受連接后,等待了指定的時間但仍未接收到requestURI line,就會拋出超時異常。

知識點收集

tomcat 7 的配置參考文檔對相關屬性已經描述的很詳細了,這里把它們收集到一起:

protocol

Sets the protocol to handle incoming traffic. The default valueis HTTP/1.1 which uses an auto-switching mechanism to select either a blockingJava based connector or an APR/native based connector. If the PATH (Windows) orLD_LIBRARY_PATH (on most unix systems) environment variables contain the Tomcatnative library, the APR/native connector will be used. If the native librarycannot be found, the blocking Java based connector will be used. Note that theAPR/native connector has different settings for HTTPS than the Java connectors.
To use an explicit protocol rather than rely on the auto-switching mechanismdescribed 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 Javaconnectors is identical, for http and https.
For more information on the APR connector and APR specific SSL settings pleasevisit the APR documentation

maxThreads

The maximum number of request processing threads to be createdby this Connector, which therefore determines the maximum number ofsimultaneous requests that can be handled. If not specified, this attribute isset to 200. If an executor is associated with this connector, this attribute isignored as the connector will execute tasks using the executor rather than aninternal thread pool.

acceptCount

The maximum queue length for incoming connection requests whenall possible request processing threads are in use. Any requests received whenthe queue is full will be refused. The default value is 100.

maxConnections

The maximum number of connections that the server will acceptand process at any given time. When this number has beenreached, the server will accept, but not process, one further connection. Thisadditional connection be blocked until the number of connections beingprocessed falls below maxConnections at which point the server will startaccepting and processing new connections again. Note that once the limit hasbeen reached, the operating system may still accept connections based on theacceptCount setting. Thedefault value varies by connector type. For BIO the default is the value of maxThreads unless an Executor isused in which case the default will be the value of maxThreads from theexecutor. 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 tothe highest multiple of 1024 that is less than or equal to maxConnections. Thisis done for performance reasons.
If set to a value of -1, the maxConnections feature is disabled and connectionsare not counted.

connectionTimeout

The number of milliseconds this Connector will wait,after accepting a connection, for the request URI lineto 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 thatthe standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20seconds). Unless disableUploadTimeout is set to false, this timeout willalso be used when reading the request body (if any).

進一步分析

tomcat的http connector有三種:bio、nio、apr。從上面的屬性描述中可以看出對於不同的connector實現,相同的屬性可能會有不同的默認值和不同的處理策略,所以在調整配置前,要先弄清楚各種實現之間的不同,以及當前部署容器使用的是哪種connector。
查閱Tomcat7 http connector 配置文檔Connector Comparison部分便可獲知各種connector實現間的差異。
怎樣才能知道容器使用的是何種connector實現?啟動tomcat后,訪問Server Status Page,看到如下信息即可知道使用的是何種connector:

我的OS是windows,所以tomcat默認使用的是aprconnector。在linux上,默認使用的是bio connector。與nio相比,bio性能較低。將<TOMCAT_HOME>/conf/server.xml中的如下配置片段:

<Connectorport="8080"protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

修改為:

<Connectorport="8080"protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               redirectPort="8443" />

就可將http connector切換至nio了。更多細節請參考修改Tomcat Connector運行模式,優化Tomcat運行性能

 

官網說明:http://tomcat.apache.org/tomcat-7.0-doc/config/http.html

一、最大線程數的設置

Tomcat的server.xml中連接器設置如下

   <Connectorport="8080"  maxThreads="150"minSpareThreads="25" maxSpareThreads="75"  enableLookups="false"redirectPort="8443" acceptCount="100"  debug="0"connectionTimeout="20000"  disableUploadTimeout="true"/> 

 tomcat在配置時設置最大線程數,當前線程數超過這個數值時會出錯,那么有沒有辦法捕獲到這個錯誤,從而在client端顯示出錯信息?

 

2. 如何加大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個左右。tomcat5中的配置示例:
    <Connector port="8080"
              maxThreads="150" minSpareThreads="25"maxSpareThreads="75"
              enableLookups="false" redirectPort="8443"acceptCount="100"
              debug="0" connectionTimeout="20000" 
              disableUploadTimeout="true" />
對於其他端口的偵聽配置,以此類推。

 

3. tomcat中如何禁止列目錄下的文件
在{tomcat_home}/conf/web.xml中,把listings參數設置成false即可,如下:

1.  <init-param>  

2.  <param-name>listings</param-name>  

3.  <param-value>false</param-value>  

4.  </init-param>  

4.如何加大tomcat可以使用的內存
tomcat默認可以使用的內存為128MB,在較大型的應用項目中,這點內存是不夠的,需要調大。
Unix下,在文件{tomcat_home}/bin/catalina.sh的前面,增加如下設置:
JAVA_OPTS='-Xms【初始化內存大小】 -Xmx【可以使用的最大內存】'
需要把這個兩個參數值調大。例如:
JAVA_OPTS='-Xms256m -Xmx512m'
表示初始化內存為256MB,可以使用的最大內存為512MB

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM