tomcat配置及调优

1、 linux下的tomcat:  

修改TOMCAT_HOME/bin/catalina.sh 
位置cygwin=false前(行数大约99)。
JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m" 

 

引言

这段时间折腾了哈java web应用的压力测试,部署容器是tomcat 7。期间学到了蛮多散碎的知识点,及时梳理总结,构建良好且易理解的知识架构把它们组织起来,以备忘。
对web应用开发者来说,我们很关心应用可同时处理的请求数,以及响应时间。应用本身和它运行在其中的web容器是两个很重要的影响因素。
对tomcat来说,每一个进来的请求(request)都需要一个线程,直到该请求结束。如果同时进来的请求多于当前可用的请求处理线程数,额外的线程就会被创建,直到到达配置的最大线程数(maxThreads属性值)。如果仍就同时接收到更多请求,这些来不及处理的请求就会在Connector创建的Server Socket中堆积起来,直到到达最大的配置值(acceptCount属性值)。至此,任何再来的请求将会收到connection refused错误,直到有可用的资源来处理它们。

分析、梳理、组织

这里我们关心的是tomcat能同时处理的请求数和请求响应时间,显然Connector元素的maxThreads和acceptCount属性对其有直接的影响。无论acceptCount值为多少,maxThreads直接决定了实际可同时处理的请求数。而不管maxThreads如何,acceptCount则决定了有多少请求可等待处理。然而,不管是可立即处理请求还是需要放入等待区,都需要tomcat先接受该请求(即接受client的连接请求,建立socket channel),那么tomcat同时可建立的连接数(maxConnections属性值)也会影响可同时处理的请求数。
我们可把tomcat想象成一家医院,你来到医院大厅挂号看病,如果人家接受了,就相当于client和server建立socket连接了。接着你来到相应的科室,科室里每位医生都有一间诊室,这就相当于处理请求的线程;如果所有诊室都有病人,科室的调度护士会让你在科室小厅中耐心等待,直到他们通知你去几号诊室就诊;如果有空闲医生,你就可以立即就诊。
有的病人到医院很仓促,结果轮到他挂号或者就诊了,他还在包里翻找病例本和医保卡,如果超过了护士或医生心里可承受的等待时间,他们就会让病人到旁边找去,先服务下位。这种情形跟Connector元素的connectionTimeout属性所起的作用很相像。如果当前连接器(Connector)在接受连接后,等待了指定的时间但仍未接收到request URI line,就会抛出超时异常。

知识点收集

tomcat 7 的配置参考文档对相关属性已经描述的很详细了,这里把它们收集到一起:

protocol

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

maxThreads

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. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool.

acceptCount

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 will be refused. The default value is 100.

maxConnections

The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server will accept, but not process, one further connection. This additional connection be blocked until the number of connections being processed falls below maxConnections at which point the server will start accepting and processing new connections again. Note that once the limit has been reached, the operating system may still accept connections based on the acceptCount setting. The default value varies by connector type. For BIO the default is the value of maxThreads unless an Executor is used in which case the default will be the value of maxThreads from the executor. 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 to the highest multiple of 1024 that is less than or equal to maxConnections. This is done for performance reasons.
If set to a value of -1, the maxConnections feature is disabled and connections are not counted.

connectionTimeout

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

进一步分析

tomcat的http connector有三种:bio、nio、apr。从上面的属性描述中可以看出对于不同的connector实现,相同的属性可能会有不同的默认值和不同的处理策略,所以在调整配置前,要先弄清楚各种实现之间的不同,以及当前部署容器使用的是哪种connector。
查阅Tomcat 7 http connector 配置文档Connector Comparison部分便可获知各种connector实现间的差异。
怎样才能知道容器使用的是何种connector实现?启动tomcat后,访问Server Status Page,看到如下信息即可知道使用的是何种connector:

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

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

修改为:

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

就可将http connector切换至nio了。更多细节请参考修改Tomcat Connector运行模式,优化Tomcat运行性能

 

 

Tomcat 优化分为系统优化,Java虚拟机调优,Tomcat本身的优化。
Tomcat 如何起停
./catalina.sh stop
./catalina.sh start
/sbin/service tomcat restart
 /webagme/tomcat/bin/catalina.sh start
1.如何调整tomcat的占用内存
A: 方法如下:
1. linux 下编辑tomcat安装目录下的bin目录下的catalina.sh文件,windows下为catalina.bat
vi  catalina.sh
2. 查找到tomcat内存参数一行:/ JAVA_OPTS,如果找不到则在第一行写上
3. 将JAVA_OPTS="-Xms 1024m –Xmx 1520m"一行的两个参数依据服务器实际内存数量分别进行更改:
        - Xms为tomcat启动初始内存,一般为服务器开机后可用空闲内存减去100M
        - Xmx为tomcat最大占用内存,一般为服务器开机后可用空闲内存减去50M
一般说来,您应该使用物理内存的 80% 作为堆大小。
说明:以上两个参数关系到tomcat承受的访问性能,但也要根据服务器实际内存情况设定。
有人建议Xms和Xmx的值取成一样比较好,说是可以加快内存回收速度。但未经本人验证过。有兴趣可以试试。
 
这两个值的大小一般根据需要进行配置。初始化堆的大小执行了虚拟机在启动时向系统申请的内存的大小。一般而言,这个参数不重要。但是有的应用程式在大负载的情况下会急剧地占用更多的内存,此时这个参数就是显得很重要,假如虚拟机启动时配置使用的内存比较小而在这种情况下有许多对象进行初始化,虚拟机就必须重复地增加内存来满足使用。由于这种原因,我们一般把-Xms和-Xmx设为相同大,而堆的最大值受限于系统使用的物理内存。一般使用数据量较大的应用程式会使用持久对象,内存使用有可能迅速地增长。当应用程式需要的内存超出堆的最大值时虚拟机就会提示内存溢出,并且导致应用服务崩溃。因此一般建议堆的最大值配置为可用内存的最大值的80%。 


