Detailed explanation of the method of improving the efficiency of Tomcat IO by using Tomcat Native

  • 2021-09-25 00:16:32
  • OfStack

Directory Introduction Tomcat Connection APR and Tomcat Native Use APR in tomcat

Brief introduction

There are many kinds of IO, from the initial Block IO, to nonblocking IO, to IO multiplexing and asynchronous IO, and the performance of IO is improved to the extreme step by step.

Today, we will introduce how to use Tomcat Native to improve the efficiency of Tomcat IO.

Connection mode of Tomcat

Connectors are used in Tomcat to handle communication with external clients. Connecter is mainly used to accept requests from external clients and forward them to the processing engine for processing.

There are two kinds of Connector in Tomcat. One is HTTP connector, and the other is AJP connector.

HTTP connector should be well understood, and it is also the default connector used by tomcat.

There is also a connector called AJP, and AJP is mainly used to communicate with web server. Because the speed of AJP protocol is faster than HTTP, AJP can not only communicate with other webserver, but also build tomcat cluster through AJP.

Both support the 4 protocols, namely BIO, NIO, NIO2, and APR.


# The following 4 Species Connector The implementation directly handles the data from the client Http Request 
org.apache.coyote.http11.Http11Protocol :  Support HTTP/1.1  Connector for the protocol. 

org.apache.coyote.http11.Http11NioProtocol :  Support HTTP/1.1  Agreement +New IO The connector of. 

org.apache.coyote.http11.Http11Nio2Protocol :  Support HTTP/1.1  Agreement +New IO2 The connector of. 

org.apache.coyote.http11.Http11AprProtocol :  Use APR ( Apache portable runtime) Connector of technology , Utilization Native


# The following 4 The implementation method is the same as the web server Deal with 
org.apache.coyote.ajp.AjpProtocol : Use AJP Protocol connector, implementation and web server (e.g. Apache httpd ) 

org.apache.coyote.ajp.AjpNioProtocol : SJP Agreement + New IO

org.apache.coyote.ajp.AjpNio2Protocol : SJP Agreement + New IO2

org.apache.coyote.ajp.AjpAprProtocol : AJP + APR

Let's talk about their differences. BIO is block IO is the most basic IO mode. We configure it like this:


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

Tomcat versions below 7 run in bio mode by default. Tomcat has removed support for BIO since Tomcat version 8.5.

New IO is an IO mode based on java. nio packet and its sub-packets. It can provide non-blocking IO mode, which has more efficient operation efficiency than traditional BIO.

We configure New IO as follows:


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

What is the difference between New IO and New IO2?

New IO2 is the IO mode introduced in tomcat8, which can be configured as follows:


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

apr is advanced in this way, which is the main function of tomcat native that we will explain today.

APR and Tomcat Native

The full name of apr is Apache Portable Runtime, and it is a highly portable library that is the core of Apache HTTP Server 2. x. APR has many uses, including accessing advanced IO features (such as sendfile, epoll, and OpenSSL), operating system-level features (generating random numbers, system state, and so on), and native process processing (shared memory, NT pipes, and Unix sockets).

Tomcat can call the core dynamic link library of Apache HTTP server in the form of JNI to handle file reading or network transmission operations, thus greatly improving the processing performance of Tomcat to static files.

By using APR, we can obtain the following features:

Non-blocking I/O and request connection retention. Support for OpenSSL and TLS/SSL.

Tomcat Native is a library through which Tomcat can use APR.

Therefore, the premise of using Tomcat Native is to install APR library, OpenSSL and JDK.

We can install apr and openssl in the following ways:

debian based linux System:


apt-get install libapr1.0-dev libssl-dev

rpm based Linux System:


yum install apr-devel openssl-devel

Under windows, tcnative is provided in the form of an dll, which can be downloaded and used directly.

But under linux, because the platform is different, tcnative needs to compile itself under linux.

1 In general, we can find the source package for tcnative at bin/tomcat-native. tar. gz. Unzip it.

Run the configure command first:


./configure --with-apr=/usr/bin/apr-1-config \
  --with-java-home=/home/jfclere/JAVA/jdk1.7.0_80/ \
  --with-ssl=yes \
  --prefix=$CATALINA_HOME

Then perform make operation:


make && make install

The resulting lib file will be put into $CATALINA_HOME/lib.

Using APR in tomcat

After installing tcnative, we can use APR in tomcat.

First check for the following configuration in 1 conf/server. xml:


<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

Then we need to modify $CATALINA_HOME/bin/setenv. sh to add the lib file for tc-native to LD_LIBRARY_PATH.


LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CATALINA_HOME/lib
export LD_LIBRARY_PATH

Finally, add the connection of APR:


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

Just run.

From the log, we will find the following contents:

org.apache.catalina.core.AprLifecycleListener init

INFO: Loaded APR based Apache Tomcat Native library 1.x.y.

org.apache.catalina.core.AprLifecycleListener init

INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].

org.apache.coyote.http11.Http11AprProtocol init

Explain that APR is installed and already in use.


Related articles: