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 improvement 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 pass 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.
It should be easy for everyone to understand HTTP connector, it is also the connector used by tomcat by default.
There is also a connector called AJP. AJP is mainly used to communicate with web servers. Because the speed of the AJP protocol is faster than HTTP, AJP can not only be used to communicate with other webservers, but also to build a tomcat cluster through AJP.
Both methods support the four protocols, namely BIO, NIO, NIO2 and APR.
#The following four Connector implementations are all directly handled Http requests from the client.http11.Http11Protocol : supportHTTP/1.1 Protocol connector。 .http11.Http11NioProtocol : supportHTTP/1.1 protocol+New IOConnectors。 .http11.Http11Nio2Protocol : supportHTTP/1.1 protocol+New IO2Connectors。 .http11.Http11AprProtocol : useAPR(Apache portable runtime)技术Connectors,useNative #The following four implementation methods are to deal with web server:useAJPProtocol connector,Implementation andweb server(likeApache httpd)Communication between :SJPprotocol+ New IO .AjpNio2Protocol:SJPprotocol+ New IO2 :AJP + APR
Let’s talk about their differences. BIO is block IO. The most basic IO method is. We configure it in this way:
<Connector port=”8080” protocol=”HTTP/1.1” maxThreads=”150” connectionTimeout=”20000” redirectPort=”8443” />
The following versions of Tomcat7 run in bio mode by default. Since Tomcat version 8.5, Tomcat has removed support for BIO.
New IO is an IO method based on packages and their subpackages. It can provide non-blocking IO methods, which has and is more efficient than traditional BIO.
We configure New IO in this way:
<Connector port="8080" protocol=".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=".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 is the core of Apache HTTP Server. APR has many uses, including access to advanced IO features such as sendfile, epoll, and OpenSSL, operating system-level features (generating random numbers, system state, etc.), and native process processing (shared memory, NT pipelines, 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 on static files.
By using APR we can obtain the following features:
- Non-blocking I/O and request connections are maintained.
- Supports OpenSSL and TLS/SSL.
Tomcat Native is a library through which Tomcat can 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 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 a DLL, and we can download and use it directly.
However, under Linux, because the platforms are different, tcnative needs to be compiled by itself under Linux.
Generally speaking, we can find the source code package of tcnative in bin/. 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 make operation:
make && make install
The generated lib file 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/:
<Listener className="" SSLEngine="on" />
Then we need to modify $CATALINA_HOME/bin/ 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=".http11.Http11AprProtocol" connectionTimeout="20000" redirectPort="8443" />
Just run.
From the log, we will find the following:
init
INFO: Loaded APR based Apache Tomcat Native library .
init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
.http11.Http11AprProtocol init
This means that the APR has been installed and has been used.
This is the end of this article about how to use Tomcat Native to improve Tomcat IO efficiency. For more related content on Tomcat Native to improve Tomcat IO efficiency, please search for my previous articles or continue to browse the related articles below. I hope everyone will support me in the future!