Tomcat默认能够使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,需要调大。 


Windows下,在文档/bin/catalina.bat,Unix下,在文档/bin/catalina.sh的前面,增加如下配置: 


JAVA_OPTS='-Xms【初始化内存大小】 -Xmx【能够使用的最大内存】' 


需要把这个两个参数值调大。例如: 


JAVA_OPTS='-Xms256m -Xmx512m' 


表示初始化内存为256MB,能够使用的最大内存为512MB。 


另外需要考虑的是Java提供的垃圾回收机制。虚拟机的堆大小决定了虚拟机花费在收集垃圾上的时间和频度。收集垃圾能够接受的速度和应用有关,应该通过分析实际的垃圾收集的时间和频率来调整。假如堆的大小很大,那么完全垃圾收集就会很慢,但是频度会降低。假如您把堆的大小和内存的需要一致,完全收集就很快,但是会更加频繁。调整堆大小的的目的是最小化垃圾收集的时间,以在特定的时间内最大化处理客户的请求。在基准测试的时候,为确保最好的性能,要把堆的大小设大,确保垃圾收集不在整个基准测试的过程中出现。 


假如系统花费很多的时间收集垃圾,请减小堆大小。一次完全的垃圾收集应该不超过 3-5 秒。假如垃圾收集成为瓶颈,那么需要指定代的大小,检查垃圾收集的周详输出,研究 垃圾收集参数对性能的影响。一般说来,您应该使用物理内存的 80% 作为堆大小。当增加处理器时,记得增加内存,因为分配能够并行进行,而垃圾收集不是并行的。 
 
2.如何调整tomcat的线程参数
A: 方法如下:
5.编辑tomcat安装目录下的conf目录下的server.xml文件
在tomcat配置文件server.xml中的<Connector />配置中,和连接数相关的参数有:
maxThreads="150"     表示最多同时处理150个连接,Tomcat使用线程来处理接收的每个请求。这个值表示Tomcat可创建的最大的线程数。默认值200。   
minSpareThreads="25"     表示即使没有人使用也开这么多空线程等待  
  maxSpareThreads="75"     表示如果最多可以空75个线程,例如某时刻有80人访问,之后没有人访问了,则tomcat不会保留80个空线程,而是关闭5个空的。  (一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket线程。默认值50。 

   
  acceptCount="100"   当同时连接的人数达到maxThreads时,还可以接收排队的连接数量,超过这个连接的则直接返回拒绝连接。(指定当任何能够使用的处理请求的线程数都被使用时,能够放到处理队列中的请求数,超过这个数的请求将不予处理。默认值100。 )
其中和最大连接数相关的参数为maxThreads和acceptCount。如果要加大并发连接数,应同时加大这两个参数。
web server允许的最大连接数还受制于操作系统的内核参数设置,通常Windows是2000个左右,Linux是1000个左右。tomcat5中的配置示例:
    <Connector port="8080"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               acceptCount="100"/>
主要是调整maxThreads 和acceptCount的值
对于其他端口的侦听配置,以此类推。
在tomcat配置文档server.xml中的配置中,和连接数相关的其他参数有: 

enableLookups: 

是否反查域名,默认值为true。为了提高处理能力,应配置为false 

connnectionTimeout: 

网络连接超时,默认值60000,单位:毫秒。配置为0表示永不超时,这样配置有隐患的。通常可配置为30000毫秒。 

maxKeepAliveRequests:
nginx动态的转给tomcat,nginx是不能keepalive的,而tomcat端默认开启了keepalive,会等待keepalive的timeout,默认不设置就是使用connectionTimeout。
所以必须设置tomcat的超时时间,并关闭tomcat的keepalive。否则会产生大量tomcat的socket timewait。
maxKeepAliveRequests="1"就可以避免tomcat产生大量的TIME_WAIT连接,从而从一定程度上避免tomcat假死。
试试设置tomcat/conf/server.xml:

maxKeepAliveRequests="1"
connectionTimeout="20000"

 maxKeepAliveRequests="1"表示每个连接只响应一次就关闭,这样就不会等待timeout了。
 
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1" 
               connectionTimeout="30000" maxKeepAliveRequests="1" 
               redirectPort="8443" bufferSize="8192" sockedBuffer="65536" acceptCount="200"/>

bufferSize: 

输入流缓冲大小,默认值2048 bytes。 

compression: 

压缩传输,取值on/off/force,默认值off。
3、tomcat中如何禁止和允许列目录下的文档 

在{tomcat_home}/conf/web.xml中,把listings参数配置成false即可,如下: 



<servlet> 
... 
<init-param> 
<param-name>listings</param-name> 
<param-value>false</param-value> 
</init-param> 
... 
</servlet> 
4、tomcat中如何禁止和允许主机或IP地址访问 
<Host name="localhost" ...> 
  ... 
  <Valve className="org.apache.catalina.valves.RemoteHostValve" 
         allow="*.mycompany.com,www.yourcompany.com"/> 
  <Valve className="org.apache.catalina.valves.RemoteAddrValve" 
         deny="192.168.1.*"/> 
  ... 
</Host> 
cat /webgame/tomcat/conf/server.xml 

猜你喜欢

转载自tcxiang.iteye.com/blog/2283782