文章列表

北京公积金重磅新政!认房又认贷 每缴1年可贷10万

作者:微信小助手

<p style="text-align: justify;"><img class="" data-copyright="0" data-ratio="0.209" src="/upload/15fffa02d2a1946352a5922e61ee42d0.gif" data-type="gif" data-w="1000" style=""></p> <p><br></p> <p style="text-align: justify;"><strong><span style="font-size: 18px;color: rgb(123, 12, 0);">刚刚,北京住房公积金管理中心发布重磅政策,将彻底改变未来市管公积金的提取和贷款!</span></strong></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">《关于调整住房公积金个人住房贷款政策的通知》、《关于落实放管服、优化营商环境提升住房公积金归集服务水平的通知》等通知中的政策,将</span><span style="color: rgb(123, 12, 0);"><strong><span style="font-size: 18px;">从9月17日(即下周一)起正式施行</span></strong></span>。</p> <p style="text-align: justify;"><br></p> <p style="text-align: justify;"><strong style="white-space: normal;"><span style="font-size: 15px;">中原</span></strong><span style="font-size: 15px;"><strong>地产</strong></span><strong style="white-space: normal;"><span style="font-size: 15px;">首席分析师张大伟分析认为,</span></strong><span style="color: rgb(123, 12, 0);font-size: 18px;"><strong>北京公积金“认房又认贷”政策对刚需影响非常大,特别是年轻人。</strong></span><strong style="white-space: normal;"><span style="font-size: 15px;">北京公积金占交易比例不高、平均在20%左右,这个政策影响下预计起码一半需求将只能选择商业贷款或者全款,这个政策对刚需影响非常大,特别是年轻人,本来公积金贷款额度就很少,按照这个政策,</span></strong><span style="font-size: 18px;"><span style="color: rgb(123, 12, 0);"><strong>公积金将基本离开刚需</strong></span><strong>。</strong></span><strong style="white-space: normal;"><span style="font-size: 15px;">而从认房又认贷看公积金政策全面收紧。</span></strong></p> <p><br></p> <p style="text-align: center;"><span style="font-size: 15px;"><strong><span style="background-color: rgb(136, 136, 136);color: rgb(255, 251, 0);">&nbsp;血拼哥划重点&nbsp;</span></strong></span></p> <p><br></p> <p style="text-align: center;"><span style="font-size: 24px;background-color: rgb(123, 12, 0);color: rgb(255, 255, 255);"><strong>认房又认贷</strong></span></p> <p><br></p> <p style="text-align: center;"><span style="font-size: 24px;background-color: rgb(123, 12, 0);color: rgb(255, 255, 255);"><strong>每缴1年可贷10万</strong></span></p> <p><br></p> <p style="text-align: center;"><span style="font-size: 24px;background-color: rgb(123, 12, 0);color: rgb(255, 255, 255);"><strong>只能买户籍地或户籍所在省会</strong></span></p> <p><span style="font-size: 15px;"></span><br></p> <p style="text-align: center;"><span style="font-size: 24px;background-color: rgb(123, 12, 0);color: rgb(255, 255, 255);"><strong>贷款年限不得超65岁</strong></span></p> <p style="text-align: justify;"><span style="font-size: 24px;color: rgb(123, 12, 0);"><strong><br></strong></span></p> <blockquote> <p style="text-align: justify;"><span style="font-size: 20px;color: rgb(123, 12, 0);"><strong>缴存超11年 才可贷120万</strong></span></p> </blockquote> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">之前公积金贷款额度最高是120万元,缴存时间的长短并不会影响额度的多少。新政将实行贷款额度与借款申请人住房公积金的缴存年限挂钩——</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>每缴存一年可贷10万元,最高可贷120万元</strong></span><span style="font-size: 15px;">。</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">计算缴存年限时,包含几种特殊情况。</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">缴存年限不够1整年的,按1整年计算。</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">如果缴存期限是13个月,也会算为两年,可贷20万元。</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">此外,如借款申请人为已婚的,核算贷款额度不会相加得出,而是以夫妻双方中缴存年限较长的一方计算。</span></p> <p style="text-align: justify;"><span style="font-size: 15px;"><br></span></p> <blockquote> <p style="text-align: justify;"><span style="font-size: 20px;color: rgb(123, 12, 0);"><strong>“二套房”认房认贷 最高仅可贷60万</strong></span></p> </blockquote> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">从下周开始,对于“二套房”,北京公积金个人贷款变成</span><span style="color: rgb(123, 12, 0);font-size: 16px;"><strong>与商贷一样的“认房又认贷”</strong></span><span style="font-size: 15px;">。</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">“房”,即借款申请人在北京名下有住房。</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">“贷”,指在人行征信系统中全国范围内的个人住房贷款记录,包括商业性住房贷款记录、住房公积金个人住房贷款记录。</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">也就是说,借款申请人如果名下无住房贷款记录且在本市无住房的,按首套房贷款政策办理;凡不属于首套房情形,被核定为是二套房的,按二套房贷款政策办理;如果被核定为有两套及以上住房的,也不予贷款。</span></p> <p><br></p> <p style="text-align: justify;"><img class="" data-ratio="0.44716692189892804" data-s="300,640" src="/upload/ce18718a8f5076d677a85b53d2d3714d.png" data-type="png" data-w="653" style=""></p> <p style="text-align: justify;"><span style="font-size: 15px;"><br></span></p> <p style="text-align: justify;"><span style="font-size: 15px;">同时,新政下调了二套房贷款最高贷款额度,由80万元</span><span style="color: rgb(123, 12, 0);font-size: 18px;"><strong>下调为60万元</strong></span><span style="font-size: 15px;">。如果“认房又认贷”后被判定为“二套房”,将面临贷款额度下降、首付比增加。</span></p> <p style="text-align: justify;"><span style="font-size: 18px;"><strong><br></strong></span></p> <blockquote> <p style="text-align: justify;"><span style="font-size: 20px;color: rgb(123, 12, 0);"><strong>首付比统一20%“<span style="font-size: 20px;text-align: justify;">一去不复返</span>”</strong></span></p> </blockquote> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">与去年“3.17新政”类似,不同房屋性质,<span style="font-size: 15px;text-align: justify;">将采用不同的公积金贷款购房首付款比例</span>:<br></span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">购买</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>经济适用住房</strong></span><span style="font-size: 15px;">的,首付款比例</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>不低于20%</strong></span><span style="font-size: 15px;">;</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">购买</span><span style="color: rgb(123, 12, 0);"><strong><span style="font-size: 15px;">共有产权等政策性住房的首套住房</span></strong></span><span style="font-size: 15px;">,首付款比例</span><strong><span style="font-size: 15px;color: rgb(123, 12, 0);">不低于30%</span></strong><span style="font-size: 15px;">;</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">购买政策性住房之外的</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>首套普通自住房</strong></span><span style="font-size: 15px;">,首付款比例</span><strong><span style="font-size: 15px;color: rgb(123, 12, 0);">不低于35%</span></strong><span style="font-size: 15px;">;</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">购买政策性住房之外的</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>首套非普通自住房</strong></span><span style="font-size: 15px;">,首付款比例</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>不低于40%</strong></span><span style="font-size: 15px;">;</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">购买</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>普通自住房且为第二套住房</strong></span><span style="font-size: 15px;">,首付款比例</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>不低于60%</strong></span><span style="font-size: 15px;">;</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">购买</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>非普通自住房且为第二套住房</strong></span><span style="font-size: 15px;">,首付款比例</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>不低于80%</strong></span><span style="font-size: 15px;">。</span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">对于普通自住房和非普通自住房的认定标准,将按照《关于调整本市享受税收优惠政策普通住房平均交易价格有关问题的通知》标准执行。一般来说,普通自住房是指容积率在1.0(含)以上、单套建筑面积在140平方米(含)以下,并参考其实际成交价格确定。</span></p> <p style="text-align: justify;"><span style="font-size: 15px;"><br></span></p> <blockquote> <p style="text-align: justify;"><span style="color: rgb(123, 12, 0);"><strong><span style="color: rgb(123, 12, 0);font-size: 20px;">贷款年限不得超过65岁</span></strong></span></p> </blockquote> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">此前,公积金个人贷款的贷款期限最长可以计算到借款申请人70周岁调整;而今则调整为原则上最高不得超过65周岁。如借款申请人为已婚的,贷款期限以夫妻双方中较长的一方计算。</span></p> <p style="text-align: justify;"><span style="font-size: 15px;"><br></span></p> <blockquote> <p><span style="font-size: 20px;color: rgb(123, 12, 0);"><strong>只能买户籍地或户籍所在省会</strong></span></p> </blockquote> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">新政将规范购房提取业务,不再是购买任何城市的住房都能在北京提取公积金。除了购买位于北京市行政区域内的住房,</span><span style="font-size: 15px;color: rgb(123, 12, 0);"><strong>提取公积金仅限于购买本人及配偶身份证、户口簿记载的县、市或户籍所在地的省会城市内的住房</strong></span><span style="font-size: 15px;">。</span></p> <p><br></p> <p><span style="background-color: rgb(178, 178, 178);color: rgb(255, 251, 0);"><strong><span style="background-color: rgb(178, 178, 178);font-size: 16px;">举例</span></strong></span></p> <p><br></p> <p style="text-align: justify;"><span style="background-color: rgb(178, 178, 178);color: rgb(255, 251, 0);"><strong><span style="color: rgb(255, 251, 0);background-color: rgb(178, 178, 178);font-size: 16px;">假如户籍在河北省张家口市,只有在购买北京住房、张家口住房、石家庄住房,才能提取公积金;购买北三县等廊坊住房时,则无法提取公积金。</span></strong></span></p> <p><br></p> <p style="text-align: justify;"><span style="font-size: 15px;">另外,如果住房公积金缴存人及配偶在北京市行政区域内有购房提取记录的,不能办理异地购房提取。</span></p> <p style="text-align: center;"><br></p> <p style="text-align: center;"><img class="" data-copyright="0" data-ratio="3.144" data-s="300,640" src="/upload/ddfb6e80099fe46f018530ebc6e2402d.jpg" data-type="jpeg" data-w="1000" style="text-align: center;white-space: normal;"></p> <p style="text-align: justify;"><br></p> <p style="text-align: center;"><span style="font-size: 15px;text-decoration: underline;">下附《<span style="font-size: 15px;text-decoration: underline;text-align: justify;">关于调整住房公积金个人住房贷款政策的通知</span>》原文</span></p> <p style="text-align: center;"><img class="" data-ratio="1.2340909090909091" data-s="300,640" src="/upload/22061a29ae9091b30c27ca353152d61b.png" data-type="png" data-w="440" style=""></p> <p style="text-align: center;"><img class="" data-ratio="1.225531914893617" data-s="300,640" src="/upload/d0c6952d00bcbb702f75e4ebb3bc9d8f.png" data-type="png" data-w="470" style=""></p> <p style="text-align: center;"><img class="" data-ratio="1.3018018018018018" data-s="300,640" src="/upload/5e43697ffaac4b308adcc53029cffbb0.png" data-type="png" data-w="444" style=""></p> <p style="text-align: center;"><img class="" data-ratio="1.3190045248868778" data-s="300,640" src="/upload/be1191af8a203ed54f79c416495c67bc.png" data-type="png" data-w="442" style=""></p> <p style="text-align: center;"><img class="" data-ratio="1.2806236080178173" data-s="300,640" src="/upload/ec6047be63edcdc87e8c5d5d66c15c23.png" data-type="png" data-w="449" style=""></p> <p style="text-align: center;"><img class="" data-ratio="1.1903669724770642" data-s="300,640" src="/upload/1a505c05fd712e7e45cf64159ed68b3a.png" data-type="png" data-w="436" style=""></p> <p style="padding-right: 1em;padding-left: 1em;max-width: 100%;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 17px;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;widows: 1;text-align: center;line-height: 1.75em;letter-spacing: 1px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;word-wrap: break-word !important;">&nbsp;</span></p> <section class="" data-style-type="1" data-tools="新媒体排版" data-id="12341" style="max-width: 100%;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" data-css="border-color: initial;border-style: none;border-width: 0px;padding: 0px" style="max-width: 100%;border-color: initial;border-style: none;border-width: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" powered-by="KolEditor.us" style="max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;"> <section class="" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;"> <section class="" style="padding-top: 1.1em;max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box;display: inline-block;vertical-align: top;word-wrap: break-word !important;"> <section class="" data-css="background-color: rgb(249, 110, 87);border-radius: 0px 0.5em 0.5em 0px;box-sizing: border-box;color: rgb(255, 255, 255);padding: 0.2em 0.4em" style="padding: 0.2em 0.4em;max-width: 100%;box-sizing: border-box;background-color: rgb(179, 1, 18);border-radius: 0px 0.5em 0.5em 0px;color: rgb(255, 255, 255);word-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;"> <strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 18px;box-sizing: border-box !important;word-wrap: break-word !important;">小助理有话说</span></strong> </section> </section> <section style="max-width: 100%;box-sizing: border-box;width: 0px;border-right: 4px solid rgb(249, 110, 87);border-top: 4px solid rgb(249, 110, 87);word-wrap: break-word !important;border-left: 4px solid transparent !important;border-bottom: 4px solid transparent !important;"></section> </section> <section class="" data-css="box-sizing: border-box;color: rgb(0, 0, 0);display: inline-block;font-size: 20.8px;padding-left: 10px;vertical-align: top" style="padding-left: 10px;max-width: 100%;box-sizing: border-box;display: inline-block;font-size: 20.8px;vertical-align: top;word-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;"> <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> </section> </section> <section class="" data-css="border-color: rgb(204, 204, 204);border-style: solid;border-width: 1px;box-sizing: border-box;margin-left: 8px;margin-top: -3.5em;padding: 3.5em 10px 10px" style="margin-top: -3.5em;margin-left: 8px;padding: 3.5em 10px 10px;max-width: 100%;box-sizing: border-box;border-color: rgb(204, 204, 204);border-style: solid;border-width: 1px;word-wrap: break-word !important;"> <section class="" powered-by="KolEditor.us" style="max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;"> <p data-css="box-sizing: border-box !important;color: rgb(51, 51, 51);font-size: 14px;letter-spacing: 1px;line-height: 1.75em;margin-bottom: 10px;margin-top: 10px;max-width: 100%;min-height: 1em;padding-left: 1em;padding-right: 1em;text-align: justify;white-space: normal;word-wrap: break-word !important" style="margin-top: 10px;margin-bottom: 10px;padding-right: 1em;padding-left: 1em;max-width: 100%;min-height: 1em;font-size: 14px;letter-spacing: 1px;line-height: 1.75em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">想了解</span><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span data-css="box-sizing: border-box !important;color: rgb(123, 12, 0);max-width: 100%;word-wrap: break-word !important" style="max-width: 100%;color: rgb(179, 1, 18);box-sizing: border-box !important;word-wrap: break-word !important;">【</span></strong><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span data-css="box-sizing: border-box !important;color: rgb(123, 12, 0);max-width: 100%;word-wrap: break-word !important" style="max-width: 100%;color: rgb(179, 1, 18);box-sizing: border-box !important;word-wrap: break-word !important;">共有产权房】</span></strong></span><span style="max-width: 100%;font-size: 15px;color: rgb(0, 0, 0);box-sizing: border-box !important;word-wrap: break-word !important;">或</span><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span data-css="box-sizing: border-box !important;color: rgb(123, 12, 0);max-width: 100%;word-wrap: break-word !important" style="max-width: 100%;color: rgb(179, 1, 18);box-sizing: border-box !important;word-wrap: break-word !important;">【<strong style="max-width: 100%;color: rgb(51, 51, 51);box-sizing: border-box !important;word-wrap: break-word !important;"><span data-css="box-sizing: border-box !important;color: rgb(123, 12, 0);max-width: 100%;word-wrap: break-word !important" style="max-width: 100%;color: rgb(179, 1, 18);box-sizing: border-box !important;word-wrap: break-word !important;">限竞房】</span></strong></span></strong><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">请加</span></span><span data-css="color: rgb(123, 12, 0);max-width: 100%" style="max-width: 100%;color: rgb(179, 1, 18);box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">“血拼哥小助理”微信(ID:msxuepin2)</strong></span><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">或扫描下方二维码,</span></span><span data-css="box-sizing: border-box !important;color: rgb(123, 12, 0);font-size: 15px;max-width: 100%;word-wrap: break-word !important" style="max-width: 100%;color: rgb(179, 1, 18);font-size: 15px;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">备注想加的区域(</strong></span><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span data-css="box-sizing: border-box !important;color: rgb(123, 12, 0);max-width: 100%;word-wrap: break-word !important" style="max-width: 100%;color: rgb(179, 1, 18);box-sizing: border-box !important;word-wrap: break-word !important;">如“海淀 共产房”、“限竞房”)</span></strong><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">,</span>不加备注或备注不符合要求一律不加!</span></span></p> <p data-css="box-sizing: border-box !important;color: rgb(51, 51, 51);font-size: 14px;letter-spacing: 1px;line-height: 1.75em;margin-bottom: 10px;margin-top: 10px;max-width: 100%;min-height: 1em;padding-left: 1em;padding-right: 1em;text-align: justify;white-space: normal;word-wrap: break-word !important" style="margin-top: 10px;margin-bottom: 10px;padding-right: 1em;padding-left: 1em;max-width: 100%;min-height: 1em;font-size: 14px;letter-spacing: 1px;line-height: 1.75em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;word-wrap: break-word !important;">由于小伙伴们加群热情太高涨,本助理忙到天昏地暗,目前</span><span data-css="color: rgb(123, 12, 0)" style="max-width: 100%;color: rgb(179, 1, 18);box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;word-wrap: break-word !important;">每天集中加群一次</strong></span><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;word-wrap: break-word !important;">,麻烦各位伙伴耐心等待噢,比心~</span></p> </section> </section> </section> </section> </section> </section> </section> </section> </section> <p style="max-width: 100%;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"><img class="" data-croporisrc="/upload/d0a9cdd2852828050830e5a48498fcf2.jpg" data-cropx1="22" data-cropx2="410" data-cropy1="21" data-cropy2="408" data-ratio="0.9974226804123711" data-s="300,640" src="https://mmbiz.qpic.cn/mmbiz_jpg/btyVNxus4qqfhxSw09NdMAMXWRJhrY8OL3cXfhLDN6zOU7LfgHT6qTsKqicTQnb33JLiaelIr40nuy3ib1JhTB5jA/640?wx_fmt=jpeg" data-type="jpeg" data-w="388" style="font-size: 14px;box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: 221px !important;" width="315.594px"></p> <section class="" powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);word-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;"> <section class="" style="padding-right: 10px;padding-left: 10px;max-width: 100%;box-sizing: border-box;font-size: 14px;word-wrap: break-word !important;"> <p style="margin-top: 20px;margin-bottom: 20px;padding-right: 1em;padding-left: 1em;max-width: 100%;min-height: 1em;text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;color: rgb(192, 0, 0);font-size: 15px;font-family: Helvetica, Arial, sans-serif;text-indent: 0em;box-sizing: border-box !important;word-wrap: break-word !important;">右下角,您点一个<strong style="max-width: 100%;text-indent: 0em;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;white-space: pre-wrap;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;color: rgb(255, 0, 0);font-family: Arial, sans-serif;line-height: 28px;white-space: normal;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(192, 0, 0);box-sizing: border-box !important;word-wrap: break-word !important;"><img class="__bg_gif" data-ratio="1" src="/upload/dd153ab34eb13de7d968f500ef94e526.gif" data-type="gif" data-w="19" style="color: rgb(62, 62, 62);font-size: 16px;vertical-align: top;box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: 19px !important;" title="跳动的心" width="auto"></span></strong></strong></strong></strong></strong></strong></strong></strong></strong></p> </section> </section> </section> <p style="margin-top: 20px;margin-bottom: 20px;padding-right: 1em;padding-left: 1em;max-width: 100%;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;color: rgb(192, 0, 0);font-size: 15px;font-family: Helvetica, Arial, sans-serif;text-indent: 0em;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;text-indent: 0em;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;white-space: pre-wrap;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;color: rgb(255, 0, 0);font-family: Arial, sans-serif;line-height: 28px;white-space: normal;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(192, 0, 0);box-sizing: border-box !important;word-wrap: break-word !important;">小助理工资涨5毛~</span></strong></strong></strong></strong></strong></strong></strong></strong></strong></p> <section class="" powered-by="xiumi.us" style="max-width: 100%;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <section style="padding-right: 10px;padding-left: 10px;max-width: 538px;box-sizing: border-box;word-wrap: break-word;line-height: 25.6px;border-radius: 0px;border-width: 1px;border-style: dashed;border-color: rgb(62, 62, 62);width: 538px;vertical-align: top;display: inline-block;"> <section style="max-width: 516px;box-sizing: border-box;word-wrap: break-word;color: rgb(62, 62, 62);line-height: 25.6px;"> <section style="margin-top: 10px;margin-bottom: 10px;max-width: 516px;box-sizing: border-box;word-wrap: break-word;"> <section style="max-width: 516px;box-sizing: border-box;word-wrap: break-word;border-bottom-left-radius: 0px;border-style: solid;border-width: 0px 0px 0px 10px;border-left-color: rgb(144, 215, 236);border-right-color: rgb(46, 172, 109);box-shadow: rgb(0, 0, 0) 0px 0px 0px;display: inline-block;width: 516px;"> <section style="max-width: 506px;box-sizing: border-box;word-wrap: break-word;"> <section style="padding-right: 3px;padding-left: 3px;max-width: 506px;box-sizing: border-box;word-wrap: break-word;"> <section style="margin-top: 15.2px;margin-bottom: -10.4px;max-width: 500px;box-sizing: border-box;word-wrap: break-word;display: inline-block;vertical-align: top;width: 500px;"> <section style="margin-top: -16px;padding: 3px 10px;max-width: 500px;box-sizing: border-box;word-wrap: break-word;color: rgb(0, 154, 68);display: inline-block;vertical-align: top;background-color: rgb(254, 255, 255);"> <p style="max-width: 64px;box-sizing: border-box;word-wrap: break-word;min-height: 16px;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-family: 宋体;color: rgb(123, 12, 0);box-sizing: border-box !important;word-wrap: break-word !important;">往期回顾</span></strong></span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p> </section> </section> </section> </section> </section> </section> </section> <section style="max-width: 516px;box-sizing: border-box;word-wrap: break-word;line-height: 25.6px;"> <section style="max-width: 516px;box-sizing: border-box;word-wrap: break-word;"> <section style="max-width: 516px;box-sizing: border-box;word-wrap: break-word;"> <p style="margin-bottom: 0.1px;max-width: 516px;box-sizing: border-box;word-wrap: break-word;min-height: 16px;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 15px;color: rgb(123, 12, 0);">共有产权丨</span><span style="max-width: 100%;font-size: 15px;color: rgb(61, 170, 214);box-sizing: border-box !important;word-wrap: break-word !important;"><a href="https://mp.weixin.qq.com/s?__biz=MzA5MzQyMjIwMA==&amp;mid=2652721596&amp;idx=1&amp;sn=57f9ef435bda9381cd172f97e8980266&amp;chksm=8bb7e6abbcc06fbd43605af8af14a6ca60265613ea8b45696553c2dec1613eca1e96de582c90&amp;scene=21#wechat_redirect" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(61, 170, 214);box-sizing: border-box !important;word-wrap: break-word !important;">“纯种”共产房 VS “杂交”共产房</span></a></span></p> <p style="margin-bottom: 0.1px;max-width: 516px;box-sizing: border-box;word-wrap: break-word;min-height: 16px;"><span style="max-width: 100%;font-size: 15px;color: rgb(123, 12, 0);box-sizing: border-box !important;word-wrap: break-word !important;">租房故事丨</span><a href="http://mp.weixin.qq.com/s?__biz=MzA5MzQyMjIwMA==&amp;mid=2652723173&amp;idx=1&amp;sn=4b6159758fb383091ba40109874c8e73&amp;chksm=8bb7e0f2bcc069e40e48983b4efc83ccac2b9afe13323dffe9ad4ae74c73f43c03bc09e69c6d&amp;scene=21#wechat_redirect" target="_blank" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(61, 170, 214);box-sizing: border-box !important;word-wrap: break-word !important;">“媳妇内裤被人偷了,不得不搬”</span></a></p> <p style="margin-bottom: 0.1px;max-width: 100%;min-height: 1em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;letter-spacing: 0.544px;font-size: 15px;color: rgb(123, 12, 0);">低价选房丨</span><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(62, 62, 62);font-size: 15px;box-sizing: border-box !important;word-wrap: break-word !important;"><a href="https://mp.weixin.qq.com/s?__biz=MzA5MzQyMjIwMA==&amp;mid=2652721263&amp;idx=1&amp;sn=e60d65098dea8da326466afda78a8162&amp;scene=21#wechat_redirect" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(61, 170, 214);box-sizing: border-box !important;word-wrap: break-word !important;">每套便宜百万!最全限竞房名单</span></a></span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p> <p style="margin-bottom: 0.1px;max-width: 516px;box-sizing: border-box;word-wrap: break-word;min-height: 16px;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 15px;color: rgb(123, 12, 0);"><span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;word-wrap: break-word !important;">政策解读丨</span><a href="http://mp.weixin.qq.com/s?__biz=MzA5MzQyMjIwMA==&amp;mid=2652721163&amp;idx=1&amp;sn=cf66a3bd4644653790f562b69463c0e8&amp;chksm=8bb7e71cbcc06e0a772c37721fa0c5d3f273244e07d96c626748f0f7fe4d4a7638a73e27a77d&amp;scene=21#wechat_redirect" target="_blank" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;font-size: 17px;letter-spacing: 0.544px;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(61, 170, 214);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">“</span>限转共”</span><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 15px;color: rgb(61, 170, 214);">政策正式落地!</span></a></span></p> <p style="margin-bottom: 0.1px;max-width: 516px;box-sizing: border-box;word-wrap: break-word;min-height: 16px;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 15px;color: rgb(123, 12, 0);">楼市爆料丨</span><span style="max-width: 100%;font-size: 15px;color: rgb(61, 170, 214);box-sizing: border-box !important;word-wrap: break-word !important;"><a href="https://mp.weixin.qq.com/s?__biz=MzA5MzQyMjIwMA==&amp;mid=2652721428&amp;idx=1&amp;sn=fd881bf626a87f26d2a6dc47daf38216&amp;chksm=8bb7e603bcc06f15e9e40f80b6cbfa0056724fe0e1cb96617650eae15d7f7780d316462d7ed3&amp;scene=21#wechat_redirect" target="_blank" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(61, 170, 214);box-sizing: border-box !important;word-wrap: break-word !important;">共有产权房被曝“内部消化”</span></a></span></p> <p style="margin-bottom: 0.1px;max-width: 100%;min-height: 1em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(123, 12, 0);box-sizing: border-box !important;word-wrap: break-word !important;">楼市热点丨</span><a href="http://mp.weixin.qq.com/s?__biz=MzA5MzQyMjIwMA==&amp;mid=2652722892&amp;idx=1&amp;sn=d94084f1042232912e5d6831fbd3a074&amp;chksm=8bb7e1dbbcc068cd5cba3a061d0c3fdb310b2dbc81d0cb6fdf2b820c931fa9cd2aed9b211f69&amp;scene=21#wechat_redirect" target="_blank" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(61, 170, 214);box-sizing: border-box !important;word-wrap: break-word !important;">房租暴涨200% 中介只用10天</span></a></p> </section> </section> </section> </section> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;word-wrap: break-word !important;"><img class="__bg_gif" data-copyright="0" data-ratio="1.4497354497354498" src="/upload/9c0aea3b9208fcc1d017ee30bffb9a98.gif" data-type="gif" data-w="1134" style="box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: 677px !important;" width="677px"></p> </section> </section> </section>

