Configure Tomcat + Apr + native to handle high concurrency under Linux

Source: Internet
Author: User
Tags server port java se

In the slow network tomcat threads open to more than 300 of the level, does not match APR, basically 300 threads will be full, the request will have to wait. However, with APR,Tomcat will call the Apache HTTP Server's core dynamic link library to handle file read or network transfer operations, when the number of concurrent threads is significantly reduced, from the original 300 may immediately drop to only dozens of, The new request will come in without a block.

Introduction of Tomcat Running mode

Tomcat has three modes of operation (BIO,NIO.APR), first of all to briefly introduce:

(1) BIO  

BIO (blocking I/O), as the name implies, is a blocking I/O operation that indicates that Tomcat is using traditional Java I/O operations (that is, the Java.io package and its sub-packages). Tomcat, by default, is run in bio mode. Unfortunately, in general, the Bio mode is the lowest performance in three operating modes. We can view the current state of the server through Tomcat Manager.

(2) NIO

NiO is a new I/O operation (i.e. the Java.nio package and its child packages) provided by Java SE 1.4 and subsequent releases. Java NiO is a buffer-based, Java API that provides non-blocking I/O operations, so NiO is also considered an abbreviation for non-blocking I/O. It has better concurrency performance than traditional I/O operations (bio). Want to run in this mode, directly modify the connector node in Server.xml, modify the protocol as:

(3) APR  

(Apache portable Runtime/apache Portable runtime), which is the support library for Apache HTTP servers. You can simply understand that Tomcat will invoke the core dynamic link Library of the Apache HTTP server in the form of JNI to handle file reads or network transfer operations, thus greatly improving the performance of Tomcat's handling of static files. Tomcat APR is also the preferred mode for running high concurrent applications on Tomcat. For Tomcat to support APR, you must install APR and native so that Tomcat can take advantage of Apache's APR interface and use part of the operating system's local operations to improve performance.

Second, the Linux configuration tomcat+apr+native

2.1 Installation Package Preparation

    • Jdk-7u76-linux-x64.tar.gz (must be JDK1.7 above to support Apr)
    • Apr-1.5.2.tar.gz
    • Apr-util-1.5.4.tar.gz
    • Apache-tomcat-7.0.56.tar.gz (the installation package containing tomcat-native in the bin directory after decompression)

2.2 Installing the JDK

2.2.1 Decompression

TAR-ZXVF jdk-7u76-linux-x64.tar.gz

2.2.2 Creating the/usr/local/java folder

Mkdir-p/usr/local/java

2.2.3 Move to/usr/local/java

MV Jdk1.7.0_76/usr/local/java

2.2.4 Adding environment variables

Vi/etc/profile

and finally add

#jdk1.7export java_home=/usr/local/java/jdk1.7.0_76export classpath= $JAVA _home/lib/export path=/usr/local/ruby/ Bin: $PATH: $JAVA _home/bin

Note: If the user installs the JDK as a non-root account, the JDK needs to be configured under the root account. At this point the JDK configuration path can be the same as the configuration path for the non-root account. After the configuration is complete, ensure that echo $JAVA _home non-empty. Examples are as follows:

Export Java_home=/opt/jdk1.7.0_79export classpath= $JAVA _home/lib/

2.2.5 Restart environment variables to make configuration effective

Source/etc/profile

See if it takes effect

2.3 Installing APR

Apr and Apr-util must be installed as root. need to install apr-1.5.2.tar.gz and apr-util-1.5.4.tar.gz

2.3.1 Decompression apr-1.5.2.tar.gz and apr-util-1.5.4.tar.gz

TAR-ZXVF APR-1.5.2.TAR.GZTAR-ZXVF apr-util-1.5.4.tar.gz

2.3.2 detection, compilation, installation

Go to apr-1.5.2 and execute the following command:

./configure--prefix=/usr/local/apr && make && make install

Note: The/user/local path is the default installation path for subsequent native, please do not modify it.

Go to apr-util-1.5.4 and execute the following command:

./configure--with-apr=/usr/local/apr/--prefix=/usr/local/apr-utils && make && make install

2.4 Modifying the maximum number of file handles and the number of open files

Since open files are much more, consider modifying the default number of open files

2.4.1 Modifying/etc/sysctl.conf

Net.ipv4.ip_local_port_range = 10240 65535net.ipv4.ip_nonlocal_bind = 1

2.4.2 adds the following two lines of records to the/etc/security/limits.conf last

* Soft Nofile 65535* hard nofile 65535

2.4.3 Loading Bridge Module

Modprobe Bridge

2.4.4 re-loaded into sysctl to make its change effective

2.4.5 again exit re-login, with ulimit-a view

You can see that the open files have changed from the default of 1024 to 65535.

2.5 Installing Tomcat-native

2.5.1 Decompression tomcat-native.tar.gz

TAR-ZXVF tomcat-native.tar.gz

2.5.2 into the extracted directory

CD tomcat-native-1.1.31-src/jni/native

2.5.3 detection, compilation, installation

./configure--with-apr=/usr/local/apr/bin/apr-1-config--with-java-home=/opt/jdk1.7.0_79 && make && Make install

The bold part can be adjusted according to the actual situation.

2.5 Configuring Tomcat

<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><server port= "9016" shutdown= "Shutdown" > <!--off HTTPS security verification-- <listener classname= "Org.apache.catalina.core.AprLifecycleListener" sslengine= "Off"/> <listener className = "Org.apache.catalina.core.JasperListener"/> <listener classname= " Org.apache.catalina.core.JreMemoryLeakPreventionListener "/> <listener classname=" Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener "/> <listener classname=" Org.apache.catalina.core.ThreadLocalLeakPreventionListener "/> <GlobalNamingResources> <resource name= "Userdatabase" auth= "Container" type= "Org.apache.catalina.UserDatabase" description= "User Databa              SE, can be updated and saved "factory=" Org.apache.catalina.users.MemoryUserDatabaseFactory " Pathname= "Conf/tomcat-users.xml"/> </GlobalNamingResources> <service name= "Catalina" > <!--the con Nectors can use a shared exEcutor, can define one or more named thread pools--> <!--configuration Thread--<executor name= "Tomcatthreadpool" nameprefix= "catalina-exec-" maxthreads= "minsparethreads=" maxidletime= "4000"/> <!- -Configure APR--<connector executor= "Tomcatthreadpool" port= "8080" protocol= "org.apache.coyote.http11.h Ttp11aprprotocol "uriencoding=" UTF-8 "enablelookups=" false "acceptcount=" ConnectionTimeout = "maxkeepaliverequests=" "redirectport=" 8443 "/> <connector port=" 9109 "protocol=" ajp/ 1.3 "redirectport=" 8443 "/> <engine name=" Catalina "defaulthost=" localhost "> <realm classname=" Org.apa               Che.catalina.realm.LockOutRealm "> <realm classname=" Org.apache.catalina.realm.UserDatabaseRealm " Resourcename= "Userdatabase"/> </Realm> 

Also add the following configuration in the catalina.sh file:

Java_home= "/opt/jdk1.7.0_79" Catalina_opts=-djava.library.path=/usr/local/apr/lib

When the configuration is complete, start Tomcat and the following prompt indicates a successful configuration:

See also: http://www.cnblogs.com/zishengY/p/7101616.html

Configure Tomcat + Apr + native to handle high concurrency under Linux

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.