IO分类
在java中,IO分为Bio、Nio、Aio三类,三者有本质的区别,下面主要讲解其在网络IO中的区别。
Bio
阻塞式IO,客户端类为Socket,服务端类为ServerSocket。该类中提供的方法全部为阻塞方法,即该操作完成后,该方法才会返回。举例说明,如果调用了Socket输入流的readLine方法,那么该方法必须在读取到换行符才会返回。优点:实现简单缺点:针对每一个客户端请求都需要创建一个IO线程进行处理,并且IO操作在等待条件未满足时,仍然占用已持有资源等待,受网络波动的影响较大。
Nio
非阻塞式IO,客户端类为SocketChannel,服务端类为ServerSocketChannel。在Nio中提供了阻塞和非阻塞两种模式,一般我们会使用Nio的非阻塞模式,即每一个方法不会等待条件满足就会立即返回。举例说明:如果调用SocketChannel的read方法时,不管读没读到数据都会立即返回,那么带来的问题也可想而知,对于IO操作的判断逻辑会变得更加复杂(因为执行IO操作时,并不知道该操作有没有完成)。优点:Nio中使用一个IO线程(Selector轮询检查每一个注册的Channel)去处理所有的客户端连接,且IO操作会立即返回,不会产生BIO中保持资源等待的情况。缺点:由于IO操作的完成情况是不确定的,导致了处理逻辑会比Nio复杂,其次IO线程通过轮询的方式检测,当注册在Selector上的Channel非常多时,会成为系统的瓶颈
Aio
异步IO,客户端类为AsynchronousSocketChannel,服务端类为AsynchronousServerSocketChannel。相比于只使用一个IO线程处理IO操作的Nio而言,Aio中不存在额外的IO线程,而是通过事件回调的方式来执行。(对Aio了解还不是很深刻,此处不做过多的讲解)优点:不需要IO线程,相比于轮询Channel的Nio而言,采用事件响应模式可以有更小的开销缺点:目前还不知道
Tomcat配置IO协议
使用指定IO模型的配置方式:
配置 server.xml 文件当中的
<Connector protocol="HTTP/1.1">
protocol 修改即可。
默认配置 8.0 protocol=“HTTP/1.1” 8.0 之前是 BIO 8.0 之后是NIO
BIO
protocol=“org.apache.coyote.http11.Http11Protocol“
NIO
protocol=”org.apache.coyote.http11.Http11NioProtocol“
AIO
protocol=”org.apache.coyote.http11.Http11Nio2Protocol“
APR
protocol=”org.apache.coyote.http11.Http11AprProtocol“
下面是摘的server.xml中的一段配置
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
This connector uses the NIO implementation. The default
SSLImplementation will depend on the presence of the APR/native
library and the useOpenSSL attribute of the
AprLifecycleListener.
Either JSSE or OpenSSL style configuration may be used regardless of
the SSLImplementation selected. JSSE style configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
This connector uses the APR/native implementation which always uses
OpenSSL for TLS.
Either JSSE or OpenSSL style configuration may be used. OpenSSL style
configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
Connector 可以配置HTTP协议的和AJP协议. 可以参考/docs/config/http.html 里面的内容 .
可以配置线程池, 链接超时时间, 监听的端口号,SSH链接配置. 重定向的端口.