一次线上问题排查所引发的思考

作者:微信小助手

<p style="white-space: normal;background-color: rgb(255, 255, 255);text-align: center;letter-spacing: 1px;line-height: 1.5em;overflow-wrap: break-word !important;" data-mpa-powered-by="yiban.io"><span style="font-size: 15px;"><span style="color: rgb(136, 136, 136);border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;line-height: inherit;font-family: PingFangSC-Regular;vertical-align: baseline;overflow-wrap: break-word !important;">点击上方</span><span style="border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;line-height: inherit;font-family: PingFangSC-Regular;vertical-align: baseline;color: rgb(61, 170, 214);overflow-wrap: break-word !important;"><strong>"</strong><strong>蓝字",&nbsp;</strong></span><span style="border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;line-height: inherit;font-family: PingFangSC-Regular;vertical-align: baseline;color: rgb(136, 136, 136);overflow-wrap: break-word !important;">右上角</span><span style="color: rgb(136, 136, 136);border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;line-height: inherit;font-family: PingFangSC-Regular;vertical-align: baseline;overflow-wrap: break-word !important;">选择“设为星标”</span></span></p> <p style="white-space: normal;background-color: rgb(255, 255, 255);text-align: center;letter-spacing: 1px;line-height: 1.5em;overflow-wrap: break-word !important;"><span style="border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;color: rgb(136, 136, 136);font-variant: inherit;font-weight: inherit;font-stretch: inherit;line-height: inherit;font-family: PingFangSC-Regular;vertical-align: baseline;font-size: 15px;overflow-wrap: break-word !important;">&nbsp;周一至周五早8点半!精品技术文章准时送上!</span></p> <p style="white-space: normal;text-align: center;"><img class="" data-copyright="0" data-ratio="0.4625" data-s="300,640" src="/upload/6657ba148b18be651d581593076c298e.jpg" data-type="jpeg" data-w="1280"></p> <h2 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.4em;white-space: normal;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"><span style="font-size: inherit;color: inherit;line-height: inherit;">前言</span></h2> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">之前或多或少分享过一些内存模型、<a href="http://mp.weixin.qq.com/s?__biz=MzIyMzgyODkxMQ==&amp;mid=2247483717&amp;idx=1&amp;sn=3540094f25f3268b925cbb2b89ab07c5&amp;chksm=e8190f85df6e8693426b7a71b8b169352a2f3df0a3d2e04faa30b52bdf48f9d82da871f9eb2b&amp;scene=21#wechat_redirect" target="_blank" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" data-linktype="2">对象创建</a>之类的内容,其实大部分人看完都是懵懵懂懂,也不知道这些的实际意义。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">直到有一天你会碰到线上奇奇怪怪的问题,如:</p> <ul class=" list-paddingleft-2" style=""> <li><p><span style="font-size: inherit;color: inherit;line-height: inherit;">线程执行一个任务迟迟没有返回,应用假死。</span></p></li> <li><p><span style="font-size: inherit;color: inherit;line-height: inherit;">接口响应缓慢,甚至请求超时。</span></p></li> <li><p><span style="font-size: inherit;color: inherit;line-height: inherit;">CPU 高负载运行。</span></p></li> </ul> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">这类问题并不像一个空指针、数组越界这样明显好查,这时就需要刚才提到的内存模型、对象创建、线程等相关知识结合在一起来排查问题了。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">正好这次借助之前的一次生产问题来聊聊如何排查和解决问题。</p> <h2 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.4em;white-space: normal;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"><span style="font-size: inherit;color: inherit;line-height: inherit;">生产现象</span></h2> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">首先看看问题的背景吧:</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">我这其实是一个定时任务,在固定的时间会开启 N 个线程并发的从 Redis 中获取数据进行运算。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"><strong style="color: rgb(233, 105, 0);font-size: inherit;line-height: inherit;">业务逻辑非常简单,但应用一般涉及到多线程之后再简单的事情都要小心对待。</strong></p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">果不其然这次就出问题了。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">现象:原本只需要执行几分钟的任务执行了几个小时都没退出。翻遍了所有的日志都没找到异常。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">于是便开始定位问题之路。</p> <h3 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.3em;white-space: normal;color: rgb(62, 62, 62);line-height: inherit;border-bottom: 2px solid rgb(239, 112, 96);letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"><span style="margin-right: 3px;padding: 3px 10px 1px;font-size: inherit;color: rgb(255, 255, 255);line-height: inherit;display: inline-block;font-weight: normal;background: rgb(239, 112, 96);border-top-right-radius: 3px;border-top-left-radius: 3px;">定位问题</span></h3> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">既然没办法直接从日志中发现异常,那就只能看看应用到底在干嘛了。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">最常见的工具就是 JDK 自带的那一套。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">这次我使用了&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;border-radius: 4px;background: rgb(248, 248, 248);">jstack</code>&nbsp;来查看线程的执行情况,它的作用其实就是 dump 当前的线程堆栈。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">当然在 dump 之前是需要知道我应用的 pid 的,可以使用&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;border-radius: 4px;background: rgb(248, 248, 248);">jps -v</code>&nbsp;这样的方式列出所有的 Java 进程。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">当然如果知道关键字的话直接使用&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;border-radius: 4px;background: rgb(248, 248, 248);">ps aux|grep java</code>&nbsp;也是可以的。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">拿到&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;border-radius: 4px;background: rgb(248, 248, 248);">pid=1523</code>&nbsp;了之后就可以利用&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;border-radius: 4px;background: rgb(248, 248, 248);">jstack 1523 &gt; 1523.log</code>&nbsp;这样的方式将 dump 文件输出到日志文件中。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">如果应用简单不复杂,线程这些也比较少其实可以直接打开查看。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">但复杂的应用导出来的日志文件也比较大还是建议用专业的分析工具。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">我这里的日志比较少直接打开就可以了。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">因为我清楚知道应用中开启的线程名称,所以直接根据线程名就可以在日志中找到相关的堆栈:</p> <figure style="white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"> <img class="" data-ratio="0.45295829291949563" data-type="jpeg" data-w="1031" title="" src="/upload/f9e50ffb655285e1a8353464522216c0.jpg" style="margin-right: auto;margin-left: auto;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;font-size: inherit;color: inherit;line-height: inherit;display: block;width: 677px !important;visibility: visible !important;"> </figure> <blockquote style="margin-top: 1em;margin-bottom: 1em;padding: 15px 15px 15px 1rem;border-left-width: 5px;border-left-color: rgb(239, 112, 96);word-wrap: normal;white-space: normal;font-size: 0.9em;line-height: inherit;background: rgb(239, 235, 233);overflow: auto;word-break: normal;letter-spacing: 2px;word-spacing: 2px;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">所以通常建议大家线程名字给的有意义,在排查问题时很有必要。</p> </blockquote> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">其实其他几个线程都和这里的堆栈类似,很明显的看出都是在做 Redis 连接。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">于是我登录 Redis 查看了当前的连接数,发现已经非常高了。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">这样 Redis 的响应自然也就变慢了。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">接着利用&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;border-radius: 4px;background: rgb(248, 248, 248);">jps -v</code>&nbsp;列出了当前所以在跑的 Java 进程,果不其然有好几个应用都在查询 Redis,而且都是并发连接,问题自然就找到了。</p> <h3 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.3em;white-space: normal;color: rgb(62, 62, 62);line-height: inherit;border-bottom: 2px solid rgb(239, 112, 96);letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"><span style="margin-right: 3px;padding: 3px 10px 1px;font-size: inherit;color: rgb(255, 255, 255);line-height: inherit;display: inline-block;font-weight: normal;background: rgb(239, 112, 96);border-top-right-radius: 3px;border-top-left-radius: 3px;">解决办法</span></h3> <blockquote style="margin-top: 1em;margin-bottom: 1em;padding: 15px 15px 15px 1rem;border-left-width: 5px;border-left-color: rgb(239, 112, 96);word-wrap: normal;white-space: normal;font-size: 0.9em;line-height: inherit;background: rgb(239, 235, 233);overflow: auto;word-break: normal;letter-spacing: 2px;word-spacing: 2px;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">所以问题的主要原因是:大量的应用并发查询 Redis,导致 Redis 的性能降低。</p> </blockquote> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">既然找到了问题,那如何解决呢?</p> <ul class=" list-paddingleft-2" style=""> <li><p><span style="font-size: inherit;color: inherit;line-height: inherit;">减少同时查询 Redis 的应用,分开时段降低 Redis 的压力。</span></p></li> <li><p><span style="font-size: inherit;color: inherit;line-height: inherit;">将 Redis 复制几个集群,各个应用分开查询。但是这样会涉及到数据的同步等运维操作,或者由程序了进行同步也会增加复杂度。</span></p></li> </ul> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">目前我们选择的是第一个方案,效果很明显。</p> <h2 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.4em;white-space: normal;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"><span style="font-size: inherit;color: inherit;line-height: inherit;">本地模拟</span></h2> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">上文介绍的是线程相关问题,现在来分析下内存的问题。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">以这个类为例:</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/oom/heap/HeapOOM.java</p> <pre style="margin-top: 0px;margin-bottom: 0px;padding: 0px;background-color: rgb(255, 255, 255);font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;"><code class="" style="margin-right: 2px;margin-left: 2px;padding: 0.5em;font-size: 14px;color: white;line-height: 18px;border-radius: 0px;background: rgb(51, 51, 51);font-family: Consolas, Inconsolata, Courier, monospace;display: block;overflow-x: auto;word-spacing: 0px;letter-spacing: 0px;word-wrap: normal !important;word-break: normal !important;overflow-y: auto !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">1</span><span class="" style="font-size: inherit;color: rgb(252, 194, 140);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">public</span>&nbsp;<span class="" style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="font-size: inherit;color: rgb(252, 194, 140);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">class</span>&nbsp;<span class="" style="font-size: inherit;color: rgb(255, 255, 170);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">HeapOOM</span>&nbsp;</span>{<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">2</span><br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">3</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="" style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="font-size: inherit;color: rgb(252, 194, 140);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">public</span>&nbsp;<span class="" style="font-size: inherit;color: rgb(252, 194, 140);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">static</span>&nbsp;<span class="" style="font-size: inherit;color: rgb(252, 194, 140);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">void</span>&nbsp;<span class="" style="font-size: inherit;color: rgb(255, 255, 170);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">main</span><span class="" style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">(String[]&nbsp;args)</span>&nbsp;</span>{<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">4</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;String&gt;&nbsp;list&nbsp;=&nbsp;<span class="" style="font-size: inherit;color: rgb(252, 194, 140);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">new</span>&nbsp;ArrayList&lt;&gt;(<span class="" style="font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">10</span>)&nbsp;;<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">5</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="" style="font-size: inherit;color: rgb(252, 194, 140);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">while</span>&nbsp;(<span class="" style="font-size: inherit;color: rgb(252, 194, 140);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">true</span>){<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">6</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(<span class="" style="font-size: inherit;color: rgb(162, 252, 162);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">"1"</span>)&nbsp;;<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">8</span>&nbsp;&nbsp;&nbsp;&nbsp;}<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">9</span>}<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"></code></pre> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">启动参数如下:</p> <pre style="margin-top: 0px;margin-bottom: 0px;padding: 0px;background-color: rgb(255, 255, 255);font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;"><code class="" style="margin-right: 2px;margin-left: 2px;padding: 0.5em;font-size: 14px;color: white;line-height: 18px;border-radius: 0px;background: rgb(51, 51, 51);font-family: Consolas, Inconsolata, Courier, monospace;display: block;overflow-x: auto;word-spacing: 0px;letter-spacing: 0px;word-wrap: normal !important;word-break: normal !important;overflow-y: auto !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">1</span>-Xms20m<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">2</span>-Xmx20m<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">3</span>-XX:+HeapDumpOnOutOfMemoryError<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="padding-right: 20px;font-size: inherit;color: rgb(211, 99, 99);line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;">4</span>-XX:HeapDumpPath=/Users/xx/Documents<br style="font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"></code></pre> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">为了更快的突出内存问题将堆的最大内存固定在 20M,同时在 JVM 出现 OOM 的时候自动 dump 内存到&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;border-radius: 4px;background: rgb(248, 248, 248);">/Users/xx/Documents</code>(不配路径则会生成在当前目录)。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">执行之后果不其然出现了异常:</p> <figure style="white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"> <img class="" data-ratio="0.2736318407960199" data-type="jpeg" data-w="804" title="" src="/upload/1fe154d434bb3918b10681d7285d9611.jpg" style="margin-right: auto;margin-left: auto;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;font-size: inherit;color: inherit;line-height: inherit;display: block;width: 677px !important;visibility: visible !important;"> </figure> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">同时对应的内存 dump 文件也生成了。</p> <h3 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.3em;white-space: normal;color: rgb(62, 62, 62);line-height: inherit;border-bottom: 2px solid rgb(239, 112, 96);letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"><span style="margin-right: 3px;padding: 3px 10px 1px;font-size: inherit;color: rgb(255, 255, 255);line-height: inherit;display: inline-block;font-weight: normal;background: rgb(239, 112, 96);border-top-right-radius: 3px;border-top-left-radius: 3px;">内存分析</span></h3> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">这时就需要相应的工具进行分析了,最常用的自然就是 MAT 了。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">我试了一个在线工具也不错(文件大了就不适合了):</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">http://heaphero.io/index.jsp</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">上传刚才生成的内存文件之后:</p> <figure style="white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"> <img class="" data-ratio="0.6106500691562933" data-type="jpeg" data-w="1446" title="" src="/upload/bbe2edc7cb303494c4fb8b78c0117c3d.jpg" style="margin-right: auto;margin-left: auto;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;font-size: inherit;color: inherit;line-height: inherit;display: block;width: 677px !important;visibility: visible !important;"> </figure> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">因为是内存溢出,所以主要观察下大对象:</p> <figure style="white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"> <img class="" data-ratio="0.448569218870843" data-type="jpeg" data-w="1293" title="" src="/upload/f797fff374b338d1574cddd213e49a14.jpg" style="margin-right: auto;margin-left: auto;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;font-size: inherit;color: inherit;line-height: inherit;display: block;width: 677px !important;visibility: visible !important;"> </figure> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">也有相应提示,这个很有可能就是内存溢出的对象,点进去之后:</p> <figure style="white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"> <img class="" data-ratio="0.5496688741721855" data-type="jpeg" data-w="755" title="" src="/upload/d981e5ec1da6fba8006fef62576f3040.jpg" style="margin-right: auto;margin-left: auto;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;font-size: inherit;color: inherit;line-height: inherit;display: block;width: 677px !important;visibility: visible !important;"> </figure> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">看到这个堆栈其实就很明显了:</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">在向 ArrayList 中不停的写入数据时,会导致频繁的扩容也就是数组复制这些过程,最终达到 20M 的上限导致内存溢出了。</p> <h2 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.4em;white-space: normal;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"><span style="font-size: inherit;color: inherit;line-height: inherit;">更多建议</span></h2> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">上文说过,一旦使用了多线程,那就要格外小心。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">以下是一些日常建议:</p> <ul class=" list-paddingleft-2" style=""> <li><p><span style="font-size: inherit;color: inherit;line-height: inherit;">尽量不要在线程中做大量耗时的网络操作,如查询数据库(可以的话在一开始就将数据从从 DB 中查出准备好)。</span></p></li> <li><p><span style="font-size: inherit;color: inherit;line-height: inherit;">尽可能的减少多线程竞争锁。可以将数据分段,各个线程分别读取。</span></p></li> <li><p>多利用&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;border-radius: 4px;background: rgb(248, 248, 248);">CAS+自旋</code>&nbsp;的方式更新数据,减少锁的使用。</p></li> <li><p>应用中加上&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;border-radius: 4px;background: rgb(248, 248, 248);">-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp</code>&nbsp;参数,在内存溢出时至少可以拿到内存日志。</p></li> <li><p><span style="font-size: inherit;color: inherit;line-height: inherit;">线程池监控。如线程池大小、队列大小、最大线程数等数据,可提前做好预估。</span></p></li> <li><p><span style="font-size: inherit;color: inherit;line-height: inherit;">JVM 监控,可以看到堆内存的涨幅趋势,GC 曲线等数据,也可以提前做好准备。</span></p></li> </ul> <h2 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.4em;white-space: normal;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);"><span style="font-size: inherit;color: inherit;line-height: inherit;">总结</span></h2> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">线上问题定位需要综合技能,所以是需要一些基础技能。如线程、内存模型、Linux 等。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;white-space: normal;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: 2px;word-spacing: 2px;background-color: rgb(255, 255, 255);">当然这些问题没有实操过都是纸上谈兵;如果第一次碰到线上问题,不要慌张,反而应该庆幸解决之后你又会习得一项技能。</p> <p style="white-space: normal;"><br></p> <p style="white-space: normal;text-align: center;"><span style="color: rgb(0, 122, 170);">文章来自公众号</span>:<strong><span style="color: rgb(87, 107, 149);letter-spacing: 0.544px;text-align: start;background-color: rgb(255, 255, 255);">crossoverJie</span></strong></p> <p style="white-space: normal;"><br></p> <section class="KolEditor" style="white-space: normal;"> <section class="KolEditor"> <p style="margin: 10px auto;width: 84px;line-height: 2em;"><img class="" data-ratio="0.6666666666666666" data-type="gif" data-w="84" src="/upload/6a286db350525e28360841d498522434.gif"></p> </section> </section> <p style="white-space: normal;text-align: center;line-height: 2em;"><span style="color: rgb(241, 136, 35);font-size: 20px;"><strong>End</strong></span></p> <p style="white-space: normal;line-height: 2em;text-align: center;"><br></p> <section class="KolEditor checkSelected" data-tools-id="56033" style="white-space: normal;"> <section class="KolEditor"> <section style="margin-top: 20px;"> <section style="margin-top: 5px;height: 4px;background: rgb(248, 212, 151);"></section> <section style="margin-top: -24px;text-align: center;"> <section style="padding: 5px 10px;background: rgb(248, 212, 151);display: inline-block;"> <p class="white title" style="min-width: 1px;font-size: 18px;color: rgb(255, 255, 255);"><span style="color: rgb(255, 76, 65);"><strong>推荐阅读</strong></span></p> </section> </section> <ul class=" list-paddingleft-2" style="list-style-type: square;"> <li><p style="line-height: 2em;"><a href="http://mp.weixin.qq.com/s?__biz=MzU0OTk3ODQ3Ng==&amp;mid=2247484255&amp;idx=1&amp;sn=60425e65474a4f2d895e05447e48d59c&amp;chksm=fba6eb5cccd1624a8988e90fc38a86c61b99e3eeb1e0f677d4c375547736109183828d8f0e9e&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2"><span style="font-size: 15px;text-decoration: underline;color: rgb(110, 191, 248);">精品专栏之微服务系列</span></a></p></li> <li><p style="line-height: 2em;"><a href="http://mp.weixin.qq.com/s?__biz=MzU0OTk3ODQ3Ng==&amp;mid=2247484255&amp;idx=1&amp;sn=60425e65474a4f2d895e05447e48d59c&amp;chksm=fba6eb5cccd1624a8988e90fc38a86c61b99e3eeb1e0f677d4c375547736109183828d8f0e9e&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2"><span style="font-size: 15px;text-decoration: underline;color: rgb(110, 191, 248);">精品专栏之分布式系列</span></a></p></li> <li><p style="line-height: 2em;"><a href="http://mp.weixin.qq.com/s?__biz=MzU0OTk3ODQ3Ng==&amp;mid=2247484255&amp;idx=1&amp;sn=60425e65474a4f2d895e05447e48d59c&amp;chksm=fba6eb5cccd1624a8988e90fc38a86c61b99e3eeb1e0f677d4c375547736109183828d8f0e9e&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2"><span style="font-size: 15px;text-decoration: underline;color: rgb(110, 191, 248);">精品专栏之亿级流量架构演进系列</span></a></p></li> <li><p style="line-height: 2em;"><a href="http://mp.weixin.qq.com/s?__biz=MzU0OTk3ODQ3Ng==&amp;mid=2247484255&amp;idx=1&amp;sn=60425e65474a4f2d895e05447e48d59c&amp;chksm=fba6eb5cccd1624a8988e90fc38a86c61b99e3eeb1e0f677d4c375547736109183828d8f0e9e&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2"><span style="font-size: 15px;text-decoration: underline;color: rgb(110, 191, 248);">精品专栏之并发系列</span></a></p></li> <li><p style="line-height: 2em;"><a href="http://mp.weixin.qq.com/s?__biz=MzU0OTk3ODQ3Ng==&amp;mid=2247484255&amp;idx=1&amp;sn=60425e65474a4f2d895e05447e48d59c&amp;chksm=fba6eb5cccd1624a8988e90fc38a86c61b99e3eeb1e0f677d4c375547736109183828d8f0e9e&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2"><span style="font-size: 15px;text-decoration: underline;color: rgb(110, 191, 248);">精品专栏之大数据系列</span></a></p></li> <li><p style="line-height: 2em;"><a href="http://mp.weixin.qq.com/s?__biz=MzU0OTk3ODQ3Ng==&amp;mid=2247484419&amp;idx=2&amp;sn=09c9e0b6adcfde3bb6edfa30678005ae&amp;chksm=fba6ec00ccd1651614cfb9912868e02381f43359439a4b0f1a5178e29173d15871df567bcdca&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2"><span style="font-size: 15px;text-decoration: underline;color: rgb(110, 191, 248);">精品专栏之Java进阶面试系列</span></a></p></li> </ul> <section style="margin-top: 10px;height: 4px;background: rgb(57, 207, 202);"></section> <section style="margin-top: 5px;height: 2px;background: rgb(248, 212, 151);"></section> </section> </section> <p style="vertical-align: baseline;letter-spacing: 1px;line-height: 1.5em;text-align: center;"><br></p> <p style="vertical-align: baseline;letter-spacing: 1px;line-height: 1.5em;text-align: center;"><strong><span style="font-size: 16px;"><span style="font-family: 宋体;">一大波</span><strong><span style="font-family: 宋体;">微服务、分布式、高并发、高可用</span></strong><span style="font-family: 宋体;">的</span><em><span style="font-family: 宋体;">原创系列</span></em><span style="font-family: 宋体;">文章正在路上。</span></span></strong></p> <p style="vertical-align: baseline;letter-spacing: 1px;line-height: 1.5em;"><br></p> <p style="vertical-align: baseline;letter-spacing: 1px;line-height: 1.5em;text-align: center;"><strong><span style="font-size: 16px;font-family: 宋体;"><span style="color: rgb(201, 56, 28);"><strong>欢迎扫描下方二维码</strong></span>,持续关注:</span></strong></p> <section class="KolEditor" data-tools-id="66348"> <section style="margin: 10px;display: flex;flex-direction: column;align-items: center;"> <section style="margin-bottom: -20px;padding: 8px;background: rgb(255, 255, 255);z-index: 4;"> <section class="KolEditor"> <section style="margin-top: 20px;"> <section style="margin-right: auto;margin-left: auto;background-image: url(&quot;https://mmbiz.qpic.cn/mmbiz_gif/1J6IbIcPCLYzapicOfUCIWp88Nib7dlgHDtCCKAEVAKI0cxmYTxOicyZJ6TsIPw1N4gJ4zU2szB8WjkUa3HfBbN6w/640?wx_fmt=gif&quot;);background-repeat: no-repeat;width: 240px;background-size: 100%;text-align: center;"> <section class="" style="margin-right: auto;margin-left: auto;padding-top: 32px;padding-bottom: 30px;width: 70px;"> <img class="" data-ratio="0.6666666666666666" data-type="jpeg" data-w="344" src="/upload/364e64b5ea4be7b9382dd0f8d4c10f86.jpg" style="width: 70px;"> </section> </section> </section> </section> <p><br></p> <p style="font-size: 15px;letter-spacing: 5px;color: rgb(59, 88, 49);text-align: center;"><strong style="letter-spacing: 1px;"><span style="font-style: inherit;font-variant-ligatures: inherit;font-variant-caps: inherit;-webkit-font-smoothing: antialiased;border-width: 0px;border-style: initial;border-color: initial;font-weight: bolder;font-stretch: inherit;vertical-align: baseline;user-select: text;color: rgba(13, 0, 19, 0.72);font-family: PingFangSC-Regular;font-variant-numeric: normal;font-variant-east-asian: normal;line-height: 23.324px;text-align: justify;widows: 1;font-size: 16px;">石杉的架构笔记(id:shishan100)</span></strong></p> <p style="font-size: 15px;letter-spacing: 5px;color: rgb(59, 88, 49);text-align: center;"><strong><span style="color: rgba(13, 0, 19, 0.72);font-family: PingFangSC-Regular;font-size: 14px;text-align: justify;widows: 1;-webkit-font-smoothing: antialiased;border-width: 0px;border-style: initial;border-color: initial;font-variant-numeric: normal;font-stretch: inherit;line-height: 23.324px;vertical-align: baseline;user-select: text;">十余年<span style="-webkit-font-smoothing: antialiased;font-variant: inherit;font-weight: inherit;font-stretch: inherit;line-height: inherit;vertical-align: baseline;user-select: text;color: rgb(247, 150, 70);">BAT架构经验</span>倾囊相授</span></strong></p> </section> </section> </section> <p><br></p> <p><br></p> <br> </section> <section class="KolEditor" style="white-space: normal;"> <section style="font-size: 16px;border-width: 0px;border-style: none;border-color: initial;"> <section style="margin-top: 0.5em;margin-bottom: 0.5em;"> <img class="" data-ratio="0.6666666666666666" data-type="png" data-w="640" src="/upload/2a91c95850337a2b69e8d27fe83c5285.null" style="width: auto !important;"> </section> </section> </section>

