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"]