1.1 问题
有时候,开发环境我们也想把Tomcat 的默认BIO模式改为NIO模式,该如何改呢?
1.2 方案
通过eclipse里面的server.xml进行修改。
1.3 步骤
首先我们来一起看看eclipse默认的server.xml文件
名词解释:
bio(blocking I/O),顾名思义,即阻塞式I/O操作,表示Tomcat使用的是传统的Java I/O操作(即java.io包及其子包)。Tomcat在默认情况下,就是以bio模式运行的。遗憾的是,就一般而言,bio模式性能比较地下。我们可以通过Tomcat Manager来查看服务器的当前状态。
其中ajp端口是用于和服务器负载均衡时使用的。
nio(new I/O),是Java SE 1.4及后续版本提供的一种新的I/O操作方式(即java.nio包及其子包)。Java nio是一个基于缓冲区、并能提供非阻塞I/O操作的Java API,因此nio也被看成是non-blocking I/O的缩写。
它拥有比传统I/O操作(bio)更好的并发运行性能。要让Tomcat以nio模式来运行也比较简单,我们只需要在Tomcat安装目录/conf/server.xml文件中将如下配置:
<div id="crayon-5b5849cc9a921050990591" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin-top: 30px; margin-bottom: 30px; font-size: 14px !important; line-height: 21px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 21px !important; z-index: 0; opacity: 0; overflow: hidden;"><Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" /></textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -19px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 21px !important;"><div class="crayon-num" data-line="crayon-5b5849cc9a921050990591-1">1</div><div class="crayon-num" data-line="crayon-5b5849cc9a921050990591-2">2</div><div class="crayon-num" data-line="crayon-5b5849cc9a921050990591-3">3</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 21px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5b5849cc9a921050990591-1"><span class="crayon-r "><Connector </span><span class="crayon-e ">port</span><span class="crayon-o">=</span><span class="crayon-s ">"8080"</span><span class="crayon-h"> </span><span class="crayon-e ">protocol</span><span class="crayon-o">=</span><span class="crayon-s ">"HTTP/1.1"</span></div><div class="crayon-line" id="crayon-5b5849cc9a921050990591-2"><span class="crayon-h"> </span><span class="crayon-e ">connectionTimeout</span><span class="crayon-o">=</span><span class="crayon-s ">"20000"</span></div><div class="crayon-line" id="crayon-5b5849cc9a921050990591-3"><span class="crayon-h"> </span><span class="crayon-e ">redirectPort</span><span class="crayon-o">=</span><span class="crayon-s ">"8443"</span><span class="crayon-r "> /></span></div></div></td>
</tr>
</tbody></table>
</div>
</div>
改成
<div id="crayon-5b5849cc9a931531607075" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin-top: 30px; margin-bottom: 30px; font-size: 14px !important; line-height: 21px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 21px !important; z-index: 0; opacity: 0; overflow: hidden;"> <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" /></textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -19px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 21px !important;"><div class="crayon-num" data-line="crayon-5b5849cc9a931531607075-1">1</div><div class="crayon-num" data-line="crayon-5b5849cc9a931531607075-2">2</div><div class="crayon-num" data-line="crayon-5b5849cc9a931531607075-3">3</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 21px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5b5849cc9a931531607075-1"><span class="crayon-h"> </span><span class="crayon-r "><Connector </span><span class="crayon-e ">port</span><span class="crayon-o">=</span><span class="crayon-s ">"8080"</span><span class="crayon-h"> </span><span class="crayon-e ">protocol</span><span class="crayon-o">=</span><span class="crayon-s ">"org.apache.coyote.http11.Http11NioProtocol"</span></div><div class="crayon-line" id="crayon-5b5849cc9a931531607075-2"><span class="crayon-h"> </span><span class="crayon-e ">connectionTimeout</span><span class="crayon-o">=</span><span class="crayon-s ">"20000"</span></div><div class="crayon-line" id="crayon-5b5849cc9a931531607075-3"><span class="crayon-h"> </span><span class="crayon-e ">redirectPort</span><span class="crayon-o">=</span><span class="crayon-s ">"8443"</span><span class="crayon-r "> /></span></div></div></td>
</tr>
</tbody></table>
</div>
</div>
如果需要修改ajp,可以这样修改,只需要把
<div id="crayon-5b5849cc9a939817964351" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin-top: 30px; margin-bottom: 30px; font-size: 14px !important; line-height: 21px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 21px !important; z-index: 0; opacity: 0; overflow: hidden;"><Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/></textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -19px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 21px !important;"><div class="crayon-num" data-line="crayon-5b5849cc9a939817964351-1">1</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 21px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5b5849cc9a939817964351-1"><span class="crayon-r "><Connector </span><span class="crayon-e ">port</span><span class="crayon-o">=</span><span class="crayon-s ">"8009"</span><span class="crayon-h"> </span><span class="crayon-e ">protocol</span><span class="crayon-o">=</span><span class="crayon-s ">"AJP/1.3"</span><span class="crayon-h"> </span><span class="crayon-e ">redirectPort</span><span class="crayon-o">=</span><span class="crayon-s ">"8443"</span><span class="crayon-r ">/></span></div></div></td>
</tr>
</tbody></table>
</div>
</div>
改成
<div id="crayon-5b5849cc9a940843846785" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin-top: 30px; margin-bottom: 30px; font-size: 14px !important; line-height: 21px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 21px !important; z-index: 0; opacity: 0; overflow: hidden;"><Connector port="8009" protocol="org.apache.coyote.ajp.AjpNioProtocol" redirectPort="8443" /></textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -19px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 21px !important;"><div class="crayon-num" data-line="crayon-5b5849cc9a940843846785-1">1</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 21px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5b5849cc9a940843846785-1"><span class="crayon-r "><Connector </span><span class="crayon-e ">port</span><span class="crayon-o">=</span><span class="crayon-s ">"8009"</span><span class="crayon-h"> </span><span class="crayon-e ">protocol</span><span class="crayon-o">=</span><span class="crayon-s ">"org.apache.coyote.ajp.AjpNioProtocol"</span><span class="crayon-h"> </span><span class="crayon-e ">redirectPort</span><span class="crayon-o">=</span><span class="crayon-s ">"8443"</span><span class="crayon-r "> /></span></div></div></td>
</tr>
</tbody></table>
</div>
</div>
部署项目,启动tomcat的效果图,如下:
<script type="text/javascript">
document.getElementById("bdshell_js").src = "http://share.baidu.com/static/js/shell_v2.js?t=" + new Date().getHours();
</script>
如果您认为本教程质量不错,读后觉得收获很大,预期工资能蹭蹭蹭的往上涨,那么不妨小额赞助我一下,让我有动力继续写出高质量的教程。
<img src="http://www.souvc.com/wp-content/uploads/2017/05/IMG_1985.jpg" style="250px;height:300px" ;="">
<img src="http://www.souvc.com/wp-content/uploads/2017/05/IMG_1986.jpg" style="250px;height:300px" ;="">