(Turn) Configure tomcat+apr+native under Linux to deal with high concurrency

Abstract : On a slow network, the number of Tomcat threads is more than 300. Without APR, basically 300 threads will be used up quickly, and future requests will have to wait. However, with APR, Tomcat will call the core dynamic link library of the Apache HTTP server in the form of JNI to process file reading or network transmission operations. At this time , the number of concurrent threads is significantly reduced, from the original 300 may immediately drop to only Dozens, new requests will come in without blocking.

1. Introduction of three operating modes

Tomcat has three (bio, nio.apr) operating modes. First, let’s briefly introduce

bio 
bio (blocking I/O), as the name suggests, is blocking I/O operations, which means that Tomcat uses traditional Java I/O operations (ie, the java.io package and its sub-packages). Tomcat runs in bio mode by default. Unfortunately, in general, bio mode is the least performant of the three operating modes. We can view the current status of the server through the Tomcat Manager.

nio 
is a new I/O operation method provided by Java SE  1.4 and later (ie, the java.nio package and its subpackages). Java nio is a buffer-based Java API that provides non-blocking I/O operations, so nio is also seen as an abbreviation for non-blocking I/O. It has better concurrent running performance than traditional I/O operations (bio).

If you want to run in this mode, directly modify the Connector node in server.xml, and modify the protocol to 

<Connector port="80" protocol="org.apache.coyote.http11.Http11NioProtocol"
	connectionTimeout="20000"
	URIEncoding="UTF-8"
	useBodyEncodingForURI="true"
	enableLookups="false"
	redirectPort="8443" />

apr 
(Apache Portable Runtime/Apache Portable Runtime) is a support library for the Apache HTTP server. You can simply understand that Tomcat will call the core dynamic link library of the Apache HTTP server in the form of JNI to process file reading or network transmission operations, thereby greatly improving Tomcat's processing performance for static files. Tomcat apr is also the preferred mode for running highly concurrent applications on Tomcat.

For tomcat to support apr, apr and native must be installed, so that tomcat can use apache's apr interface and use some local operations of the operating system to improve performance.

There are 3 operating modes of Tomcat. Modify their operating modes. Whether the operation of the 3 modes is successful, you can see his startup console or startup log. Or log in to their default page http://localhost:8080/ to view them server status. 

Next, introduce tomcat+apr+native configuration under linux

 

2. Configure tomcat+apr+native under linux

2.1 Installation package preparation

jdk-7u76-linux-x64.tar.gz (apr must be supported by JDK1.7 or above)

apr-1.5.2.tar.gz

apr-util-1.5.4.tar.gz

apache-tomcat-7.0.56.tar.gz (after decompression, its bin directory contains the installation package of tomcat-native)

2.2 Install JDK

This is the primary environment for running Tomcat, so this step is performed first.

2.2.1 Decompression

tar -zxvf jdk-7u76-linux-x64.tar.gz

2.2.2 Create /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

add at the end

# jdk1.7
export JAVA_HOME=/usr/local/java/jdk1.7.0_76
export CLASSPATH=$JAVA_HOME/lib/
export PATH=/usr/local/ruby/bin:$PATH:$JAVA_HOME/bin

2.2.5 Restart the environment variable to make the configuration take effect

source /etc/profile

Check if it works

   If the following words appear, the installation is successful, and then install tomcat

2.3 Install tomcat

2.3.1 Decompress tomcat

tar -zxvf apache-tomcat-7.0.56.tar.gz

2.3.2 Renamed to tomcat7

mv apache-tomcat-7.0.56 tomcat7

2.3.3 Modify tomcat memory (jvm memory)

I put tomcat here in the /home directory

vi /home/tomcat7/bin/catalina.sh

add in

JAVA_OPTS="-server -Xms1024M -Xmx1024M -XX:PermSize=512M -XX:MaxNewSize=512M -XX:MaxPermSize=512M"

  2.3.4 Modify the operating mode

vi /home/tomcat7/conf/server.xml

add on

copy code
<!-- configure apr -->
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"
               URIEncoding="UTF-8" enableLookups="false" acceptCount="50"
               connectionTimeout="1000" maxKeepAliveRequests="250"
               redirectPort="8443" />
copy code

A complete configuration of server.xml is posted below

copy code
<?xml version='1.0' encoding='utf-8'?>

<Server port="9016" shutdown="SHUTDOWN">

  <!--Turn 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 database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!-- configure thread-->
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="500" minSpareThreads="25"
        maxIdleTime="4000"
        />

    <!-- configure apr -->
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"
               URIEncoding="UTF-8" enableLookups="false" acceptCount="50"
               connectionTimeout="1000" maxKeepAliveRequests="250"
               redirectPort="8443" />
    

    <Connector port="9109" protocol="AJP/1.3" redirectPort="8443" />


    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">

        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

         <!-- Configure access log format-->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>
