Tomcat支持三种接收请求的处理方式:BIO、NIO、APR html
1) BIO模式:阻塞式I/O操做,表示Tomcat使用的是传统Java I/O操做(即java.io包及其子包)。Tomcat7如下版本默认状况下是以bio模式运行的,因为每一个请求都要建立一个线程来处理,线程开销较大,不能处理高并发的场景,在三种模式中性能也最低。启动tomcat看到以下日志,表示使用的是BIO模式: java
2) NIO模式:是Java SE 1.4及后续版本提供的一种新的I/O操做方式(即java.nio包及其子包)。是一个基于缓冲区、并能提供非阻塞I/O操做的Java API,它拥有比传统I/O操做(bio)更好的并发运行性能。要让Tomcat以nio模式来运行比较简单,只须要在Tomcat安装目录/conf/server.xml文件中将以下配置:node
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
修改为:apache
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443" />
注意:Tomcat8 以上版本,默认使用的就是 NIO 模式,不须要额外修改 tomcat
23-Apr-2018 11:47:30.593 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/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 23-Apr-2018 11:47:30.767 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"] 23-Apr-2018 11:47:30.785 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 23-Apr-2018 11:47:30.788 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"] 23-Apr-2018 11:47:30.790 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 23-Apr-2018 11:47:30.791 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 673 ms
3) apr模式:简单理解,就是从操做系统级别解决异步IO问题,大幅度的提升服务器的处理和响应性能, 也是 Tomcat 运行高并发应用的首选模式。
启用这种模式稍微麻烦一些,须要安装一些依赖库,下面以在 CentOS7 mini 版环境下 Tomcat-8.0.35 为例,介绍安装步聚:bash
APR 1.2+ development headers (libapr1-dev package) OpenSSL 0.9.7+ development headers (libssl-dev package) JNI headers from Java compatible JDK 1.4+ GNU development environment (gcc, make)
由于 apr 模式本质是使用 JNI 技术调用操做系统 IO 接口,须要用到相关 API 的头文件服务器
yum install apr-devel yum install openssl-devel yum install gcc yum install make
注意:openssl库要求在0.9.7以上版本,APR要求在1.2以上版本,用rpm -qa | grep openssl检查本机安装的依赖库版本是否大于或等于apr要求的版本。并发
进入 tomcat 的 bin 目录,解压 tomcat-native.tar.gz 文件,并进入 tomcat-native-1.2.7-src/native 目录,执行 ./configure && make && make install 命令,动态库默认安装在 /usr/local/apr/lib 目录下,以下图所示: app
make[1]: Leaving directory `/opt/tomcat/bin/tomcat-native-1.2.7-src/native' make[1]: Entering directory `/opt/tomcat/bin/tomcat-native-1.2.7-src/native' make[1]: Nothing to be done for `local-all'. make[1]: Leaving directory `/opt/tomcat/bin/tomcat-native-1.2.7-src/native' /usr/lib64/apr-1/build/mkdir.sh /usr/local/apr/include/apr-1 /usr/local/apr/lib/pkgconfig \ /usr/local/apr/lib /usr/local/apr/bin mkdir /usr/local/apr mkdir /usr/local/apr/include mkdir /usr/local/apr/include/apr-1 mkdir /usr/local/apr/lib mkdir /usr/local/apr/lib/pkgconfig mkdir /usr/local/apr/bin /usr/bin/install -c -m 644 tcnative.pc /usr/local/apr/lib/pkgconfig/tcnative-1.pc list=''; for i in $list; do \ ( cd $i ; make DESTDIR= install ); \ done /bin/sh /usr/lib64/apr-1/build/libtool --mode=install /usr/bin/install -c -m 755 libtcnative-1.la /usr/local/apr/lib libtool: install: /usr/bin/install -c -m 755 .libs/libtcnative-1.so.0.2.7 /usr/local/apr/lib/libtcnative-1.so.0.2.7 libtool: install: (cd /usr/local/apr/lib && { ln -s -f libtcnative-1.so.0.2.7 libtcnative-1.so.0 || { rm -f libtcnative-1.so.0 && ln -s libtcnative-1.so.0.2.7 libtcnative-1.so.0; }; }) libtool: install: (cd /usr/local/apr/lib && { ln -s -f libtcnative-1.so.0.2.7 libtcnative-1.so || { rm -f libtcnative-1.so && ln -s libtcnative-1.so.0.2.7 libtcnative-1.so; }; }) libtool: install: /usr/bin/install -c -m 755 .libs/libtcnative-1.lai /usr/local/apr/lib/libtcnative-1.la libtool: install: /usr/bin/install -c -m 755 .libs/libtcnative-1.a /usr/local/apr/lib/libtcnative-1.a libtool: install: chmod 644 /usr/local/apr/lib/libtcnative-1.a libtool: install: ranlib /usr/local/apr/lib/libtcnative-1.a libtool: finish: PATH="/usr/local/src/node-v8.11.2/bin:/opt/jdk1.8.0_92/bin:/opt/jdk1.8.0_92/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/sbin" ldconfig -n /usr/local/apr/lib ---------------------------------------------------------------------- Libraries have been installed in: /usr/local/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. ----------------------------------------------------------------------
设置 LD_LIBRARY_PATH和LD_RUN_PATH 环境变量,指向 /usr/local/apr/lib 目录,可配置到 $HOME/.profile 文件中dom
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib export LD_RUN_PATH=$LD_RUN_PATH:/usr/local/apr/lib
拷贝 /usr/local/apr/lib 目录下全部动态库到 /usr/lib 或 /lib 系统共享库搜索目录下便可。
copy /usr/local/apr/lib/libtcnative* /usr/lib/
编辑 $TOMCAT_HOME/bin/catalina.sh 文件,在虚拟机启动参数 JAVA_OPTS 中添加 java.library.path 参数,指定 apr 库的路径
JAVA_OPTS="$JAVA_OPTS -Djava.library.path=/usr/local/apr/lib"
Tomcat8 如下版本,须要指定运行模式,将 protocol从HTTP/1.1 改为 org.apache.coyote.http11.Http11AprProtocol
<Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" connectionTimeout="20000" redirectPort="8443" />
运行Tomcat后,看到以下日志表示开启apr模式成功:
18-Jul-2018 14:58:05.243 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 18-Jul-2018 14:58:05.250 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.2k-fips 26 Jan 2017) 18-Jul-2018 14:58:05.489 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"] 18-Jul-2018 14:58:05.503 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"] 18-Jul-2018 14:58:05.504 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 896 ms 18-Jul-2018 14:58:05.549 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina 18-Jul-2018 14:58:05.550 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.36 18-Jul-2018 14:58:05.565 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"] 18-Jul-2018 14:58:05.581 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"] 18-Jul-2018 14:58:05.589 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 85 ms
若是没有配置 SSL 相关参数,而且开启了 SSL,启动时会发生 org.apache.tomcat.jni.Error: 70023: This function has not been implemented on this platform 异常:
若是不想启用 SSL,将 server.xml 中 apr 模式下 ssl 关闭便可:
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
将 SSLEngine 的值从 on 改为 off 便可:
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" />
Tomcat 6.x 版本从 6.0.32 开始就默认支持 apr。
Tomcat 7.x 版本从 7.0.30 开始就默认支持 apr。
所以,若是使用的 Tomcat 版本比较陈旧的话,强烈建议升级到最新的稳定版本。
Tomcat官网APR配置:http://tomcat.apache.org/tomcat-8.0-doc/apr.html#Linux
参考资料