Tomcat网站的文档专门有一块是介绍在Tomcat中如何使用APR库的,说白了就是如何在Tomcat中使用JNI的方式来读取文件以及进行网络传输。这个东西可以大大提升Tomcat对静态文件的处理性能,同时如果你使用了HTTPS方式 传输的话,也可以提升SSL的处理性能。 在Linux下,可以直接解压和安装bin目录下的tomcat_native.tar.gz文件,编译之前要确保apr库已经安装。
另外我属于有强迫症的一类人...看不得tomcat启动有这个提示,虽然能够正常运行...
The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:
参考文档:http://tomcat.apache.org/native-doc/
以下是安装步骤:
[root@www ]# yum install -y apr-devel openssl-devel Setting up Install Process Package openssl-devel-1.0.1e-16.el6_5.15.x86_64 already installed and latest version Resolving Dependencies --> Running transaction check ---> Package apr-devel.x86_64 0:1.3.9-5.el6_2 will be installed --> Finished Dependency Resolution Running Transaction Installing : apr-devel-1.3.9-5.el6_2.x86_64 1/1 Verifying : apr-devel-1.3.9-5.el6_2.x86_64 1/1 Installed: apr-devel.x86_64 0:1.3.9-5.el6_2 Complete! [root@www ~]# cd /data/tomcat/bin/ [root@www bin]# ll -rw-r--r-- 1 root root 384311 May 19 18:50 tomcat-native.tar.gz [root@www bin]# tar xf tomcat-native.tar.gz [root@www bin]# ll drwxrwxr-x 3 10848 10848 4096 Apr 10 18:37 tomcat-native-1.1.30-src -rw-r--r-- 1 root root 384311 May 19 18:50 tomcat-native.tar.gz [root@www bin]# cd tomcat-native-1.1.30-src/jni/native/ [root@www native]# ll total 252 drwxrwxr-x 3 10848 10848 4096 Apr 10 18:37 build -rwxrwxr-x 1 10848 10848 2581 Feb 10 2010 buildconf -rw-rw-r-- 1 10848 10848 1098 Aug 5 2007 build.conf -rw-rw-r-- 1 10848 10848 3291 Feb 24 2012 BUILDING -rw-rw-r-- 1 10848 10848 3477 Apr 10 18:37 build-outputs.mk -rw-rw-r-- 1 10848 10848 8739 Aug 5 2007 config.layout -rwxrwxr-x 1 10848 10848 146900 Apr 10 18:37 configure -rw-rw-r-- 1 10848 10848 6965 Feb 14 2013 configure.in drwxrwxr-x 2 10848 10848 4096 Apr 10 18:37 include -rw-rw-r-- 1 10848 10848 6669 Feb 24 2012 libtcnative.dsp -rw-rw-r-- 1 10848 10848 1214 May 13 2005 libtcnative.dsw -rw-rw-r-- 1 10848 10848 2751 Aug 5 2007 Makefile.in -rw-rw-r-- 1 10848 10848 4510 Feb 7 2013 NMAKEmakefile -rw-rw-r-- 1 10848 10848 9610 Feb 25 2012 NMAKEmakefile.inc drwxrwxr-x 5 10848 10848 4096 Apr 10 18:37 os drwxrwxr-x 2 10848 10848 4096 Apr 10 18:37 src drwxrwxr-x 4 10848 10848 4096 Apr 10 18:37 srclib -rw-rw-r-- 1 10848 10848 6657 Feb 24 2012 tcnative.dsp -rw-rw-r-- 1 10848 10848 1206 Aug 5 2007 tcnative.pc.in -rw-rw-r-- 1 10848 10848 2892 Apr 10 18:37 tcnative.spec [root@www native]# ll /data/tomcat/lib/ total 5500 -rw-r--r-- 1 root root 15268 May 19 18:50 annotations-api.jar -rw-r--r-- 1 root root 54669 May 19 18:50 catalina-ant.jar -rw-r--r-- 1 root root 132326 May 19 18:50 catalina-ha.jar -rw-r--r-- 1 root root 1248872 May 19 18:50 catalina.jar -rw-r--r-- 1 root root 239242 May 19 18:50 catalina-tribes.jar -rw-r--r-- 1 root root 1830791 May 19 18:50 ecj-4.3.1.jar -rw-r--r-- 1 root root 34141 May 19 18:50 el-api.jar -rw-r--r-- 1 root root 112553 May 19 18:50 jasper-el.jar -rw-r--r-- 1 root root 532459 May 19 18:50 jasper.jar -rw-r--r-- 1 root root 76856 May 19 18:50 jsp-api.jar -rw-r--r-- 1 root root 132931 May 19 18:50 servlet-api.jar -rw-r--r-- 1 root root 797689 May 19 18:50 tomcat-coyote.jar -rw-r--r-- 1 root root 217961 May 19 18:50 tomcat-dbcp.jar -rw-r--r-- 1 root root 70816 May 19 18:50 tomcat-i18n-es.jar -rw-r--r-- 1 root root 51918 May 19 18:50 tomcat-i18n-fr.jar -rw-r--r-- 1 root root 54486 May 19 18:50 tomcat-i18n-ja.jar [root@www native]# ./configure --with-apr=/usr/bin/apr-1-config \ --with-java-home=/data/jdk1.7.0_67/ --with-ssl=yes --prefix=$CATALINA_HOME [root@www native]# make && make install make[1]: Leaving directory `/data/apache-tomcat-6.0.41/bin/tomcat-native-1.1.30-src/jni/native' /usr/lib64/apr-1/build/mkdir.sh /data/tomcat/include/apr-1 /data/tomcat/lib/pkgconfig \ /data/tomcat/lib /data/tomcat/bin mkdir /data/tomcat/include mkdir /data/tomcat/include/apr-1 mkdir /data/tomcat/lib/pkgconfig /usr/bin/install -c -m 644 tcnative.pc /data/tomcat/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 /data/tomcat/lib libtool: install: /usr/bin/install -c -m 755 .libs/libtcnative-1.so.0.1.30 /data/tomcat/lib/libtcnative-1.so.0.1.30 libtool: install: (cd /data/tomcat/lib && { ln -s -f libtcnative-1.so.0.1.30 libtcnative-1.so.0 || { rm -f libtcnative-1.so.0 && ln -s libtcnative-1.so.0.1.30 libtcnative-1.so.0; }; }) libtool: install: (cd /data/tomcat/lib && { ln -s -f libtcnative-1.so.0.1.30 libtcnative-1.so || { rm -f libtcnative-1.so && ln -s libtcnative-1.so.0.1.30 libtcnative-1.so; }; }) libtool: install: /usr/bin/install -c -m 755 .libs/libtcnative-1.lai /data/tomcat/lib/libtcnative-1.la libtool: install: /usr/bin/install -c -m 755 .libs/libtcnative-1.a /data/tomcat/lib/libtcnative-1.a libtool: install: chmod 644 /data/tomcat/lib/libtcnative-1.a libtool: install: ranlib /data/tomcat/lib/libtcnative-1.a libtool: finish: PATH="/usr/local/nginx/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/data/jdk1.7.0_67/bin:/data/jdk1.7.0_67/jre/bin:/sbin" ldconfig -n /data/tomcat/lib ---------------------------------------------------------------------- Libraries have been installed in: /data/tomcat/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. ---------------------------------------------------------------------- [root@www native]# ll /data/tomcat/lib/ total 8012 -rw-r--r-- 1 root root 15268 May 19 18:50 annotations-api.jar -rw-r--r-- 1 root root 54669 May 19 18:50 catalina-ant.jar -rw-r--r-- 1 root root 132326 May 19 18:50 catalina-ha.jar -rw-r--r-- 1 root root 1248872 May 19 18:50 catalina.jar -rw-r--r-- 1 root root 239242 May 19 18:50 catalina-tribes.jar -rw-r--r-- 1 root root 1830791 May 19 18:50 ecj-4.3.1.jar -rw-r--r-- 1 root root 34141 May 19 18:50 el-api.jar -rw-r--r-- 1 root root 112553 May 19 18:50 jasper-el.jar -rw-r--r-- 1 root root 532459 May 19 18:50 jasper.jar -rw-r--r-- 1 root root 76856 May 19 18:50 jsp-api.jar -rw-r--r-- 1 root root 1636194 Sep 18 09:27 libtcnative-1.a -rwxr-xr-x 1 root root 1023 Sep 18 09:27 libtcnative-1.la lrwxrwxrwx 1 root root 23 Sep 18 09:27 libtcnative-1.so -> libtcnative-1.so.0.1.30 lrwxrwxrwx 1 root root 23 Sep 18 09:27 libtcnative-1.so.0 -> libtcnative-1.so.0.1.30 -rwxr-xr-x 1 root root 921791 Sep 18 09:27 libtcnative-1.so.0.1.30 drwxr-xr-x 2 root root 4096 Sep 18 09:27 pkgconfig -rw-r--r-- 1 root root 132931 May 19 18:50 servlet-api.jar -rw-r--r-- 1 root root 797689 May 19 18:50 tomcat-coyote.jar -rw-r--r-- 1 root root 217961 May 19 18:50 tomcat-dbcp.jar -rw-r--r-- 1 root root 70816 May 19 18:50 tomcat-i18n-es.jar -rw-r--r-- 1 root root 51918 May 19 18:50 tomcat-i18n-fr.jar -rw-r--r-- 1 root root 54486 May 19 18:50 tomcat-i18n-ja.jar [root@www native]# vi /data/tomcat/conf/server.xml 修改<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 为: <Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" connectionTimeout="20000" redirectPort="8443" /> [root@www native]# cd /data/tomcat/bin/ [root@www bin]# ls bootstrap.jar catalina-tasks.xml cpappend.bat digest.sh shutdown.bat startup.sh tomcat-native.tar.gz version.bat catalina.bat commons-daemon.jar daemon.sh setclasspath.bat shutdown.sh tomcat-juli.jar tool-wrapper.bat version.sh catalina.sh commons-daemon-native.tar.gz digest.bat setclasspath.sh startup.bat tomcat-native-1.1.30-src tool-wrapper.sh [root@www bin]# vi setenv.sh #新建该文件添加 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CATALINA_HOME/lib export LD_LIBRARY_PATH [root@www bin]# vi catalina.sh Apache Tomcat comes with the AprLifecycleListener enabled by default. Still, you should check your conf/server.xml to ensure that something like the following is present, and uncommented: <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> Please see the Apache Tomcat documentation for configuration specifics. [root@www bin]# ./startup.sh Using CATALINA_BASE: /data/tomcat Using CATALINA_HOME: /data/tomcat Using CATALINA_TMPDIR: /data/tomcat/temp Using JRE_HOME: /data/jdk1.7.0_67 Using CLASSPATH: /data/tomcat/bin/bootstrap.jar [root@www bin]# cd ../logs/ [root@www logs]# ls -lrt total 12 -rw-r--r-- 1 root root 0 Sep 18 09:36 catalina.out -rw-r--r-- 1 root root 0 Sep 18 09:36 manager.2014-09-18.log -rw-r--r-- 1 root root 0 Sep 18 09:36 host-manager.2014-09-18.log -rw-r--r-- 1 root root 232 Sep 18 09:37 localhost.2014-09-18.log -rw-r--r-- 1 root root 1906 Sep 18 09:37 catalina.2014-09-18.out -rw-r--r-- 1 root root 1906 Sep 18 09:37 catalina.2014-09-18.log [root@www logs]# cat catalina.2014-09-18.out Sep 18, 2014 9:36:59 AM org.apache.catalina.core.AprLifecycleListener init INFO: Loaded APR based Apache Tomcat Native library 1.1.30 using APR version 1.3.9. Sep 18, 2014 9:36:59 AM org.apache.catalina.core.AprLifecycleListener init INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. Sep 18, 2014 9:36:59 AM org.apache.catalina.core.AprLifecycleListener initializeSSL INFO: OpenSSL successfully initialized with version OpenSSL 1.0.1e 11 Feb 2013 Sep 18, 2014 9:37:00 AM org.apache.coyote.http11.Http11AprProtocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 Sep 18, 2014 9:37:00 AM org.apache.coyote.ajp.AjpAprProtocol init INFO: Initializing Coyote AJP/1.3 on ajp-8009 Sep 18, 2014 9:37:00 AM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 821 ms Sep 18, 2014 9:37:00 AM org.apache.catalina.core.StandardService start INFO: Starting service Catalina Sep 18, 2014 9:37:00 AM org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/6.0.41 Sep 18, 2014 9:37:00 AM org.apache.catalina.startup.HostConfig deployDescriptor INFO: Deploying configuration descriptor host-manager.xml Sep 18, 2014 9:37:00 AM org.apache.catalina.startup.HostConfig deployDescriptor INFO: Deploying configuration descriptor manager.xml Sep 18, 2014 9:37:00 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory docs Sep 18, 2014 9:37:00 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory examples Sep 18, 2014 9:37:01 AM org.apache.coyote.http11.Http11AprProtocol start INFO: Starting Coyote HTTP/1.1 on http-8080 Sep 18, 2014 9:37:01 AM org.apache.coyote.ajp.AjpAprProtocol start INFO: Starting Coyote AJP/1.3 on ajp-8009 Sep 18, 2014 9:37:01 AM org.apache.catalina.startup.Catalina start INFO: Server startup in 1048 ms