tomcat 性能优化(转)

简介: tomcat nginx默许的post大小限制 tomcat nginx默认的post大小限制执行大文件上传,或者,大数据量提交时,当提交的数据大小超过一定限制时,发现后台从request取值的代码request.

 

tomcat nginx默许的post大小限制

tomcat nginx默认的post大小限制
执行大文件上传,或者,大数据量提交时,当提交的数据大小超过一定限制时,发现后台从request取值的代码request.getParameter("message")返回值为null,原因是因为服务器对于提交的post请求的大小有一定的限制

tomcat:默认大小2097152,当maxPostSize=0时,不限制;maxPostSize=20971520时,为20M
nginx:默认的最大请求body大小为8m,修改设置client_max_body_size=100m;
resin:没有发现默认限制请求大小的地方!

tomcat:maxPostSize

The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing.
The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).

Nginx的"413 request entiry too large"

tomcat返回400

tomcat请求端口都加上maxPostSize="0"

tomcat默认参数是为开发环境制定,而非适合生产环境,尤其是内存和线程的配置,默认都很低,容易成为性能瓶颈。

 http://www.cnblogs.com/sunxucool/archive/2013/07/30/3225812.html

maxPostSize

The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).

maxSavePostSize

The maximum size in bytes of the POST which will be saved/buffered by the container during FORM or CLIENT-CERT authentication. For both types of authentication, the POST will be saved/buffered before the user is authenticated. For CLIENT-CERT authentication, the POST is buffered for the duration of the SSL handshake and the buffer emptied when the request is processed. For FORM authentication the POST is saved whilst the user is re-directed to the login form and is retained until the user successfully authenticates or the session associated with the authentication request expires. The limit can be disabled by setting this attribute to -1. Setting the attribute to zero will disable the saving of POST data during authentication . If not specified, this attribute is set to 4096 (4 kilobytes).

 

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.

 

maxSpareThreads

The maximum number of unused request processing threads that will be allowed to exist until the thread pool starts stopping the unnecessary threads. The default value is 50.

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.

minSpareThreads

The number of request processing threads that will be 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.

http://tomcat.apache.org/tomcat-5.5-doc/config/http.html

之前我以为是method=post的问题,删除 method=post 就好了,其实是请求变成了get所以好了,今天才发现是 server.xml 里面有一个设置在作怪!

删掉这两项就好了!

上网查了一下 maxPostSize 和 maxSavePostSize 是设置最大的表单长度的,给我设成1了!结果post表单没了!

http://blog.csdn.net/nsrainbow/article/details/8948092

 

 

tomcat内存优化

linux修改TOMCAT_HOME/bin/catalina.sh,在前面加入

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

windows修改TOMCAT_HOME/bin/catalina.bat,在前面加入

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

最大堆内存是1024m,对于现在的硬件还是偏低,实施时,还是按照机器具体硬件配置优化。

 

tomcat 线程优化

<Connector port="80" protocol="HTTP/1.1" maxThreads="600" minSpareThreads="100" maxSpareThreads="500" acceptCount="700" connectionTimeout="20000" redirectPort="8443" />

maxThreads="600"       ///最大线程数 minSpareThreads="100"///初始化时创建的线程数 maxSpareThreads="500"///一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket线程。 acceptCount="700"//指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理

 

这里是http connector的优化,如果使用apache和tomcat做集群的负载均衡,并且使用ajp协议做apache和tomcat的协议转发,那么还需要优化ajp connector。

<Connector port="8009" protocol="AJP/1.3" maxThreads="600" minSpareThreads="100" maxSpareThreads="500" acceptCount="700" connectionTimeout="20000" redirectPort="8443" />

 

由于tomcat有多个connector,所以tomcat线程的配置,又支持多个connector共享一个线程池。

首先。打开/conf/server.xml,增加

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

最大线程500(一般服务器足以),最小空闲线程数20,线程最大空闲时间60秒。

 

然后,修改<Connector ...>节点,增加executor属性,executor设置为线程池的名字:

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

可以多个connector公用1个线程池,所以ajp connector也同样可以设置使用tomcatThreadPool线程池。

 

禁用DNS查询

当web应用程序向要记录客户端的信息时,它也会记录客户端的IP地址或者通过域名服务器查找机器名 转换为IP地址。

DNS查询需要占用网络,并且包括可能从很多很远的服务器或者不起作用的服务器上去获取对应的IP的过程,这样会消耗一定的时间。

修改server.xml文件中的Connector元素,修改属性enableLookups参数值: enableLookups="false"

如果为true,则可以通过调用request.getRemoteHost()进行DNS查询来得到远程客户端的实际主机名,若为false则不进行DNS查询,而是返回其ip地址

 

 

设置session过期时间

conf\web.xml中通过参数指定:

    <session-config>   
        <session-timeout>180</session-timeout> </session-config> 单位为分钟。

 

Apr插件提高Tomcat性能

Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术.

