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常见错误 Tomcat常见错误 Tomcat常见错误
tomcat6.0配置数据库连接池常见错误解决方案 tomcat6.0配置数据库连接池常见错误解决方案 tomcat6.0配置数据库连接池常见错误解决方案
归纳了tomcat服务器常见的错误及解决办法
tomcat优化配置tomcat优化配置tomcat优化配置tomcat优化配置
Tomcat7性能优化调优Tomcat7性能优化调优Tomcat7性能优化调优
该文件是关于tomcat在window中的配置过程和一些错误总结,希望可以帮助到你们!
tomcat常见错误与解决方法.pdf
tomcat优化 服务器安全 ,tomcat优化方案
Tomcat7性能优化Tomcat7性能优化Tomcat7性能优化Tomcat7性能优化Tomcat7性能优化Tomcat7性能优化Tomcat7性能优化Tomcat7性能优化
在MyEclipse布署Tomcat 5时的常见错误与解决方法
本文件是关于tomcat一些常见问题和及其解决方案。
TOMCAT架构服务器优化指南TOMCAT架构服务器优化指南TOMCAT架构服务器优化指南TOMCAT架构服务器优化指南
解决tomcat配置ssl错误的解决办法,不一定有用,只是一个备份。不需要分就是因为不一定能帮到谁。
比较全的 Tomcat常见的错误代码 ,讲解的很详细,方便我们找出错误原因
Tomcat优化相关问题.pdf
本文件是关于tomcat常见问题及其解决方法
基于tomcat7的优化,主要优化配置、禁用AJP连接器、JVM参数的优化等
这是tomcat8的配置中文说明,包含了tomcat8优化的全部,读者可参考本文档就行所有tomcat8的优化 PS:只针对tomcat8,不针对9
tomcat性能优化,了解tomcat内部结构
Tomcat性能优化 1. 操作系统2. JAVA 虚拟机3. Tomcat 自身的调整策略