Tomcat6 內存和線程配置

1、修改啓動時內存參數、並指定JVM時區 (在windows server 2008 下時間少了8個小時)

在Tomcat上運行j2ee項目代碼時,經常會出現內存溢出的情況,解決辦法是在系統參數中增加系統參數: 

window下, 在catalina.bat最前面

set JAVA_OPTS=-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m

一定加在catalina.bat最前面

即set "CURRENT_DIR=%cd%"前面

linux下,在catalina.sh最前面增加

JAVA_OPTS="-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m -Duser.timezone=Asia/Shanghai"
 
注意:前後二者區別,有無set,有無雙引號


2、線程池配置(Tomcat6下)

使用線程池,用較少的線程處理較多的訪問,可以提高tomcat處理請求的能力

使用方式:首先,打開/conf/server.xml,增加

[html] view plaincopyprint?

  1. <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"  

  2.         maxThreads="500" minSpareThreads="20" maxIdleTime="60000" />  

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="500" minSpareThreads="20" maxIdleTime="60000" />


最大線程500(一般服務器足以),最小空閒線程數20,線程最大空閒時間60秒。

然後,修改<Connector ...>節點,增加executor屬性,如:


[html] view plaincopyprint?

  1. <Connector executor="tomcatThreadPool"  

  2.                port="80" protocol="HTTP/1.1"  

  3.                connectionTimeout="60000"  

  4.                keepAliveTimeout="15000"  

  5.                maxKeepAliveRequests="1"  

  6.                redirectPort="443"  

  7.                ....../>  

<Connector executor="tomcatThreadPool"
               port="80" protocol="HTTP/1.1"
               connectionTimeout="60000"
               keepAliveTimeout="15000"
               maxKeepAliveRequests="1"
               redirectPort="443"
               ....../>


注意:可以多個connector公用1個線程池


3、調整連接相關Connector的參數


[html] view plaincopyprint?

  1. <Connector executor="tomcatThreadPool"  

  2.                port="80" protocol="HTTP/1.1"  

  3.                connectionTimeout="60000"  

  4.                keepAliveTimeout="15000"  

  5.                maxKeepAliveRequests="1"  

  6.                redirectPort="443"  

  7.                maxHttpHeaderSize="8192" URIEncoding="UTF-8" enableLookups="false" acceptCount="100" disableUploadTimeout="true"/>  

<Connector executor="tomcatThreadPool"
               port="80" protocol="HTTP/1.1"
               connectionTimeout="60000"
               keepAliveTimeout="15000"
               maxKeepAliveRequests="1"
               redirectPort="443"
               maxHttpHeaderSize="8192" URIEncoding="UTF-8" enableLookups="false" acceptCount="100" disableUploadTimeout="true"/>


參數說明:


connectionTimeout - 網絡連接超時,單位:毫秒。設置爲0表示永不超時,這樣設置有隱患的。通常可設置爲30000毫秒

keepAliveTimeout - 長連接最大保持時間(毫秒)。此處爲15秒

maxKeepAliveRequests - 最大長連接個數(1表示禁用,-1表示不限制個數,默認100個。一般設置在100~200之間) the maximum number of HTTP requests that can be held in the pipeline until the connection is closed by the server. Setting this attribute to 1 disables HTTP/1.0 keep-alive, as well as HTTP/1.1 keep-alive and pipelining. Setting this to -1 allows an unlimited number of pipelined or keep-alive HTTP requests. If not specified, this attribute is set to 100

maxHttpHeaderSize - http請求頭信息的最大程度,超過此長度的部分不予處理。一般8K

URIEncoding - 指定Tomcat容器的URL編碼格式

acceptCount - 指定當所有可以使用的處理請求的線程數都被使用時,可以放到處理隊列中的請求數,超過這個數的請求將不予處理,默認爲10個。defines 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 are refused. The default value is 10.

disableUploadTimeout - 上傳時是否使用超時機制

enableLookups - 是否反查域名,取值爲:true或false。爲了提高處理能力,應設置爲false

bufferSize - defines the size (in bytes) of the buffer to be provided for input streams created by this connector. By default, buffers of 2048 bytes are provided.

maxSpareThreads - 做多空閒連接數,一旦創建的線程超過這個值,Tomcat就會關閉不再需要的socket線程 the maximum number of unused request processing threads that are allowed to exist until the thread pool starts stopping the unnecessary threads. The default value is 50

maxThreads - 最多同時處理的連接數,Tomcat使用線程來處理接收的每個請求。這個值表示Tomcat可創建的最大的線程數。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

minSpareThreads - 最小空閒線程數,Tomcat初始化時創建的線程數 the number of request processing threads that are created when this Connector is first started. The connector will also make sure it has the specified number of idle processing threads available. This attribute should be set to a value smaller than that set for maxThreads. The default value is 4

minProcessors - 最小空閒連接線程數,用於提高系統處理性能,默認值爲10。(用於Tomcat4中)

maxProcessors - 最大連接線程數,即:併發處理的最大請求數,默認值爲75。(用於Tomcat4中)

備註:

Tomcat4中可以通過修改minProcessors和maxProcessors的值來控制線程數。

在Tomcat5+主要對以下參數調整

maxThreads

Tomcat使用線程來處理接收的每個請求。這個值表示Tomcat可創建的最大的線程數。

acceptCount

指定當所有可以使用的處理請求的線程數都被使用時,可以放到處理隊列中的請求數,超過這個數的請求將不予處理。

connnectionTimeout

網絡連接超時,單位:毫秒。設置爲0表示永不超時,這樣設置有隱患的。通常可設置爲30000毫秒。

minSpareThreads

Tomcat初始化時創建的線程數。

maxSpareThreads

一旦創建的線程超過這個值,Tomcat就會關閉不再需要的socket線程。  


4、負載均衡、集羣的配置

Tomcat6支持分佈式部署,可以實現集羣功能,提高響應能力。


5、利用JMX監控Tomcat運行情況,需要手工調整啓動參數,如下:

打開cataline.bat,增加一行

set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.port=10090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"

linux下修改cataline.sh:

JAVA_OPTS="-Dcom.sun.management.jmxremote.port=10090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=%CATALINA_BASE\conf\logging.properties"

注意JDK\jre\lib\management\management.properties文件必須存在。

重新啓動tomcat節點,然後用jconsole連接(此處端口wei10090)


6、Tomcat增加一個應用

在server.xml的Host標籤中增加行

<Context displayName="OA" docBase="/app/web-apps/GACWP" path="" />

path代表上下文名稱,空表示是根路徑。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章