﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-xiaomage234-随笔分类-app server</title><link>http://www.blogjava.net/xiaomage234/category/53838.html</link><description>生命本就是一次凄美的漂流，记忆中放不下的，永远是孩提时代的那一份浪漫与纯真！</description><language>zh-cn</language><lastBuildDate>Thu, 12 Oct 2017 15:00:50 GMT</lastBuildDate><pubDate>Thu, 12 Oct 2017 15:00:50 GMT</pubDate><ttl>60</ttl><item><title>转： Nginx + Tomcat + HTTPS 配置不需要在 Tomcat 上启用 SSL 支持</title><link>http://www.blogjava.net/xiaomage234/archive/2017/10/12/432853.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 12 Oct 2017 03:02:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2017/10/12/432853.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/432853.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2017/10/12/432853.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/432853.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/432853.html</trackback:ping><description><![CDATA[from:http://blog.csdn.net/vfush/article/details/51086274<br /><br /><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">最近做了个Web项目, 架构上使用了 Nginx +tomcat 集群, 且全站HTTPS，用nginx 做负载，nginx和tomcat 使用内网http通信，遇到http css，js静态资源被浏览器拦截问题，网上搜索到的很多文章在描述 Nginx + Tomcat 启用 HTTPS 支持的时候，都必须在 Nginx 和 Tomcat 两边同时配置 SSL 支持，今天做个总结。</p><h4><a name="t1" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>遇到问题</h4><ol style="box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;"><li style="box-sizing: border-box;">nginx强制使用https访问(http跳转到https)</li><li style="box-sizing: border-box;">http的js，css 等静态资源被浏览器拦截（http不被信任）</li></ol><h3><a name="t2" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>最后的解决方案</h3><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">首先解决<strong style="box-sizing: border-box;">第一个问题</strong>全站https&nbsp;<br style="box-sizing: border-box;" /><a href="http://blog.csdn.net/wzy_1988/article/details/8549290" target="_blank" style="text-decoration-line: none; box-sizing: border-box; color: #0c89cf;">参考</a>&nbsp;<br style="box-sizing: border-box;" />三种方式，跟大家共享一下</p><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;"><strong style="box-sizing: border-box;">nginx的rewrite方法</strong></p><pre style="white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; margin-top: 0px; margin-bottom: 10.5px; font-family: &quot;Source Code Pro&quot;, monospace; padding: 10px; line-height: 1.45; word-break: break-all; color: #333333; background-color: rgba(128, 128, 128, 0.05); border: 1px solid rgba(128, 128, 128, 0.075); border-radius: 0px;"><code style="box-sizing: border-box; font-family: &quot;Source Code Pro&quot;, monospace; padding: 0px; font-size: inherit; color: inherit; background-color: transparent; white-space: pre; border-radius: 0px; word-wrap: normal;">server {   listen  192.168.1.111:80;   server_name test.com;   rewrite ^(.*)$  https://$host$1 permanent; }   </code></pre><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;"><strong style="box-sizing: border-box;">nginx的497状态码，我选择了这种方式</strong></p><pre style="white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; margin-top: 0px; margin-bottom: 10.5px; font-family: &quot;Source Code Pro&quot;, monospace; padding: 10px; line-height: 1.45; word-break: break-all; color: #333333; background-color: rgba(128, 128, 128, 0.05); border: 1px solid rgba(128, 128, 128, 0.075); border-radius: 0px;"><code style="box-sizing: border-box; font-family: &quot;Source Code Pro&quot;, monospace; padding: 0px; font-size: inherit; color: inherit; background-color: transparent; white-space: pre; border-radius: 0px; word-wrap: normal;">server {       listen       192.168.1.11:443;  #ssl端口       listen       192.168.1.11:80;   #用户习惯用http访问，加上80，后面通过497状态码让它自动跳到443端口       server_name  test.com;       #为一个server{......}开启ssl支持       ssl                  on;       #指定PEM格式的证书文件        ssl_certificate      /etc/nginx/test.pem;        #指定PEM格式的私钥文件       ssl_certificate_key  /etc/nginx/test.key;        #让http请求重定向到https请求        error_page 497  https://$host$uri?$args;   }   </code></pre><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;"><strong style="box-sizing: border-box;">index.html刷新网页</strong></p><pre style="white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; margin-top: 0px; margin-bottom: 10.5px; font-family: &quot;Source Code Pro&quot;, monospace; padding: 10px; line-height: 1.45; word-break: break-all; color: #333333; background-color: rgba(128, 128, 128, 0.05); border: 1px solid rgba(128, 128, 128, 0.075); border-radius: 0px;"><code style="box-sizing: border-box; font-family: &quot;Source Code Pro&quot;, monospace; padding: 0px; font-size: inherit; color: inherit; background-color: transparent; white-space: pre; border-radius: 0px; word-wrap: normal;">&lt;html&gt;   &lt;meta http-equiv="refresh" content="0;url=https://test.com/"&gt;   &lt;/html&gt;  </code></pre><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">当http访问到index.html时候自动跳转到https</p><hr style="box-sizing: border-box; margin: 2em 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; border-image: initial; border-top-style: solid; border-top-color: rgba(128, 128, 128, 0.1); color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">接下来解决<strong style="box-sizing: border-box;">第二个问题</strong>&nbsp;<br style="box-sizing: border-box;" />如果tomcat 和nginx 双方没有配置X-Forwarded-Proto tomcat就不能正确区分实际用户是http 还是https，导致tomcat 里配置的静态资源被认为是http而被浏览器拦截，request.getScheme()总是 http，而不是实际的http或https</p><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">分别配置一下 Nginx 和 Tomcat ，果然好了。&nbsp;<br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">配置 Nginx 的转发选项：</strong></p><pre name="code" style="white-space: nowrap; word-wrap: break-word; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-top: 0px; margin-bottom: 1.1em; font-family: &quot;Source Code Pro&quot;, monospace; padding: 5px 5px 5px 60px; line-height: 1.45; word-break: break-all; color: #333333; background-color: rgba(128, 128, 128, 0.05); border: 0px solid #888888; border-radius: 0px;"><code lasso=""  has-numbering"="" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: &quot;Source Code Pro&quot;, monospace; font-size: inherit; white-space: pre; border-radius: 0px; word-wrap: normal;"> proxy_set_header       Host <span style="color: #660066; box-sizing: border-box;">$host</span>;       proxy_set_header  X<span style="box-sizing: border-box;">-Real</span><span style="box-sizing: border-box;">-IP</span>  <span style="color: #660066; box-sizing: border-box;">$remote_addr</span>;       proxy_set_header  X<span style="box-sizing: border-box;">-Forwarded</span><span style="box-sizing: border-box;">-For</span> <span style="color: #660066; box-sizing: border-box;">$proxy_add_x_forwarded_for</span>;       proxy_set_header X<span style="box-sizing: border-box;">-Forwarded</span><span style="box-sizing: border-box;">-Proto</span>  <span style="color: #660066; box-sizing: border-box;">$scheme</span>;  </code><ul style="box-sizing: border-box; position: absolute; width: 50px; background-color: #eeeeee; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid #dddddd; list-style: none;"><li style="box-sizing: border-box; padding: 0px 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding: 0px 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding: 0px 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding: 0px 5px; list-style: none; margin-left: 0px;">4</li></ul></pre><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;"><strong style="box-sizing: border-box;">配置Tomcat server.xml 的 Engine 模块下配置一个 Valve：</strong></p><pre name="code" style="white-space: nowrap; word-wrap: break-word; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-top: 0px; margin-bottom: 1.1em; font-family: &quot;Source Code Pro&quot;, monospace; padding: 5px 5px 5px 60px; line-height: 1.45; word-break: break-all; color: #333333; background-color: rgba(128, 128, 128, 0.05); border: 0px solid #888888; border-radius: 0px;"><code xml=""  has-numbering"="" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: &quot;Source Code Pro&quot;, monospace; font-size: inherit; white-space: pre; border-radius: 0px; word-wrap: normal;"><span style="color: #006666; box-sizing: border-box;">&lt;<span style="box-sizing: border-box; color: #000088;">Valve</span> <span style="box-sizing: border-box; color: #660066;">className</span>=<span style="box-sizing: border-box; color: #008800;">"org.apache.catalina.valves.RemoteIpValve"</span>   <span style="box-sizing: border-box; color: #660066;">remoteIpHeader</span>=<span style="box-sizing: border-box; color: #008800;">"X-Forwarded-For"</span>   <span style="box-sizing: border-box; color: #660066;">protocolHeader</span>=<span style="box-sizing: border-box; color: #008800;">"X-Forwarded-Proto"</span>   <span style="box-sizing: border-box; color: #660066;">protocolHeaderHttpsValue</span>=<span style="box-sizing: border-box; color: #008800;">"https"</span>/&gt;</span>  </code><ul style="box-sizing: border-box; position: absolute; width: 50px; background-color: #eeeeee; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid #dddddd; list-style: none;"><li style="box-sizing: border-box; padding: 0px 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding: 0px 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding: 0px 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding: 0px 5px; list-style: none; margin-left: 0px;">4</li></ul></pre><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">非80端口配置&nbsp;<br style="box-sizing: border-box;" />Nginx增加以下配置&nbsp;<br style="box-sizing: border-box;" /><code style="box-sizing: border-box; font-family: &quot;Source Code Pro&quot;, monospace; padding: 2px 4px; font-size: 13.5px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">proxy_set_header Host $host:$server_port; 非80端口 ，用80端口时 不需要$server_port&nbsp;<br style="box-sizing: border-box;" />proxy_set_header X-Real-IP $remote_addr;&nbsp;<br style="box-sizing: border-box;" />proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;&nbsp;<br style="box-sizing: border-box;" />proxy_set_header X-Forwarded-Proto $scheme;&nbsp;<br style="box-sizing: border-box;" /></code></p><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">Tomcat server.xml配置&nbsp;<br style="box-sizing: border-box;" /><code style="box-sizing: border-box; font-family: &quot;Source Code Pro&quot;, monospace; padding: 2px 4px; font-size: 13.5px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">&lt;Engine name="Catalina" defaultHost="localhost"&gt;&nbsp;<br style="box-sizing: border-box;" />&lt;Valve className="org.apache.catalina.valves.RemoteIpValve"&nbsp;<br style="box-sizing: border-box;" />remoteIpHeader="X-Forwarded-For"&nbsp;<br style="box-sizing: border-box;" />protocolHeader="X-Forwarded-Proto"&nbsp;<br style="box-sizing: border-box;" />protocolHeaderHttpsValue="https" httpsServerPort="7001"/&gt; 非80端口时，必须增加httpsServerPort配置，不然request.getServerPort()方法返回 443.&nbsp;<br style="box-sizing: border-box;" />&lt;/Engine&gt;</code></p><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">关于 RemoteIpValve,可以阅读下 doc</p><p style="margin: 0px 0px 1.1em; padding: 0px; box-sizing: border-box; color: #3f3f3f; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;"><a href="http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/valves/RemoteIpValve.html" target="_blank" style="text-decoration-line: none; box-sizing: border-box; color: #0c89cf;">http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/valves/RemoteIpValve.html</a></p><img src ="http://www.blogjava.net/xiaomage234/aggbug/432853.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2017-10-12 11:02 <a href="http://www.blogjava.net/xiaomage234/archive/2017/10/12/432853.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Tomcat7调试运行环境搭建与源代码分析入门</title><link>http://www.blogjava.net/xiaomage234/archive/2015/12/24/428814.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 24 Dec 2015 07:54:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2015/12/24/428814.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/428814.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2015/12/24/428814.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/428814.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/428814.html</trackback:ping><description><![CDATA[<span style="color: #999999; font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; float: left; background-color: #fefefe;">原文</span><span style="color: #999999; font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; background-color: #fefefe;">&nbsp;&nbsp;</span><a cut70"="" href="http://zhh2009.iteye.com/blog/1557891?utm_source=tuicool&amp;utm_medium=referral" style="color: #333333; text-decoration: none; transition: 0.25s; overflow: hidden; white-space: nowrap; word-break: keep-all; text-overflow: ellipsis; max-width: 69%; font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; display: inline-block; background-color: #fefefe;">http://zhh2009.iteye.com/blog/1557891<br /><br /><br /></a><h3>1. 需要准备好下面这些工具</h3><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">JDK 1.6+</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">Maven 2或3</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">TortoiseSVN 1.7+ (从1.7开始".svn"目录集中放在一处了，不再每个目录下都放一份)</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">Eclipse 3.5+</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">这4个工具不在这里描述怎么配置了，如果你是有两三年开发经验的Java开发人员，正常来讲都一直在用了。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">另外，分析tomcat源代码不需要对这4个工具做什么特殊配置。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><h3>2. 下载Tomcat的源代码</h3><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">Apache旗下的开源项目基本上都放在这:&nbsp;<a href="http://svn.apache.org/repos/asf" title="http://svn.apache.org/repos/asf" style="color: #949494; text-decoration: none; transition: 0.25s; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #949494; font-style: italic; font-weight: bold;">http://svn.apache.org/repos/asf</a></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">里面包含了tomcat、struts、hadoop、hbase等流行的开源项目的源代码，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">可以直接用浏览器打开这个URL，或者用TortoiseSVN的Repository Browser打开它。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">tomcat的svn是:&nbsp;<a href="http://svn.apache.org/repos/asf/tomcat" title="http://svn.apache.org/repos/asf/tomcat" style="color: #949494; text-decoration: none; transition: 0.25s; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #949494; font-style: italic; font-weight: bold;">http://svn.apache.org/repos/asf/tomcat</a>&nbsp;, 如下图所示:</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;"><br /><img src="http://img1.tuicool.com/IRZJNn.jpg!web" alt="" style="max-width: 550px; height: auto; vertical-align: middle; border: 0px none; text-align: center; margin: 0px auto; display: block;" /></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">目前tomcat有4个大分支:&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">5.5 :&nbsp;<a href="http://svn.apache.org/repos/asf/tomcat/tc5.5.x" title="http://svn.apache.org/repos/asf/tomcat/tc5.5.x" style="color: #949494; text-decoration: none; transition: 0.25s; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #949494; font-style: italic; font-weight: bold;">http://svn.apache.org/repos/asf/tomcat/tc5.5.x</a></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">6.0 :&nbsp;<a href="http://svn.apache.org/repos/asf/tomcat/tc6.0.x" title="http://svn.apache.org/repos/asf/tomcat/tc6.0.x" style="color: #949494; text-decoration: none; transition: 0.25s; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #949494; font-style: italic; font-weight: bold;">http://svn.apache.org/repos/asf/tomcat/tc6.0.x</a></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">7.0 :&nbsp;<a href="http://svn.apache.org/repos/asf/tomcat/tc7.0.x" title="http://svn.apache.org/repos/asf/tomcat/tc7.0.x" style="color: #949494; text-decoration: none; transition: 0.25s; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #949494; font-style: italic; font-weight: bold;">http://svn.apache.org/repos/asf/tomcat/tc7.0.x</a></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">8.0 :&nbsp;<a href="http://svn.apache.org/repos/asf/tomcat/trunk" title="http://svn.apache.org/repos/asf/tomcat/trunk" style="color: #949494; text-decoration: none; transition: 0.25s; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #949494; font-style: italic; font-weight: bold;">http://svn.apache.org/repos/asf/tomcat/trunk</a></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">5.5分支会在今年9月30号后停止维护，所以除非有历史遗留系统，不推荐再去读它的代码，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">6.0分支是比较成熟的，在生产环境用得比较多，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">目前官方对这个分支进入维护、bugfix阶段，很少有新功能添加进来了，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">我个人也不推荐读它的代码，代码相对7.0来讲比较脏乱。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;"><span style="color: #008000;">7.0分支完整实现了servlet 3.0规范，已陆续发布了27个小版本，己经稳定了，可用于生产环境，</span></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;"><span style="color: #008000;">代码比5.5、6.0分支干净整洁得多，这也是我强烈向你推荐的版本。</span></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">8.0分支主要关注web socket和spdy，正处于活跃开发阶段，代码变动比较频繁，保持关注即可。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">所以这篇文章讲的是7.0分支，研究tomcat推荐直接提取svn的源代码:</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">用TortoiseSVN checkout这个svn的代码:<a href="http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk" title="http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk" style="color: #949494; text-decoration: none; transition: 0.25s; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #949494; font-style: italic; font-weight: bold;">http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk</a></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">放到D:\Tomcat7\trunk (你可以换别的目录)</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">然后再从这下载一个二进制分发包(Binary Distributions)</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;"><a href="http://labs.mop.com/apache-mirror/tomcat/tomcat-7/v7.0.27/bin/apache-tomcat-7.0.27.zip" title="http://labs.mop.com/apache-mirror/tomcat/tomcat-7/v7.0.27/bin/apache-tomcat-7.0.27.zip" style="color: #949494; text-decoration: none; transition: 0.25s; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #949494; font-style: italic; font-weight: bold;">http://labs.mop.com/apache-mirror/tomcat/tomcat-7/v7.0.27/bin/apache-tomcat-7.0.27.zip</a></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">解压后放到D:\Tomcat7，顺便把"apache-tomcat-7.0.27"重命名成launch吧，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">用这个二进制分发包而不是从源代码构建只是为了节省时间，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">直接用它conf目录里面的配置文件和webapps下的例子。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><h3>3. 把它变成maven工程</h3><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">主要是添加几个依赖(ecj、ant、jaxrpc等)，否则的话导入eclipse后会有编译错误，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">另外，因为tomcat不是标准的maven工程项目，比如没有src\main\java这样的目录，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">所以要调整一下sourceDirectory和testSourceDirectory，下面是一个完整的pom文件，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">直接放到D:\Tomcat7目录即可(pom.xml与之前的launch、trunk目录并列)</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;"><span style="color: #008000;">(注: pom.xml文件在附件中)</span></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><pre xml"="" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; border-radius: 4px; margin-top: 0px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: #f6f6f6;"><span style="color: #000080;">&lt;project <span style="color: #008080;">xmlns</span>=<span style="color: #dd1144;">"http://maven.apache.org/POM/4.0.0"</span> <span style="color: #008080;">xmlns:xsi</span>=<span style="color: #dd1144;">"http://www.w3.org/2001/XMLSchema-instance"</span>  <span style="color: #008080;">xsi:schemaLocation</span>=<span style="color: #dd1144;">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"</span>&gt;</span>  <span style="color: #000080;">&lt;modelVersion&gt;</span>4.0.0<span style="color: #000080;">&lt;/modelVersion&gt;</span>   <span style="color: #000080;">&lt;groupId&gt;</span>org.apache.tomcat<span style="color: #000080;">&lt;/groupId&gt;</span>  <span style="color: #000080;">&lt;artifactId&gt;</span>Tomcat7.0<span style="color: #000080;">&lt;/artifactId&gt;</span>  <span style="color: #000080;">&lt;name&gt;</span>Tomcat7.0<span style="color: #000080;">&lt;/name&gt;</span>  <span style="color: #000080;">&lt;version&gt;</span>7.0<span style="color: #000080;">&lt;/version&gt;</span>   <span style="color: #000080;">&lt;build&gt;</span>   <span style="color: #000080;">&lt;finalName&gt;</span>Tomcat7.0<span style="color: #000080;">&lt;/finalName&gt;</span>   <span style="color: #000080;">&lt;sourceDirectory&gt;</span>trunk/java<span style="color: #000080;">&lt;/sourceDirectory&gt;</span>   <span style="color: #000080;">&lt;testSourceDirectory&gt;</span>trunk/test<span style="color: #000080;">&lt;/testSourceDirectory&gt;</span>   <span style="color: #000080;">&lt;resources&gt;</span>    <span style="color: #000080;">&lt;resource&gt;</span>     <span style="color: #000080;">&lt;directory&gt;</span>trunk/java<span style="color: #000080;">&lt;/directory&gt;</span>    <span style="color: #000080;">&lt;/resource&gt;</span>   <span style="color: #000080;">&lt;/resources&gt;</span>   <span style="color: #000080;">&lt;testResources&gt;</span>    <span style="color: #000080;">&lt;testResource&gt;</span>     <span style="color: #000080;">&lt;directory&gt;</span>trunk/test<span style="color: #000080;">&lt;/directory&gt;</span>    <span style="color: #000080;">&lt;/testResource&gt;</span>   <span style="color: #000080;">&lt;/testResources&gt;</span>   <span style="color: #000080;">&lt;plugins&gt;</span>    <span style="color: #000080;">&lt;plugin&gt;</span>     <span style="color: #000080;">&lt;groupId&gt;</span>org.apache.maven.plugins<span style="color: #000080;">&lt;/groupId&gt;</span>     <span style="color: #000080;">&lt;artifactId&gt;</span>maven-compiler-plugin<span style="color: #000080;">&lt;/artifactId&gt;</span>     <span style="color: #000080;">&lt;version&gt;</span>2.3<span style="color: #000080;">&lt;/version&gt;</span>     <span style="color: #000080;">&lt;configuration&gt;</span>      <span style="color: #000080;">&lt;source&gt;</span>1.6<span style="color: #000080;">&lt;/source&gt;</span>      <span style="color: #000080;">&lt;target&gt;</span>1.6<span style="color: #000080;">&lt;/target&gt;</span>     <span style="color: #000080;">&lt;/configuration&gt;</span>    <span style="color: #000080;">&lt;/plugin&gt;</span>   <span style="color: #000080;">&lt;/plugins&gt;</span>  <span style="color: #000080;">&lt;/build&gt;</span>   <span style="color: #000080;">&lt;dependencies&gt;</span>   <span style="color: #000080;">&lt;dependency&gt;</span>    <span style="color: #000080;">&lt;groupId&gt;</span>junit<span style="color: #000080;">&lt;/groupId&gt;</span>    <span style="color: #000080;">&lt;artifactId&gt;</span>junit<span style="color: #000080;">&lt;/artifactId&gt;</span>    <span style="color: #000080;">&lt;version&gt;</span>4.4<span style="color: #000080;">&lt;/version&gt;</span>    <span style="color: #000080;">&lt;scope&gt;</span>test<span style="color: #000080;">&lt;/scope&gt;</span>   <span style="color: #000080;">&lt;/dependency&gt;</span>   <span style="color: #000080;">&lt;dependency&gt;</span>    <span style="color: #000080;">&lt;groupId&gt;</span>org.eclipse.jdt.core.compiler<span style="color: #000080;">&lt;/groupId&gt;</span>    <span style="color: #000080;">&lt;artifactId&gt;</span>ecj<span style="color: #000080;">&lt;/artifactId&gt;</span>    <span style="color: #000080;">&lt;version&gt;</span>3.7.2<span style="color: #000080;">&lt;/version&gt;</span>   <span style="color: #000080;">&lt;/dependency&gt;</span>   <span style="color: #000080;">&lt;dependency&gt;</span>    <span style="color: #000080;">&lt;groupId&gt;</span>ant<span style="color: #000080;">&lt;/groupId&gt;</span>    <span style="color: #000080;">&lt;artifactId&gt;</span>ant<span style="color: #000080;">&lt;/artifactId&gt;</span>    <span style="color: #000080;">&lt;version&gt;</span>1.7.0<span style="color: #000080;">&lt;/version&gt;</span>   <span style="color: #000080;">&lt;/dependency&gt;</span>   <span style="color: #000080;">&lt;dependency&gt;</span>    <span style="color: #000080;">&lt;groupId&gt;</span>wsdl4j<span style="color: #000080;">&lt;/groupId&gt;</span>    <span style="color: #000080;">&lt;artifactId&gt;</span>wsdl4j<span style="color: #000080;">&lt;/artifactId&gt;</span>    <span style="color: #000080;">&lt;version&gt;</span>1.6.2<span style="color: #000080;">&lt;/version&gt;</span>   <span style="color: #000080;">&lt;/dependency&gt;</span>   <span style="color: #000080;">&lt;dependency&gt;</span>    <span style="color: #000080;">&lt;groupId&gt;</span>javax.xml<span style="color: #000080;">&lt;/groupId&gt;</span>    <span style="color: #000080;">&lt;artifactId&gt;</span>jaxrpc<span style="color: #000080;">&lt;/artifactId&gt;</span>    <span style="color: #000080;">&lt;version&gt;</span>1.1<span style="color: #000080;">&lt;/version&gt;</span>   <span style="color: #000080;">&lt;/dependency&gt;</span>  <span style="color: #000080;">&lt;/dependencies&gt;</span>  <span style="color: #000080;">&lt;/project&gt;</span></pre>&nbsp;<p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><h3>4. 导入Eclipse</h3><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">在命令行窗口中进入D:\Tomcat7目录，执行 mvn eclipse:eclipse 就可以转成eclipse工程项目了，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">然后打开eclipse，点"File-&gt;Import-&gt;General-&gt;Existing Projects into Workspace"，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">最后打开D:\Tomcat7就能看到Tomcat7.0这个项目了。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">(如果eclipse装了m2e插件不用执行mvn eclipse:eclipse的，可以直接导入maven工程)</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><h3>5. 在Eclipse中让Tomcat跑起来</h3><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">在Eclipse中打开org.apache.catalina.startup.Bootstrap类，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">在编辑区右击，点"Run As-&gt;Run configurations"，然后双击"Java Aplication"就会出来一个新的"Bootstrap"，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">选中它，在右边点击"Arguments"那一栏，把下面的内容copy到"VM arguments"中:</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;"><span style="color: #008000;">-Dcatalina.home=launch -Dcatalina.base=launch -Djava.endorsed.dirs=launch/endorsed -Djava.io.tmpdir=launch/temp -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=launch/conf/logging.properties</span></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">如下图:</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;"><br /><img src="http://img0.tuicool.com/JZRryu.jpg!web" alt="" style="max-width: 550px; height: auto; vertical-align: middle; border: 0px none; text-align: center; margin: 0px auto; display: block;" /></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">然后点run按钮，就可以启动tomcat了，启动成功会在Eclipse的console中显示:</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><pre sql"="" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; border-radius: 4px; margin-top: 0px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: #f6f6f6;">2012-6-10 14:25:31 org.apache.catalina.startup.Catalina <span style="font-weight: bold;">start 信息: Server startup in <span style="color: #009999;">359</span> ms</span></pre>&nbsp;<p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">除上面这种方式外，这里还有一个Eclipse的launch脚本&nbsp;<span style="color: #008000;">(start-tomcat7.launch&nbsp;</span>&nbsp;<span style="color: #008000;">(注: 在附件中)</span>&nbsp;<span style="color: #008000;">)</span>&nbsp;，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><pre xml"="" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; border-radius: 4px; margin-top: 0px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: #f6f6f6;"><span style="color: #999999; font-weight: bold;">&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;</span> <span style="color: #000080;">&lt;launchConfiguration <span style="color: #008080;">type</span>=<span style="color: #dd1144;">"org.eclipse.jdt.launching.localJavaApplication"</span>&gt;</span> <span style="color: #000080;">&lt;listAttribute <span style="color: #008080;">key</span>=<span style="color: #dd1144;">"org.eclipse.debug.core.MAPPED_RESOURCE_PATHS"</span>&gt;</span> <span style="color: #000080;">&lt;listEntry <span style="color: #008080;">value</span>=<span style="color: #dd1144;">"/Tomcat7.0/trunk/java/org/apache/catalina/startup/Bootstrap.java"</span>/&gt;</span> <span style="color: #000080;">&lt;/listAttribute&gt;</span> <span style="color: #000080;">&lt;listAttribute <span style="color: #008080;">key</span>=<span style="color: #dd1144;">"org.eclipse.debug.core.MAPPED_RESOURCE_TYPES"</span>&gt;</span> <span style="color: #000080;">&lt;listEntry <span style="color: #008080;">value</span>=<span style="color: #dd1144;">"1"</span>/&gt;</span> <span style="color: #000080;">&lt;/listAttribute&gt;</span> <span style="color: #000080;">&lt;stringAttribute <span style="color: #008080;">key</span>=<span style="color: #dd1144;">"org.eclipse.jdt.launching.MAIN_TYPE"</span> <span style="color: #008080;">value</span>=<span style="color: #dd1144;">"org.apache.catalina.startup.Bootstrap"</span>/&gt;</span> <span style="color: #000080;">&lt;stringAttribute <span style="color: #008080;">key</span>=<span style="color: #dd1144;">"org.eclipse.jdt.launching.PROGRAM_ARGUMENTS"</span> <span style="color: #008080;">value</span>=<span style="color: #dd1144;">"start"</span>/&gt;</span> <span style="color: #000080;">&lt;stringAttribute <span style="color: #008080;">key</span>=<span style="color: #dd1144;">"org.eclipse.jdt.launching.PROJECT_ATTR"</span> <span style="color: #008080;">value</span>=<span style="color: #dd1144;">"Tomcat7.0"</span>/&gt;</span> <span style="color: #000080;">&lt;stringAttribute <span style="color: #008080;">key</span>=<span style="color: #dd1144;">"org.eclipse.jdt.launching.VM_ARGUMENTS"</span> <span style="color: #008080;">value</span>=<span style="color: #dd1144;">"-Dcatalina.home=launch -Dcatalina.base=launch -Djava.endorsed.dirs=launch/endorsed -Djava.io.tmpdir=launch/temp -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=launch/conf/logging.properties"</span>/&gt;</span> <span style="color: #000080;">&lt;/launchConfiguration&gt;</span></pre>&nbsp;<p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">可以放到D:\Tomcat7目录，然后flush一下Eclipse，在Eclipse中右击这个文件，点Run As启动Tomcat，点Debug As可以调试Tomcat。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">下图是Tomcat在Eclipse中的项目布局:</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;"><br /><img src="http://img1.tuicool.com/iMvqmy.jpg!web" alt="" style="max-width: 550px; height: auto; vertical-align: middle; border: 0px none; text-align: center; margin: 0px auto;" /></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">最后，打开你的浏览器，输入&nbsp;<a href="http://127.0.0.1:8080/examples/" title="http://127.0.0.1:8080/examples/" style="color: #949494; text-decoration: none; transition: 0.25s; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #949494; font-style: italic; font-weight: bold;">http://127.0.0.1:8080/examples/</a>&nbsp;看看例子吧。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><h3>6. 简单的源代码阅读指南:</h3><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><pre diff"="" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; border-radius: 4px; margin-top: 0px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: #f6f6f6;">包名                    用途 ================================================= javax                 servlet/jsp/el相关的api org.apache.catalina   tomcat自身架构 org.apache.coyote     http、ajp协议实现相关的类 org.apache.el         实现el规范 org.apache.jasper     实现jsp规范、编译jsp文件 org.apache.juli       tomcat的日志系统 org.apache.naming     jndi实现 org.apache.tomcat     tomcat的工具包、net、digester xml解析器</pre>&nbsp;<p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">阅读顺序:</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">可以从org.apache.catalina.startup.Bootstrap这个类开始看起，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">然后到org.apache.catalina.startup.Catalina,&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">在Catalina类中会触发conf/server.xml文件的解析，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">这时要看org.apache.tomcat.util.digester中的类，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">解析的过程中会用到org.apache.catalina.startup包中的很多RuleSet类，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">server.xml文件解析完后，会生成org.apache.catalina.core包中的各种StandardXXX类的实例，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">比如StandardServer、StandardService、StandardEngine等等，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">这些Standard组件都是有生命周期的，接着会调用他们的init、start等方法，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">会触发下面这些组件进入init、start状态</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">org.apache.catalina.connector.Connector</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">org.apache.coyote.http11.Http11Protocol</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">org.apache.tomcat.util.net.JIoEndpoint</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">在JIoEndpoint(或NioEndpoint、AprEndpoint)中会监听8080这样的端口，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">有请求进来了，就进行相关的io操作，接着转到org.apache.coyote包中的相应类进行协议解析，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">生成org.apache.catalina.connector.Request和org.apache.catalina.connector.Response实例，</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">然后转到各种Valve、应用Filter，最后到达应用的Servlet/JSP。</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">下图描述了Tomcat7的核心架构:</p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;"><br /><img src="http://img0.tuicool.com/YrQjay.png!web" alt="" style="max-width: 550px; height: auto; vertical-align: middle; border: 0px none; text-align: center; margin: 0px auto; display: block;" /></p><p style="margin: 0px 0px 0.75em; font-size: 16px; line-height: 27.2000007629395px; text-indent: 1em;">&nbsp;</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/428814.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2015-12-24 15:54 <a href="http://www.blogjava.net/xiaomage234/archive/2015/12/24/428814.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>zookeeper 大量连接断开重连原因排查[转]</title><link>http://www.blogjava.net/xiaomage234/archive/2015/09/10/427237.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 10 Sep 2015 02:41:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2015/09/10/427237.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/427237.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2015/09/10/427237.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/427237.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/427237.html</trackback:ping><description><![CDATA[from:http://blog.csdn.net/zheng0518/article/details/44943357<br /><br /><h2>问题现象</h2><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">最后发现线上的zookeeper的日志zookeeper.out 文件居然有6G，后来设置下日志为滚动输出，参考：</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"><a target="_blank" href="http://blog.csdn.net/hengyunabc/article/details/19006911" style="color: #336699; text-decoration: none;">http://blog.csdn.net/hengyunabc/article/details/19006911</a><br /></p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">但是改了之后，发现一天的日志量就是100多M，滚动日志一天就被冲掉了，这个不科学。</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">再仔细查看下日志里的内容，发现有很多连接建立好，马上又断开：</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><div bg_java"="" style="width: 936.53125px; line-height: 26px;"><div><div><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="view plain" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="copy" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-position: 0% 0%; background-repeat: no-repeat;">copy</a><a href="https://code.csdn.net/snippets/531402" target="_blank" title="在CODE上查看代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a href="https://code.csdn.net/snippets/531402/fork" target="_blank" title="派生到我的代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 470px; top: 593px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1"><li style="line-height: 18px;"><span style="color: #c00000;">2014</span>-<span style="color: #c00000;">11</span>-<span style="color: #c00000;">24</span>&nbsp;<span style="color: #c00000;">15</span>:<span style="color: #c00000;">38</span>:<span style="color: #c00000;">33</span>,<span style="color: #c00000;">348</span>&nbsp;[myid:<span style="color: #c00000;">3</span>]&nbsp;-&nbsp;INFO&nbsp;&nbsp;[NIOServerCxn.Factory:<span style="color: #c00000;">0.0</span>.<span style="color: #c00000;">0.0</span>/<span style="color: #c00000;">0.0</span>.<span style="color: #c00000;">0.0</span>:<span style="color: #c00000;">2181</span>:NIOServerCnxn<span style="color: #646464;">@1001</span>]&nbsp;-&nbsp;Closed&nbsp;socket&nbsp;connection&nbsp;for&nbsp;client&nbsp;/<span style="color: #c00000;">10.0</span>.<span style="color: #c00000;">0.3</span>:<span style="color: #c00000;">47772</span>&nbsp;(no&nbsp;session&nbsp;established&nbsp;for&nbsp;client)&nbsp;&nbsp;</li><li style="line-height: 18px;"><span style="color: #c00000;">2014</span>-<span style="color: #c00000;">11</span>-<span style="color: #c00000;">24</span>&nbsp;<span style="color: #c00000;">15</span>:<span style="color: #c00000;">38</span>:<span style="color: #c00000;">33</span>,<span style="color: #c00000;">682</span>&nbsp;[myid:<span style="color: #c00000;">3</span>]&nbsp;-&nbsp;INFO&nbsp;&nbsp;[NIOServerCxn.Factory:<span style="color: #c00000;">0.0</span>.<span style="color: #c00000;">0.0</span>/<span style="color: #c00000;">0.0</span>.<span style="color: #c00000;">0.0</span>:<span style="color: #c00000;">2181</span>:NIOServerCnxnFactory<span style="color: #646464;">@197</span>]&nbsp;-&nbsp;Accepted&nbsp;socket&nbsp;connection&nbsp;from&nbsp;/<span style="color: #c00000;">10.0</span>.<span style="color: #c00000;">0.3</span>:<span style="color: #c00000;">32119</span>&nbsp;&nbsp;</li><li style="line-height: 18px;"><span style="color: #c00000;">2014</span>-<span style="color: #c00000;">11</span>-<span style="color: #c00000;">24</span>&nbsp;<span style="color: #c00000;">15</span>:<span style="color: #c00000;">38</span>:<span style="color: #c00000;">33</span>,<span style="color: #c00000;">682</span>&nbsp;[myid:<span style="color: #c00000;">3</span>]&nbsp;-&nbsp;WARN&nbsp;&nbsp;[NIOServerCxn.Factory:<span style="color: #c00000;">0.0</span>.<span style="color: #c00000;">0.0</span>/<span style="color: #c00000;">0.0</span>.<span style="color: #c00000;">0.0</span>:<span style="color: #c00000;">2181</span>:NIOServerCnxn<span style="color: #646464;">@349</span>]&nbsp;-&nbsp;caught&nbsp;end&nbsp;of&nbsp;stream&nbsp;exception&nbsp;&nbsp;</li><li style="line-height: 18px;">EndOfStreamException:&nbsp;Unable&nbsp;to&nbsp;read&nbsp;additional&nbsp;data&nbsp;from&nbsp;client&nbsp;sessionid&nbsp;<span style="color: #c00000;">0x0</span>,&nbsp;likely&nbsp;client&nbsp;has&nbsp;closed&nbsp;socket&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;org.apache.zookeeper.server.NIOServerCnxn.doIO(NIOServerCnxn.java:<span style="color: #c00000;">220</span>)&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;org.apache.zookeeper.server.NIOServerCnxnFactory.run(NIOServerCnxnFactory.java:<span style="color: #c00000;">208</span>)&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.lang.Thread.run(Thread.java:<span style="color: #c00000;">745</span>)&nbsp;&nbsp;</li><li style="line-height: 18px;"><span style="color: #c00000;">2014</span>-<span style="color: #c00000;">11</span>-<span style="color: #c00000;">24</span>&nbsp;<span style="color: #c00000;">15</span>:<span style="color: #c00000;">38</span>:<span style="color: #c00000;">33</span>,<span style="color: #c00000;">682</span>&nbsp;[myid:<span style="color: #c00000;">3</span>]&nbsp;-&nbsp;INFO&nbsp;&nbsp;[NIOServerCxn.Factory:<span style="color: #c00000;">0.0</span>.<span style="color: #c00000;">0.0</span>/<span style="color: #c00000;">0.0</span>.<span style="color: #c00000;">0.0</span>:<span style="color: #c00000;">2181</span>:NIOServerCnxn<span style="color: #646464;">@1001</span>]&nbsp;-&nbsp;Closed&nbsp;socket&nbsp;connection&nbsp;for&nbsp;client&nbsp;/<span style="color: #c00000;">10.0</span>.<span style="color: #c00000;">0.0</span>:<span style="color: #c00000;">32119</span>&nbsp;(no&nbsp;session&nbsp;established&nbsp;for&nbsp;client)&nbsp;&nbsp;</li></ol></div><span style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">从日志输出的时间来看，秒连秒断，非常诡异。</span><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><h2><a name="t1" style="color: rgb(202, 0, 0);"></a>排查问题</h2><h3><a name="t2" style="color: rgb(202, 0, 0);"></a>用netstat查看网络连接状态</h3><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">到client的服务器上查看连接的状态：</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><div bg_plain"="" style="width: 936.53125px; line-height: 26px;"><div><div><strong>[plain]</strong>&nbsp;<a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="view plain" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="copy" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-position: 0% 0%; background-repeat: no-repeat;">copy</a><a href="https://code.csdn.net/snippets/531402" target="_blank" title="在CODE上查看代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a href="https://code.csdn.net/snippets/531402/fork" target="_blank" title="派生到我的代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 474px; top: 982px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1"><li style="line-height: 18px;">netstat&nbsp;-antp&nbsp;|&nbsp;grep&nbsp;2181&nbsp;&nbsp;</li></ol></div><br style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;" /><span style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">发现有很多TIME_WAIT状态的连接：</span><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><div bg_plain"="" style="width: 936.53125px; line-height: 26px;"><div><div><strong>[plain]</strong>&nbsp;<a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="view plain" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="copy" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-position: 0% 0%; background-repeat: no-repeat;">copy</a><a href="https://code.csdn.net/snippets/531402" target="_blank" title="在CODE上查看代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a href="https://code.csdn.net/snippets/531402/fork" target="_blank" title="派生到我的代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 474px; top: 1121px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1"><li style="line-height: 18px;">tcp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;10.0.0.3:44269&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10.0.1.77:2181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIME_WAIT&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="line-height: 18px;">tcp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;10.0.0.3:43646&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10.0.1.77:2181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIME_WAIT&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="line-height: 18px;">tcp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;10.0.0.3:44184&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10.0.1.77:2181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIME_WAIT&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="line-height: 18px;">tcp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;10.0.0.3:44026&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10.0.1.77:2181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIME_WAIT&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="line-height: 18px;">tcp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;10.0.0.3:43766&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10.0.1.77:2181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIME_WAIT&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;</li></ol></div><br style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;" /><span style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">但是TIME_WAIT状态的连接是看不到进程号的。搜索研究了下netstat的参数，发现没有办法输出TIME_WAIT状态的连接的pid，只好尝试其它的办法。</span><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">再用 jstack -l pid 来查看进程的线程栈，也没有发现什么异常的东东。查看到有几个zookeeper连接的线程，但也是正常状态。<br /></p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">再检查了机器的IO，CPU，内存，也没有异常的情况。</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">没找到什么有用的信息，只好再研究下netstat的参数：<br />发现用 netstat -ae 输出了一些信息：<br /></p><div bg_plain"="" style="width: 936.53125px; line-height: 26px;"><div><div><strong>[plain]</strong>&nbsp;<a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="view plain" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="copy" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-position: 0% 0%; background-repeat: no-repeat;">copy</a><a href="https://code.csdn.net/snippets/531402" target="_blank" title="在CODE上查看代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a href="https://code.csdn.net/snippets/531402/fork" target="_blank" title="派生到我的代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 474px; top: 1436px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1"><li style="line-height: 18px;">tcp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;10.0.0.3:41772&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10.0.1.77:eforward&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIME_WAIT&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="line-height: 18px;">tcp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;10.0.0.3:41412&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10.0.1.77:eforward&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIME_WAIT&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="line-height: 18px;">tcp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;10.0.0.3:24226&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10.0.1.77:2181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIME_WAIT&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="line-height: 18px;">tcp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;10.0.0.3:24623&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10.0.1.77:2181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIME_WAIT&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;</li></ol></div><br style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;" /><span style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">发现user是root。于是以为是非Java应用，在不断地连接zookeeper。于是停止java程序，发现没有TIME_WAIT连接了。</span><br style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;" /><span style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">但是确认是Java应用的问题，于是再重启Java应用，但没有再发现TIME_WAIT情况。很诡异。</span><br style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;" /><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">问题不能重现了，相当的蛋疼。忽然想到线上的应用也许也有这个问题，于是到线下zookeeper服务器上查看了下，果然发现有同样的问题。</p><h3><a name="t3" style="color: rgb(202, 0, 0);"></a>用tcpdump抓包和wireshark分析</h3><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">先用tcpdump来查看下具体的网络连接，发现的确是连接连上再断开。于是先保存成cap文件，再用wireshark来分析：</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><div bg_plain"="" style="width: 936.53125px; line-height: 26px;"><div><div><strong>[plain]</strong>&nbsp;<a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="view plain" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="copy" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-position: 0% 0%; background-repeat: no-repeat;">copy</a><a href="https://code.csdn.net/snippets/531402" target="_blank" title="在CODE上查看代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a href="https://code.csdn.net/snippets/531402/fork" target="_blank" title="派生到我的代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 474px; top: 1733px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1"><li style="line-height: 18px;">tcpdump&nbsp;-vv&nbsp;host&nbsp;192.168.66.27&nbsp;and&nbsp;port&nbsp;2181&nbsp;-w&nbsp;2181.cap&nbsp;&nbsp;</li></ol></div><span style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">但是也没有发现什么有用信息，的确是TCP连接连上，再FIN，ACK连接断开。</span><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><h2><a name="t4" style="color: rgb(202, 0, 0);"></a>查看应用日志，发现Tomcat webcontext没有正常启动</h2><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">没办法了，有两种考虑，一个是用strace，二是用btrace。但是btrace好久没用过了，不太想再去看例子文档。</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">还好，去下btrace之后，先去看了下应用的日志，发现应用报了一些ClassLoader的错误：</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><div bg_plain"="" style="width: 936.53125px; line-height: 26px;"><div><div><strong>[plain]</strong>&nbsp;<a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="view plain" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="copy" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-position: 0% 0%; background-repeat: no-repeat;">copy</a><a href="https://code.csdn.net/snippets/531402" target="_blank" title="在CODE上查看代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a href="https://code.csdn.net/snippets/531402/fork" target="_blank" title="派生到我的代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 474px; top: 1924px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1"><li style="line-height: 18px;">Nov&nbsp;24,&nbsp;2014&nbsp;7:32:43&nbsp;PM&nbsp;org.apache.catalina.loader.WebappClassLoader&nbsp;loadClass&nbsp;&nbsp;</li><li style="line-height: 18px;">INFO:&nbsp;Illegal&nbsp;access:&nbsp;this&nbsp;web&nbsp;application&nbsp;instance&nbsp;has&nbsp;been&nbsp;stopped&nbsp;already.&nbsp;&nbsp;Could&nbsp;not&nbsp;load&nbsp;org.apache.zookeeper.ClientCnxnSocketNIO.&nbsp;&nbsp;The&nbsp;eventual&nbsp;following&nbsp;stack&nbsp;trace&nbsp;is&nbsp;caused&nbsp;by&nbsp;an&nbsp;err&nbsp;&nbsp;</li><li style="line-height: 18px;">or&nbsp;thrown&nbsp;for&nbsp;debugging&nbsp;purposes&nbsp;as&nbsp;well&nbsp;as&nbsp;to&nbsp;attempt&nbsp;to&nbsp;terminate&nbsp;the&nbsp;thread&nbsp;which&nbsp;caused&nbsp;the&nbsp;illegal&nbsp;access,&nbsp;and&nbsp;has&nbsp;no&nbsp;functional&nbsp;impact.&nbsp;&nbsp;</li><li style="line-height: 18px;">java.lang.IllegalStateException&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1564)&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ch.qos.logback.classic.spi.PackagingDataCalculator.loadClass(PackagingDataCalculator.java:198)&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ch.qos.logback.classic.spi.PackagingDataCalculator.bestEffortLoadClass(PackagingDataCalculator.java:226)&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ch.qos.logback.classic.spi.PackagingDataCalculator.computeBySTEP(PackagingDataCalculator.java:132)&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ch.qos.logback.classic.spi.PackagingDataCalculator.populateUncommonFrames(PackagingDataCalculator.java:107)&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ch.qos.logback.classic.spi.PackagingDataCalculator.populateFrames(PackagingDataCalculator.java:99)&nbsp;&nbsp;</li></ol></div><br style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;" /><span style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">因为有经验了，马上知道这个Tomcat因为其它原因webcontext实始化失败退出，然后后面的一些线程继续跑时，会抛出ClassLoader，或者Class not found的异常。</span><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">于是猜想到原因了：</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"><strong>Tomcat webcontext初始化失败，zookeeper的重连线程自动不断重连。</strong></p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">但是为什么重启Tomcat之后，没有重现TIME_WAIT的情况？</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">再折腾了下，发现只有当zookeeper重启后，应用才会出现大量的TIME_WAIT连接。报的是下面这个异常：</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><div bg_plain"="" style="width: 936.53125px; line-height: 26px;"><div><div><strong>[plain]</strong>&nbsp;<a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="view plain" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a href="http://blog.csdn.net/zheng0518/article/details/44943357#" title="copy" style="padding: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-position: 0% 0%; background-repeat: no-repeat;">copy</a><a href="https://code.csdn.net/snippets/531402" target="_blank" title="在CODE上查看代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a href="https://code.csdn.net/snippets/531402/fork" target="_blank" title="派生到我的代码片" style="padding: 1px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 474px; top: 2373px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1"><li style="line-height: 18px;">2014-11-24&nbsp;19:42:44,399&nbsp;[Thread-3-SendThread(192.168.90.147:4181)]&nbsp;WARN&nbsp;&nbsp;org.apache.zookeeper.ClientCnxn&nbsp;-&nbsp;Session&nbsp;0x149c21809731325&nbsp;for&nbsp;server&nbsp;192.168.90.147/192.168.90.147:4181,&nbsp;unexpected&nbsp;error,&nbsp;closing&nbsp;socket&nbsp;connection&nbsp;and&nbsp;attempting&nbsp;reconnect&nbsp;&nbsp;</li><li style="line-height: 18px;">java.lang.NoClassDefFoundError:&nbsp;org/apache/zookeeper/proto/SetWatches&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;org.apache.zookeeper.ClientCnxn$SendThread.primeConnection(ClientCnxn.java:867)&nbsp;~[zookeeper-3.4.5.jar:3.4.5-1392090]&nbsp;&nbsp;</li><li style="line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:352)&nbsp;~[zookeeper-3.4.5.jar:3.4.5-1392090]&nbsp;&nbsp;</li></ol></div><br style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;" /><span style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">这个异常的原因，是某些zookeeper的类没有加载到。</span><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><h2><a name="t5" style="color: rgb(202, 0, 0);"></a>最终原因分析</h2><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">梳理下整个流程：</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><ol style="font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"><li>Tomcat启动，初始化webcontext；</li><li>初始化spring, spring初始某些些bean，这些bean包括了zookeeper的连接相关的bean；</li><li>这时zkClient（独立线程）已经连接上服务器了，但是classloader没有加载到org/apache/zookeeper/proto/SetWatches类；</li><li>spring初始化失败，导致Tomcat webcontext初始化也失败，应用在挂起状态，但zkClient线程还是正常的；</li><li>zookeeper服务器重启，zkClient开始重连，连接上zookeeper服务器；</li><li>zkClient触发watch的一些代码，ClassLoader尝试加载org/apache/zookeeper/proto/SetWatches类，但是发现找不到类，于是抛出异常；</li><li>zkClient捕获到异常，认为重连失败，close掉connection，休眠几秒之后，再次重连；</li></ol><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"></p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">于是出现了zkClient反复重试连接zookeeper服务器，而且都是秒连秒断的情况。</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;"><br /></p><h2><a name="t6" style="color: rgb(202, 0, 0);"></a>总结：</h2><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">这次排查花了不少时间，有个原因是开始没有去查看应用的日志，以为应用的是正常的，而且zookeeper.out的输出日志很多，也有一段时间了。</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">还有线上的应用比较坑爹，活动已经过期很久了，但是程序还是线上跑，也没有人管是否出问题了。</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">所以，主要精力放在各种网络连接状态的获取上。对去查看应用日志比较排斥。</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">还有一个原因是，问题比较诡异，有点难重现，当发现可以重现时，基本已经发现问题所在了。</p><p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; color: #333333; background-color: #ffffff;">排查问题还是要耐心收集信息，再分析判断。</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/427237.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2015-09-10 10:41 <a href="http://www.blogjava.net/xiaomage234/archive/2015/09/10/427237.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>提升tomcat服务器性能的七条经验</title><link>http://www.blogjava.net/xiaomage234/archive/2014/07/29/416280.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Tue, 29 Jul 2014 02:31:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2014/07/29/416280.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/416280.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2014/07/29/416280.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/416280.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/416280.html</trackback:ping><description><![CDATA[from:http://passover.blog.51cto.com/2431658/732629<br /><br /><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">在线上环境中我们是采用了tomcat作为Web服务器，它的处理性能直接关系到用户体验，在平时的工作和学习中，归纳出以下七种调优经验。</p><h1><span style="padding: 0px; margin: 0px; font-size: 22px;">1. 服务器资源</span></h1><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; 服务器所能提供CPU、内存、硬盘的性能对处理能力有决定性影响。<br style="padding: 0px; margin: 0px;" />&nbsp; &nbsp; (1) 对于高并发情况下会有大量的运算，那么CPU的速度会直接影响到处理速度。<br style="padding: 0px; margin: 0px;" />&nbsp; &nbsp; (2) 内存在大量数据处理的情况下，将会有较大的内存容量需求，可以用-Xmx -Xms -XX:MaxPermSize等参数对内存不同功能块进行划分。我们之前就遇到过内存分配不足，导致虚拟机一直处于full GC，从而导致处理能力严重下降。<br style="padding: 0px; margin: 0px;" />&nbsp; &nbsp; (3) 硬盘主要问题就是读写性能，当大量文件进行读写时，磁盘极容易成为性能瓶颈。最好的办法还是利用下面提到的缓存。</p><h1><span style="padding: 0px; margin: 0px; font-size: 22px;">2. 利用缓存和压缩</span></h1><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; 对于静态页面最好是能够缓存起来，这样就不必每次从磁盘上读。这里我们采用了Nginx作为缓存服务器，将图片、css、js文件都进行了缓存，有效的减少了后端tomcat的访问。</p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; 另外，为了能加快网络传输速度，开启gzip压缩也是必不可少的。但考虑到tomcat已经需要处理很多东西了，所以把这个压缩的工作就交给前端的Nginx来完成。可以参考之前写的《<a target="_blank" href="http://passover.blog.51cto.com/2431658/588602" style="padding: 0px; margin: 0px; color: #4b0002; text-decoration: none;">利用nginx加速web访问</a>》。</p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; 除了文本可以用gzip压缩，其实很多图片也可以用图像处理工具预先进行压缩，找到一个平衡点可以让画质损失很小而文件可以减小很多。曾经我就见过一个图片从300多kb压缩到几十kb，自己几乎看不出来区别。</p><h1><span style="padding: 0px; margin: 0px; font-size: 22px;">3. 采用集群</span></h1><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; 单个服务器性能总是有限的，最好的办法自然是实现横向扩展，那么组建tomcat集群是有效提升性能的手段。我们还是采用了Nginx来作为请求分流的服务器，后端多个tomcat共享session来协同工作。可以参考之前写的《<a target="_blank" href="http://passover.blog.51cto.com/2431658/648182" style="padding: 0px; margin: 0px; color: #4b0002; text-decoration: none;">利用nginx+tomcat+memcached组建web服务器负载均衡</a>》。</p><h1><span style="padding: 0px; margin: 0px; font-size: 22px;">4. 优化tomcat参数</span></h1><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; 这里以tomcat7的参数配置为例，需要修改conf/server.xml文件，主要是优化连接配置，关闭客户端dns查询。</p><pre style="padding: 0px; margin-top: 0px; font-family: 'Courier New', monospace; font-size: 12px; overflow: auto; width: 712.796875px; color: #555555; background: #f7f7f7;"><ol style="padding: 5px 0px; margin: 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; border-left-width: 3px; border-style: none none none solid; border-left-color: #146b00; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: #006699; font-weight: bold; background-color: inherit;">&lt;</span><span style="padding: 0px; margin: 0px; border: none; color: #006699; font-weight: bold; background-color: inherit;">Connector</span>&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">port</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"8080"</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">protocol</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"org.apache.coyote.http11.Http11NioProtocol"</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">connectionTimeout</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"20000"</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;&nbsp;</span></span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">redirectPort</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"8443"</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">maxThreads</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"500"</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">minSpareThreads</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"20"</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">acceptCount</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"100"</span>&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">disableUploadTimeout</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"true"</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">enableLookups</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"false"</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">URIEncoding</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">=</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">"UTF-8"</span>&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; font-weight: bold; background-color: inherit;">/&gt;</span>&nbsp;</span></li></ol></pre><h1><span style="padding: 0px; margin: 0px; font-size: 22px;">5. 改用APR库</span></h1><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; tomcat默认采用的BIO模型，在几百并发下性能会有很严重的下降。tomcat自带还有NIO的模型，另外也可以调用APR的库来实现操作系统级别控制。</p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; NIO模型是内置的，调用很方便，只需要将上面配置文件中protocol修改成<span style="padding: 0px; margin: 0px; font-family: 宋体;"><span style="padding: 0px; margin: 0px; font-size: 16px;"><span style="padding: 0px; margin: 0px; line-height: 18px;">org.apache.coyote.http11.Http11NioProtocol，重启即可生效。上面配置我已经改过了，默认的是HTTP/1.1。</span></span></span></p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; APR则需要安装第三方库，在高并发下会让性能有明显提升。具体安装办法可以参考<a href="http://www.cnblogs.com/huangjingzhou/articles/2097241.html" style="padding: 0px; margin: 0px; color: #4b0002; text-decoration: none;">http://www.cnblogs.com/huangjingzhou/articles/2097241.html</a>。安装完成后<span style="padding: 0px; margin: 0px; font-size: 16px; line-height: 18px; font-family: 宋体;">重启即可生效。如使用默认protocal就是apr，但最好把将protocol修改成<span style="padding: 0px; margin: 0px; font-size: 14px; line-height: 25px; font-family: Helvetica, Tahoma, Arial, sans-serif; background-color: #efefef;">org.apache.coyote.http11.Http11AprProtocol，会更加明确</span></span><span style="padding: 0px; margin: 0px; line-height: 25px; font-family: Helvetica, Tahoma, Arial, sans-serif; background-color: #efefef;">。</span></p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; 在官方找到一个表格详细说明了这三种方式的区别：</p><pre style="padding: 0px; margin-top: 0px; font-family: 'Courier New', monospace; font-size: 12px; overflow: auto; width: 712.796875px; color: #555555; background: #f7f7f7;"><ol style="padding: 5px 0px; margin: 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; border-left-width: 3px; border-style: none none none solid; border-left-color: #146b00; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Java&nbsp;Blocking&nbsp;Connector&nbsp;&nbsp;&nbsp;Java&nbsp;Nio&nbsp;Blocking&nbsp;Connector&nbsp;&nbsp;&nbsp;APR/native&nbsp;Connector&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BIO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NIO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;APR&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">Classname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AjpProtocol&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AjpNioProtocol&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AjpAprProtocol&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">Tomcat&nbsp;Version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.x&nbsp;onwards&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7.x&nbsp;onwards&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.5.x&nbsp;onwards&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">Support&nbsp;Polling&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YES&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">Polling&nbsp;Size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N/A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxConnections&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxConnections&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">Read&nbsp;Request&nbsp;Headers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Blocking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sim&nbsp;Blocking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Blocking&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">Read&nbsp;Request&nbsp;Body&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Blocking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sim&nbsp;Blocking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Blocking&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">Write&nbsp;Response&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Blocking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sim&nbsp;Blocking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Blocking&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">Wait&nbsp;for&nbsp;next&nbsp;Request&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Blocking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Non&nbsp;Blocking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Non&nbsp;Blocking&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">Max&nbsp;Connections&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxConnections&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxConnections&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxConnections&nbsp;</span></li></ol></pre><h1><span style="padding: 0px; margin: 0px; font-size: 22px;">6. 优化网络</span></h1><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; Joel也明确提出了优化网卡驱动可以有效提升性能，这个对于集群环境工作的时候尤为重要。由于我们采用了linux服务器，所以优化内核参数也是一个非常重要的工作。给一个参考的优化参数：</p><pre style="padding: 0px; margin-top: 0px; font-family: 'Courier New', monospace; font-size: 12px; overflow: auto; width: 712.796875px; color: #555555; background: #f7f7f7;"><ol style="padding: 5px 0px; margin: 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; border-left-width: 3px; border-style: none none none solid; border-left-color: #146b00; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">1.&nbsp;修改/etc/sysctl.cnf文件，在最后追加如下内容：&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.core.netdev_max_backlog</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">32768</span>&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.core.somaxconn</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">32768</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.core.wmem_default</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">8388608</span>&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.core.rmem_default</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">8388608</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.core.rmem_max</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">16777216</span>&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.core.wmem_max</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">16777216</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.ip_local_port_range</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">1024</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;65000&nbsp;</span></span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.route.gc_timeout</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">100</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_fin_timeout</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">30</span>&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_keepalive_time</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">1200</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_timestamps</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">0</span>&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_synack_retries</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">2</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_syn_retries</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">2</span>&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_tw_recycle</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">1</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_tw_reuse</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">1</span>&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_mem</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">94500000</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;915000000&nbsp;927000000&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_max_orphans</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">3276800</span>&nbsp;</span></li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;"><span style="padding: 0px; margin: 0px; border: none; color: red; background-color: inherit;">net.ipv4.tcp_max_syn_backlog</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: blue; background-color: inherit;">65536</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; color: inherit; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none;">&nbsp;</li><li style="margin-left: 20px; line-height: 1.3em; list-style: decimal-leading-zero outside; border: none; padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important;"><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit;">2.&nbsp;保存退出，执行sysctl&nbsp;-p生效&nbsp;</span></li></ol></pre><h1><span style="padding: 0px; margin: 0px; font-size: 22px;">7. 让测试说话</span></h1><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp; &nbsp; 优化系统最忌讳的就是只调优不测试，有时不适当的优化反而会让性能更低。以上所有的优化方法都要在本地进行性能测试过后再不断调整参数，这样最终才能达到最佳的优化效果。</p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp;</p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">补充Bio、Nio、Apr模式的测试结果：</p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 对于这几种模式，我用ab命令模拟1000并发测试10000词，测试结果比较意外，为了确认结果，我每种方式反复测试了10多次，并且在两个服务器上都测试了一遍。结果发现Bio和Nio性能差别非常微弱，难怪默认居然还是Bio。但是采用apr，连接建立的速度会有50%～100%的提升。直接调用操作系统层果然神速啊，这里强烈推荐apr方式！</p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp;</p><p style="padding: 0px; margin: 0px; clear: both; height: auto; overflow: hidden; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">参考资料：<br style="padding: 0px; margin: 0px;" /><a href="http://16.199.geisvps.com/bbs/2836/24238.html" style="padding: 0px; margin: 0px; color: #4b0002; text-decoration: none;">http://16.199.geisvps.com/bbs/2836/24238.html</a></p><img src ="http://www.blogjava.net/xiaomage234/aggbug/416280.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2014-07-29 10:31 <a href="http://www.blogjava.net/xiaomage234/archive/2014/07/29/416280.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Tomcat7调试运行环境搭建与源代码分析入门</title><link>http://www.blogjava.net/xiaomage234/archive/2013/08/21/403124.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Wed, 21 Aug 2013 04:30:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2013/08/21/403124.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/403124.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2013/08/21/403124.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/403124.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/403124.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 1. 需要准备好下面这些工具JDK 1.6+Maven 2或3TortoiseSVN 1.7+ (从1.7开始&#8221;.svn&#8221;目录集中放在一处了，不再每个目录下都放一份)Eclipse 3.5+这4个工具不在这里描述怎么配置了，如果你是有两三年开发经验的Java开发人员，正常来讲都一直在用了。另外，分析tomcat源代码不需要对这4个工具做什么特殊配置。2. 下载Tomcat的...&nbsp;&nbsp;<a href='http://www.blogjava.net/xiaomage234/archive/2013/08/21/403124.html'>阅读全文</a><img src ="http://www.blogjava.net/xiaomage234/aggbug/403124.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2013-08-21 12:30 <a href="http://www.blogjava.net/xiaomage234/archive/2013/08/21/403124.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>下载Tomcat源码，并作为工程导入到Eclipse中 </title><link>http://www.blogjava.net/xiaomage234/archive/2013/08/21/403118.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Wed, 21 Aug 2013 02:41:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2013/08/21/403118.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/403118.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2013/08/21/403118.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/403118.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/403118.html</trackback:ping><description><![CDATA[<div style="color: #464646; font-family: simsun; background-color: #7798a3;">导入Tomcat的源代码，其实很简单，可是如果没有找到正确的方法，你会浪费大量的时间，我就是浪费了好多时间在配置编译环境上。最正确的办法是到Tomcat的官方网址去找到答案。</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;"></div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">可以参考下边的连接，这个是关于Tomcat7的。</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">http://tomcat.apache.org/tomcat-7.0-doc/building.html</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;"></div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">1. 设置环境</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;1.1 JDK1.6 or above &nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;JAVA_HOME</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;1.2 Ant 1.8.1 or later &nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>ANT_HOME</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr></div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">2.&nbsp;<wbr>Check out the latest tomcat source code</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">svn --force export http://svn.apache.org/repos/asf/tomcat/tc7.0.x/tags/TOMCAT_7_0_11/ D:\Tomcat7\</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">上边的命令SVN会把最新的Tomcat7011的源码下载到你的硬盘D:\Tomcat7\ 下<br />SVN 下载地址&nbsp;http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr></div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">3. 为Eclipse编译工程</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;cd ${tomcat.source}</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;ant&nbsp;ide-eclipse</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>[注]&nbsp;a.&nbsp;${tomcat.source}&nbsp;是你下载的源码在机器上的路径</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;b. ant ide-eclipse: 这个过程会比较慢，因为会下载一些依赖的第三方jar，这些jar都会放到你在</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>build.properties 中设置的&nbsp;${base.path}&nbsp;所指向的路径</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;"></div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">4. 设置Eclipse 环境 (Class-path Variables)</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;设置TOMCAT_LIBS_BASE，ANT_HOME</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;TOMCAT_LIBS_BASE 要指向&nbsp;${base.path}</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;ANT_HOME 指向安装ant的目录就好了</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;"></div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">5. 导入工程</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;File -&gt; Import and choose Existing Projects into Workspace</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;"></div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">6. 编译问题</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;往往我们在这时会遇到一些编译错误，这是因为我们Eclipse中JRE环境设置的不正确，需要调整</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;6.1 Java Compiler, 需要 6.0 (1.6) 以上</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;Window -&gt; Preferences -&gt; Java -&gt;&nbsp;<wbr>Complier -&gt; JDK Compliance -&gt; 6.0 (1.6)</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;6.2 Default JRE, 需要 1.6 以上</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;Window -&gt; Preferences -&gt; Java -&gt; Installed JREs -&gt; jdk1.6</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;当然，这个也可以在工程中设定，如果你还有其他的工程必须用以前的jdk版本的话。</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;6.3 Eclipse 版本问题</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;调整好Java Compiler 和 JRE 后，一般可以编译通过了，但是我就遇到了Eclipse版本问题，总有一个&nbsp;<wbr></div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;compiler ArrayIndexOutofBoundaryE<wbr>xception 的错误，后来更新了一下Eclipse,就好了，我用的</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;Eclipse 版本是3.2的。我又用了Eclipse Galileo试着打开工程，就没有类似的问题。</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;"></div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">7. 最周到的服务</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;到了这里，您的工程应该就编译成功了。该想想怎么run, debug TOMCAT了，怎么设置啊？哈，最周到的服务</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;来了，在 run(debug) ... -&gt; Java Application 中，我们会看到已经有两个applications 了，分别叫做</div><div style="color: #464646; font-family: simsun; background-color: #7798a3;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;start-tomcat, stop-tomcat.</div><img src ="http://www.blogjava.net/xiaomage234/aggbug/403118.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2013-08-21 10:41 <a href="http://www.blogjava.net/xiaomage234/archive/2013/08/21/403118.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>