Tomcat 的三种(bio,nio,apr) 高级 Connector 运行模式及apr配置

omcat的运行模式有3种.修改他们的运行模式.3种模式的运行是否成功,可以看他的启动控制台,或者启动日志.或者登录他们的默认页面http://localhost:8080/查看其中的服务器状态。 

1)bio 

默认的模式,性能非常低下,没有经过任何优化处理和支持. 

2)nio 

利用java的异步io护理技术,no blocking IO技术. 

想运行在该模式下,直接修改server.xml里的Connector节点,修改protocol为 

<Connector port="80" protocol="org.apache.coyote.http11.Http11NioProtocol" 
connectionTimeout="20000" 
URIEncoding="UTF-8" 
useBodyEncodingForURI="true" 
enableLookups="false" 
redirectPort="8443" />

启动后,就可以生效。 

3)apr 

安装起来最困难,但是从操作系统级别来解决异步的IO问题,大幅度的提高性能. 

必须要安装apr和native,直接启动就支持apr。下面的修改纯属多余,仅供大家扩充知识,但仍然需要安装apr和native 

如nio修改模式,修改protocol为org.apache.coyote.http11.Http11AprProtocol

有兴趣的朋友可以上去看一下,地址是:http://tomcat.apache.org/native-doc/,tomcat native在具体的运行平台上,提供了一种优化技术,它本身是基于APR(Apache Portable Runtime)技术,在tomcat的document当中提到:

    Tomcat can use the Apache Portable Runtime to provide superior scalability, performance, and better integration with native server technologies. The Apache Portable Runtime is a highly portable library that is at the heart of Apache HTTP Server 2.x. APR has many uses, including access to advanced IO functionality (such as sendfile, epoll and OpenSSL), OS level functionality (random number generation, system status, etc), and native process handling (shared memory, NT pipes and Unix sockets).
    These features allows making Tomcat a general purpose webserver, will enable much better integration with other native web technologies, and overall make Java much more viable as a full fledged webserver platform rather than simply a backend focused technology.

也就是说,我们应用了tomcat native技术之后,tomcat在跟操作系统级别的交互方面可以做得更好,并且它更像apache一样,可以更好地作为一台web server。

APR是什么作用呢?Tomcat 可以用 Apache Portable Runtime 来提供更强性能,提升Web静态页面的处理能力。

所以在安装tomcat-native之前要先安装apr和apr-util这两个包

要测APR给tomcat带来的好处最好的方法是在慢速网络上(模拟Internet),将Tomcat线程数开到300以上的水平,然后模拟一大堆并发请求。如果不配APR,基本上300个线程狠快就会用满,以后的请求就只好等待。但是配上APR之后,并发的线程数量明显下降,从原来的300可能会马上下降到只有几十,新的请求会毫无阻塞的进来。

APR对于Tomcat最大的作用就是socket调度。

你在局域网环境测,就算是400个并发,也是一瞬间就处理/传输完毕,但是在真实的Internet环境下,页面处理时间只占0.1%都不到,绝大部分时间都用来页面传输。如果不用APR,一个线程同一时间只能处理一个用户,势必会造成阻塞。所以生产环境下用apr是非常必要的。

apr配置安装 

今天在机子上配置了由Apache Software Foundation研发的开源产品Tomcat服务器,以用于jsp开发。

 首先,在运行Tomcat之前,我们要确保本机已经安装了JDK(Java developing kit),这是运行Tomcat的首要环境。目前,sun公司已经将jdk升级为JDK 6.0,我们可以直接到下面的网址上去下载http://java.sun.com/javase/downloads/index.jsp。下载完成后 按照向导完成安装。然后如下配置环境变量: 

    JAVA_HOME: C:\Program Files\Java\jdk1.6.0(jdk的安装路径) 
    CLASSPATH: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; 
    PATH: %JAVA_HOME%\bin;

接下来我们就可以到下面的网址去下载Tomcat的最新版本:http://tomcat.apache.org/download-60.cgi,我下载的是Tomcat 6.0.10解压版本。下载完成后直接解压缩就可以用了。 

Tomcat的目录下面有一个bin目录,我们双击里面的startup.bat就可以使Tomcat运行。 

