Tomcat optimization details

Source: Internet
Author: User
Tags domain name server domain name server lookup name server lookup xms

1 Overview
This document describes how to optimize Tomcat performance. It can serve as a technical guide for the company's technical staff to optimize the customer's Tomcat System, or provide the customer's technical staff as a guide to their performance tuning.

2. tuning Classification
Because Tomcat runs on JVM, we describe Tomcat adjustments from the perspective of virtual machines in two ways: External Environment optimization and Self-tuning.
2.1 Optimization of external environment

Adjust the operating system parameters of the Tomcat running environment and the Java Virtual Machine Parameters for running tomcat.
2.1.1 Java virtual machine performance optimization

Tomcat depends on the Java Virtual Machine. Select the JDK version based on the operating system of the host selected by the customer. The latest JDK version is recommended for any vendor.
The VM can change the memory used by the VM through the command line. The following table shows two parameters used to set the memory used by the VM.
Parameter description
-XMS <size> JVM initialization heap size
-Xmx <size> maximum JVM heap Value
By default, Tomcat can use 128 MB of memory. In large application projects, this memory is insufficient and needs to be increased.
[LD1] in windows, add the following settings in front of the tomcat_home/bin/Catalina. BAT and Unix File tomcat_home/bin/Catalina. sh:
Java_opts = '-XMS [initial memory size]-xmx [maximum memory available ]''
You need to increase the value of these two parameters. For example:

JAVA_OPTS='-Xms256m -Xmx512m' 

Indicates that the initial memory is 256 MB, and the maximum memory available is 512 MB.
In addition, you need to consider the garbage collection mechanism provided by Java. The size of the Virtual Machine heap determines the time and frequency of the Virtual Machine spending on garbage collection. The acceptable speed of garbage collection is related to the application. The actual garbage collection time and frequency should be analyzed to adjust.
If the heap space is large, full garbage collection will be slow, but the frequency will decrease. If the size of the heap is the same as that of the memory in the customer's system, the full collection will be fast, but it will be more frequent. The purpose of adjusting the heap size is to minimize the garbage collection time to maximize the processing of customer requests within a specific period of time. For virtual machines such as Sun and HP, we recommend that you set the minimum heap size and maximum heap size to the same value, because this can avoid wasting VM resources required to adjust the heap size from time to time.
Of course, if the customer system uses an IBM virtual machine, pay special attention to setting the same size as-XMS and-xmx will delay the start of garbage collection until it is full, in this way, the first garbage collection will become very expensive. We recommend that you set-XMS to the minimum value required by the application to generate efficient garbage collection.

2.1.2 Operating System Performance Optimization

Take the customer system as an example of HP-UX.
Parameters that affect tomcat in the HP System:
Where:
Max_thread_proc: Maximum number of threads that a process can create
Nkthread: Maximum number of core threads allowed simultaneously on the System
Is the suggestions in the maxfiles table inappropriate?
If the message "Java. Lang. outofmemoryerror: Unable to create new Native thread" is displayed in the output, the Unix kernel setting named max_thread_proc is too small. Max_thread_proc is the maximum number of threads in a single process. It must be large enough to accommodate all threads in Java applications and some additional threads in the virtual machine itself.

View core parameters: $ ulimit-

Nofiles in the output of [LD2] indicates the maximum number of file handles that can be opened by a user process at the same time. If the "Two too open files" exception occurs in the log, check this parameter. The coredump parameter is the maximum value of the core file. The size of the core file cannot exceed the maximum value when the process coredump. If the core file is incomplete during the log file check, you need to increase the value of this parameter. Run the ulimit-N command to set the nofiles parameter and run the ulimit-C command to set the maximum value of the core file.
If you are using Tomcat on a Windows operating system, you 'd better select the server version. In non-Server versions, there are limits on the number of end user authorizations, the number of users that the operating system can afford, the number of available network connections, and other aspects. In addition, the operating system must be installed with the latest patches based on security considerations.
2.1.3 Tomcat integrated with other Web Servers

