Tomcat 的三种(bio,nio.apr)模式

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

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

1)bio java

默认的模式,性能很是低下,没有通过任何优化处理和支持. linux

2)nio web

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

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

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

启动后,就能够生效。 ubuntu

3)apr tomcat

安装起来最困难,可是从操做系统级别来解决异步的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是很是必要的。
 

2、转:http://hi.baidu.com/lucene1853/item/2fdadb17e4498a27f7625c78

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: 
    一、APR library 
    二、JNI wrappers for APR used by Tomcat (libtcnative) 
    三、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
说明:
一、若是报下面异常,修改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 ,就不用管了,先留个坑吧。。

二、若是安装上面三个都没有问题。但报 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

相关文章
相关标签/搜索