Tomcat Connector三种运行模式(BIO, NIO, APR)的比较和优化

Tomcat Connector三种运行模式(BIO, NIO, APR)的比较和优化


这三种模式的不同之处如下:

BIO:

一个线程处理一个请求。缺点:并发量高时,线程数较多,浪费资源。

Tomcat7或以下,在Linux系统中默认使用这种方式。


NIO:

利用Java的异步IO处理,可以通过少量的线程处理大量的请求。

Tomcat8在Linux系统中默认使用这种方式。

Tomcat7必须修改Connector配置来启动:

         connectionTimeout="20000" redirectPort="8443"/>


APR:

即Apache Portable Runtime,从操作系统层面解决io阻塞问题。

Linux如果安装了apr和native,Tomcat7或Tomcat8在Linux系统中默认使用这种方式。


安装apr和native

tar -xzf apr-1.6.3.tar.gz

cd apr-1.6.3/

./configure --prefix=/usr/local/apr

make

make install


tar -xaf apr-util-1.6.1.tar.gz

cd apr-util-1.6.1/

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

make

make install


cd apache-tomcat-7.0.88/bin/

tar -xzf tomcat-native.tar.gz

cd tomcat-native-1.2.16-src/

cd native/

./configure --with-apr=/usr/local/apr --with-java-  home=/root/Java/jdk1.8.0_91

make

make install


vim /etc/profile

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib


source /etc/profile


apache-tomcat-7.0.88/bin/catalina.sh run

apache-tomcat-8.0.12/bin/catalina.sh run





官方对这三种的区别的详细说明:

                  Java Blocking Connector   Java Nio Blocking Connector   APR/native Connector

                             BIO                         NIO                       APR

Classname                AjpProtocol               AjpNioProtocol           AjpAprProtocol

Tomcat Version           3.x onwards                 7.x onwards              5.5.x onwards

Support Polling              NO                          YES                       YES

Polling Size                 N/A                   maxConnections             maxConnections

Read Request Headers      Blocking                  Sim Blocking                   Blocking

Read Request Body         Blocking                  Sim Blocking                   Blocking

Write Response            Blocking                  Sim Blocking                   Blocking

Wait for next Request     Blocking                  Non Blocking               Non Blocking

Max Connections        maxConnections              maxConnections             maxConnections


Tomcat启动的时候,可以通过log看到Connector使用的是哪一种运行模式:

Starting ProtocolHandler ["http-bio-8080"]

Starting ProtocolHandler ["http-nio-8080"]

Starting ProtocolHandler ["http-apr-8080"]



大家可以测试下三种情况下系统cpu、内存的消耗情况


参考资料:http://www.cnblogs.com/nb-blog/p/5278933.html

你可能感兴趣的:(Tomcat Connector三种运行模式(BIO, NIO, APR)的比较和优化)