Although Tomcat can also be used as a Web server, its processing speed of static html is inferior to that of Apache, and its function as a web server is far inferior to that of Apache. Therefore, we want to integrate Apache and tomcat, define the division of functions between HTML and JSP, so that Tomcat can only process JSP and other Web servers such as Apache and IIS, this greatly saves Tomcat's limited working threads [ld3].
2.2 Self-Tuning
This section describes Tomcat performance tuning techniques and methods that are independent of the operating system or Java virtual machine. The following methods are the best methods for adjusting Tomcat performance.
2.2.1 disable DNS query
When a web application wants to record the client information, it also records the Client IP address or converts the machine name to IP address through Domain Name Server Lookup. DNS queries require network occupation, and include the process of obtaining the corresponding IP address from many servers that are far away or do not work, which consumes a certain amount of time. To eliminate the impact of DNS queries on performance, we can disable DNS queries by modifying the value of the enablelookups parameter in the server. xml file:
Different Tomcat versions are slightly different.
Tomcat4

<Connector className=“org.apache.coyote.tomcat4.CoyoteConnector”port=“80”minProcessors=“5” maxProcessors=“75” enableLookups=“false”redirectPort=“8443” acceptCount=“100” debug=“0” connectionTimeout=“20000”useURIValidationHack=“false” disableUploadTimeout=“true” /> 

Tomcat5

<Connector port=“80” maxThreads=“150” minSpareThreads=“25”maxSpareThreads=“75” enableLookups=“false” redirectPort=“8443”acceptCount=“100” debug=“0” connectionTimeout=“20000”disableUploadTimeout=“true” /> 

Unless you need to connect to the machine name of each HTTP client on the site, we recommend that you disable the DNS query function in the production environment. You can obtain the machine name in a way other than tomcat. This not only saves network bandwidth, query time, and memory, but also reduces log data with less traffic, obviously saving hard disk space. For sites with low traffic, disabling DNS queries may not have a significant effect on sites with large traffic volumes.

2.2.2 adjust the number of threads
Another parameter that can be controlled by the application connector is the number of threads created to process requests. Tomcat uses the thread pool to accelerate the response speed to process requests. In Java, a thread is the path when the program is running and a code segment that is independent of other control threads in a program. They share the same address space. Multithreading helps programmers write efficient programs with the maximum CPU utilization, keeping idle time at the lowest level, and thus receiving more requests.
In tomcat4, you can control the number of threads by modifying the values of minprocessors and maxprocessors. These values have been set to the default value after installation and are enough for use, but these values are increased as the site expands. The number of threads created when the minprocessors server is started should be sufficient to process a small amount of load. That is to say, if only five click events occur per second in a day and each request task takes one second to process, it is enough to set the number of threads in advance. However, when your site traffic is large, you need to set a larger number of threads to the value of the maxprocessors parameter. The value of maxprocessors is also limited, so it is necessary to prevent uncontrollable traffic (or malicious service attacks), thus exceeding the memory used by the virtual machine. To increase the number of concurrent connections, increase these two parameters at the same time. Web
The maximum number of connections allowed by the server is also subject to the kernel parameter settings of the operating system. Generally, there are about 2000 connections in windows and about 1000 connections in Linux.

The parameters have been adjusted in tomcat5. See the following table:

The best way is to set multiple times and perform tests to observe the response time and memory usage. Different machines, operating systems, or virtual machines may have different combinations, and not all web sites have the same traffic, therefore, there is no one-size-fits-all solution to determine the number of threads.

2.2.3 accelerate JSP Compilation
When a JSP file is accessed for the first time, it is converted to the Java Servlet Source code and then compiled into a Java bytecode. The customer engineer can control which compiler to use. By default, Tomcat uses the command line javac for the compiler. You can also use faster compilers. Here we will introduce how to optimize them.
[Ld4] Another method is not to use JSP pages for all implementations, but to use different Java template engine variables.
Popular and free jikes compilers can be used in Tomcat 4.0. The jikes compiler is faster than Sun's Java compiler. First install jikes (accessible to the http://oss.software.ibm.com/pub/jikes for more information) and then set jikespath in the environment variable to include the JAR file required for the system runtime. After jikes is installed, you also need to set jikes for JSP compilation servlet. You need to modify the jspcompilerplugin value in the web. xml file:

