SoFunction
Updated on 2025-04-16

Detailed explanation and practical records of Springboot Tomcat APR model

The previous article explains the nio threading model of tomcat. Nio is also an io mode that we use more often in daily life. I won’t talk about the advantages of nio compared to bio. There are many articles on the Internet that are very detailed. Today, I will mainly introduce the apr mode of tomcat. The full name of apache portable runtime is Apache portable runtime. Here is the explanation of wiki for apr:

It is easy to understand that it is a set of class libraries based on the underlying operating system prepared for apache server.

Let me first talk about the performance improvement of Tomcat after switching from Nio to Apr mode. It uses the company's 4c 8g PC server and the pressure test data when concurrent 400 is transmitted. The performance improvement here is not as big as many netizens said. Some netizens even achieved 80% performance improvement. I have tested several rounds and increased by about 20%. The pressure test project TPS has increased from the original 1500 to the maximum of about 1800. I am quite satisfied with this result. This indicator can be achieved without changing the business logic code.

The overall mode of APR is still non-blocking IO, and the implemented thread model is also implemented according to the standard model of NIO, from the official documentation (/docs/apr/1.6/) You can see that APR has rewritten most of the IO and system thread operation modules in c according to different operating systems. This is why APR can be improved without changing the code. For the specific principles, please refer to the Tomcat NIO thread mode article I wrote below.

The following are the modules rewritten by APR:

The Tomcat embedded in Springboot is enabled by default. If we want to use APR mode on the Linux kernel system, we need to install some lib libraries, which can be done throughrpm -q | grep aprTo check whether Apr is installed, if it is installed, it no longer needs to be installed. If it is not installed, the following libraries need to be installed:

1) Openssl requires that the version is greater than 1.0.2. If you do not use https openssl, you can not install it. That is, an error of openssl will be reported at startup, and you can just ignore it directly;

2) apr, you can download the latest version of 1.6.2 on the official website to download it/

apr-util, download on the same page, the latest version is version 1.6.0

apr-iconv, downloaded on the same page, the latest version is version 1.2.1

tomcat-native, comes with an installation package in tomcat, which can be found in the bin directory of tomcat;

Install apr

Download the apr installation package apr-1.6.

tar -xvf apr-1.6.
cd apr-1.6.2

./configure Check whether the installation conditions meet and configure the installation parameters, check whether the class library is missing. Generally speaking, if the installed system is not a simplified version, it can be successfully passed.

make & make install

If the installation path is not set, the default installation path of the system is /usr/local/apr/lib

Install apr-util

Download the apr-util installation package apr-util-1.6.

tar -xvf apr-util-1.6.
cd apr-util-1.6.0
./configure --with-apr=/usr/local/apr/lib --with-java-home=/usr/lib/jvm/jdk-8u144-linux-x64/jdk1.8.0_144 Installapr-utilRequires configurationaprPath andjvmpath,Otherwise, an error will be reported and cannot be foundapr
make & make install

Install apr-iconv

download

tar -xvf 
cd apr-iconv
./configure --with-apr=/usr/local/apr/lib --with-java-home=/usr/lib/jvm/jdk-8u144-linux-x64/jdk1.8.0_144
make & make install

Install tomcat-native

cd tomcat/bin
tar -xvf tomcat-native
cd tomcat-native
./configure --with-apr=/usr/local/apr/lib --with-java-home=/usr/lib/jvm/jdk-8u144-linux-x64/jdk1.8.0_144
make & make install

At this point, the installation work is complete. If you want to view the java path installed by the machine, you can view it through which java

Configure apr

vi /etc/profile

Add export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr-1.6.2/lib in front of the profile

Enter source /etc/profile on the command line to make it take effect

Added APRConfig class

Most of the articles on the Internet that explain configuring tomcat apr are just about how to configure apr on independent tomcat services. You only need to modify the protocol of the connector in the process. Springboot will be a little more complicated. You need to add an apr configuration class to modify the tomcat connector network access protocol of Embed when starting.

Start springboot

I thought that after doing this, you could start springboot and open the apr mode directly, but you would find an error when you start it, and this error would be very puzzled. The error message should be that the service startup port is occupied, but in fact, this is just a superficial phenomenon and not the fundamental reason.

After opening debug, checking the system log and finding that the real reason is that the system cannot find the lib library of apr.

When I saw this error prompt, I suddenly realized that I quickly added the path of apr to the startup parameters and restarted.

After successful startup, you will see the following content in the log, which means that the apr mode is successfully launched and you will start to enjoy the rapid experience brought by APR.

Source code demo git download address:/feiweiwei/

This is the end of this article about the detailed explanation and practice of Springboot Tomcat APR model. For more related content in Springboot Tomcat APR model, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!