(1)下载
APR没有二进制包可以下载,所以只能下载源代码版,下载后需要构建,APR的官网为:http://apr.apache.org/
(2)安装
windows下构建源代码需要Visual Studio支持。
Linux下构建相对简单:
(3)tomcat 8.5.51使用apr(8.0开始默认nio)
1、源码安装以下apr组件,需要挂载os的yum源或镜像,方便部署依赖包
apr-1.7.0.tar.gz
apr-iconv-1.2.2.tar.gz
apr-util-1.6.1.tar.gz
tomcat-native-1.2.35-src.tar.gz
2、apr、apr-icon、apr-util安装
2.1 apr安装
./configure --prefix=/home/antif/.projects/apr
make
make install
安装成功后,APR会安装在指定安装目录/home/antif/.projects/apr。
libtool: install: /bin/install -c -m 755 .libs/libapr-1.lai /home/antif/.projects/apr/lib/libapr-1.la
libtool: install: /bin/install -c -m 755 .libs/libapr-1.a /home/antif/.projects/apr/lib/libapr-1.a
libtool: install: chmod 644 /home/antif/.projects/apr/lib/libapr-1.a
libtool: install: ranlib /home/antif/.projects/apr/lib/libapr-1.a
libtool: warning: remember to run 'libtool --finish /usr/local/apr/lib'
/bin/install -c -m 644 apr.exp /home/antif/.projects/apr/lib/apr.exp
/bin/install -c -m 644 apr.pc /home/antif/.projects/apr/lib/pkgconfig/apr-1.pc
for f in libtool shlibtool; do \
if test -f ${f}; then /bin/install -c -m 755 ${f} /home/antif/.projects/apr/build-1; fi; \
done
/bin/install -c -m 755 /home/antif/.projects/tomcat-apr/apr-1.7.0/build/mkdir.sh /home/antif/.projects/apr/build-1
for f in make_exports.awk make_var_export.awk; do \
/bin/install -c -m 644 /home/antif/.projects/tomcat-apr/apr-1.7.0/build/${f} /home/antif/.projects/apr/build-1; \
done
/bin/install -c -m 644 build/apr_rules.out /home/antif/.projects/apr/build-1/apr_rules.mk
/bin/install -c -m 755 apr-config.out /home/antif/.projects/apr/bin/apr-1-config
[antif@mtg172 apr-1.7.0]$ libtool --finish /usr/local/apr/lib
libtool: error: invalid argument '/usr/local/apr/lib'
[antif@mtg172 apr-1.7.0]$ libtool --finish /home/antif/.projects/apr/lib/
libtool: finish: PATH="/home/antif/hadoop-2.7.3/bin:/home/antif/hadoop-2.7.3/sbin:/home/antif/jdk1.8.0_92/bin:.:/opt/jdk1.8.0/bin:/home/antif/ipa-server/jenkins/git/bin:/home/antif/ipa-server/jenkins/maven/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/home/antif/ipa-server/jenkins/node-v16.13.2-linux-x64/bin:/home/antif/.local/bin:/home/antif/bin:/homme/antif/ipa-server/jenkins/python3/Python-3.7.1:/home/antif/redis-6/migrate/go/bin:/sbin" ldconfig -n /home/antif/.projects/apr/lib/
----------------------------------------------------------------------
Libraries have been installed in:
/home/antif/.projects/apr/lib/
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
2.2 apr-icon 安装
apr-iconv安装时需要指定apr的安装位置
./configure --prefix=/home/antif/.projects/apr-iconv --with-apr=/home/antif/.projects/apr
make
libtool: warning: library '/home/antif/.projects/apr/lib/libapr-1.la' was moved.
libtool: warning: library '/home/antif/.projects/apr/lib/libapr-1.la' was moved.
make install
2.3 apr-util安装
apr-util安装时需要指定apr的安装位置和apr-iconv的执行文件
./configure --prefix=/home/antif/.projects/apr-util --with-apr=/home/antif/.projects/apr --with-apr-iconv=/home/antif/.projects/apr-iconv/bin/apriconv
make
make install
安装完成后其实是无法直接使用APR的,想使用APR还需要安装Tomcat Native,否则Tomcat启动时会报以下错误:
INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/local/apr/lib:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
3、Tomcat Native安装
Tomcat Native是 Tomcat可选组件,它可以让 Tomcat使用 Apache 的 APR包来处理包括文件和网络IO操作,从而提升性能及兼容性
Apache Tomcat® - Tomcat Native Downloads
安装Tomcat Native前需要安装以下组件:
• APR library(我们已安装)
• OpenSSL libraries
• Java SE Development Kit (JDK)(已安装)
# yum install apr-devel openssl-devel
安装
[antif@mtg172 native]$ ./configure --with-apr=/home/antif/.projects/apr/bin/apr-1-config --with-java-home=/home/antif/jdk1.8.0_92 --with-ssl=yes --prefix=/home/antif/.projects/tomcat-8.5.51 --with-ssl=$HOME/.projects/OPENSSL
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
checking for a BSD-compatible install... /bin/install -c
checking for working mkdir -p... yes
Tomcat Native Version: 1.2.35
checking for chosen layout... tcnative
checking for APR... yes
configure: APR 1.7.0 detected.
setting CC to "gcc"
setting CPP to "gcc -E"
checking JAVA_HOME... /home/antif/jdk1.8.0_92
adding "-I/home/antif/jdk1.8.0_92/include" to TCNATIVE_PRIV_INCLUDES
checking for JDK os include directory... linux
adding "-I/home/antif/jdk1.8.0_92/include/linux" to TCNATIVE_PRIV_INCLUDES
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
checking for gcc option to enable C11 features... -std=gnu11
checking for OpenSSL library... using openssl from /home/antif/.projects/OPENSSL/lib and /home/antif/.projects/OPENSSL/include
checking OpenSSL library version >= 1.0.2... ok
checking for OpenSSL DSA support... no
adding "-I/home/antif/.projects/OPENSSL/include" to TCNATIVE_PRIV_INCLUDES
setting TCNATIVE_LDFLAGS to "-L/home/antif/.projects/OPENSSL/lib -Wl,-rpath,/home/antif/.projects/OPENSSL/lib -lssl -lcrypto"
adding "-DHAVE_OPENSSL" to CFLAGS
setting TCNATIVE_LIBS to ""
setting TCNATIVE_LIBS to " /home/antif/.projects/apr/lib/libapr-1.la -lrt -lcrypt -lpthread -ldl"
adding "-DTCNATIVE_LINUX" to CFLAGS
checking for apr_pollset_wakeup in -lapr-1... yes
adding "-DHAVE_POLLSET_WAKEUP" to CFLAGS
configure: creating ./config.status
config.status: creating Makefile
config.status: executing default commands
[antif@mtg172 native]$ make
make[1]: Entering directory `/home/antif/.projects/tomcat-apr/tomcat-native-1.2.35-src/native'
make[1]: Nothing to be done for `local-all'.
make[1]: Leaving directory `/home/antif/.projects/tomcat-apr/tomcat-native-1.2.35-src/native'
[antif@mtg172 native]$ make install
make[1]: Entering directory `/home/antif/.projects/tomcat-apr/tomcat-native-1.2.35-src/native'
make[1]: Nothing to be done for `local-all'.
make[1]: Leaving directory `/home/antif/.projects/tomcat-apr/tomcat-native-1.2.35-src/native'
/home/antif/.projects/apr/build-1/mkdir.sh /home/antif/.projects/tomcat-8.5.51/lib
/bin/sh /home/antif/.projects/apr/build-1/libtool --mode=install /bin/install -c -m 755 libtcnative-1.la /home/antif/.projects/tomcat-8.5.51/lib
libtool: install: /bin/install -c -m 755 .libs/libtcnative-1.so.0.2.35 /home/antif/.projects/tomcat-8.5.51/lib/libtcnative-1.so.0.2.35
libtool: install: (cd /home/antif/.projects/tomcat-8.5.51/lib && { ln -s -f libtcnative-1.so.0.2.35 libtcnative-1.so.0 || { rm -f libtcnative-1.so.0 && ln -s libtcnative-1.so.0.2.35 libtcnative-1.so.0; }; })
libtool: install: (cd /home/antif/.projects/tomcat-8.5.51/lib && { ln -s -f libtcnative-1.so.0.2.35 libtcnative-1.so || { rm -f libtcnative-1.so && ln -s libtcnative-1.so.0.2.35 libtcnative-1.so; }; })
libtool: install: /bin/install -c -m 755 .libs/libtcnative-1.lai /home/antif/.projects/tomcat-8.5.51/lib/libtcnative-1.la
libtool: install: /bin/install -c -m 755 .libs/libtcnative-1.a /home/antif/.projects/tomcat-8.5.51/lib/libtcnative-1.a
libtool: install: chmod 644 /home/antif/.projects/tomcat-8.5.51/lib/libtcnative-1.a
libtool: install: ranlib /home/antif/.projects/tomcat-8.5.51/lib/libtcnative-1.a
libtool: finish: PATH=".:/opt/jdk1.8.0/bin:/home/antif/ipa-server/jenkins/git/bin:/home/antif/ipa-server/jenkins/maven/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/home/antif/ipa-server/jenkins/node-v16.13.2-linux-x64/bin:/sbin" ldconfig -n /home/antif/.projects/tomcat-8.5.51/lib
----------------------------------------------------------------------
Libraries have been installed in:
/home/antif/.projects/tomcat-8.5.51/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
4、配置Tomcat
4.1 新增(必须)
vim ./bin/setenv.sh
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CATALINA_HOME/lib
4.2 修改server.xml配置
打开conf/server.xml文件
修改Connector 标志的protocol属性:
protocol="org.apache.coyote.http11.Http11AprProtocol"
确认开启apr监听Listener:
4.3、启动tomcat
保存配置文件后,启动Tomcat,从日志中看到以下输出时,说明全部功能都已配置成功:
03-Aug-2022 10:07:09.299 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.35] using APR version [1.7.0].
03-Aug-2022 10:07:09.300 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [fa
lse], random [true].
03-Aug-2022 10:07:09.300 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
03-Aug-2022 10:07:09.378 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8180"]
03-Aug-2022 10:07:09.448 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 438 ms
03-Aug-2022 10:07:09.459 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
03-Aug-2022 10:07:09.460 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.51
03-Aug-2022 10:07:09.493 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8180"]
03-Aug-2022 10:07:09.497 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 48 ms
打开监听opssll引擎
修改tomcat中server.xml中的Listener:
03-Aug-2022 10:34:57.806 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.35] using APR version [1.7.0].
03-Aug-2022 10:34:57.806 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
03-Aug-2022 10:34:57.806 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
03-Aug-2022 10:34:57.809 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.0.2k-fips 26 Jan 2017]
03-Aug-2022 10:34:57.878 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8180"]
03-Aug-2022 10:34:57.890 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 367 ms
03-Aug-2022 10:34:57.899 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
03-Aug-2022 10:34:57.899 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.51
03-Aug-2022 10:34:57.928 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8180"]
03-Aug-2022 10:34:57.932 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 41 ms
Apr处理请求最快,Nio次之,Bio最慢;
Apr宽带占用最高,Bio最低;但Apr的内存占用最高,而Nio的内存占用最低
所以综合来看,Nio适用于一般需求;Apr适用于高并发需。