How to optimize tomcat performance in production
Tomcat maxThreads configuration
Tomcat maxThreads represents the maximum number of request processing threads to be created by the HTTPConnector.
<Connector port="8080" address="localhost" maxThreads="250" maxHttpHeaderSize="8192" emptySessionPath="true" protocol="HTTP/1.1" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" />
This determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to the default value of 200.
How the process works:
* At server startup, the HTTP Connector will create a number of processing threads based on the value configured for the minSpareThreads attribute. * Each incoming request requires a thread for the duration of that request. * If the number of simultaneous requests cannot be handled by the currently available request processing threads, additional threads will be created up to the configured maximum (the value of the maxThreads attribute). * If still more simultaneous requests are received, they are stacked up up to the configured maximum (the value of the acceptCount attribute). * Any further simultaneous requests will receive "connection refused" errors, until resources are available to process them.
Guidelines for maxThreads:
maxThreads is an important tuning parameter, however if you are reaching an error like:
org.apache.tomcat.util.threads.ThreadPool logFull SEVERE: All threads (150) are currently busy, waiting. Increase maxThreads (150) or check the servlet status
you should at first investigate if it's rather a problem of individual requests taking too long: are your threads returning to the pool? if, for example, database connections are not released, threads pile up waiting to obtain a database connection thereby making it impossible to process additional requests. This is a problem in your webapp.
Take a thread dump to find out where they're stuck. Increasing too much maxThreads will lead to :
* Consume a good chunk of memory. * Your system will spend too much time context switching
So once you have already optimized your application try raising you maxThread attribute up to 500-750. I wouldn't advice to create larger Connectors, rather if 750 Connections are not enough create a Cluster configuration with several Tomcat instances. For example 2 instances of tomcat each one with maxThreads=500 instead of a single Tomcat with maxThreads=1000
Solving multipart/form-data Read timed out issue
If you are facing issues like this org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Read timed out
then set disableUploadTimeout to false and increase connectionUploadTimeout value. This value is specified in milli-seconds
connectionUploadTimeout | Specifies the timeout, in milliseconds, to use while a data upload is in progress. This only takes effect if disableUploadTimeout is set to false. |
disableUploadTimeout | This flag allows the servlet container to use a different, usually longer connection timeout during data upload. If not specified, this attribute is set to true which disables this longer timeout. |
<Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" connectionUploadTimeout="300000" disableUploadTimeout="false"/>
Logging settings in Production
• conf/logging.properties
.handlers = \ 1catalina.org.apache.juli.FileHandler, \ java.util.logging.ConsoleHandler
• Causes duplicate logging
• May fill up catalina.out (no rotation)
Change to
• conf/logging.properties
.handlers = \ 1catalina.org.apache.juli.FileHandler
• Only log to file
• For development, logging to stdout/stderr is sometimes easier to work with
• http://www.cronolog.org - example tool
>> "$CATALINA_BASE"/logs/catalina.out 2>&1 &
• Rotate on a daily basis
2>&1 | /bin/cronolog %Y-%m-%d.catalina.out
• Changes made in catalina.sh
Access logging settings similar to httpd
• Access Logging can be done using a valve
– Valve logs as soon as the request is done
– Introspects request and response to generate output
<Valve className="org.apache.catalina.valves.AccessLogValve" pattern="%h %l %u %t %r %s %b %S %D" directory="${catalina.base}/logs" prefix="tomcat_access_" suffix=".log" /> The %D pattern gives the duration of the URL in miliis
• Pattern similar to that of httpd [1]
Sharing Global Connection Pool
• Sharing a connection pool
<GlobalNamingResources> <Resource type="javax.sql.DataSource" name="sharedpool"/> </GlobalNamingResources>
• conf/context.xml
<Context> <ResourceLink global="sharedpool" name="jdbc/DS"/> </Context>
• All global defaults can be configured in
– conf/context.xml
• Can be overridden by application
– conf/web.xml
• Can be overridden by application
Templating Server.xml
• Use catalina.properties for server.xml substitution variables
#server shutdown port in catalina.properties shutdown.port=-1 <Server port="${shutdown.port}"> … </Server>
Virtual hosting Host definition
• Each host requires an entry in server.xml
• Each host requires their own appBase
• Default host still required
<Engine name="Catalina" defaultHost="bart.foo.com"> <Host name="bart.foo.com" appBase="webapps-bart"/> <Host name="homer.foo.com" appBase="webapps-homer"/> </Engine>
• Standard rules apply
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
• http://bart.foo.com/ is served by the root context for the host bart.foo.com
• ROOT.xml, ROOT.war, ROOT
Choosing right connectors for production
Requirement Connectors in preference order
Stability BIO NIO/APR SSL APR NIO BIO Low concurrency BIO APR NIO High concurrency No Keep-Alive BIO APR NIO High concurrency Keep-Alive APR NIO BIO
If you send a request to tomcat then with all of the connectors you will use tomcat thread to process that request
BIO is Blocking IO Connectors and NIO and APR are Non-Blocking IO Connectors.
BIO means if you use http with keep-alive parameter then you will continue to use that thread in order to maintain keep-alive connection while with Non-Blocking IO Connectors you don't need to use thread to maintain keep-alive requests, so you can make efficient use of threads here
Undocumented options ExtendedAccessLogValve
• W3C Extended Log File Format
http://www.w3.org/TR/WD-logfile.html
http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/valves/ExtendedAccessLogValve.html
• Enhancements include logging of:
– Request parameters (helpful for POST)
– ServletContext attributes
– HttpServletRequest methods
Undocumented options Caching
• Content requiring authentication should not be cached
• Tomcat sets caching headers to enforce this
• IE uses a local cache to download files
• IE downloads the file and then obeys the headers and deletes it from the cache before you have a chance to open it
• Tomcat can be configured to
– allow private caching – recommended
– allow public caching – not recommended
<Context ... > <Valve className="...Authenticator" securePagesWithPragma="false" </Context> <Context ... > <Valve className="...Authenticator" disableProxyCaching="false" </Context>
• className depends on the authentication type configured
• All in package
org.apache.catalina.authenticator
• BasicAuthenticator
• FormAuthenticator
• DigestAuthenticator
• SSLAuthenticator
相关推荐
以下是对"Tomcat常见问题及其解决方法"的详细解析。 一、启动问题 1. **启动失败**:这可能是由于JDK版本不兼容或者环境变量配置错误导致的。确保安装了与Tomcat版本匹配的JDK,并正确设置了JAVA_HOME、CATALINA_...
### Tomcat部署项目常见问题及ExtJS包说明 #### 一、Tomcat部署项目的注意事项 在使用Tomcat部署项目时,有一些重要的事项需要注意: 1. **确保项目与Tomcat版本兼容**:首先需要确保项目的JDK版本与Tomcat版本相...
【标题】:“Tomcat常见问题集锦(持续更新)” 在Java Web开发中,Tomcat作为最常用的开源应用服务器,其稳定性和性能是开发者关注的重点。这篇博客文章旨在收集和解决Tomcat在实际运行中遇到的各种问题,为开发者...
为了解决这一问题,我们需要对Tomcat进行性能优化,特别是调整其内存设置。 在Windows环境下,我们通常会在`%TOMCAT_HOME%\bin\catalina.bat`文件的开头添加以下设置来调整JVM的内存分配: ```bash set JAVA_OPTS=...
《Java开发常见错误及解决方案文件》是一份指导Java开发者避免和处理开发过程中常见问题的文档。该文档中提到了一些典型的错误和对应的解决方案,下面详细展开其中的一些关键知识点。 1. 类定义未找到错误(java....
### Java常见错误及解决方案 #### 1. 类定义未找到错误 `java.lang.NoClassDefFoundError` **原因分析** - **程序调用的JAVA类文件未正确上传:** 当程序试图加载一个不存在或未正确部署的类时,会出现此类错误。 ...
【标题】:“Tomcat优化” 在Java Web开发中,Tomcat作为一款开源、轻量级的应用服务器,被广泛用于部署和运行Servlet和JSP应用。然而,随着应用规模的扩大和用户数量的增长,Tomcat的性能优化变得至关重要。优化...
在部署应用至 Tomcat 服务器时,经常会遇到一个常见的错误:`java.lang.OutOfMemoryError: PermGen space`。该错误表明 Java 虚拟机 (JVM) 的永久代 (PermGen space) 已经达到了其最大容量限制。PermGen space 主要...
为了确保Tomcat服务的稳定运行,管理员需要定期检查并优化以上各项配置,同时密切关注系统日志,以便及时发现并解决问题。在部署生产环境前,进行充分的压力测试和安全评估也是必要的步骤,以预防潜在的自动关闭风险...
### Eclipse启动Tomcat内存泄漏解决方案 #### 一、问题背景 在使用Eclipse集成开发环境(IDE)启动Tomcat服务器时,可能会遇到内存泄漏的问题。这种情况不仅会导致应用程序运行缓慢,严重时甚至会使得Tomcat服务器...
这个文件可能包含有关如何配置、启动、停止Tomcat以及解决常见问题的指南。具体内容可能包括端口更改、服务器优化建议、日志查看方法等。 总的来说,Apache Tomcat 1.6在Windows上的部署非常简单,适合初学者和...
学习如何处理常见的错误,如404、500等,以及如何阅读和理解错误日志,这对于快速解决问题至关重要。 十、持续学习与进阶 理解Tomcat的工作原理后,你可以进一步学习高级特性,如JNDI、JMX监控、连接器优化等。同时...
常见的问题可能包括库文件版本不匹配、环境变量配置错误等。根据错误信息进行相应的调整。 在压缩包中的`介绍.txt`文件,可能包含了更详细的安装指南和注意事项,包括不同操作系统版本下的兼容性问题、32位和64位...
6. **故障排查**:书中会介绍常见的错误日志分析方法,帮助读者定位问题,如404错误、500错误等,并提供解决策略。同时,还会讲解如何使用调试工具进行问题诊断。 7. **集群与负载均衡**:对于高可用性和扩展性需求...
11. **故障排查**:学习如何通过日志、控制台输出以及服务器状态检查来诊断和解决常见的运行问题。 12. **更新与升级Tomcat**:了解如何安全地升级到新版本的Tomcat,以保持最新特性和安全补丁。 通过这个视频教程...