String 10 equals Integer 10 相等吗?

作者:Happy生龙

``` Integer i = 10; System.out.println("10".equals(i)); System.out.println(i.toString().equals("10")); ``` 结果 ``` false true ``` integer 不能直接和 string 比较

linux服务器时间同步问题

作者:じ☆ve宝贝

## Linux自动时间同步: ``` # 安装ntp服务 yum install ntp # 重新启动ntp服务 systemctl restart ntpd # NTP服务对等端的列表信息 ntpq -p # 开机自启 systemctl enable ntpd ``` ## linux服务器手动时间同步问题: ``` # 安装 ntpdate yum install ntpdate # 开启防火墙端口 iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT # 同步时间 # 删除本地时间配置 rm -rf /etc/localtime # 设置本地时间 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 跟服务器同步时间 #ntpdate -b pool.ntp.org ntpdate -b ntp.aliyun.com # 检查结果 date # 启动服务 service ntpdate restart # 开机自启 systemctl enable ntpdate ``` ## yum 不可用时可以: ``` cd /etc/yum.repos.d/ # 删除所有文件: # rm -f /etc/yum.repos.d/* # 重新下载阿里的 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # 清理缓存 yum clean all ```

外行人都能看懂的 Spring Cloud,错过了血亏!

作者:微信小助手