<servlet><servlet-name>jsp</servlet-name><servlet-class>org.apache.jasper.servlet.JspServlet </servlet-class><init-param><param-name>logVerbosityLevel</param-name><param-value>WARNING</param-value></init-param><init-param><param-name>jspCompilerPlugin</param-name><param-value>org.apache.jasper.compiler.JikesJavaCompiler</param-value></init-param><init-param><!-- <param-name>org.apache.catalina.jsp_classpath</param-name> --><param-name>classpath</param-name><param-value>/usr/local/jdk1.3.1-linux/jre/lib/rt.jar:/usr/local/lib/java/servletapi/servlet.jar </param-value></init-param><load-on-startup>3</load-on-startup></servlet> 

In Tomcat 4.1 (or later), JSP compilation is directly executed by the ant Program Controller contained in Tomcat. The customer developer needs to define a "compiler" in the element, and there is a compiler name that supports compilation in the value, for example:

<servlet><servlet-name>jsp</servlet-name><servlet-class>org.apache.jasper.servlet.JspServlet </servlet-class><init-param><param-name>logVerbosityLevel</param-name><param-value>WARNING</param-value></init-param><init-param><param-name>compiler</param-name><param-value>jikes</param-value></init-param><load-on-startup>3</load-on-startup></servlet> 

Ant Compiler Available

Since the JSP page has been compiled for the first time, you may want to compile it immediately after updating the new JSP page. In fact, this process can be completely automated, because it can be confirmed that the new JSP page runs the same effect on the production server and the test server.
In the bin directory of Tomcat 4, there is a script named jspc. It only runs the translation phase, rather than the compilation phase. You can use it to generate Java source files in the current directory. It is a powerful means to debug JSP pages.
You can access it in a browser and then confirm the compilation result. This ensures that the file is converted into a Servlet and compiled for direct execution. This accurately imitates the functions provided by real users to access JSP pages. We also need to modify the bug at the last moment and modify it.
Tomcat provides a function to compile JSP pages through requests. You can enter http: // localhost: 8080/examples/JSP/dates/date. jsp? Jsp_precompile = true, so Tomcat will compile data. jsp instead of executing it. This is an easy way to verify the correctness of pages.


