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

Basic operations on invisible columns in MySQL 8.0

Table of contents 01 Create invisible columns 02 ...

Instructions for nested use of MySQL ifnull

Nested use of MySQL ifnull I searched online to s...

Summary of the application of transition components in Vue projects

​Transtion in vue is an animation transition enca...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

JavaScript function call, apply and bind method case study

Summarize 1. Similarities Both can change the int...

Detailed explanation of the application of the four states of hyperconnection

Although you think it may be a browser problem, i...

Implementation of pushing Docker images to Docker Hub

After the image is built successfully, it can be ...

MySQL Database Basics SQL Window Function Example Analysis Tutorial

Table of contents Introduction Introduction Aggre...

Implementing simple tabs with js

Tab selection cards are used very frequently on r...

The difference between distinct and group by in MySQL

Simply put, distinct is used to remove duplicates...

A brief introduction to the usage of decimal type in MySQL

The floating-point types supported in MySQL are F...

Detailed explanation of how to view MySQL memory usage

Preface This article mainly introduces the releva...

About Zabbix forget admin login password reset password

The problem of resetting the password for Zabbix ...