Tomcat Connector三种执行模式(BIO, NIO, APR)的比較和优化
Tomcat Connector的三种不同的执行模式性能相差非常大,有人測试过的结果例如以下:
这三种模式的不同之处例如以下:
- BIO:
一个线程处理一个请求。缺点:并发量高时,线程数较多,浪费资源。
Tomcat7或下面,在Linux系统中默认使用这样的方式。
- NIO:
利用Java的异步IO处理。能够通过少量的线程处理大量的请求。
Tomcat8在Linux系统中默认使用这样的方式。
Tomcat7必须改动Connector配置来启动:
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000" redirectPort="8443"/>
- APR:
即Apache Portable Runtime,从操作系统层面解决io堵塞问题。
Tomcat7或Tomcat8在Win7或以上的系统中启动默认使用这样的方式。
Linux假设安装了apr和native,Tomcat直接启动就支持apr。(安装方法:http://my.oschina.net/lsw90/blog/181161)
官方对这三种的差别的具体说明:
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
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"]
(原创文章,转载请注明转自Clement-Xu的csdn博客)