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表明上下文名称,空表示是根路径。

相关文章
相关标签/搜索