Introduction to the

IO has many kinds, from the beginning of Block IO, to nonblocking IO, and then to IO multiplexing and asynchronous IO, step by step to improve the performance of IO to the extreme.

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

Tomcat connection mode

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

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

HTTP connector is the default tomcat connector.

There is also a connector called AJP, which is mainly used to communicate with Web servers. AJP is a faster protocol than HTTP, so AJP can be used to communicate with other Webservers as well as to build Tomcat clusters.

Both support four protocols, namely BIO, NIO, NIO2, and APR.

# the following four Connector implementation is directly with the Http request from the client org. Apache. Coyote. Http11. Http11Protocol: support Http /1.1Protocol connector. Org. Apache. Coyote. Http11. Http11NioProtocol: support HTTP /1.1Protocol +New IO connector. Org. Apache. Coyote. Http11. Http11Nio2Protocol: support HTTP /1.1Protocol +New IO2 connector. org.apache.coyote.http11.Http11AprProtocol : Using APR (Apache portable runtime) technology connectors, using Native # the following four kind of implementation method is to deal with the web server org. Apache. Coyote. Ajp. AjpProtocol: Using the AJP protocol connector, with a web server (such as Apache HTTPD) the communication between the org. Apache. Coyote. AJP. AjpNioProtocol: SJP agreement + New IO org. Apache. Coyote. Ajp. AjpNio2Protocol: SJP agreement + New IO2 org. Apache. Coyote. Ajp. AjpAprProtocol: ajp + APRCopy the code

To make the difference, BIO is block IO, which is the most basic IO method, and we configure it like this:

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

Versions below Tomcat7 run in BIO mode by default. Starting with Tomcat version 8.5, Tomcat removed support for BIO.

New IO is an IO approach based on the java.nio package and its subpackages. It provides non-blocking IO and is more efficient than traditional BIO.

We configure New IO like this:

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

What’s the difference between New IO and New IO2?

New IO2 is an I/O mode introduced in Tomcat8. You can configure it as follows:

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

Apr is advanced, and this is the main function of Tomcat Native that we are going to talk about today.

APR and Tomcat Native

Apr stands for Apache Portable Runtime and is a highly Portable library that is at the heart of Apache HTTP Server 2.x. APR has many uses, including access to advanced IO functions (such as SendFile, ePoll, and OpenSSL), operating system-level functions (generating random numbers, system state, etc.), and native process processing (shared memory, NT pipes, and Unix sockets).

Tomcat can use JNI to invoke the core dynamic link library of Apache HTTP server to process file reading or network transfer operations, thus greatly improving Tomcat’s performance in processing static files.

By using APR we can get the following features:

  1. Blocking I/O and request connections remain open.
  2. Supports 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 as follows:

Debian Based Linux system:

apt-get install libapr1. 0-dev libssl-dev
Copy the code

RPM Based Linux system:

yum install apr-devel openssl-devel
Copy the code

Under Windows, TCnative is provided in the form of a DLL, which can be downloaded and used directly.

However, under Linux, tcNative needs to be compiled by itself due to platform differences.

Generally speaking, we can find tcnative source package in 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_HOMECopy the code

Make again:

make && make install
Copy the code

The generated lib file will be placed in $CATALINA_HOME/lib.

Use APR in Tomcat

With TCNative installed, we are ready to use APR in Tomcat.

Check whether the following configuration is present in conf/server.xml:

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

Then we need to modify $CATALINA_HOME/bin/setenv.sh to add tC-native’s lib file to LD_LIBRARY_PATH.

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CATALINA_HOME/lib
export LD_LIBRARY_PATH
Copy the code

Finally add the connection for APR:

<Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"
connectionTimeout="20000"
redirectPort="8443" />
Copy the code

Run.

From the log, we can see the following:

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], The random [true]. Org. Apache. Coyote. Http11. Http11AprProtocol init INFO: the Initializing coyote to HTTP / 1.1 on HTTP - 8080Copy the code

APR is installed and is already in use.

Author: Flydean program stuff

Link to this article: www.flydean.com/tomcat-nati…

Source: Flydean’s blog

Welcome to pay attention to my public number: “procedures those things” the most popular interpretation, the most profound dry goods, the most concise tutorial, many you do not know the small skills you find!