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
參考資料