<section class="output_wrapper" data-mpa-powered-by="yiban.io"> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: left;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">这篇主要来讲讲 Spring Cloud 的一些基础知识。以下是&nbsp;<span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">SpringCloud GitHub Demo ,看完文章的同学可以自己练手玩玩:</span></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: left;"><span style="color: rgb(61, 170, 214);font-size: 13px;letter-spacing: 1px;text-decoration: underline;">https://github.com/ZhongFuCheng3y/msc-Demo</span></p> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></h1> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="letter-spacing: 1px;font-size: 16px;color: rgb(255, 169, 0);">一、集群/分布式/微服务/SOA是什么?</span></strong></h1> <p style="line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">技术小白看到这些词(集群/分布式/微服务/SOA)时,感觉遥不可及。</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">但真正接触到"面向切面编程"的时候,发现原来如此啊,只不过当时被它的名字给唬住了。</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">下面我就简单说说这些名词的意思:</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;"><br></span></p> <h2 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(217, 33, 66);"><strong><span style="font-size: 14px;letter-spacing: 1px;">1. 什么是集群</span></strong></span></h2> <blockquote> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">计算机集群简称集群是一种计算机系统,它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作。在某种意义上,他们可以被看作是一台计算机。</span></p> </blockquote> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">集群技术特点:</span></strong></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">通过多台计算机完成同一个工作,达到更高的效率。</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">两机或多机内容、工作过程等完全一样。如果一台死机,另一台可以起作用。</span></p></li> </ul> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <h2 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(217, 33, 66);"><strong><span style="font-size: 14px;letter-spacing: 1px;">2. 什么是分布式</span></strong></span></h2> <blockquote> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">分布式系统是一组计算机,通过网络相互连接传递消息与通信后并协调它们的行为而形成的系统。组件之间彼此进行交互以实现一个共同的目标。</span></p> </blockquote> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;">&nbsp;</p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">好处:</span></strong></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">模块之间独立,各做各的事,便于扩展,复用性高</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">高吞吐量。某个任务需要一个机器运行 10 个小时,将该任务用 10 台机器的分布式跑(将这个任务拆分成 10 个小任务),可能 2 个小时就跑完了</span></p></li> </ul> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <h2 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 14px;letter-spacing: 1px;color: rgb(217, 33, 66);">3. 集群/分布式</span></strong></h2> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">集群和分布式并不冲突,可以有分布式集群</span></strong></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">Java,前端,测试,DBA 的关系看作是分布式的</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">5 个 Java 看作是集群的(前端,测试同理)</span></p></li> </ul> <h2 style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="color: rgb(217, 33, 66);"><strong><span style="font-size: 14px;letter-spacing: 1px;"><br></span></strong></span></h2> <h2 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(217, 33, 66);"><strong><span style="color: rgb(217, 33, 66);font-size: 14px;letter-spacing: 1px;">4. 分布式/微服务/SOA</span></strong></span></h2> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">其实我认为分布式/微服务/SOA 这三个概念是差不多的,了解了其中的一个,然后将自己的理解往上面套就好了。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><br></p> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="letter-spacing: 1px;color: rgb(255, 169, 0);">二、CAP 理论</span></strong></span></h1> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">从上面所讲的分布式概念我们已经知道,分布式简单理解就是:一个业务分拆多个子业务,部署在不同的服务器上。</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">一般来说,一个子业务我们称为节点。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">如果你接触过一些分布式的基础概念,那肯定会听过 CAP 这个理论。就比如说:你学了 MySQL 的 InnoDB 存储引擎相关知识,你肯定听过 ACID!</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(217, 33, 66);"><strong><span style="font-size: 14px;letter-spacing: 1px;">1. CAP 分别代表的是什么意思:</span></strong></span></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">C:数据一致性(consistency)</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">A:可用性(availability)</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">P:分区容错性(partition-tolerance)</span></p></li> </ul> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">下面有三个节点(它们是集群的),此时三个节点都能够相互通信:</span></p> <p style="text-align: center;"><img class="" data-copyright="0" data-ratio="0.41574074074074074" data-s="300,640" src="/upload/e04334133e518b99bf67500a9b1a00f5.jpg" data-type="jpeg" data-w="1080" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;text-align: justify;">由于我们的系统是分布式的,节点之间的通信是通过网络来进行的。只要是分布式系统,那很有可能会出现一种情况:因为一些故障,使得有些节点之间不连通了,整个网络就分成了几块区域。</span><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">数据就散布在了这些不连通的区域中,这就叫分区</span></p></li> </ul> <p style="text-align: center;"><img class="" data-copyright="0" data-ratio="0.41759259259259257" data-s="300,640" src="/upload/538e1ab4a5d8f0aa4ab84d7a9c531b0b.jpg" data-type="jpeg" data-w="1080" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">现在出现了网络分区后,此时有一个请求过来了,想要注册一个账户。</span></p> <p style="text-align: center;"><img class="" data-copyright="0" data-ratio="0.40925925925925927" data-s="300,640" src="/upload/db844e1f39b5ae2fa398446efcc5668f.jpg" data-type="jpeg" data-w="1080" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">此时我们节点一和节点三是不可通信的,这就有了抉择:</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">如果允许当前用户注册一个账户,此时注册的记录数据只会在节点一和节点二或者节点二和节点三同步,因为节点一和节点三的记录不能同步的。</span></p></li> <p style="line-height: 1.75em;"><br></p> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">如果不允许当前用户注册一个账户(就是要等到节点一和节点三恢复通信)。节点一和节点三一旦恢复通信,我们就可以保证节点拥有的数据是最新版本。</span></p></li> </ul> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="letter-spacing: 1px;color: rgb(255, 169, 0);">三、Spring Cloud 就是这么简单</span></strong></span></h1> <p style="line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">下面来讲讲 SpringCloud 最基础的知识:</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;"><br></span></p> <h2 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(217, 33, 66);"><strong><span style="font-size: 14px;letter-spacing: 1px;">1. 为什么需要 Spring Cloud</span></strong></span></h2> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">举个可能不太恰当的例子:</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><img class="" data-copyright="0" data-ratio="0.46111111111111114" data-s="300,640" src="/upload/befb6a60c67ed301b90a723529fad18d.jpg" data-type="jpeg" data-w="1080" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">拆分出多个模块以后,就会出现各种各样的问题,而 SpringCloud 提供了一整套的解决方案!</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(217, 33, 66);"><strong><span style="font-size: 14px;letter-spacing: 1px;">2. Spring Cloud 的基础功能:</span></strong></span></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">服务治理: Spring &nbsp;Cloud Eureka</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">客户端负载均衡: Spring Cloud Ribbon</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">服务容错保护: Spring &nbsp;Cloud Hystrix &nbsp;</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">声明式服务调用: Spring &nbsp;Cloud Feign </span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">API 网关服务:Spring Cloud Zuul </span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">分布式配置中心: Spring Cloud Config</span></p></li> </ul> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></h1> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;color: rgb(255, 169, 0);"><strong><span style="font-size: 16px;letter-spacing: 1px;">四、引出 Eureka</span></strong></span></h1> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;"></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">当子系统与子系统之间不是在同一个环境下,那就需要远程调用。远程调用可能就会想到 httpClient,Web Service 等这些技术来实现。</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">在服务多的情况下,手动来维护这些静态配置就是噩梦!</span></p> <p style="line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">为了解决微服务架构中的服务实例维护问题(ip地址), 产生了大量的服务治理框架和产品。&nbsp;</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">在 Spring Cloud 中我们的服务治理框架一般使用的就是 Eureka。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><br></p> <h2 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(217, 33, 66);"><strong><span style="font-size: 14px;letter-spacing: 1px;">1. Eureka 细节</span></strong></span></h2> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">Eureka 专门用于给其他服务注册的称为 Eureka Server(服务注册中心),其余注册到 Eureka Server 的服务称为 Eureka Client。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><img class="" data-copyright="0" data-ratio="0.5194444444444445" data-s="300,640" src="/upload/6454485375a9f5ce7e70827649fff92b.jpg" data-type="jpeg" data-w="1080" style="width: 74%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;text-align: center;line-height: normal;"><br></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">Eureka Client 分为服务提供者和服务消费者。</span></strong></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">但很可能,某服务既是服务提供者又是服务消费者。</span></p></li> </ul> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">如果在网上看到 SpringCloud 的某个服务配置没有"注册"到 Eureka-Server 也不用过于惊讶,</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">很可能只是作者把该服务认作为单纯的服务消费者,也就无须注册到 Eureka 中了。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;"></span><br></p> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(255, 169, 0);font-size: 16px;"><strong><span style="color: rgb(255, 169, 0);letter-spacing: 1px;">五、引出 RestTemplate 和 Ribbon</span></strong></span></h1> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">通过 Eureka 服务治理框架,我们可以通过服务名来获取具体的服务实例的位置了(IP)。一般在使用 Spring Cloud 的时候不需要自己手动创建 HttpClient 来进行远程调用。</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">可以使用 Spring 封装好的 RestTemplate 工具类,使用起来很简单:</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;"><br></span></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;"><img class="" data-ratio="0.4189406099518459" data-s="300,640" src="/upload/23779976e30f218a00d40b96d3c0aec8.png" data-type="png" data-w="623" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><br></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">为了实现服务的高可用,我们可以将服务提供者集群。</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">Spring Cloud 也支持的负载均衡功能,只不过它是客户端的负载均衡,这个功能实现就是 Ribbon!</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></strong></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">负载均衡又区分了两种类型:</span></strong></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">客户端负载均衡(Ribbon)</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">服务端负载均衡(Nginx)</span></p></li> </ul> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">所以,我们的图可以画成这样:</span></strong></p> <p style="text-align: center;"><img class="" data-copyright="0" data-ratio="0.5370370370370371" data-s="300,640" src="/upload/7f46e495cf8a80572cc2a35961ac6503.jpg" data-type="jpeg" data-w="1080" style="width: 79%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(217, 33, 66);"><strong><span style="font-size: 14px;letter-spacing: 1px;">1. Ribbon 细节</span></strong></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">Ribbon 是支持负载均衡,默认的负载均衡策略是轮询,我们也是可以根据自己实际的需求自定义负载均衡策略的。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;"><img class="" data-ratio="0.30781758957654726" data-s="300,640" src="/upload/710523e5a5160f143f48efdb752cfeaf.png" data-type="png" data-w="614" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">实现起来也很简单:继承 AbstractLoadBalancerRule 类,重写 public Server choose(ILoadBalancer lb, Object key)即可。</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">SpringCloud 在 CAP 理论是选择了 AP 的,在 Ribbon 中还可以配置重试机制的。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: justify;"><strong><span style="letter-spacing: 1px;font-size: 16px;color: rgb(255, 169, 0);">六、引出 Hystrix</span></strong></h1> <p style="line-height: normal;"><strong><span style="letter-spacing: 1px;font-size: 16px;color: rgb(255, 169, 0);"><br></span></strong></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: justify;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">到目前为止,我们的服务看起来好像挺好的了:能够根据服务名来远程调用其他的服务,可以实现客户端的负载均衡。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><img class="" data-copyright="0" data-ratio="0.6413043478260869" data-s="300,640" src="/upload/f7dd18d2e83af3fe38a95609b05b108c.jpg" data-type="jpeg" data-w="1012" style="text-align: center;width: 62%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">但是,如果我们在调用多个远程服务时,某个服务出现延迟,会怎么样?</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><img class="" data-copyright="0" data-ratio="0.909375" data-s="300,640" src="/upload/b196a2d425ff9b89c8f0bf18e9adff3a.jpg" data-type="jpeg" data-w="640" style="width: 74%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">在高并发的情况下,由于单个服务的延迟,可能导致所有的请求都处于延迟状态,甚至在几秒钟就使服务处于负载饱和的状态,资源耗尽,直到不可用,最终导致这个分布式系统都不可用,这就是“雪崩”。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><img class="" data-copyright="0" data-ratio="0.9109375" data-s="300,640" src="/upload/4cad340e54bddc26410f446a3fb8a71b.jpg" data-type="jpeg" data-w="640" style="width: 62%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">针对上述问题, Spring Cloud Hystrix 实现了断路器、线程隔离等一系列服务保护功能。</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">Hystrix 提供几个熔断关键参数:滑动窗口大小(20)、 熔断器开关间隔(5s)、错误率(50%)</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <h2 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(217, 33, 66);"><strong><span style="font-size: 14px;letter-spacing: 1px;">1. Hystrix 仪表盘</span></strong></span></h2> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">它主要用来实时监控 Hystrix 的各项指标信息。通过 Hystrix Dashboard 反馈的实时信息,可以帮助我们快速发现系统中存在的问题,从而及时地采取应对措施。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">启动时的页面:</span></strong></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><img class="" data-copyright="0" data-ratio="0.5916666666666667" data-s="300,640" src="/upload/2920a155c83e8737bff89154ec446261.jpg" data-type="jpeg" data-w="1080" style="width: 74%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><br></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: left;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">监控单服务的页面</span></strong></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><img class="" data-copyright="0" data-ratio="0.5935114503816794" data-s="300,640" src="/upload/59baff56f8087443b843e99b32f5df11.jpg" data-type="jpeg" data-w="1048" style="text-align: center;width: 61%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">我们现在的服务是这样的:</span></strong></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><img class="" data-copyright="0" data-ratio="0.47962962962962963" data-s="300,640" src="/upload/9831670efe0257536903beace62f78dd.jpg" data-type="jpeg" data-w="1080" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">除了可以开启单个实例的监控页面之外,还有一个监控端点 /turbine.stream 是对集群使用的。 从端点的命名中,可以引入 Turbine, 通过它来汇集监控信息,并将聚合后的信息提供给 HystrixDashboard 来集中展示和监控。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><img class="" data-copyright="0" data-ratio="0.4074074074074074" data-s="300,640" src="/upload/13dec8905831f811f2a0b2c03b43a900.jpg" data-type="jpeg" data-w="1080" style="width: 74%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><br></p> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(255, 169, 0);"><strong><span style="letter-spacing: 1px;font-size: 16px;">七、引出 Feign</span></strong></span></h1> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">为了简化我们的开发,Spring Cloud Feign 出现了!它基于 Netflix Feign 实现,整合了 Spring Cloud Ribbon 与 Spring Cloud Hystrix,&nbsp;除了整合这两者的强大功能之外,它还提供了声明式的服务调用(不再通过 RestTemplate)。</span><br></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <blockquote> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">Feign 是一种声明式、模板化的 HTTP 客户端。在 Spring Cloud 中使用 Feign, 我们可以做到使用 HTTP 请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个 HTTP 请求。</span></p> </blockquote> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">下面就简单看看 Feign 是怎么优雅地实现远程调用的:</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">服务绑定:</span></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;"><img class="" data-ratio="0.37009063444108764" data-s="300,640" src="/upload/5667a0bcb6b8b88e6574c748c60f36ad.png" data-type="png" data-w="662" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;"></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">Feign 中使用熔断器:</span></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;"><img class="" data-ratio="0.6199095022624435" data-s="300,640" src="/upload/1cda5e878c2662fbcc8965f6ae6ef2e3.png" data-type="png" data-w="663" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;"></span><br></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">调用:</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><img class="" data-copyright="0" data-ratio="0.39814814814814814" data-s="300,640" src="/upload/c48cea50d1c6846b04d96d7b92447d98.jpg" data-type="jpeg" data-w="1080" style=""></p> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></h1> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(255, 169, 0);font-size: 16px;"><strong><span style="color: rgb(255, 169, 0);letter-spacing: 1px;">八、引出 Spring Cloud Config</span></strong></span></h1> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">随着业务的扩展,我们的服务会越来越多。每个服务都有自己的配置文件,</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">既然是配置文件,给我们配置的东西,那难免会有些改动的。</span><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;">比如我们的 Demo 中,每个服务都写上相同的配置文件。万一我们有一天,配置文件中的密码需要更换了,那就得三个都要重新更改。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="color: rgb(86, 86, 86);font-size: 14px;letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><img class="" data-copyright="0" data-ratio="0.3111111111111111" data-s="300,640" src="/upload/db4d283cc72f3d9d9e2b47f15affdbeb.jpg" data-type="jpeg" data-w="1080" style=""></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><br></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">Spring Cloud Config 项目是一个解决分布式系统的配置管理方案。它包含了Client 和 Server 两个部分,server 提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client 通过接口获取数据、并依据此数据初始化自己的应用。</span></p> <ul style="margin-left: 8px;margin-right: 8px;" class=" list-paddingleft-2"> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">简单来说,使用 Spring Cloud Config 就是将配置文件放到统一的位置管理(比如GitHub),客户端通过接口去获取这些配置文件。</span></p></li> <li><p style="line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">在GitHub上修改了某个配置文件,应用加载的就是修改后的配置文件。</span></p></li> </ul> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><img class="" data-copyright="0" data-ratio="0.5961538461538461" data-s="300,640" src="/upload/168664538c07eff2c9929fe73322b46c.jpg" data-type="jpeg" data-w="936" style="width: 74%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;text-align: center;"><br></p> <h1 style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="letter-spacing: 1px;font-size: 16px;color: rgb(255, 169, 0);">总结</span></strong></h1> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">本文主要写了 SpringCloud 的基础知识,希望大家看完能有所帮助。</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">SpringCloud 的资料也很多,我整理了一些我认为比较好,想要深入了解的同学不妨看看下边的课程~~~</span></p> <p style="margin-left: 8px;margin-right: 8px;line-height: normal;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><br></span></p> <p style="text-align: center;"><img class="" data-copyright="0" data-ratio="1.7777777777777777" data-s="300,640" src="/upload/115ffe3d0e230bdea4d8208aa81a040d.jpg" data-type="jpeg" data-w="720" style="width: 79%;height: auto !important;"></p> <p style="margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"></span></p> <p style="line-height: 1.5em;margin-left: 8px;margin-right: 8px;text-align: justify;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">本文大家可以当做学习 Spring Cloud 的笔记!当然了,一些概念或者知识点可能不会马上理解,大家可以跟着课程一起来学习,点击</span><span style="letter-spacing: 1px;color: rgb(217, 33, 66);font-size: 15px;"><strong>阅读原文</strong></span><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;">,试读课程。</span></p> <p style="line-height: 1.5em;margin-left: 8px;margin-right: 8px;text-align: justify;"><span style="font-size: 14px;color: rgb(86, 86, 86);letter-spacing: 1px;"><img class="mpa-image" data-ratio="1" src="/upload/29316807de8eec165a101cfe6173a39c.null" data-w="64" style="height: 20px !important;max-height: 20px !important;width: 20px !important;"><img class="mpa-image" data-ratio="1" src="/upload/29316807de8eec165a101cfe6173a39c.null" data-w="64" style="height: 20px !important;max-height: 20px !important;width: 20px !important;"><img class="mpa-image" data-ratio="1" src="/upload/29316807de8eec165a101cfe6173a39c.null" data-w="64" style="height: 20px !important;max-height: 20px !important;width: 20px !important;"></span></p> </section>

