`

20 Tips for Using Tomcat in Production

阅读更多

来源:

http://digitalsanctum.com/2007/08/18/20-tips-for-using-tomcat-in-production/

http://kenwublog.com/docs/java6-jvm-options-chinese-edition.htm

 

I've been working with Apache Tomcat for years and always seem to stumble upon new information related to the proper setup and configuration for a production environment. I've decided to put the instructions and tips I've collected together in one place.

So here are some helpful hints for running Tomcat in a production environment:

1. If you're running on a 1.5+ JVM...

Add the following to your JAVA_OPTS in catalina.sh (or catalina.bat for Windows): -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/j2ee/heapdumps Then use a tool such asYourKit to analyze the heapdump file.

2. When using Jasper 2 in a production Tomcat server you should consider...

Straight from the Tomcat documentation on Jasper 2...

When using Jasper 2 in a production Tomcat server you should consider making the following changes from the default configuration. development: To disable on access checks for JSP pages compilation set this to falsegenStringAsCharArray: To generate slightly more efficient char arrays, set this to true.modificationTestInterval: If development has to be set to true for any reason (such as dynamic generation of JSPs), setting this to a high value will improve performance a lot. trimSpaces: To remove useless bytes from the response, set this to true.

3. Use Tomcat's clustering/session replication capability.

Use Tomcat's clustering/session replication capability to minimize application user impact during maintenance periods.



Recommended Books

  1. Java Performance and Scalability: A Quantitative Approach
  2. The Well-Grounded Java Developer: Vital techniques of Java 7 and polyglot programming
  3. Java Concurrency in Practice
  4. Web Performance Daybook Volume 2
  5. High Performance Web Sites: Essential Knowledge for Front-End Engineers



4. Implement custom error pages

To hide raw exception messages from the users. To do this, simply add something like the following to your web.xml:

<error-page>
   <error-code>404</error-code>
   <location>/error/404.html</location>
</error-page>

5. Use a logging toolkit.

Eliminate System.out and System.err statements from application code and use a logging toolkit such as Log4J for application logging.

6. Leverage Tomcat's shared library directory.

If you're loading several applications with several of the same library dependencies, consider moving them from the applications' WEB-INF/lib directory to Tomcat's shared library{catalina.home}/shared/lib. This will reduce the memory used by each application and result in smaller WAR files.

Update (comments from the user@tomcat.apache.org mailing list): The following should be considered when using the shared library directory: 1. The shared classloader is searched in last resort when looking for classes, according to http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html. 2. Because the classes are shared, they share configuration and singletons and if they store objects statically they will prevent your application from unloading.

This is turning out to be a more controversial suggestion...

Starting with Servlet Spec 2.3 (I think) there has been an emphasis on putting everything a web app needs to run into its war file.

Shared classloaders are evil, but not as evil as the invoker servlet. With a shared loader you can easily get Singleton assumptions being wrong, class cast exceptions, versioning woes, and other issues. Saving a little perm memory just doesn't justify it.

7. Tweak memory parameters.

Most of the time you will want to make a change to the default settings. The best advice here is to create a development environment that matches your production environment and load test the application. While you do this you can also use a profiler to identify bottlenecks, etc.

8. Remove unnecessary applications.

9. Secure the Manager application.

By default there are no users with the manager role. To make use of the manager webapp you need to add a new role and user into the CATALINA_HOME/conf/tomcat-users.xml file.

10. Use a valve to filter by IP or hostname to only allow a subset of machines to connect.

This can be configured at the Engine, Host, or Context level in the conf/server.xml by adding something like the following:

<!-- allow only LAN IPs to connect to the manager webapp -->
<!-- contrary to the current Tomcat 5.5 documation the value for 'allow' is not a regular expression -->
<!-- future versions may have to be specified as 192.168.1.* -->
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="192.168.1.*"></Valve>

11. Strip down server.xml.

By removing comments to make it easier to read and remove connectors that you don't need. An easy way to do this is the following: Rename CATALINA_HOME/conf/server.xml toCATALINA_HOME/conf/server-original.xml and rename CATALINA_HOME/conf/server-minimal.xml to CATALINA_HOME/conf/server.xml. The minimal configuration provides the same basic configuration, but without the nested comments is much easier to maintain and understand. Do not delete the original file as the comments make it useful for reference if you ever need to make changes. Unless you are using Tomcat with the Apache server, comment out this line in CATALINA_HOME/conf/server.xml:

<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3"></Connector>

12. Split your Tomcat installation for added flexibility when it comes time to upgrade Tomcat.

See the "Advanced Configuration - Multiple Tomcat Instances" section in the RUNNING.txt file of the Tomcat distribution.

14. Do NOT run Tomcat as root.

Look to my previous post, "3 Ways to Run a Servlet Container on Port 80 as Non-Root", for tips.

15. Precompile JSPs.

Precompile JSPs (at build time).

16. Secure directory listings.

In CATALINA_HOME/conf/web.xml:

<servlet>
   <servlet-name>default</servlet-name>
   <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
   <init-param>
      <param-name>debug</param-name>
      <param-value>0</param-value>
   </init-param>
   <init-param>
      <param-name>listings</param-name>
      <param-value>false</param-value>  <!-- make sure this is false -->
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>

17. If you have multi-core CPUs or more than one CPUs on your server...

It might be beneficial to increase the thread pool beyond the default 250. On the other hand, if you have a slow server, decreasing the thread pool will decrease the overhead on the server.

18. Monitor application applications via Tomcat MBeans.

This article provides some great insight on how to do this.

Consider JDK 1.5 or even better JDK 1.6

To take advantage of performance improvements.

Update (comments from users@tomcat.apache.org mailing list):

Note that you can gain even more performance if you recompile your "string concatenation hungry" (d="aaaa"+b+"ccc") support libraries for JDK 5+ on a multi-CPU system. This is because JDK 5 uses the non-synchronized StringBuilder instead of the JDK 4- synchronized StringBuffer. And synchronization over multiple CPUs takes a few more cycles than on single CPU machines.

19. Use the -server JVM option.

This enables the server JVM, which JIT compiles bytecode much earlier, and with stronger optimizations. Startup and first calls will be slower due to JIT compilation taking more time, but subsequent ones will be faster.

20. Use GZIP compression.

Look for the service connector you wish to configure for compression and add two attributes, compression and compressableMimeType. For example:

<Connector>
   port="80"
   maxHttpHeaderSize="8192"
   URIEncoding="UTF-8"
   maxThreads="150"
   minSpareThreads="25"
   maxSpareThreads="75"
   enableLookups="false"
   redirectPort="8443"
   acceptCount="100"
   connectionTimeout="20000"
   disableUploadTimeout="true"
   compression="on"
   compressableMimeType="text/html,text/xml,text/plain,application/xml">
</Connector>

For more information, read the Tomcat HTTP Connector documentation.

The default Tomcat configuration provides good protection for most requirements, but does not prevent a malicious application from compromising the security of other applications running in the same instance. To prevent this sort of attack, Tomcat can be run with a Security Manager enabled which strictly controls access to server resources. Tomcat documentation has a good section on enabling the Security Manager.

Who's Using Tomcat in Production

Curious about what other organizations run Tomcat in a production environment? The Tomcat wiki has a list.

Update: If you're running with Java 6 JVM, I found this great resource which gives a complete list of -XX options. This is helpful if you're doing performance tweaking for your environment or debugging an issue with your system.

Related Reading

  1. SpringSource Team Blog: Optimising and Tuning Apache Tomcat
  2. Real-Time Tracking and Tuning for Busy Tomcat Servers
  3. The official Apache Tomcat Wiki
  4. Apache Tomcat
  5. Tomcat 6.0 Documentation
  6. Tomcat 5.5 Documentation
  7. Tomcat 5.0 Documentation

 

Java and Tomcat Jobs

Tomcat Web Server Expert - Software Engineer 4 - Tomcat Web Server Expert... of Apache/Tomcat Security and its configuration. Must have strong knowledge of Java Security . Must have... Newark, DE 19702, 2 days ago

Java Developer - a primary focus on Java developmentmost new development work will be done in Java. A secondary focus of... servers (Tomcat, Apache, etc.). Java frameworks or... Madison, WI, 2 days ago

Software Engineer II Spring, Hibernate, JBoss, Tomcat - Hibernate, JBoss, Tomcat - 610906 Description... in development of Web/Application Servers: JBoss, Tomcat • Experience with relational databases and data... Duluth, GA, 7 days ago

Senior Technical Consultant - Java, Tomcat, Jetty - Our client is seeking a Senior Technical Consultant in Atlanta, Georgia (GA). The job description for the position of Senior Technical Consultant includes, but... Atlanta, GA, 3 days ago

Java Architect - Experience working with Tomcat, Spring, Object relational mapping, and Java based technologies • Have... years of experience with Java and various frameworks... San Francisco, CA, 1 day ago

jobs by Indeed job search
分享到:
评论

相关推荐

    Tips for using MapObjects with VC++

    #import "Mo20.ocx" \ rename("Font", "IFontDisp"), \ rename("EOF", "MoEOF"), \ rename("_DMap", "IMoMap") using MapObjects2::SymbolTypeConstants; using MapObjects2::AlignmentConstants; using ...

    Spark: Big Data Cluster Computing in Production

    Spark: Big Data Cluster Computing in Production goes beyond general Spark overviews to provide targeted guidance toward using lightning-fast big-data clustering in production. Written by an expert ...

    Tips for Using Adobe Photoshop

    ### 使用Adobe Photoshop的小贴士与技巧 #### 一、了解Photoshop工具栏 在Adobe Photoshop中,工具栏是用户执行各种图像编辑任务的关键界面之一。根据文档中的描述,工具栏通常显示在屏幕右侧,并且各个工具的位置...

    100 power tips for fpga designers

    ### 《100 Power Tips for FPGA Designers》核心知识点概览 #### 一、书籍简介与背景 《100 Power Tips for FPGA Designers》是一本由Evgeni Stavinov撰写的专为FPGA设计人员提供的实用指南。本书版权为2011年,...

    6 Tips For Speaking Natural English_Speak English With Vanessa

    6 Tips For Speaking Natural English_Speak English With Vanessa

    Practical Tips for Using MySQL as a Scalable Key-Value Store

    ### 使用MySQL作为可扩展键值存储的实际技巧 #### 概述 本文档旨在探讨如何将MySQL用作一种可扩展的键值存储系统,并提供了一系列实际的建议与技巧。作者Sunny Gleason是分布式系统工程师,在亚马逊及Ning等公司...

    机器学习 -- Tips for Deep Learning

    Tips for Deep Learning Do not always blame Overfitting Hard to get the power of Deep ... Vanishing Gradient Problem ReLU Maxout RMSProp

    Mind Hacks Tips & Tricks For Using Your Brain (2005) - Oreilly Media, Inc

    提供了适用于实际生活中的秘诀和方法(tips and tools),运用这些方法能使你的大脑能够超常工作,让你成为更好的思想者。在当今快速发展的信息经济时代,管理你的生活需要开发你的大脑的潜能。《心理和脑与生活:训练...

    100 power tips for FPGA designers

    本书标题《100 power tips for FPGA designers》明确指出了目标读者群体——FPGA(现场可编程门阵列)设计师。此书旨在向他们提供100个实用的设计技巧,帮助设计师们在工作中更有效率、更优化地利用FPGA。 FPGA是...

    100 Power Tips for FPGA Designers

    根据提供的文件信息,我们可以推断出这是一本关于FPGA设计的专业书籍——《100 Power Tips for FPGA Designers》,作者是Evgeni Stavinov。本书提供了针对FPGA设计者的100条实用建议,旨在帮助读者提高设计效率、...

    Tips for prospective and early-stage PhD students

    标题中的“Tips for prospective and early-stage PhD students”表明这是一个针对有意攻读或正在早期阶段进行博士学位的学生的指导性资料。ICLR 2020(International Conference on Learning Representations 2020...

    50 Tips and Tricks for MongoDB Developers

    Data Safety Tips: Using replication and journaling to keep data safe—without sacrificing too much performance Administration Tips: How to configure MongoDB and keep it running smoothly

    Unite Europe 2017 - Squeezing Unity: Tips for raising performance

    Unite Europe 2017 - Squeezing Unity: Tips for raising performance 就是关于如何提升性能的 YouTube 对应的 https://www.youtube.com/watch?v=_wxitgdx-UI&index=7&list=PLX2vGYjWbI0Rzo8D-vUCFVb_hHGxXWd9j

    Tips for Optimizing C/C++ Code

    此外,如果可能,将循环移入函数调用内(例如将for循环直接写入函数内),并且对于长的if-else链,应该尽可能转换为switch语句,这是因为编译器有时会将switch语句优化为使用单一跳转的表查找。如果switch语句不适用...

    Tips for writing good use cases.pdf

    ### 编写优秀用例的关键技巧 #### 引言 编写优秀的用例更像是一门艺术而非科学。正如任何一门艺术一样,在创造杰作时并没有绝对的规则可循。最终,用例的目标在于清晰地向多元化的受众传达详细的信息,并实现创建...

    Concise tips and examples for using Open Liberty.zip

    Open Liberty 是一个轻量级、模块化的Java EE和Jakarta EE应用服务器,它提供了一种高效、灵活的方式来运行和管理Java应用程序。以下是一些关于如何有效利用Open Liberty的知识点: 1. **安装与配置** ...

    Ten tips for your successful migration

    Ten tips for your successful migration

    Asterisk tips for integration with Avaya

    Asterisk tips for integration with Avaya

Global site tag (gtag.js) - Google Analytics