前言
Tomcat 连接器有三种方式: bio、nio 和 apr,三种方式性能差别很大,apr 的性能最优, bio 的性能最差。
Tomcat 7 使用的 Connector 默认就启用的 apr 协议,但需要系统安装 apr 库,否则就会使用 bio 方式。
如果系统没有安装apr,那么在启动tomcat的时候,catalina.out会打印出如下语句:
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
安装 apr 和 apr-util
如下网址找到apr最新版:http://apache.fayea.com/apr/
#download the latest version
cd /usr/local/
wget http://apache.fayea.com/apr/apr-1.5.2.tar.gz
wget http://apache.fayea.com/apr/apr-util-1.5.4.tar.gz
#unzip the package
tar -zxvf apr-1.5.2.tar.gz
tar -zxvf apr-util-1.5.4.tar.gz
#install the apr and apr-util
cd apr-1.5.2
./configure --prefix=/usr/local/apr
make
make install
cd ..
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make insatll
安装tomcat-native
在tomcat安装目录bin下面已经存在了对应版本的tomcat-native安装包了
#The package already in below path
cd /var/tomcat/tomcat-7/bin
tar -zxvf tomcat-native.tar.gz
cd tomcat-native-1.1.33-src/jni/native
./configure --with-apr=/usr/local/apr
make
make install
设置APR环境变量
vim /etc/profile
#add below line to end of the file
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
#save the file and execute below to make effective
source /etc/profile
重启tomcat查看catalina.out日志
如果发现有下面的log说明APR启用成功
@H_502_102@INFO: Loaded APR based Apache Tomcat Native library 1.1.33 using APR version 1.5.2.