设为首页 收藏本站
查看: 716|回复: 0

[经验分享] (转)tomcat优化及常见错误解决

[复制链接]

尚未签到

发表于 2017-1-27 12:55:27 | 显示全部楼层 |阅读模式
How to optimize tomcat performance in production

 


Contents

 [hide]



  • 1 Tomcat maxThreads configuration
  • 2 How the process works:
  • 3 Guidelines for maxThreads:
  • 4 Solving multipart/form-data Read timed out issue
  • 5 Logging settings in Production
  • 6 Access logging settings similar to httpd
  • 7 Sharing Global Connection Pool
  • 8 Templating Server.xml
  • 9 Virtual hosting Host definition
  • 10 Choosing right connectors for production
  • 11 Undocumented options ExtendedAccessLogValve
  • 12 Undocumented options Caching


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

connectionUploadTimeoutSpecifies the timeout, in milliseconds, to use while a data upload is in progress. This only takes effect if disableUploadTimeout is set to false.
disableUploadTimeoutThis 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 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-334117-1-1.html 上篇帖子: Tomcat 启动时类加载顺序 下篇帖子: Tomcat服务器session持久化
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表