在谈到tomcat优化时,必然要说到apr库,这个库是C语言实现的,tomcat通过JNI方式使用该库可以大大提高性能。
tomcat在使用apr时需要安装apr,apr-util和tomcat-native。
1. 下载
apr和apr-util需要单独下载:http://apr.apache.org/download.cgi。
tomcat-native就在$CATALINA_HOME/bin目录下:tomcat-native.tar.gz。
2. 安装apr库
(1)安装apr:安装在/usr/local/apr
[root@centosx64_tomcat1 ~]# tar xvf apr-1.6.2.tar.gz
[root@centosx64_tomcat1 ~]# cd apr-1.6.2
[root@centosx64_tomcat1 apr-1.6.2]# ./configure --prefix=/usr/local/apr
[root@centosx64_tomcat1 apr-1.6.2]# make && make test && make install
(2)安装apr-util:安装到/usr/local/apr-util
[root@centosx64_tomcat1 ~]# tar xvf apr-util-1.6.0.tar.gz
[root@centosx64_tomcat1 ~]# cd apr-util-1.6.0
[root@centosx64_tomcat1 apr-util-1.6.0]# ./configure --with-apr=/usr/local/apr --prefix=/usr/local/apr-util
[root@centosx64_tomcat1 apr-util-1.6.0]# make && make install
3. 安装tomcat-native
[root@centosx64_tomcat1 ~]# cd TOMCAT_HOME/bin
[root@centosx64_tomcat1 ~]# tar xvf tomcat-native.tar.gz
[root@centosx64_tomcat1 ~]# cd tomcat-native-1.1.30-src/jni/native
[root@centosx64_tomcat1 ~]# ./configure --with-apr=/usr/local/apr --with-java-home=$JAVA_HOME && make && make install
4. 配置tomcat使用apr库
有2种方式配置tomcat使用apr库:
方式一:配置环境变量
在unix系统中,只要$LD_LIBRARY_PATH变量值所在路径包含tomcat本地库,默认就使用org.apache.coyote.http11.Http11AprProtocol。
vim /etc/profile
export LD_LIBRARY_PATH=/usr/local/apr/lib
source /etc/profile
方式二:将apr库路径参数添加到系统变量中(推荐)
编辑$CATALINA_HOME/bin/setenv.sh
文件(如果文件不存在,新建文件):
export CATALINA_OPTS="$CATALINA_OPTS -Djava.library.path=/usr/local/apr/lib"
修改$TOMCAT_HOME/conf/server.xml:
<Connector port="8000" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443"
protocal修改为:org.apache.coyote.http11.Http11AprProtocol,重启tomcat。
5. 检查是否安装成功
如果看到下面的启动日志,表示成功.
Sep 17, 2017 1:59:19 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-apr-8080"]
Sep 17, 2017 1:59:19 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-apr-8009"]
Sep 17, 2017 1:59:19 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 31002 ms
6. 解决报错
(1)./configure时报错:
[root@centosx64_tomcat1 apr-1.6.2]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.6.2
checking for chosen layout... apr
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in '/root/apr-1.6.2':
**configure: error: no acceptable C compiler found in $PATH**
See `config.log' for more details
fix: 需要安装gcc编译器
[root@centosx64_tomcat1 apr-1.6.2]# yum -y install gcc
(2)./configure时报错:
[root@centosx64_tomcat1 apr-1.6.2]# ./configure --prefix=/usr/local/apr
configure: creating ./config.status
config.status: creating Makefile
config.status: creating include/apr.h
config.status: creating build/apr_rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating apr-1-config
config.status: creating apr.pc
config.status: creating test/Makefile
config.status: creating test/internal/Makefile
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
**rm: cannot remove `libtoolT': No such file or directory**
config.status: executing default commands
fix: 修改configure脚本
Edit your configure file:vim configure
Change the line
$RM "$cfgfile"
to
$RM -f "$cfgfile"
This will resolve the error
rm: cannot remove `libtoolT': No such file or directory
Then try run configure. That's it :)
参考:https://*.com/questions/18091991/error-while-compiling-apache-apr-make-file-not-found