APR(Apache Portable Runtime)是一个高可移植库,它是Apache HTTP Server 2.x的核心。APR有很多用途,包括访问高级IO功能(例如sendfile,epoll和OpenSSL),OS级别功能(随机数生成,系统状态等等),本地进程管理(共享内存,NT管道和UNIX sockets)。这些功能可以使Tomcat作为一个通常的前台WEB服务器,能更好地和其它本地web技术集成,总体上让Java更有效率作为一个高性能web服务器平台而不是简单作为后台容器。

在产品环境中,特别是直接使用Tomcat做WEB服务器的时候,应该使用Tomcat Native来提高其性能 

要测APR给tomcat带来的好处最好的方法是在慢速网络上(模拟Internet),将Tomcat线程数开到300以上的水平,然后模拟一大堆并发请求。 如果不配APR,基本上300个线程狠快就会用满,以后的请求就只好等待。但是配上APR之后,并发的线程数量明显下降,从原来的300可能会马上下降到只有几十,新的请求会毫无阻塞的进来。 在局域网环境测,就算是400个并发,也是一瞬间就处理/传输完毕,但是在真实的Internet环境下,页面处理时间只占0.1%都不到,绝大部分时间都用来页面传输。如果不用APR,一个线程同一时间只能处理一个用户,势必会造成阻塞。所以生产环境下用apr是非常必要的。

复制代码
(1)安装APR tomcat-native
    apr-1.3.8.tar.gz   安装在/usr/local/apr #tar zxvf apr-1.3.8.tar.gz #cd apr-1.3.8 #./configure;make;make install apr-util-1.3.9.tar.gz 安装在/usr/local/apr/lib #tar zxvf apr-util-1.3.9.tar.gz #cd apr-util-1.3.9 #./configure --with-apr=/usr/local/apr ----with-java-home=JDK;make;make install #cd apache-tomcat-6.0.20/bin #tar zxvf tomcat-native.tar.gz #cd tomcat-native/jni/native #./configure --with-apr=/usr/local/apr;make;make install (2)设置 Tomcat 整合 APR 修改 tomcat 的启动 shell (startup.sh),在该文件中加入启动参数: CATALINA_OPTS="$CATALINA_OPTS -Djava.library.path=/usr/local/apr/lib" 。 (3)判断安装成功: 如果看到下面的启动日志,表示成功。 2007-4-26 15:34:32 org.apache.coyote.http11.Http11AprProtocol init
复制代码

 

参考 http://blog.sina.com.cn/s/blog_417b97470100glmi.html

       http://datadig.blog.163.com/blog/static/171229928201082075946726/

 

http://www.cnblogs.com/ggjucheng/archive/2013/04/16/3024731.html

相关文章
|
6月前
|
Java 应用服务中间件 测试技术
Tomcat压力测试tps性能下降问题
Tomcat压力测试tps性能下降问题
|
3月前
|
Java 应用服务中间件
优化tomcat的性能
优化tomcat的性能
|
8月前
|
缓存 监控 Java
springboot tomcat性能优化
springboot tomcat性能优化
435 0
|
4月前
|
架构师 Java 关系型数据库
一线架构师开发总结:剖析并发编程+JVM性能,深入Tomcat与MySQL
每一个程序员都有自己清晰的职业规划和终极目标,无论之后是继续钻研技术,还是转管理岗、产品岗,都是需要自己具备有一定的实力,换句话说技术要牛逼。架构师,是很多程序员的终极目标,而成为一名Java架构师,那就需要对自己自身有一定要求,不仅技术能力要过硬,还需要有组织能力和提出解决方案的能力。那么作为架构师,需要掌握哪些技术呢?
一线架构师开发总结:剖析并发编程+JVM性能,深入Tomcat与MySQL
|
7月前
|
存储 缓存 Java
101分布式电商项目 - Tomcat性能优化(JVM调优)
101分布式电商项目 - Tomcat性能优化(JVM调优)
39 0
|
7月前
|
网络协议 应用服务中间件 Apache
100分布式电商项目 - Tomcat性能优化(禁用AJP连接器)
100分布式电商项目 - Tomcat性能优化(禁用AJP连接器)
35 0
|
7月前
|
应用服务中间件
99分布式电商项目 - Tomcat性能优化(NIO配置)
99分布式电商项目 - Tomcat性能优化(NIO配置)
30 0
|
7月前
|
Java 应用服务中间件
98分布式电商项目 - Tomcat性能优化(使用线程池)
98分布式电商项目 - Tomcat性能优化(使用线程池)
31 0
|
7月前
|
Java 应用服务中间件 API
97分布式电商项目 - Tomcat性能优化(运行方式)
97分布式电商项目 - Tomcat性能优化(运行方式)
32 0
|
9月前
|
消息中间件 监控 NoSQL
tomcat8和tomcat7性能比较
tomcat8和tomcat7性能比较
164 0