你写的 Java 代码是如何一步步输出结果的?

作者:微信小助手

<section class="" style="color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; text-align: justify; white-space: normal;"> <section style="margin: 10px auto; display: flex; justify-content: center; align-items: flex-end;"> <p style="color: rgb(88, 16, 13); line-height: 1.5; text-align: center;"><strong style="color: rgb(57, 137, 31);"><img class="rich_pages" data-copyright="0" data-ratio="0.6666666666666666" data-s="300,640" data-type="jpeg" data-w="1280" src="/upload/398d99ea818be829b0277e1b59a977c8.jpg" style="letter-spacing: 0.544px; box-shadow: rgb(170, 170, 170) 0em 0em 1em 0px; visibility: visible !important; width:auto !important;max-width:100% !important;height:auto !important;" width="auto"></strong><br></p> </section> </section> <p style="margin-top: 25px; margin-bottom: 15px; color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; white-space: normal; line-height: 2em; text-align: center;"><span style="font-size: 20px;">扫描下方二维码<span style="color: rgb(201, 56, 28);"><strong>试读</strong></span></span></p> <p style="margin-bottom: 15px; color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; white-space: normal; text-align: center;"><span style="color: rgb(57, 137, 31);"><strong><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="600" src="/upload/3f6bb75371fa02356c9a40f699003d25.png" style="border-radius: 0em; height: auto !important; visibility: visible !important; width: 216.625px !important;" width="216.625px"></strong></span></p> <p style="white-space: normal;color: rgb(153, 80, 51);line-height: normal;"><br></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><span style="font-size: 18px;color: rgb(64, 118, 0);"><strong><span style="font-size: 18px;background-color: rgb(255, 255, 255);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;">目录</span></strong></span><strong><span style="background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;font-size: 16px;"></span></strong></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><span style="font-size: 14px;background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;">一、编写Java源程序</span></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><span style="font-size: 14px;background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;">二、编译Java源代码</span></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><span style="font-size: 14px;background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;">三、执行Java源文件</span></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><span style="font-size: 14px;background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;">四、JVM如何执行字节码文件</span></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><br></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><span style="font-size: 15px;background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;">对于任何一门语言,要想达到精通的水平,研究它的执行原理(或者叫底层机制)不失为一种良好的方式。</span></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><span style="font-size: 15px;background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;"><br></span></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><span style="font-size: 15px;"><span style="background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;">在本篇文章中,将重点研究java源代码的执行原理,即从程</span><span style="background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;">序员编写JAVA源代码,到最终形成产品,在整个过程中,都经历了什么?每一步又是怎么执行的?执行原理又是什么?</span></span></p> <p style="white-space: normal;color: rgb(153, 80, 51);text-align: left;line-height: 2em;"><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="637" src="/upload/487053bca91d4c97ebdf0dee96ed3861.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="font-size: 18px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(64, 118, 0);"><strong><span style="font-size: 18px;">一 编写java源程序</span></strong></span><strong><span style="font-size: 18px;"></span></strong></p> <hr style="color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">java源文件:</span><span style="font-size: 15px;">指存储java源码的文件。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">先来看看如下代码:</span></p> <p><img data-ratio="0.6666666666666666" data-type="png" src="/upload/6f851cdaccb9cbbb40e1ef2979d45925.png"></p> <p><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">(1)java源文件名就是该源文件中public类的名称</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="818" src="/upload/39b9a58d3f838978af60c1843277b5eb.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">(2)一个java源文件可以包含多个类,但只允许一个类为public</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="font-size: 18px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="font-size: 18px;color: rgb(64, 118, 0);"><strong>二 编译java源代码</strong></span><span style="font-size: 18px;"></span></p> <hr style="color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <p style="margin: 10px auto; color: rgb(0, 0, 0); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; text-align: left; white-space: normal; background-color: rgb(255, 255, 255); line-height: 2em;"><span style="font-size: 15px;">当java源程序编码结束后,就需要编译器编译。</span></p> <p style="margin: 10px auto; color: rgb(0, 0, 0); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; text-align: left; white-space: normal; background-color: rgb(255, 255, 255); line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto; color: rgb(0, 0, 0); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; text-align: left; white-space: normal; background-color: rgb(255, 255, 255); line-height: 2em;"><span style="font-size: 15px;">安装好jdk后,我们打开jdk目录,有两个.exe文件,即javac.exe(编译源代码,xxx.java文件) 和 java.exe(执行字节码,xxx.class文件).</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">如下图所示:</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="893" height="386" src="/upload/b89ff33068de7225e40d9c28ed9d45e7.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;" width="578"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">1、切换到MyTest.java文件夹</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="202" src="/upload/9705ab5072bdd0d452f5e193dcfce11b.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">2、javac.exe编译MyTest.java</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="247" src="/upload/9cf1a2a7414c9ecb033d8741a99b919d.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">编译后,发现e:\Blogs&nbsp;目录多了以class为后缀的文件:A.class,B.class和MyTest.class</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="401" src="/upload/83f9bc44c2b39d495a9e62b222e46f53.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(64, 179, 230);"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><span style="color: rgb(64, 179, 230);">Tip</span>:当javac.exe编译java源代码时,java源代码有几个类,就会编译成一个对应的字节码文件(.class文件)</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">其中,字节码文件的文件名就是每个类的类名。需要注意的是,类即使不在源文件中定义,但被源文件引用,编译后,也会编程相应的字节码文件。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">如类A引用类C,但类C不定义在类A的源文件中,编译后,类C也被编译成对应的字节码文件C.class</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(64, 118, 0);"><strong><span style="font-size: 18px;">三 执行java源文件</span></strong></span><strong><span style="font-size: 18px;"></span></strong></p> <hr style="color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);">&nbsp;</p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">执行java源文件,用java.exe执行即可</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="213" src="/upload/53c6e99250549b0d484d66c1a046010d.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">到现在,java源程序基本执行结果,并正确打印我们期望的结果,那么,如上的步骤,我们可以总结如下:</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="869" height="94" src="/upload/381422dea7ec285dbe99d1a29ef116ff.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;" width="648"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">如上总结,已经抽象化了在JVM中的执行。接下来,我们将分析字节码文件(.class文件)如何在虚拟机中一步一执行的。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(64, 118, 0);"><strong><span style="font-size: 18px;">四 JVM如何执行字节码文件</span></strong></span><strong><span style="font-size: 18px;"></span></strong></p> <hr style="color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><strong>(一)装载字节码文件</strong></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">当&nbsp;.java&nbsp;源码被&nbsp;javac.exe&nbsp;编译器编译成&nbsp;.class&nbsp;字节码文件后,接下来的工作就交给JVM处理。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">JVM首先通过类加载器(ClassLoader),将class文件和相关Java&nbsp;API加载装入JVM,以供JVM后续处理。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">在该阶段中,涉及到如下一些基本概念和知识。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(64, 179, 230);font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;color: rgb(64, 179, 230);">1.JDK,JRE和JVM关系<br></span></p> <ul class=" list-paddingleft-2" style="list-style-type: disc;"> <li><p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">JDK(Java Development Kit),Java开发工具包,主要用于开发,在JDK7前,JDK包括JRE</span></p><p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p></li> <li><p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">JRE(Java Runtime Environment),Java程序运行的核心环境,包括JVM和一些核心库</span></p><p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p></li> <li><p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">JVM(Java&nbsp;Virtual&nbsp;Machine),VM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的,是JRE核心模块。</span></p></li> </ul> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="color: rgb(64, 179, 230);font-size: 15px;">2.JVM</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">Java虚拟机的主要任务是装载class文件,并执行其中的字节码,不同的Java虚拟机中,执行引擎可能有不同的实现。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">大致有如下几种引擎:</span></p> <ul style="margin-left: 30px;" class=" list-paddingleft-2"> <li><p style="line-height: 2em;"><span style="font-size: 15px;">&nbsp; &nbsp;一次性解释字节码引擎</span></p></li> <li><p style="line-height: 2em;"><span style="font-size: 15px;">&nbsp; &nbsp; 即时编译引擎</span></p></li> <li><p style="line-height: 2em;"><span style="font-size: 15px;">&nbsp; &nbsp;自适应优化器</span></p></li> </ul> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">关于虚拟机的实现方式,采用软件方式、硬件方式和软件硬件结合方式,这个要根据具体厂商而定。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="color: rgb(64, 179, 230);font-size: 15px;">3.什么是ClassLoader</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">虚拟机的主要任务是装载class文件并执行其中的字节码,而class文件是由虚拟机的类加载器(ClassLoader)完成的,在一个Java虚拟机中有可能存在多个类加载器。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">任何java运用程序,可能会使用两种类加载器,即启动类加载器(bootstrap)和用户自定义类加载器。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">&nbsp; &nbsp; &nbsp; &nbsp;</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">启动类加载器是Java虚拟机唯一实现的一部分,它又可分为原始类装载器,系统类装载器或默认类装载器。</span><span style="font-size: 15px;">它的主要作用是从操作系统的磁盘装载相应的类,如Java&nbsp;API类等。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">用户自定义装载类,即按照用户自定义的方式来装载类。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="657" src="/upload/86887a1ff13ce155610dd10779b80877.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><strong><br></strong></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><strong>(二)将字节码文件存储在JVM内存区</strong></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">当JAVA虚拟机运行一个程序时,它需要内存来存储许多东西。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">比如如字节码,程序创建的对象,传递给方法的参数,返回值,局部变量以及运算的中间结果等,这些相关信息被组织到“<span style="font-size: 15px;color: rgb(241, 136, 35);">运行时数据区</span>”。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">根据厂商的不同,在Java虚拟机中,运行时数据区也有所不同。有些运行时数据区由线程共享,有些只能由某个特定线程共享。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">运行时数据区大致可分几个区:方法区,堆区,栈区,PC寄存器区和本地方法栈区。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">在该阶段中,涉及到如下基本概念和知识。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;color: rgb(64, 179, 230);">1、方法区</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">方法区用来存储解析被加载的class文件的相关信息。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">当虚拟装载一个class文件后,它会从这个class文件包含的二进制数据中解析类型信息,然后将该相关信息存储到方法区中。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;color: rgb(64, 179, 230);">2.堆</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">堆是用来存储相关引用类型的,如new对象。当程序运行时,虚拟机会把所有该程序在运行时创建的对象都放到堆中。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;color: rgb(64, 179, 230);">3.PC寄存器</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">PC寄存器主要用来存储线程。当新创建一个线程时,该线程都将得到一个自己的PC寄存器(程序计数器)以及一个java栈。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">Java虚拟机没有寄存器,其指令集使用Java栈来存储中间数据。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;color: rgb(64, 179, 230);">4.栈区</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">栈区主要用来存储值类型的,如基本数据类型。需要注意的是,String为引用类型,是存在堆中的。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">Java栈是由许多栈帧组成的,一个栈帧包含一个Java方法调用的状态,当线程调用一个方法时,虚拟机压入一个新的栈帧到该线程的Java栈中,当该方法返回时,这个栈帧从Java栈中弹出。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="633" src="/upload/47197c4a6bdff248083e877fdae19ad6.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><strong><br></strong></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><strong>(三)执行引擎与运行时数据区交互</strong></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">运行时数据区为执行引擎提供了执行环境和相关数据,执行引擎通过与运行时数据区交互,从而获取执行时需要的相关信息,存储执行的中间结果等</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);">&nbsp;<img class="" data-ratio="0.6666666666666666" data-type="png" data-w="633" src="/upload/cb9a742fc3ce897868548adc92865a5.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><strong><br></strong></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><strong>(四)执行引擎与本地方法接口</strong></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">当要执行本地方法时,执行引擎将调用本地方法接口来获取相关OS本地方法。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">需要注意的是,本地方法与操作系统强耦合的。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="634" src="/upload/dadd2d8e8ae84c634c69c14083a1a168.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><strong><br></strong></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;"><strong>(五)JVM在具体操作系统上执行</strong></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 15px;">JVM通过调用本地接口来获取本地方法,从而实现在具体的平台上执行。比如在Linux系统上执行,在Window系统上执行和在Unix系统上执行。</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><img class="" data-ratio="0.6666666666666666" data-type="png" data-w="637" src="/upload/558dc8d672b4d39cdc4fae35e408fa5b.png" style="border-width: 0px;border-style: initial;border-color: initial;max-width: 820px;"></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="font-size: 18px;"><br></span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(64, 118, 0);"><strong><span style="font-size: 18px;">五&nbsp; 参考文献</span></strong></span><strong><span style="font-size: 18px;"></span></strong></p> <hr style="color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="font-size: 12px;">1、深入Java虚拟机(原书第2版)(美)Bill Venners 著&nbsp;&nbsp;</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="font-size: 12px;">2、Core Java Volume I - Fundamententals(10th Edition)&nbsp;</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="font-size: 12px;">3、Core Java Volume I - Advanced Features(10th Edition)&nbsp;</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: center;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(64, 118, 0);"><strong><span style="font-size: 24px;">End</span></strong></span><strong><span style="color: rgb(241, 136, 35);font-size: 24px;"></span></strong></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 12px;">作者:Alan</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: normal;"><span style="font-size: 12px;">来源:</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 12px;">http://www.cnblogs.com/wangjiming/</span></p> <p style="margin: 10px auto;color: rgb(0, 0, 0);font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;"><span style="font-size: 12px;">本文版权归作者所有</span></p> <ol class=" list-paddingleft-2" style="padding-left: 2.2em; color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; text-align: justify; white-space: normal;"></ol> <hr style="color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; text-align: justify; white-space: normal;"> <p style="color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; white-space: normal; line-height: 2em; text-align: center;"><span style="font-size: 14px; letter-spacing: 0.544px;">长按下图二维码,即刻关注【</span><span style="font-size: 14px; letter-spacing: 0.544px; color: rgb(241, 136, 35);"><strong>狸猫技术窝</strong></span><span style="font-size: 14px; letter-spacing: 0.544px;">】</span><br></p> <p style="color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; white-space: normal; line-height: 2em; text-align: center;"><span style="color: rgb(0, 0, 0); font-size: 14px; letter-spacing: 0.544px;">阿里、京东、美团、字节跳动</span><br></p> <p style="color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; white-space: normal; line-height: 2em; text-align: center;"><span style="font-size: 14px;"><strong><span style="font-size: 13px; color: rgb(0, 0, 0);">顶尖技术专家</span></strong><span style="font-size: 13px; color: rgb(0, 0, 0);">坐镇</span></span></p> <p style="color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; white-space: normal; line-height: 2em; text-align: center;"><span style="color: rgb(0, 0, 0); font-size: 14px;">为IT人打造一个 “有温度” 的技术窝!</span></p> <p></p> <section class="" data-tools-id="79539" style="color: rgb(51, 51, 51); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; text-align: justify; white-space: normal;"> <section style="margin: 10px auto; display: flex; justify-content: center; align-items: center;"> <section style="padding: 30px; background-image: url(&quot;https://mmbiz.qpic.cn/mmbiz_gif/vnOqylzBGCQHu6jrGZMlKlQlriarYFuN2jzLbPCJRlbydFZSlxHHLALHPenWJGC9WFtiaRFyMvicibKbv6nwdHibbiaw/640?wx_fmt=gif&quot;); background-position: center center; background-repeat: repeat;"> <p style="text-align: center;"><img class="rich_pages" data-copyright="0" data-ratio="0.6666666666666666" data-s="300,640" data-type="jpeg" data-w="258" src="/upload/578288c965b89f74b583291d3fc98c86.jpg" style="height: auto !important; visibility: visible !important; width: 258px !important;" width="258px"></p> </section> </section> </section> <p></p> <p><br></p>

Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍

作者:じ☆ve宝贝

在这里对jedis关于事务、管道和分布式的调用方式做一个简单的介绍和对比: ###一、普通同步方式 最简单和基础的调用方式, ``` @Test public void test1Normal() { Jedis jedis = new Jedis("localhost"); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = jedis.set("n" + i, "n" + i); } long end = System.currentTimeMillis(); System.out.println("Simple SET: " + ((end - start)/1000.0) + " seconds"); jedis.disconnect(); } ``` 很简单吧,每次set之后都可以返回结果,标记是否成功。 ###二、事务方式(Transactions) redis的事务很简单,他主要目的是保障,一个client发起的事务中的命令可以连续的执行,而中间不会插入其他client的命令。 看下面例子: ``` @Test public void test2Trans() { Jedis jedis = new Jedis("localhost"); long start = System.currentTimeMillis(); Transaction tx = jedis.multi(); for (int i = 0; i < 100000; i++) { tx.set("t" + i, "t" + i); } List<Object> results = tx.exec(); long end = System.currentTimeMillis(); System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds"); jedis.disconnect(); } ``` 我们调用jedis.watch(…)方法来监控key,如果调用后key值发生变化,则整个事务会执行失败。另外,事务中某个操作失败,并不会回滚其他操作。这一点需要注意。还有,我们可以使用discard()方法来取消事务。 ###三、管道(Pipelining) 有时,我们需要采用异步方式,一次发送多个指令,不同步等待其返回结果。这样可以取得非常好的执行效率。这就是管道,调用方法如下: ``` @Test public void test3Pipelined() { Jedis jedis = new Jedis("localhost"); Pipeline pipeline = jedis.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("p" + i, "p" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("Pipelined SET: " + ((end - start)/1000.0) + " seconds"); jedis.disconnect(); } ``` ###四、管道中调用事务 就Jedis提供的方法而言,是可以做到在管道中使用事务,其代码如下: ``` @Test public void test4combPipelineTrans() { jedis = new Jedis("localhost"); long start = System.currentTimeMillis(); Pipeline pipeline = jedis.pipelined(); pipeline.multi(); for (int i = 0; i < 100000; i++) { pipeline.set("" + i, "" + i); } pipeline.exec(); List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("Pipelined transaction: " + ((end - start)/1000.0) + " seconds"); jedis.disconnect(); } ``` 但是经测试(见本文后续部分),发现其效率和单独使用事务差不多,甚至还略微差点。 ###五、分布式直连同步调用 ``` @Test public void test5shardNormal() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6380)); ShardedJedis sharding = new ShardedJedis(shards); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = sharding.set("sn" + i, "n" + i); } long end = System.currentTimeMillis(); System.out.println("Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds"); sharding.disconnect(); } ``` 这个是分布式直接连接,并且是同步调用,每步执行都返回执行结果。类似地,还有异步管道调用。 ###六、分布式直连异步调用 ``` @Test public void test6shardpipelined() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6380)); ShardedJedis sharding = new ShardedJedis(shards); ShardedJedisPipeline pipeline = sharding.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("sp" + i, "p" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds"); sharding.disconnect(); } ``` ###七、分布式连接池同步调用 如果,你的分布式调用代码是运行在线程中,那么上面两个直连调用方式就不合适了,因为直连方式是非线程安全的,这个时候,你就必须选择连接池调用。 ``` @Test public void test7shardSimplePool() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6380)); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis one = pool.getResource(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = one.set("spn" + i, "n" + i); } long end = System.currentTimeMillis(); pool.returnResource(one); System.out.println("Simple@Pool SET: " + ((end - start)/1000.0) + " seconds"); pool.destroy(); } ``` 上面是同步方式,当然还有异步方式。 ###八、分布式连接池异步调用 ``` @Test public void test8shardPipelinedPool() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6380)); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis one = pool.getResource(); ShardedJedisPipeline pipeline = one.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("sppn" + i, "n" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); pool.returnResource(one); System.out.println("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds"); pool.destroy(); } ``` ###九、需要注意的地方 事务和管道都是异步模式。在事务和管道中不能同步查询结果。比如下面两个调用,都是不允许的: ``` Transaction tx = jedis.multi(); for (int i = 0; i < 100000; i++) { tx.set("t" + i, "t" + i); } System.out.println(tx.get("t1000").get()); //不允许 List<Object> results = tx.exec(); … … Pipeline pipeline = jedis.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("p" + i, "p" + i); } System.out.println(pipeline.get("p1000").get()); //不允许 List<Object> results = pipeline.syncAndReturnAll(); ``` 事务和管道都是异步的,个人感觉,在管道中再进行事务调用,没有必要,不如直接进行事务模式。 分布式中,连接池的性能比直连的性能略好(见后续测试部分)。 分布式调用中不支持事务。 因为事务是在服务器端实现,而在分布式中,每批次的调用对象都可能访问不同的机器,所以,没法进行事务。 ###十、测试 运行上面的代码,进行测试,其结果如下: ``` Simple SET: 5.227 seconds Transaction SET: 0.5 seconds Pipelined SET: 0.353 seconds Pipelined transaction: 0.509 seconds Simple@Sharing SET: 5.289 seconds Pipelined@Sharing SET: 0.348 seconds Simple@Pool SET: 5.039 seconds Pipelined@Pool SET: 0.401 seconds ``` 另外,经测试分布式中用到的机器越多,调用会越慢。上面是2片,下面是5片: ``` Simple@Sharing SET: 5.494 seconds Pipelined@Sharing SET: 0.51 seconds Simple@Pool SET: 5.223 seconds Pipelined@Pool SET: 0.518 seconds ``` 下面是10片: ``` Simple@Sharing SET: 5.9 seconds Pipelined@Sharing SET: 0.794 seconds Simple@Pool SET: 5.624 seconds Pipelined@Pool SET: 0.762 seconds ``` 下面是100片: ``` Simple@Sharing SET: 14.055 seconds Pipelined@Sharing SET: 8.185 seconds Simple@Pool SET: 13.29 seconds Pipelined@Pool SET: 7.767 seconds ``` 分布式中,连接池方式调用不但线程安全外,根据上面的测试数据,也可以看出连接池比直连的效率更好。 ###十一、完整的测试代码 ``` package com.example.nosqlclient; import java.util.Arrays; import java.util.List; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.Pipeline; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPipeline; import redis.clients.jedis.ShardedJedisPool; import redis.clients.jedis.Transaction; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestJedis { private static Jedis jedis; private static ShardedJedis sharding; private static ShardedJedisPool pool; @BeforeClass public static void setUpBeforeClass() throws Exception { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6379)); //使用相同的ip:port,仅作测试 jedis = new Jedis("localhost"); sharding = new ShardedJedis(shards); pool = new ShardedJedisPool(new JedisPoolConfig(), shards); } @AfterClass public static void tearDownAfterClass() throws Exception { jedis.disconnect(); sharding.disconnect(); pool.destroy(); } @Test public void test1Normal() { long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = jedis.set("n" + i, "n" + i); } long end = System.currentTimeMillis(); System.out.println("Simple SET: " + ((end - start)/1000.0) + " seconds"); } @Test public void test2Trans() { long start = System.currentTimeMillis(); Transaction tx = jedis.multi(); for (int i = 0; i < 100000; i++) { tx.set("t" + i, "t" + i); } //System.out.println(tx.get("t1000").get()); List<Object> results = tx.exec(); long end = System.currentTimeMillis(); System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds"); } @Test public void test3Pipelined() { Pipeline pipeline = jedis.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("p" + i, "p" + i); } //System.out.println(pipeline.get("p1000").get()); List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("Pipelined SET: " + ((end - start)/1000.0) + " seconds"); } @Test public void test4combPipelineTrans() { long start = System.currentTimeMillis(); Pipeline pipeline = jedis.pipelined(); pipeline.multi(); for (int i = 0; i < 100000; i++) { pipeline.set("" + i, "" + i); } pipeline.exec(); List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("Pipelined transaction: " + ((end - start)/1000.0) + " seconds"); } @Test public void test5shardNormal() { long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = sharding.set("sn" + i, "n" + i); } long end = System.currentTimeMillis(); System.out.println("Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds"); } @Test public void test6shardpipelined() { ShardedJedisPipeline pipeline = sharding.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("sp" + i, "p" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds"); } @Test public void test7shardSimplePool() { ShardedJedis one = pool.getResource(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = one.set("spn" + i, "n" + i); } long end = System.currentTimeMillis(); pool.returnResource(one); System.out.println("Simple@Pool SET: " + ((end - start)/1000.0) + " seconds"); } @Test public void test8shardPipelinedPool() { ShardedJedis one = pool.getResource(); ShardedJedisPipeline pipeline = one.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("sppn" + i, "n" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); pool.returnResource(one); System.out.println("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds"); } } ```

springboot java.lang.ClassNotFoundException: javax.servlet.jsp.JspFactory

作者:じ☆ve宝贝

> springboot 使用jsp 出现java.lang.ClassNotFoundException: javax.servlet.jsp.JspFactory 原因:我使用的springboot 1.5.6.RELEASE版本,使用时的jdk1.8.0_101会出现如上错误,升级为jdk1.8.0_121 即可解决

Pycharm 软件注册

作者:じ☆ve宝贝

1 ,安装 2 , 调整时间到2038年。 3 ,申请30天试用 4, 退出pycharm 5, 时间调整回来。 ##注册方法2### 注册方法: 在 注册时选择 License server ,填 http://idea.lanyus.com ,然后点击 OK

MySQL update jine = jine -10 where jine > 0 当jine 等于 5 的时候居然会执行成功

作者:じ☆ve宝贝

``` mysql update jine = jine -10 where jine > 0 当jine 等于 5 的时候居然会执行成功 ``` 检查**sql_mode**,有无启用严格模式,或特别设置了NO_UNSIGNED_SUBTRACTION。正常应该是有1690错误。