copy code

2.4 Install tomcat-native

2.4.1 Go to /home/tomcat7/bin/

cd /home/tomcat7/bin

2.4.2 Decompress tomcat-native.tar.gz

tar -zxvf tomcat-native.tar.gz

2.4.3 Go to the decompressed directory

cd tomcat-native-1.1.31-src/jni/native

2.4.4 Detection, compilation and installation

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

2.5 Install apr

  Need to install apr-1.5.2.tar.gz and apr-util-1.5.4.tar.gz

2.5.1 Decompress apr-1.5.2.tar.gz and apr-util-1.5.4.tar.gz

tar -zxvf apr-1.5.2.tar.gz
tar -zxvf apr-util-1.5.4.tar.gz

2.5.2 Detection, compilation and installation

Enter into apr-1.5.2 and execute
./configure --prefix=/usr/local/apr && make && make install
Enter into apr-util-1.5.4 and execute
./configure --with-apr=/usr/local/apr/ --prefix=/usr/local/apr-utils && make && make install

2.6 Modify the maximum number of file handles and the number of open files

  Since there will be more open files, consider modifying the default number of open files

2.6.1 Modify /etc/sysctl.conf

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

2.6.2 Add the following two lines at the end of /etc/security/limits.conf         

* soft nofile 65535
 * hard nofile 65535

 2.6.3 Load bridge module

modprobe bridge

 2.6.4 Reload sysctl to make changes take effect

sysctl -p

2.6.5 Log out and log in again, use ulimit -a to view

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

2.7 Start tomcat

/home/tomcat7/bin/startup.sh

If the following prompt appears, the configuration is successful

3. Error handling

  3.1、启动tomcat时报“The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/local/apr/lib”

          Solution : Make sure tomcat-native is installed successfully, otherwise, execute 2.5 above. If it is still unsuccessful, execute the following steps
            #vi /opt/tomcat_api_8035/bin/catalina.sh
            CATALINA_OPTS="-Djava.library.path=/usr/local/apr/lib"
      3.2 、安装apr时报 ‘Neither the JAVA_HOME nor the JRE_HOME environment variable is defined“
          Solution : This is the unset environment JAVA_HOME and JAVA_JRE directory, please ensure that the jdk installation is successful, and confirm that the /etc/profile environment variable is configured correctly
      3.3 . When executing sysctl -p, the following errors were found in the output
         net.ipv4.ip_forward = 0 
        net.ipv4.conf.default.rp_filter = 1 
        net.ipv4.conf.default.accept_source_route = 0 
        kernel.sysrq = 0 
        kernel.core_uses_pid = 1 
        net.ipv4.tcp_syncookies = 1 
        error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key 
        error: "net.bridge.bridge-nf-call-iptables" is an unknown key 
        error: "net.bridge.bridge-nf-call-arptables" is an unknown key 
        kernel.msgmnb = 65536 
        kernel.msgmax = 65536 
        kernel.shmmax = 68719476736 
        kernel.shmall = 4294967296
       Reason : The above three parameters depend on the bridge module. If the module is not loaded, the above output error will appear
       Solution : Execute the modprobe bridge command to load the bridge module
 

3. Error handling

  3.1、启动tomcat时报“The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/local/apr/lib”

          Solution : Make sure tomcat-native is installed successfully, otherwise, execute 2.5 above. If it is still unsuccessful, execute the following steps
            #vi /opt/tomcat_api_8035/bin/catalina.sh
            CATALINA_OPTS="-Djava.library.path=/usr/local/apr/lib"
      3.2 、安装apr时报 ‘Neither the JAVA_HOME nor the JRE_HOME environment variable is defined“
          Solution : This is the unset environment JAVA_HOME and JAVA_JRE directory, please ensure that the jdk installation is successful, and confirm that the /etc/profile environment variable is configured correctly
      3.3 . When executing sysctl -p, the following errors were found in the output
         net.ipv4.ip_forward = 0 
        net.ipv4.conf.default.rp_filter = 1 
        net.ipv4.conf.default.accept_source_route = 0 
        kernel.sysrq = 0 
        kernel.core_uses_pid = 1 
        net.ipv4.tcp_syncookies = 1 
        error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key 
        error: "net.bridge.bridge-nf-call-iptables" is an unknown key 
        error: "net.bridge.bridge-nf-call-arptables" is an unknown key 
        kernel.msgmnb = 65536 
        kernel.msgmax = 65536 
        kernel.shmmax = 68719476736 
        kernel.shmall = 4294967296
       Reason : The above three parameters depend on the bridge module. If the module is not loaded, the above output error will appear
       Solution : Execute the modprobe bridge command to load the bridge module

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256243&siteId=291194637