还有一个问题,如果我们注意的话,在运行Tomcat之前,窗口中会打印出如下信息:tomcat6.0The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path。如果你忽略它并不会影响你的正常使用。这是因为Tomcat 可以用 Apache Portable Runtime 来提供更强性能,提升Web静态页面的处理能力,不再需要专门的Web Server来处理静态页面了。 

    但是APR的支持需要安装三个本地组件 

    APR support requires three main native components to be installed: 

    1、APR library 

    2、JNI wrappers for APR used by Tomcat (libtcnative) 

    3、OpenSSL libraries 

    我们可以点击这里http://tomcat.heanet.ie/native/下载编译好的 tcnative-1.dll 文件,然后把它放到bin目录中就OK了。 

    相关的文档请参见:http://tomcat.apache.org/tomcat-6.0-doc/apr.html


如果没有apr技术,启动tomcat 时出现如下提示:

信 息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:

/usr/java/jdk1.6.0_06/jre/lib/i386/client:/usr/java/jdk1.6.0_06/jre/lib/i386:/usr/java/jdk1.6.0_06/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib

如果安装了apr技术,可以看到以下结果:

信息: Loaded APR based Apache Tomcat Native library 1.1.14.2009-1-13 11:12:51 org.apache.catalina.core.AprLifecycleListener init信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 使用APR提高Tomcat性能Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术. 

APR(Apache Portable Runtime)是一个高可移植库,它是Apache HTTP Server 2.x的核心。APR有很多用途,包括访问高级IO功能(例如sendfile,epoll和OpenSSL),OS级别功能(随机数生成,系统状态等 等),本地进程管理(共享内存,NT管道和UNIX sockets)。这些功能可以使Tomcat作为一个通常的前台WEB服务器,能更好地和其它本地web技术集成,总体上让Java更有效率作为一个高 性能web服务器平台而不是简单作为后台容器。

在产品环境中,特别是直接使用Tomcat做WEB服务器的时候,应该使用Tomcat Native来提高其性能

要 测APR给tomcat带来的好处最好的方法是在慢速网络上(模拟Internet),将Tomcat线程数开到300以上的水平,然后模拟一大堆并发请 求。如果不配APR,基本上300个线程狠快就会用满,以后的请求就只好等待。但是配上APR之后,并发的线程数量明显下降,从原来的300可能会马上下 降到只有几十,新的请求会毫无阻塞的进来。

在局域网环境测,就算是400个并发,也是一瞬间就处理/传输完毕,但是在真实的Internet环境 下,页面处理时间只占0.1%都不到,绝大部分时间都用来页面传输。如果不用APR,一个线程同一时间只能处理一个用户,势必会造成阻塞。所以生产环境下 用apr是非常必要的。

(1)安装APR

#apr-1.3.8.tar.gz
#tar zxvf apr-1.3.8.tar.gz
#cd apr-1.3.8
#./configure
#make
#make install
安装在/usr/local/apr
#apr-util-1.3.9.tar.gz
#tar zxvf apr-util-1.3.9.tar.gz
#cd apr-util-1.3.9 
#./configure --with-apr=/usr/local/apr 
#make 
#make install 
安装在/usr/local/apr/lib

安装 tomcat-native

#cd /usr/local/tomcat5.5/bin 
#tar zxvf tomcat-native.tar.gz 
#cd tomcat-native-1.1.3/jni/native 
#./configure --with-apr=/usr/local/apr
#make 
#make install

说明:安装tomcat-native时如果报错 "Can’t locate a valid JDK location" 使用下面方式解决,

参照http://evgeny-goldin.com/blog/ubuntu-installing-apr-tomcat/

$ which java
/home/user/java/jdk/bin/java
$ echo $JAVA_HOME
/home/user/java/jdk
$ java -version
java version &quot;1.6.0_24&quot;
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)
But for some reason "tomcat-native-1.1.20-src/jni/native/configure" script didn’t see my "JAVA_HOME" variable no matter what and even installing "sun-java6-jdk" didn’t help much. After patching the "configure" script to dump locations it was looking for “valid JDK” I had:
configure: [/usr/local/1.6.1]
configure: [/usr/local/IBMJava2-1.6.0]
configure: [/usr/local/java1.6.0]
configure: [/usr/local/java-1.6.0]
configure: [/usr/local/jdk1.6.0]
configure: [/usr/local/jdk-1.6.0]
configure: [/usr/local/1.6.0]
configure: [/usr/local/IBMJava2-1.6]
configure: [/usr/local/java1.6]
configure: [/usr/local/java-1.6]
configure: [/usr/local/jdk1.6]
configure: [/usr/local/jdk-1.6]
..
Ok then, here you have it now:
    
