Detailed explanation of how to use Tomcat Native to improve Tomcat IO efficiency

Detailed explanation of how to use Tomcat Native to improve Tomcat IO efficiency

Introduction

There are many types of IO, from the initial Block IO, to nonblocking IO, to IO multiplexing and asynchronous IO, step by step to maximize the performance of IO.

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

How to connect to Tomcat

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

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

The HTTP connector should be well understood by everyone. It is also the default connector used by Tomcat.

There is also a connector called AJP, which is mainly used to communicate with web servers. Because the AJP protocol is faster than HTTP, in addition to communicating with other web servers, AJP can also be used to build a Tomcat cluster.

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

#The following four Connector implementations all directly process Http requests from clients org.apache.coyote.http11.Http11Protocol: A connector that supports the HTTP/1.1 protocol.

org.apache.coyote.http11.Http11NioProtocol: A connector that supports HTTP/1.1 protocol + New IO.

org.apache.coyote.http11.Http11Nio2Protocol: A connector that supports HTTP/1.1 protocol + New IO2.

org.apache.coyote.http11.Http11AprProtocol : A connector that uses APR (Apache portable runtime) technology, using Native


#The following four implementation methods are to deal with the web server org.apache.coyote.ajp.AjpProtocol: Use the AJP protocol connector to achieve communication with the web server (such as Apache httpd) org.apache.coyote.ajp.AjpNioProtocol: SJP protocol + New IO

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

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

Let's talk about their differences. BIO is block IO, which is the most basic IO method. 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. Since Tomcat 8.5, Tomcat has removed support for BIO.

New IO is an IO method based on the java.nio package and its subpackages. It can provide non-blocking IO mode and has more efficient operation efficiency than traditional BIO.

We configure New IO like this:

<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 method introduced in tomcat8. We can configure it like this:

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

The apr method is advanced, and this is the main function of tomcat native that we are going to explain today.

APR and Tomcat Native

The full name of apr is Apache Portable Runtime, which is a highly portable library and the core of Apache HTTP Server 2.x. APR has many uses, including access to high-level IO functionality (such as sendfile, epoll, and OpenSSL), OS-level functionality (generating random numbers, system status, etc.), and native process handling (shared memory, NT pipes, and Unix sockets).

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

By using APR we can get the following features:

  • Non-blocking I/O and request connection maintenance.
  • Supports OpenSSL and TLS/SSL.

Tomcat Native is a library that allows Tomcat to use APR.

Therefore, the prerequisite for using Tomcat Native is to install APR library, OpenSSL and JDK.

We can install apr and openssl in the following way:

Debian based Linux system:

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

rpm based Linux systems:

yum install apr-devel openssl-devel

Under Windows, tcnative is provided in the form of a dll, and we can directly download and use it.

However, under Linux, due to the different platforms, tcnative needs to be compiled by itself under Linux.

Generally speaking, we can find the source package of tcnative 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_HOME

Then perform the make operation:

make && make install

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

Using APR in tomcat

After installing tcnative, we can use APR in tomcat.

First check whether there is the following configuration in 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 tc-native lib file to LD_LIBRARY_PATH.

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CATALINA_HOME/lib
export LD_LIBRARY_PATH

Finally, add the APR connection:

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

Just run it.

From the log, we can find the following:

org.apache.catalina.core.AprLifecycleListener init

INFO: Loaded APR based Apache Tomcat Native library 1.xy

org.apache.catalina.core.AprLifecycleListener init

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

org.apache.coyote.http11.Http11AprProtocol init

This indicates that APR has been installed and is being used.

This is the end of this article about how to use Tomcat Native to improve Tomcat IO efficiency. For more information about how to use Tomcat Native to improve Tomcat IO efficiency, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Tutorial on installing and configuring Tomcat in Native APR mode
  • Detailed explanation of Tomcat's Server Options
  • Tomcat startup error: solution to java.util.zip.ZipException
  • How to set BIO, NIO and APR modes of Tomcat on Linux server
  • Tomcat implements session sharing (session replication)

<<:  Detailed explanation of replication configuration example between mysql containers

>>:  What are the rules for context in JavaScript functions?

Recommend

Detailed explanation of Vue's list rendering

Table of contents 1. v-for: traverse array conten...

Summary of Several Methods for Implementing Vertical Centering with CSS

In the front-end layout process, it is relatively...

How to optimize images to improve website performance

Table of contents Overview What is Image Compress...

A complete guide to Linux environment variable configuration

Linux environment variable configuration When cus...

Modification of the default source sources.list file of ubuntu20.04 LTS system

If you accidentally modify the source.list conten...

Detailed explanation of the usage of position attribute in HTML (four types)

The four property values ​​of position are: 1.rel...

15 important variables you must know about MySQL performance tuning (summary)

Preface: MYSQL should be the most popular WEB bac...

Linux 6 steps to change the default remote port number of ssh

The default ssh remote port in Linux is 22. Somet...

A brief analysis of the use of the HTML webpack plugin

Using the html-webpack-plugin plug-in to start th...

About the pitfalls of implementing specified encoding in MySQL

Written in front Environment: MySQL 5.7+, MySQL d...

MySQL aggregate function sorting

Table of contents MySQL result sorting - Aggregat...

Implementation of Nginx hot deployment

Table of contents Semaphore Nginx hot deployment ...

Example of converting spark rdd to dataframe and writing it into mysql

Dataframe is a new API introduced in Spark 1.3.0,...

Example of using setInterval function in React

This article is based on the Windows 10 system en...