2.2.4 NiO Configuration
NiO (no-blocking I/O) Since JDK 1.4, NiO API is introduced as a buffer-based API that can provide non-blocking I/O operations [ld6].
Tomcat supports enterprise-level applications with high concurrency. One major reason is that APR (Apache Portable Runtime) is used for Tomcat with good configuration. Apr is the core of Apache HTTP server2.x and a highly portable local library, it uses high-performance uxin I/O operations and Low-performance Java I/O operations. However, APR may be a little difficult for the customer's developers and may need to re-compile APR on many OS platforms. However, since tomcat6.0, the customer developers can easily use NiO Technology to Improve Tomcat's concurrent processing capability. But why can NiO improve Tomcat's concurrent processing capabilities? Let's take a look at Java
The difference between traditional Io and Java NiO.
Traditional Java I/O operations are blocking I/O operations. If you have socket programming basics, you will be exposed to blocking socket and non-blocking socket, blocking Socket means that when I/O operations such as accept, read, and write are performed, if no qualified resource is available, I will not return immediately and wait until there is a resource. The socket is blocked when the SELECT statement is executed, and when there is no resource, a signal is returned when there is a qualified resource, then the program can execute the accept, read, write and other operations. Generally, if the socket is blocked, we usually open a thread accept.
Socket. When the socket request is read, open a separate thread to process the socket request. If the socket is not blocked, there is usually only one thread, Which is select at the beginning, when there is a signal, you can pass the multiplexing technology to a specified thread pool to process the request, and then the original thread continues the select state. The simplest multiplexing technology can be achieved through the Java pipeline (PIPE. In other words, if the client has a large number of concurrent requests, the customer system can use less than the number of concurrent requests of the client to process these requests, these requests that cannot be processed immediately will be blocked in the Java pipeline or queue, waiting for processing by the thread pool.
One of the important differences between IO (bio) AND NiO blocking on Web servers is that when the customer system uses bio, multithreading is often introduced for each Web request, each Web request has a separate thread, so once the concurrency goes up, the number of threads goes up, and the CPU is busy with thread switching, so Bio is not suitable for high-throughput, highly scalable Web servers; NIO uses a single thread (single CPU) or only a small number of threads (multiple CPUs) to accept sockets, And the thread pool is used to process requests blocked in pipe or queue. in this way, the web server can process the request as long as the OS can accept the TCP connection. This greatly improves the scalability of web servers.
The customer only needs to make the following changes to HTTP ctor in server. xml:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 

Change

<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443" /> 

Then start the server. If the message org. Apache. Coyote. http11.http11nioprotocol start appears, it indicates that NiO has been started. For other configurations, see the official Configuration documentation.
2.2.5 others
As mentioned above, the operating system uses some restrictions to prevent malicious service attacks. Tomcat also provides settings to prevent malicious attacks or prohibit access from certain machines.
Tomcat provides two parameters for you to configure: remotehostvalve and remoteaddrvalve.
By configuring these two parameters, you can filter the requested host or IP address and allow or deny the host/IP address. Similarly, each directory is allowed/denied in the httpd file of Apache.
For example, you can set Admin web application to allow local access only, as follows:

<Context path=“/path/to/secret_files”><Valve className=“org.apache.catalina.valves.RemoteAddrValve” allow=“127.0.0.1”deny=““ /></Context>

If you do not specify the allowed host, the host that matches the rejected host will be denied. Similarly, if you do not specify a denial host, the host that matches the allowed host will be allowed. Otherwise, the host will be rejected.
3. Server Load balancer
Under the idea of Server Load balancer, multiple servers are in a peering mode, and each server has the same status. It can provide external services independently without the assistance of other servers. By using the Load Balancing Technology, requests sent from outside are distributed to a server in the symmetric structure according to certain rules, and the servers that receive the requests Independently respond to the client's requests.
A group of servers providing services form an application server cluster. The peer-to-peer multi-machine environment in the cluster can increase the system's concurrent processing capability, and the error redundancy capability of a faulty system on a single machine. It also achieves load balancing and high system reliability.

Four Load Balancing Methods:
The first is through DNS, but it can only achieve simple round-robin allocation and cannot handle faults;
Second, if it is based on ms iis, Windows 2003 Server itself will bring the Server Load balancer service;
The third is the hardware mode, which can be implemented through the switch function or a dedicated load balancing device;
The fourth is the software method, which is carried out through a Server Load balancer server. The software is installed on the server Load balancer server. Use Apache httpd server for Load balancer.
The customer system generally uses Apache httpd as the Web server, that is, as the front-end Processor of Tomcat, depending on the specific situation. In some cases, Apache httpd is not required as the web server, if the system does not display static pages, Apache httpd is not required. At that time, Tomcat can be directly used as a web server. Apache httpd is mainly used to process more static pages than tomcat.

3.1.1 configure a Server Load balancer instance
Configure the server Load balancer in Apache in three steps. Do not forget to restart Apache every time you modify httpd. conf and workers2.properties.
Step 1: Install and debug Apache
The JK2 module of the Server Load balancer is a plug-in of the Apache WWW Service. Therefore, you must install Apache before configuring the Server Load balancer. Assume that the user downloads the Windows version 2.0.43. execute setup.exe and answer some simple questions to complete the apache task. It is worth noting that after Apache is installed and started, if Apache is configured with http: // localhost/hosts.
Step 2: Install JK2
Change the downloaded mod_jk2-2.0.43.dll to mod_jk2.dll to the modules directory of Apache, modify httpd. conf of Apache, that is, insert the loading information of mod_jk2 module under loadmodule foo_module modules/mod_foo.so [ld7]:

# Example:# LoadModule foo_module modules/mod_foo.so#LoadModule jk2_module modules/mod_jk2.dll 

Step 3: Configure JK2

JK2 configurations are all in one configuration file named workers2.properties, which is in the same directory as Apache httpd. conf. The content of this file is as follows:

# ++ # Only beginnin. in production uncomment it out [logger. apache2] level = debug # SHM must be configured with [SHM] file = D: \ Program Files \ apache group \ apache2 \ logs \ SHM. filesize = 1048576 # The First tomcat address # example socket channel, override port and host. [channel. socket: cmdat1] Port = 11009 host = 127.0.0.1 # define the first worker pointing to the First tomcat # define the worker [ajp13: cmdat1] Channel = channel. socket: cmdat1 # Second Tomcat address # example socket channel, override port and host. [channel. socket: tomcat2] Port = 12009 host = 10.1.36.123 # define the second worker pointing to the second Tomcat # define the worker [ajp13: tomcat2] Channel = channel. socket: tomcat2 # define the Server Load balancer so that it contains two workers [LB: lB1] worker = ajp13: tomcat2worker = ajp13: tomcat1 # specify the Server Load balancer to map a single address, make sure that the URI of the Apache service points to the root # URI mapping [URI:/*] group = LB: on two Tomcat servers: lB1 # ++ ++

3.1.2 configure Tomcat

It belongs to two service entities in a cluster and requires the same feature. Therefore, we can install and configure the First tomcat, copy it to form the second tomcat, and configure the second tomcat.

Installing Tomcat is very simple and will not be described in this article. Assume that the First tomcat installation path is D: \ tomcat1.
Configure the First tomcat:

The default port of JK2 ctor in Tomcat is 8009. To run two Tomcat servers on one machine, modify D: \ tomcat1 \ conf \ jk2.properties and set the port of JK2 connector to 11009, the content of the entire file is as follows:

 #++++++++++++++                 channelSocket.port=11009               #++++++++++++++ 

To run two Tomcat servers on one machine, modify the Tomcat Stop command listening port of server. conf:

 <Server port="8005" shutdown="SHUTDOWN" debug="0">

Change

<Server port="11005" shutdown="SHUTDOWN" debug="0">

Then open JK2 AJP Connector and close other connector. The following figure shows JK2 AJP 1.3. Here, we have changed its port to 11009:

<!-- Define a Coyote/JK2 AJP 1.3 Connector on port 8009 --><Connector className="org.apache.coyote.tomcat4.CoyoteConnector"port="11009" minProcessors="5" maxProcessors="75" enableLookups="true"redirectPort="8443" acceptCount="10" debug="0" connectionTimeout="20000"useURIValidationHack="false" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" /> 

Configure the context of the webapp (such as examples) that requires cluster support and add the following Manager:

<Manager className="org.apache.catalina.session.InMemoryReplicationManager"protocolStack="UDP(mcast_addr=228.1.2.3;mcast_port=45566;ip_ttl=32):PING(timeout=3000; num_initial_members=6):FD(timeout=5000):VERIFY_SUSPECT(timeout=1500): pbcast.STABLE(desired_avg_gossip=10000):pbcast.NAKACK(gc_lag=10; retransmit_timeout=3000):UNICAST(timeout=5000;min_wait_time=2000): MERGE2:FRAG:pbcast.GMS(join_timeout=5000;join_retry_timeout=2000; shun=false;print_local_addr=false)"></Manager>

Note that the value of protocolstack must be written in one row.
Configure the second Tomcat:
Copy the first Tomcat that has been configured to form the second tomcat. Assume the path is D: \ tomcat2.
Modify D: \ tomcat2 \ conf \ jk2.properties and set port 12009 of JK2 connector. The content of the entire file is as follows:

#++++++++++++++channelSocket.port=12009#++++++++++++++

Modify server. conf
With the First tomcat configuration, we only need to modify the Tomcat Stop command listening port of server. conf:
<Server port = "11005" shutdown = "shutdown" DEBUG = "0"> changed
<Server port = "12005" shutdown = "shutdown" DEBUG = "0">
Set JK2 AJP Connector port to 12009.
3.1.3 run the test
Start Apache, tomcat1, and tomcat2.

We first prepare two files, the first file is test. jsp, and copy them to the directory of the root web application of the First tomcat, that is, D: \ tomcat1 \ webapps \ Root:

The second file is also test. jsp. Copy it to the directory of the root web application of the second tomcat, that is, under D: \ tomcat2 \ webapps \ Root:

Enter the address http: // localhost/test. jsp multiple times in different browsers to see different colors, which indicates that the JK2 module in Apache plays a role in Server Load balancer.

4 capacity plan
Capacity Planning is another important topic to improve performance when Tomcat is used in the production environment. If you do not consider the hardware and bandwidth under the expected network traffic, no matter how you modify the configuration or test it.
Here, we will first make a brief definition of the mentioned capacity plan: capacity plan refers to evaluating hardware, operating system, and network bandwidth to determine the service scope of the Application Service, seek an activity of software and hardware that suits the needs and software features. Therefore, the software mentioned here includes not only tomcat, but also any third-party web server software used in combination with Tomcat.
If you have no idea about the capacity plan before you purchase software and hardware or deploy the system, you do not know how much traffic the existing software and hardware environment can support, even worse, it may be too late to change the configuration when you realize that the configuration is faulty until you have already delivered and deployed the product in the production environment. In this case, you can only increase hardware investment, increase hard disk capacity, and even purchase better servers. If we plan the capacity in advance, we won't be so overwhelmed.
Here we will only introduce tomcat-related content.
First of all, to determine the capacity plan for Tomcat to use machines, you should start from the list of projects:
4.1.1 hardware
What kind of hardware system is used? How many computers are needed? Can I use a large computer or multiple minicomputers? How many CPUs are used on each computer? How much memory is used? What storage devices are used and what are the requirements for I/O processing speed? How to maintain these computers? How can different JVMs run on these hardware (for example, the ibm aix system can only run on the designed hardware system )?
4.1.2 network bandwidth
What is the maximum bandwidth usage? How does a web application process too many requests?
4.1.3 Server Operating System
Which operating system is used as the best site server? Which JVM is the best to use on a specific operating system? For example, does JVM support local multithreading and Symmetric Multi-processing on such systems? Which system makes Web servers faster, more stable, and cheaper. Does it support multiple CPUs?
4.1.4 Tomcat capacity plan
The following describes how to plan the capacity of Tomcat:
1) Quantify the load. If the site has been created and runs, you can use tools to simulate user access and determine the resource demand.
2) analyze the test results or the test process. You need to know that these requests cause heavy loads or use too many resources, and compare them with other requests to determine the bottleneck of the system. For example, if the servlet takes a long time to query the database, you need to consider using the buffer pool to reduce the response time.
3) determine the minimum performance standard. For example, you don't want users to spend 20 seconds waiting for the return of the result page, that is, even when the traffic limit is reached, the user's waiting time cannot exceed 20 seconds (from the click link to see the first returned data ). This time contains the database query time and file access time. Performance of similar products may have different standards in different companies. Generally, it is best to adopt the lowest standards in the same industry or evaluate this standard.
4) Determine how to reasonably use underlying resources and perform tests one by one. The underlying resources include CPU, memory, memory, bandwidth, operating system, and JVM. Deploy and test in order in various production environments to see if they meet the requirements. When testing tomcat, try to use several JVM types as much as possible, and adjust the JVM memory usage and tomcat thread pool size for testing. At the same time, in order to achieve the effect of fully reasonable and stable use of resources, it is also necessary to deal with the hardware system bottlenecks encountered during the test to determine reasonable resource configuration. This process is the most complex and generally relies on Theoretical Inference and empirical summarization because there is no reference value.
5) if the optimal combination is achieved through repeated tests in step 1, the product can be deployed in the same production environment.

In addition, remember to document your test process and results, because tests may be conducted later, so that you can take the previous test results as a reference. In addition, the test process should be repeated multiple times, and the conditions may be different each time. Therefore, the comparison of results and the selection of the best conditions can be performed only after record.

In this way, the customer system has found the best combination method through testing, and various resources have been reasonably configured, and the system performance has been greatly improved.

This article from: http://tangchenglin.iteye.com/blog/700705

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.