tomcat 優化

本文來源社區:java

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

 

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

 

window下, 在catalina.bat最前面:
set JAVA_OPTS=-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m
必定加在catalina.bat最前面。apache

 

linux下,在catalina.sh最前面增長:windows

JAVA_OPTS="-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m -Duser.timezone=Asia/Shanghai"tomcat

 

注意:先後兩者區別,有無set,有無雙引號。服務器

 

 

二、線程池配置(Tomcat6下)網絡

使用線程池,用較少的線程處理較多的訪問,能夠提升tomcat處理請求的能力。使用方式:併發

首先。打開/conf/server.xml,增長app

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

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

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

<Connector executor="tomcatThreadPool" 
               port="80"

protocol="HTTP/1.1"

maxThreads="600"

minSpareThreads="100"

maxSpareThreads="300"
               connectionTimeout="60000"
               keepAliveTimeout="15000"
               maxKeepAliveRequests="1"
               redirectPort="443"
               ....../>

maxThreads:Tomcat可建立的最大的線程數,每個線程處理一個請求;

minSpareThreads:最小備用線程數,tomcat啓動時的初始化的線程數;

maxSpareThreads:最大備用線程數,一旦建立的線程超過這個值,Tomcat就會關閉再也不須要的socket線程;

acceptCount:指定當全部能夠使用的處理請求的線程數都被使用時,能夠放處處理隊列中的請求數,就是被排隊的請求數,超過這個數的請求將拒絕鏈接。

connnectionTimeout:網絡鏈接超時,單位:毫秒。設置爲0表示永不超時,這樣設置有隱患的。一般可設置爲30000毫秒。 
enableLookups:是否容許DNS查詢

 

注意:能夠多個connector公用1個線程池。

 

三、調整鏈接相關Connector的參數:

<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線程。  

 

 

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

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

 

五、

利用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)

 

六、Tomcat增長一個應用

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

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

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

相關文章
相關標籤/搜索