sudo ln -s ~/java/jdk/ /usr/local/jdk-1.6
sudo ./configure --with-apr=/usr/local/apr
sudo make
sudo make install

(2)设置 Tomcat 整合 APR

有两种方法:

(1)修改 tomcat 的启动 shell ( catalina.sh ),在该文件中加入启动参数: 

CATALINA_OPTS="$CATALINA_OPTS -Djava.library.path=/usr/local/apr/lib"

(2)也可以在环境变量中配置/etc/profile中加入 

export CATALINA_OPTS="$CATALINA_OPTS -Djava.library.path=/usr/local/apr/lib"
source /etc/profile

 (3)判断安装成功:

如果看到下面的启动日志,表示成功。

2007-4-26 15:34:32 org.apache.coyote.http11.Http11AprProtocol init

说明:

1、如果报下面异常,修改conf/server.xml下的 SSLEngine="on"  改为  SSLEngine="off" 

<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" />

SEVERE: Failed to initialize the SSLEngine.

org.apache.tomcat.jni.Error: 70023: This function has not been implemented on this platform

我其实是想解决这个问题,但太懒了。项目暂时不用ssl ,就不用管了,先留个坑吧。。

2、如果安装上面三个都没有问题。但报 APR/native library which is not available。查看一下【Tomcat 整合 APR】时是如何整何的,如果用的是第一种方法 ,tomcat启动时,使用./catalina.sh start  启动。建议使用第二种方式。如果第二种仍报错。

修改为下面的信息

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib:/home/ngsteam/apache-tomcat-7.0.42/lib

3)

checking for JDK location (please wait)... checking Try to guess JDK location... configure: error: can't locate a valid JDK location

如果遇到上述错误使用下面命令建个软链接即可,切记~/java/jdk/ 是相对于当前目录的。不要建错了,如果建错软链接也会报同样的错,错误的软链接颜色为红色,且有背景。或直接cd   /usr/local/jdk-1.6 可以进入,说明软链建成功

sudo ln -s ~/java/jdk/ /usr/local/jdk-1.6

参照:http://evgeny-goldin.com/blog/ubuntu-installing-apr-tomcat/

2. Installing Tomcat Native.

wget http://off.co.il/apache//tomcat/tomcat-connectors/native/1.1.20/source/tomcat-native-1.1.20-src.tar.gz
tar -xzf tomcat-native-1.1.20-src.tar.gz
rm tomcat-native-1.1.20-src.tar.gz
cd tomcat-native-1.1.20-src/jni/native
sudo ./configure --with-apr=/usr/local/apr

The result was

checking build system type... x86_64-unknown-linux-gnu

..

checking for APR... yes

..

checking for JDK location (please wait)... checking Try to guess JDK location... configure: error: can't locate a valid JDK location


Ouch! “Can’t locate a valid JDK location” ? On my machine?

$ which java
/home/user/java/jdk/bin/java
$ echo $JAVA_HOME
/home/user/java/jdk
$ java -version
java version &quot;1.6.0_24&quot;
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)

But for some reason "tomcat-native-1.1.20-src/jni/native/configure" script didn’t see my "JAVA_HOME" variable no matter what and even installing "sun-java6-jdk" didn’t help much. After patching the "configure" script to dump locations it was looking for “valid JDK” I had:

..

configure: [/usr/local/1.6.1]
configure: [/usr/local/IBMJava2-1.6.0]
configure: [/usr/local/java1.6.0]
configure: [/usr/local/java-1.6.0]
configure: [/usr/local/jdk1.6.0]
configure: [/usr/local/jdk-1.6.0]
configure: [/usr/local/1.6.0]
configure: [/usr/local/IBMJava2-1.6]
configure: [/usr/local/java1.6]
configure: [/usr/local/java-1.6]
configure: [/usr/local/jdk1.6]
configure: [/usr/local/jdk-1.6]
..

Ok then, here you have it now:

sudo ln -s ~/java/jdk/ /usr/local/jdk-1.6
sudo ./configure --with-apr=/usr/local/apr
sudo make
sudo make install

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/214.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。