1桨醋、修改啟動(dòng)時(shí)內(nèi)存參數(shù)拉队、并指定JVM時(shí)區(qū) (在windows server 2008 下時(shí)間少了8個(gè)小時(shí)):
在Tomcat上運(yùn)行j2ee項(xiàng)目代碼時(shí),經(jīng)常會(huì)出現(xiàn)內(nèi)存溢出的情況,解決辦法是在系統(tǒng)參數(shù)中增加系統(tǒng)參數(shù):
window下香追, 在catalina.bat最前面:
set JAVA_OPTS=-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m;-Duser.timezone=GMT+08;
一定加在catalina.bat最前面。
linux下坦胶,在catalina.sh最前面增加:
JAVA_OPTS="-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m -Duser.timezone=Asia/Shanghai"
注意:前后二者區(qū)別透典,有無(wú)set歪玲,有無(wú)雙引號(hào)。
2掷匠、線程池配置(Tomcat6下)
使用線程池滥崩,用較少的線程處理較多的訪問(wèn),可以提高tomcat處理請(qǐng)求的能力讹语。使用方式:
首先钙皮。打開(kāi)/conf/server.xml,增加
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="500" minSpareThreads="20" maxIdleTime="60000" />
最大線程500(一般服務(wù)器足以)顽决,最小空閑線程數(shù)20短条,線程最大空閑時(shí)間60秒。
然后才菠,修改<Connector ...>節(jié)點(diǎn)茸时,增加executor屬性,如:
?executor="tomcatThreadPool"
port="80"
protocol="HTTP/1.1"
maxThreads="600"
minSpareThreads="100"
maxSpareThreads="300"
connectionTimeout="60000"
keepAliveTimeout="15000"
maxKeepAliveRequests="1"
redirectPort="443"
....../>
maxThreads:Tomcat可創(chuàng)建的最大的線程數(shù)赋访,每一個(gè)線程處理一個(gè)請(qǐng)求可都;
minSpareThreads:最小備用線程數(shù),tomcat啟動(dòng)時(shí)的初始化的線程數(shù)蚓耽;
maxSpareThreads:最大備用線程數(shù)渠牲,一旦創(chuàng)建的線程超過(guò)這個(gè)值,Tomcat就會(huì)關(guān)閉不再需要的socket線程步悠;
acceptCount:指定當(dāng)所有可以使用的處理請(qǐng)求的線程數(shù)都被使用時(shí)签杈,可以放到處理隊(duì)列中的請(qǐng)求數(shù),就是被排隊(duì)的請(qǐng)求數(shù)鼎兽,超過(guò)這個(gè)數(shù)的請(qǐng)求將拒絕連接答姥。
connnectionTimeout:網(wǎng)絡(luò)連接超時(shí),單位:毫秒谚咬。設(shè)置為0表示永不超時(shí)鹦付,這樣設(shè)置有隱患的。通承蚧拢可設(shè)置為30000毫秒睁壁。
enableLookups:是否允許DNS查詢
注意:可以多個(gè)connector公用1個(gè)線程池。
3互捌、調(diào)整連接相關(guān)Connector的參數(shù):
<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"/>
參數(shù)說(shuō)明:
connectionTimeout?- 網(wǎng)絡(luò)連接超時(shí)潘明,單位:毫秒。設(shè)置為0表示永不超時(shí)秕噪,這樣設(shè)置有隱患的钳降。通常可設(shè)置為30000毫秒腌巾。
keepAliveTimeout?- 長(zhǎng)連接最大保持時(shí)間(毫秒)遂填。此處為15秒铲觉。
maxKeepAliveRequests?- 最大長(zhǎng)連接個(gè)數(shù)(1表示禁用,-1表示不限制個(gè)數(shù)吓坚,默認(rèn)100個(gè)撵幽。一般設(shè)置在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請(qǐng)求頭信息的最大程度,超過(guò)此長(zhǎng)度的部分不予處理礁击。一般8K盐杂。
URIEncoding?- 指定Tomcat容器的URL編碼格式。
acceptCount?- 指定當(dāng)所有可以使用的處理請(qǐng)求的線程數(shù)都被使用時(shí)哆窿,可以放到處理隊(duì)列中的請(qǐng)求數(shù)链烈,超過(guò)這個(gè)數(shù)的請(qǐng)求將不予處理,默認(rèn)為10個(gè)挚躯。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?- 上傳時(shí)是否使用超時(shí)機(jī)制
enableLookups?- 是否反查域名强衡,取值為:true或false。為了提高處理能力码荔,應(yīng)設(shè)置為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?- 做多空閑連接數(shù)漩勤,一旦創(chuàng)建的線程超過(guò)這個(gè)值,Tomcat就會(huì)關(guān)閉不再需要的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?- 最多同時(shí)處理的連接數(shù)目胡,Tomcat使用線程來(lái)處理接收的每個(gè)請(qǐng)求锯七。這個(gè)值表示Tomcat可創(chuàng)建的最大的線程數(shù)。誉己。 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?- 最小空閑線程數(shù),Tomcat初始化時(shí)創(chuàng)建的線程數(shù) 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?- 最小空閑連接線程數(shù)域蜗,用于提高系統(tǒng)處理性能巨双,默認(rèn)值為10。(用于Tomcat4中)
maxProcessors?- 最大連接線程數(shù)霉祸,即:并發(fā)處理的最大請(qǐng)求數(shù)筑累,默認(rèn)值為75。(用于Tomcat4中)
備注:
Tomcat4中可以通過(guò)修改minProcessors和maxProcessors的值來(lái)控制線程數(shù)丝蹭。
在Tomcat5+主要對(duì)以下參數(shù)調(diào)整
maxThreads
Tomcat使用線程來(lái)處理接收的每個(gè)請(qǐng)求慢宗。這個(gè)值表示Tomcat可創(chuàng)建的最大的線程數(shù)。
acceptCount
指定當(dāng)所有可以使用的處理請(qǐng)求的線程數(shù)都被使用時(shí)奔穿,可以放到處理隊(duì)列中的請(qǐng)求數(shù)镜沽,超過(guò)這個(gè)數(shù)的請(qǐng)求將不予處理。
connnectionTimeout
網(wǎng)絡(luò)連接超時(shí)贱田,單位:毫秒缅茉。設(shè)置為0表示永不超時(shí),這樣設(shè)置有隱患的男摧。通呈叨眨可設(shè)置為30000毫秒译打。
minSpareThreads
Tomcat初始化時(shí)創(chuàng)建的線程數(shù)。
maxSpareThreads
一旦創(chuàng)建的線程超過(guò)這個(gè)值拇颅,Tomcat就會(huì)關(guān)閉不再需要的socket線程奏司。
4、負(fù)載均衡樟插、集群的配置
Tomcat6支持分布式部署结澄,可以實(shí)現(xiàn)集群功能,提高響應(yīng)能力岸夯。
5麻献、利用JMX監(jiān)控Tomcat運(yùn)行情況,需要手工調(diào)整啟動(dòng)參數(shù)猜扮,如下:
打開(kāi)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文件必須存在。
重新啟動(dòng)tomcat節(jié)點(diǎn)旅赢,然后用jconsole連接(此處端口wei10090)
6齿桃、Tomcat增加一個(gè)應(yīng)用
在server.xml的Host標(biāo)簽中增加行
<Context displayName="OA" docBase="/app/web-apps/GACWP" path="" />
path代表上下文名稱,空表示是根路徑煮盼。