锘??xml version="1.0" encoding="utf-8" standalone="yes"?>BlogJava-SIMONEhttp://www.8634070.com/wangxinsh55/zh-cnFri, 07 Jun 2019 05:02:47 GMTFri, 07 Jun 2019 05:02:47 GMT60nodejs灏嗗璞¤浆鎹㈡垚瀛楃涓蹭唬鐮?鍔?#37804;佹墽琛?#28699;楃涓蹭唬鐮?requirejs浣跨敤r.js鎵撳寘鏃?#37716;?#37804;佺敓?#22426;?#21412;缃枃浠?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/11/01/431944.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Tue, 01 Nov 2016 08:24:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/11/01/431944.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/431944.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/11/01/431944.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/431944.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/431944.html</trackback:ping><description><![CDATA[<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000FF; ">var</span><span style="color: #000000; "> path </span><span style="color: #000000; ">=</span><span style="color: #000000; "> require('path');<br /> </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> fs </span><span style="color: #000000; ">=</span><span style="color: #000000; "> require('fs');<br /> </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> vm </span><span style="color: #000000; ">=</span><span style="color: #000000; "> require('vm');<br /> </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> os </span><span style="color: #000000; ">=</span><span style="color: #000000; "> require('os');<br /> <br /> </span><span style="color: #008000; ">/*</span><span style="color: #008000; ">*<br />  * 鏍煎紡鍖栫缉杩涚殑涓暟<br />  </span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br /> </span><span style="color: #0000FF; ">function</span><span style="color: #000000; "> toIndent(indent) {<br />     </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> s </span><span style="color: #000000; ">=</span><span style="color: #000000; "> [];<br />     </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> i </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">; i </span><span style="color: #000000; "><</span><span style="color: #000000; "> indent; i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">) {<br />         s.push('\t');<br />     }<br />     </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> s.join('');<br /> }<br /> <br /> </span><span style="color: #008000; ">/*</span><span style="color: #008000; ">*<br />  * 灏嗘暟缁勫璞¤浆鎹㈡垚鍘熷瀛楃涓?br />  </span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br /> </span><span style="color: #0000FF; ">function</span><span style="color: #000000; "> array2string(arr, indent) {<br />     </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> s </span><span style="color: #000000; ">=</span><span style="color: #000000; "> ['[', os.EOL], hasProp </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">;<br />     </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> i </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">; i </span><span style="color: #000000; "><</span><span style="color: #000000; "> arr.length; i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">) {<br />         </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (</span><span style="color: #000000; ">!</span><span style="color: #000000; ">hasProp) {<br />             hasProp </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">;<br />         }<br /> <br />         s.push(toIndent(indent </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">));<br /> <br />         </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> item </span><span style="color: #000000; ">=</span><span style="color: #000000; "> arr[i];<br />         </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> itemtp </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">typeof</span><span style="color: #000000; ">(item);<br />         </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (itemtp </span><span style="color: #000000; ">===</span><span style="color: #000000; "> 'object') {<br />             </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (item </span><span style="color: #0000FF; ">instanceof</span><span style="color: #000000; "> Array) {<br />                 s.push(array2string(item, indent </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">));<br />             } </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "> {<br />                 s.splice(s.length </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">2</span><span style="color: #000000; ">, </span><span style="color: #000000; ">2</span><span style="color: #000000; ">);<br />                 s.push(object2strng(item, indent).trim());<br />             }<br />         } </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "> {<br />             s.push(JSON.stringify(item));<br />         }<br />         s.push(',');<br />         s.push(os.EOL);<br />     }<br />     </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (hasProp) {<br />         s.splice(s.length </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">2</span><span style="color: #000000; ">, </span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br />     }<br />     s.push(toIndent(indent));<br />     s.push(']');<br />     </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> s.join('');<br /> }<br /> <br /> </span><span style="color: #008000; ">/*</span><span style="color: #008000; ">*<br />  * 灏嗗璞¤浆鎹㈡垚鍘熷瀛楃涓?br />  </span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br /> </span><span style="color: #0000FF; ">function</span><span style="color: #000000; "> object2strng(obj, indent) {<br />     </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> s </span><span style="color: #000000; ">=</span><span style="color: #000000; "> ['{', os.EOL], hasProp </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">;<br /> <br />     </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> o </span><span style="color: #0000FF; ">in</span><span style="color: #000000; "> obj) {<br />         </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (</span><span style="color: #000000; ">!</span><span style="color: #000000; ">hasProp) {<br />             hasProp </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">;<br />         }<br />         s.push(toIndent(indent </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">));<br />         s.push(JSON.stringify(o));<br />         s.push(':');<br /> <br />         </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> tp </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">typeof</span><span style="color: #000000; ">(obj[o]);<br />         </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (tp </span><span style="color: #000000; ">===</span><span style="color: #000000; "> 'object') {<br />             </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (obj[o] </span><span style="color: #0000FF; ">instanceof</span><span style="color: #000000; "> Array) {<br />                 s.push(array2string(obj[o], indent </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">));<br />             } </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "> {<br />                 s.push(object2strng(obj[o], indent </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">));<br />             }<br />         } </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (tp </span><span style="color: #000000; ">===</span><span style="color: #000000; "> '</span><span style="color: #0000FF; ">function</span><span style="color: #000000; ">') {<br />             s.push(obj[o].toString());<br />         } </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "> {<br />             s.push(JSON.stringify(obj[o]));<br />         }<br />         s.push(',');<br />         s.push(os.EOL);<br />     }<br />     </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (hasProp) {<br />         s.splice(s.length </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">2</span><span style="color: #000000; ">, </span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br />     }<br />     s.push(toIndent(indent));<br />     s.push('}');<br />     </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> s.join('');<br /> }<br /> <br /> </span><span style="color: #008000; ">//</span><span style="color: #008000; ">鎻愬彇姝e紡?#21804;鐮?#38322;岀殑requirejs?#27537;?#21412;缃?#28699;楃涓?骞?#37716;?#37804;佹墽琛岃浆鎹㈡垚json瀵?#29854;? 淇敼鐩稿叧?#27537;鍊?#28103;?#37805;?#28051;?#28051;嬭竟?#27537;鎵撳寘?#25655;浣滃仛鍑嗗; 骞?#28751;嗛厤缃?#28103;?#37805;?#37712;嶈浆?#22426;瀛楃涓?#35120;㈠紡鍐欏埌涓?#37827;舵枃浠?#28051;?/span><span style="color: #008000; "><br /> </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> mainPath </span><span style="color: #000000; ">=</span><span style="color: #000000; "> path.resolve(process.cwd(), '..</span><span style="color: #000000; ">/</span><span style="color: #000000; ">js</span><span style="color: #000000; ">/</span><span style="color: #000000; ">main.js');<br /> </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> mainContent </span><span style="color: #000000; ">=</span><span style="color: #000000; "> fs.readFileSync(mainPath, 'utf</span><span style="color: #000000; ">-</span><span style="color: #000000; ">8</span><span style="color: #000000; ">').replace(</span><span style="color: #000000; ">/</span><span style="color: #000000; ">(requirejs\.config\()</span><span style="color: #000000; ">?</span><span style="color: #000000; ">([</span><span style="color: #000000; ">^</span><span style="color: #000000; ">)]]</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)(\);)</span><span style="color: #000000; ">?/</span><span style="color: #000000; ">gm, '$</span><span style="color: #000000; ">2</span><span style="color: #000000; ">');<br /> vm.runInThisContext('</span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> mainCfg</span><span style="color: #000000; ">=</span><span style="color: #000000; "> ' </span><span style="color: #000000; ">+</span><span style="color: #000000; "> mainContent);</span><span style="color: #008000; ">//</span><span style="color: #008000; ">灏嗘?#24876;?#26667;殑瀛楃涓茶浆?#22426;mainCfg瀵?#29854;?/span><span style="color: #008000; "><br /> </span><span style="color: #000000; ">mainCfg.baseUrl </span><span style="color: #000000; ">=</span><span style="color: #000000; "> '</span><span style="color: #000000; ">/</span><span style="color: #000000; ">static</span><span style="color: #000000; ">/</span><span style="color: #000000; ">js</span><span style="color: #000000; ">/</span><span style="color: #000000; ">dist</span><span style="color: #000000; ">/</span><span style="color: #000000; ">lib';<br /> </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> nMainCfgStr </span><span style="color: #000000; ">=</span><span style="color: #000000; "> 'requirejs.config(' </span><span style="color: #000000; ">+</span><span style="color: #000000; "> object2strng(mainCfg, </span><span style="color: #000000; ">0</span><span style="color: #000000; ">) </span><span style="color: #000000; ">+</span><span style="color: #000000; "> ');';</span><span style="color: #008000; ">//</span><span style="color: #008000; ">閲?#37826;扮敓?#22426;main.js?#21412;缃枃浠?涓?#28051;嬭竟?#27537;鎵撳寘?#20187;鍑嗗</span><span style="color: #008000; "><br /> </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> buildPath </span><span style="color: #000000; ">=</span><span style="color: #000000; "> path.resolve(process.cwd(), '.</span><span style="color: #000000; ">/</span><span style="color: #000000; ">main.js');<br /> fs.writeFileSync(buildPath, nMainCfgStr);<br /> console.log('write temp file main.js fininshed');<br /> <br /> </span><span style="color: #008000; ">//</span><span style="color: #008000; ">鎵撳寘?#27537;?#21412;缃?#28103;?#37805;?/span><span style="color: #008000; "><br /> </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> buildJson </span><span style="color: #000000; ">=</span><span style="color: #000000; "> {<br />     appDir: '..</span><span style="color: #000000; ">/</span><span style="color: #000000; ">js',<br />     baseUrl: 'lib',<br />     mainConfigFile: '.</span><span style="color: #000000; ">/</span><span style="color: #000000; ">main.js',<br />     dir: '..</span><span style="color: #000000; ">/</span><span style="color: #000000; ">js</span><span style="color: #000000; ">/</span><span style="color: #000000; ">dist',<br />     modules: [{<br />         'name': '..</span><span style="color: #000000; ">/</span><span style="color: #000000; ">main',<br />         include: []<br />     }]<br /> };<br /> </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> p </span><span style="color: #0000FF; ">in</span><span style="color: #000000; "> mainCfg.paths) {</span><span style="color: #008000; ">//</span><span style="color: #008000; ">杩?#38322;?#37819;愬彇鎵鏈夌殑渚濊禆妯″潡,鎵撳寘鏃舵斁?#22476;main.js?#26499;浠?#28051;?/span><span style="color: #008000; "><br /> </span><span style="color: #000000; ">    buildJson.modules[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">].include.push(p);<br /> }<br /> <br /> </span><span style="color: #0000FF; ">var</span><span style="color: #000000; "> buildPath </span><span style="color: #000000; ">=</span><span style="color: #000000; "> path.resolve(process.cwd(), '.</span><span style="color: #000000; ">/</span><span style="color: #000000; ">build_main.json');<br /> fs.writeFileSync(buildPath, object2strng(buildJson, </span><span style="color: #000000; ">0</span><span style="color: #000000; ">));</span><span style="color: #008000; ">//</span><span style="color: #008000; ">?#25939;?#22426;鎵撳寘?#21412;缃枃浠?/span><span style="color: #008000; "><br /> </span><span style="color: #000000;">console.log('wirte temp file build_main.json fininshed');<br /> <br /> </span></div><br /><br />鍐?#28051;鎵?#28598;?#37918;嗘枃浠禸uild.bat<br /><div><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:10.5pt;">@echo off<br />node build.js<br />node r.js -o build_main.json<br />@pause</pre></div>?#22717;琛?#28751;?#37721;?#28000;ヤ簡<img src ="http://www.8634070.com/wangxinsh55/aggbug/431944.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-11-01 16:24 <a href="http://www.8634070.com/wangxinsh55/archive/2016/11/01/431944.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title>java keytool璇佷功宸ュ叿浣跨敤灏?#32513;?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/10/20/431905.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Thu, 20 Oct 2016 03:20:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/10/20/431905.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/431905.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/10/20/431905.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/431905.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/431905.html</trackback:ping><description><![CDATA[<br /><div>http://www.micmiu.com/lang/java/keytool-start-guide/</div><br /><div><h2>java keytool璇佷功宸ュ叿浣跨敤灏?#32513;?/h2></div><br /><div><p>Keytool 鏄?#28051;涓狫ava?#26271;鎹?#29831;佷功?#27537;绠?#37918;嗗伐?#21503; ,Keytool灏嗗瘑閽ワ紙key锛夊?#23683;?#20343;功?#32025;certificates锛?#28699;?#37734;?#28051;涓?#32457;?#28051;簁eystore?#27537;?#26499;浠?#28051;?#37734;╧eystore閲?#38171;屽寘?#24776;涓?#32457;嶆暟鎹?瀵?#38333;?#28729;?#28003;擄紙Key entity锛?瀵?#38333;ワ紙secret key锛?#37812;?#38000;呮槸绉?#38333;ュ拰?#21412;瀵瑰叕閽ワ紙閲囩敤闈?#28725;圭О鍔?#28725;?#38171;?#37721;?#28103;?#28000;荤殑璇佷功瀹?#28003;擄紙trusted certificate entries锛?鍙寘?#24776;鍏?#38333;?<br /> <span style="color: #0000ff;">JDK涓璳eytool甯哥敤鍙傛暟璇存槑?#32025;<span style="color: #ff0000;">涓?#37722;岀増鏈?#37832;?#23480;?#23534;?#38171;岃缁嗗彲鍙?#29785;併愰?#21227;?#26328;?#28051;殑瀹?#37826;规枃妗i摼鎺?/span>锛?</span></p> <ul><li>-genkey 鍦ㄧ敤鎴?#28051;?#37929;?#35120;?#28051;?#37714;?#23524;?#28051;涓粯璁ゆ枃浠?#8221;.keystore”,杩樹細浜х敓涓涓猰ykey?#27537;鍒?#37722;?#38171;宮ykey涓寘?#24776;?#25956;鎴风殑鍏?#38333;?#37510;佺閽?#37724;岃?#20343;功(鍦ㄦ病鏈夋寚瀹氱敓?#22426;浣?#32515;殑鎯?#37712;?#28051;?keystore?#32048;瀛?#37734;ㄧ敤鎴风郴缁熼粯璁?#37929;?#35120;?</li><li>-alias 浜х敓鍒?#37722;?姣?#28051;猭eystore閮藉叧鑱?#26473;?#28051;涓嫭涓鏃?#27996;岀殑alias锛?#26473;?#28051;猘lias閫氬父涓?#37718;?#37714;嗗ぇ灏?#37712;?/li><li>-keystore ?#23514;瀹?#28725;?#38333;?#25652;撶殑鍚嶇О(浜х敓?#27537;鍚勭被淇?#37805;?#28751;?#28051;?#37734;?keystore?#26499;浠?#28051;?</li><li>-keyalg ?#23514;瀹?#28725;?#38333;ョ殑?#30075;娉?(濡?RSA DSA锛岄粯璁?#37706;?#28051;?#38171;欴SA)</li><li>-validity ?#23514;瀹氬垱寤虹殑璇佷功鏈?#37825;?#37832;熷灏?#28598;??#31919;璁?90)</li><li>-keysize ?#23514;瀹?#28725;?#38333;?#38336;?#25652;??#32025;?#31919;璁?1024锛?/li><li>-storepass ?#23514;瀹?#28725;?#38333;?#25652;撶殑瀵?#37934;?鑾峰彇keystore淇?#37805;?#37813;闇?#27537;瀵?#37934;?</li><li>-keypass ?#23514;瀹氬埆鍚嶆潯鐩殑瀵?#37934;?绉?#38333;ョ殑瀵?#37934;?</li><li>-dname ?#23514;瀹?#29831;佷功鍙?#29723;岃?#28103;?#37805;?鍏?#28051;?#38171;?“CN=鍚?#28699;?#28051;庡姘?OU=缁?#32513;囧崟浣?#37722;嶇О,O=缁?#32513;?#37722;嶇О,L=鍩?#29999;?#37812;?#37718;?#37737;熷悕 绉?ST=宸?#37812;栫渷浠?#37722;嶇О,C=鍗?#28003;嶇殑涓?#28699;?#23011;嶅浗瀹朵唬鐮?#8221;</li><li>-list 鏄剧ず瀵?#38333;?#25652;?#28051;殑璇佷功淇?#37805;?keytool -list -v -keystore ?#23514;瀹歬eystore -storepass 瀵?#37934;?/li><li>-v 鏄剧ず瀵?#38333;?#25652;?#28051;殑璇佷功璇?#32513;?#28103;?#37805;?/li><li>-export 灏嗗埆鍚嶆寚瀹氱殑璇佷功瀵煎嚭?#22476;?#26499;浠?keytool -export -alias 闇瑕?#28725;煎嚭?#27537;鍒?#37722;?-keystore ?#23514;瀹歬eystore -file ?#23514;瀹?#28725;煎嚭?#27537;璇佷功浣?#32515;?#37721;?#29831;佷功鍚嶇О -storepass 瀵?#37934;?/li><li>-file 鍙傛暟?#23514;瀹?#28725;煎嚭?#22476;?#26499;浠剁殑?#26499;浠跺悕</li><li>-delete 鍒犻櫎瀵?#38333;?#25652;?#28051;?#37836;?#37833;?#37929;?keytool -delete -alias ?#23514;瀹?#38343;鍒犻櫎?#27537;鍒?-keystore ?#23514;瀹歬eystore – storepass 瀵?#37934;?/li><li>-printcert 鏌?#37930;?#28725;煎嚭?#27537;璇佷功淇?#37805;?keytool -printcert -file g:\sso\michael.crt</li><li>-keypasswd 淇敼瀵?#38333;?#25652;?#28051;寚瀹?#37833;?#37929;?#37721;?#28000;?keytool -keypasswd -alias 闇淇敼?#27537;鍒?#37722;?-keypass 鏃?#28725;?#37934;?-new 鏂?#28725;?#37934;?-storepass keystore瀵?#37934;?-keystore sage</li><li>-storepasswd 淇敼keystore鍙?#28000;?keytool -storepasswd -keystore g:\sso\michael.keystore(闇淇敼鍙?#28000;ょ殑keystore) -storepass pwdold(鍘熷瀵?#37934;? -new pwdnew(鏂?#28725;?#37934;?</li><li>-import 灏嗗凡绛?#37722;嶆暟瀛?#29831;佷功瀵煎叆瀵?#38333;?#25652;?keytool -import -alias ?#23514;瀹?#28725;煎叆鏉?#37929;殑鍒?#37722;?-keystore ?#23514;瀹歬eystore -file 闇瀵煎叆?#27537;璇佷功</li></ul> <div><strong><span style="line-height: 18px;">鐩?#35120;?#29831;存槑锛?/span></strong></div> <div> <ol><li><span style="line-height: 19px;">?#25939;?#22426;璇佷功</span></li><li><span style="line-height: 19px;">鏌?#37930;嬭?#20343;功</span></li><li><span style="line-height: 19px;">璇佷功瀵煎嚭</span></li><li><span style="line-height: 19px;">闄勫綍璧勬枡</span></li></ol> </div> <div><span style="color: #0000ff;">涓銆佺敓?#22426;璇佷功</span></div> <div> 鎸墂in閿?R锛屽脊鍑?#26473;?#29723;岀獥鍙?#38171;岃?#25779;叆 cmd 鍥炶溅锛?#37813;撳紑鍛?#28000;?#29723;岀獥鎴?#38171;岃?#25779;叆濡?#28051;?#37723;?#28000;?#38171;?/div> <div> <div id="crayon-5806e9cc4d76c170187796" crayon-theme-solarized-dark="" crayon-font-monaco="" crayon-os-pc="" print-yes="" notranslate"="" data-settings=" minimize scroll-mouseover" style="margin-top: 12px; margin-bottom: 12px; font-size: 12px ! important; line-height: 15px ! important; height: auto;"> <div data-settings=" show" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"> </div> <div><textarea print-no"="" data-settings="dblclick" readonly="readonly" style="-moz-tab-size: 4; font-size: 12px ! important; line-height: 15px ! important; z-index: 0; opacity: 0; overflow: hidden;" wrap="soft">keytool -genkey -alias michaelkey -keyalg RSA -keysize 1024 -keypass michaelpwd -validity 365 -keystore g:\sso\michael.keystore -storepass michaelpwd2</textarea></div> <div style="position: relative; z-index: 1; overflow: hidden;"> <table> <tbody><tr> <td "="" data-settings="show"> <div style="font-size: 12px !important; line-height: 15px !important;"><div data-line="crayon-5806e9cc4d76c170187796-1">1</div></div> </td> <td><div style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div id="crayon-5806e9cc4d76c170187796-1">keytool -genkey -alias michaelkey -keyalg RSA -keysize 1024 -keypass michaelpwd -validity 365 -keystore g:\sso\michael.keystore -storepass michaelpwd2</div></div></td> </tr> </tbody></table> </div> </div> </div> <div>鎴?#37733;?#28641;?#28051;?#38171;?/div> <div><a href="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-01.jpg"><img size-full="" wp-image-600"="" title="keytool-01" src="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-01.jpg" alt="" height="286" width="647" /></a></div> <div><span style="color: #0000ff;">浜?#37510;佹煡鐪嬭?#20343;功</span></div> <div> <p>缂?#37930;佹儏鍐?#28051;?#38171;?list 鍛?#28000;?#37813;撳嵃璇佷功?#27537; MD5 鎸囩汗銆?#38000;屽?#20635;?#28356;寚瀹氫簡 -v 閫夐」锛?#28751;?#28000;?#37721;?#29831;?#37837;煎紡鎵撳嵃璇佷功锛屽?#20635;?#28356;寚瀹氫簡 -rfc 閫夐」锛?#28751;?#28000;?#37721;?#37813;撳嵃?#27537;缂?#37934;佹?#29006;紡杈撳嚭璇佷功銆?/p> </div> <div><span style="color: #0000ff;">-v 鍛?#28000;?#28641;?#28051;?#38171;?/span></div> <div> <div id="crayon-5806e9cc4d78c797748914" crayon-theme-solarized-dark="" crayon-font-monaco="" crayon-os-pc="" print-yes="" notranslate"="" data-settings=" minimize scroll-mouseover" style="margin-top: 12px; margin-bottom: 12px; font-size: 12px ! important; line-height: 15px ! important; height: auto;"> <div data-settings=" show" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"> </div> <div><textarea print-no"="" data-settings="dblclick" readonly="readonly" style="-moz-tab-size: 4; font-size: 12px ! important; line-height: 15px ! important; z-index: 0; opacity: 0; overflow: hidden;" wrap="soft">keytool -list -v -keystore g:\sso\michael.keystore -storepass michaelpwd2</textarea></div> <div style="position: relative; z-index: 1; overflow: hidden;"> <table> <tbody><tr> <td "="" data-settings="show"> <div style="font-size: 12px !important; line-height: 15px !important;"><div data-line="crayon-5806e9cc4d78c797748914-1">1</div></div> </td> <td><div style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div id="crayon-5806e9cc4d78c797748914-1">keytool -list  -v -keystore g:\sso\michael.keystore -storepass michaelpwd2</div></div></td> </tr> </tbody></table> </div> </div> </div> <div>鍥炶溅鐪嬪埌?#27537;淇?#37805;?#28641;?#28051;?#38171;?/div> <div><a href="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-02.jpg"><img size-full="" wp-image-601"="" title="keytool-02" src="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-02.jpg" alt="" height="429" width="647" /></a></div> <div><span style="color: #0000ff;">-rfc 鍛?#28000;?#28641;?#28051;?#38171;?/span></div> <div> <div id="crayon-5806e9cc4d79f689847244" crayon-theme-solarized-dark="" crayon-font-monaco="" crayon-os-pc="" print-yes="" notranslate"="" data-settings=" minimize scroll-mouseover" style="margin-top: 12px; margin-bottom: 12px; font-size: 12px ! important; line-height: 15px ! important; height: auto;"> <div data-settings=" show" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"> </div> <div><textarea print-no"="" data-settings="dblclick" readonly="readonly" style="-moz-tab-size: 4; font-size: 12px ! important; line-height: 15px ! important; z-index: 0; opacity: 0; overflow: hidden;" wrap="soft">keytool -list -rfc -keystore g:\sso\michael.keystore -storepass michaelpwd2</textarea></div> <div style="position: relative; z-index: 1; overflow: hidden;"> <table> <tbody><tr> <td "="" data-settings="show"> <div style="font-size: 12px !important; line-height: 15px !important;"><div data-line="crayon-5806e9cc4d79f689847244-1">1</div></div> </td> <td><div style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div id="crayon-5806e9cc4d79f689847244-1">keytool -list -rfc -keystore g:\sso\michael.keystore -storepass michaelpwd2</div></div></td> </tr> </tbody></table> </div> </div> </div> <div>鍥炶溅鐪嬪埌?#27537;淇?#37805;?#28641;?#28051;?#38171;?/div> <div><a href="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-03.jpg"><img size-full="" wp-image-602"="" title="keytool-03" src="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-03.jpg" alt="" height="491" width="649" /></a></div> <div><span style="color: #0000ff;">涓?#37510;?#29831;佷功?#27537;瀵煎嚭?#25328;鏌?#37930;?#38171;?/span></div> <div><span style="color: #ff0000;">瀵煎嚭璇佷功鍛?#28000;?/span>锛?/div> <div> <div id="crayon-5806e9cc4d7b3204187001" crayon-theme-solarized-dark="" crayon-font-monaco="" crayon-os-pc="" print-yes="" notranslate"="" data-settings=" minimize scroll-mouseover" style="margin-top: 12px; margin-bottom: 12px; font-size: 12px ! important; line-height: 15px ! important; height: auto;"> <div data-settings=" show" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"> </div> <div><textarea print-no"="" data-settings="dblclick" readonly="readonly" style="-moz-tab-size: 4; font-size: 12px ! important; line-height: 15px ! important; z-index: 0; opacity: 0; overflow: hidden;" wrap="soft">keytool -export -alias michaelkey -keystore g:\sso\michael.keystore -file g:\sso\michael.crt -storepass michaelpwd2</textarea></div> <div style="position: relative; z-index: 1; overflow: hidden;"> <table> <tbody><tr> <td "="" data-settings="show"> <div style="font-size: 12px !important; line-height: 15px !important;"><div data-line="crayon-5806e9cc4d7b3204187001-1">1</div></div> </td> <td><div style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div id="crayon-5806e9cc4d7b3204187001-1">keytool -export -alias michaelkey -keystore g:\sso\michael.keystore -file g:\sso\michael.crt -storepass michaelpwd2</div></div></td> </tr> </tbody></table> </div> </div> </div> <div>鍥炶溅濡?#28051;?#38171;?/div> <div><a href="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-04.jpg"><img size-full="" wp-image-604"="" title="keytool-04" src="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-04.jpg" alt="" height="80" width="645" /></a></div> <div><span style="color: #ff0000;">鏌?#37930;?#28725;煎嚭?#27537;璇佷功淇?#37805;?/span>锛?/div> <div> <div id="crayon-5806e9cc4d7c5981815595" crayon-theme-solarized-dark="" crayon-font-monaco="" crayon-os-pc="" print-yes="" notranslate"="" data-settings=" minimize scroll-mouseover" style="margin-top: 12px; margin-bottom: 12px; font-size: 12px ! important; line-height: 15px ! important; height: auto;"> <div data-settings=" show" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"> </div> <div><textarea print-no"="" data-settings="dblclick" readonly="readonly" style="-moz-tab-size: 4; font-size: 12px ! important; line-height: 15px ! important; z-index: 0; opacity: 0; overflow: hidden;" wrap="soft">keytool -printcert -file g:\sso\michael.crt</textarea></div> <div style="position: relative; z-index: 1; overflow: hidden;"> <table> <tbody><tr> <td "="" data-settings="show"> <div style="font-size: 12px !important; line-height: 15px !important;"><div data-line="crayon-5806e9cc4d7c5981815595-1">1</div></div> </td> <td><div style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div id="crayon-5806e9cc4d7c5981815595-1">keytool -printcert -file g:\sso\michael.crt</div></div></td> </tr> </tbody></table> </div> </div> </div> <div>鍥炶溅鐪嬪埌淇?#37805;?#28641;?#28051;?#38171;?/div> <div><a href="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-05.jpg"><img size-full="" wp-image-603"="" title="keytool-05" src="http://www.micmiu.com/wp-content/uploads/2012/05/keytool-05.jpg" alt="" height="181" width="642" /></a></div> <div><span style="color: #0000ff;">鍥?#38171;?#38340;勫綍</span></div> <div>瀹?#37826;?#37832;夊叧keytool鍛?#28000;ょ殑浠?#32513;嶆枃妗?#38171;?/div> <div> <ul><li><span style="line-height: 19px;">jdk1.4.2 锛歨ttp://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/keytool.html</span></li><li><span style="line-height: 19px;">jdk1.6    锛歨ttp://docs.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html</span></li><li><span style="line-height: 19px;">jdk1.7    锛歨ttp://docs.oracle.com/javase/7/docs/technotes/tools/windows/keytool.html</span></li></ul> </div></div><img src ="http://www.8634070.com/wangxinsh55/aggbug/431905.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-10-20 11:20 <a href="http://www.8634070.com/wangxinsh55/archive/2016/10/20/431905.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title>绉?#37716;?#32468;疻eb寮鍙戣皟璇?#28052;婥hrome杩滅?#23341;皟璇?Remote Debugging)http://www.8634070.com/wangxinsh55/archive/2016/10/12/431884.htmlSIMONESIMONEWed, 12 Oct 2016 02:29:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/10/12/431884.htmlhttp://www.8634070.com/wangxinsh55/comments/431884.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/10/12/431884.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431884.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431884.html
http://www.cnblogs.com/terrylin/p/4606277.html


]]>
Genymotion 瑙?#37712;?#38095;氭?#29116;?#28355;儚涓嬭浇閫熷害鐗?#37714;?#37809;㈢殑闂?#26864;?http://www.8634070.com/wangxinsh55/archive/2016/10/11/431881.htmlSIMONESIMONETue, 11 Oct 2016 06:44:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/10/11/431881.htmlhttp://www.8634070.com/wangxinsh55/comments/431881.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/10/11/431881.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431881.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431881.htmlhttp://blog.csdn.net/qing666888/article/details/51622762

Genymotion鍙?#32457;?a href="http://lib.csdn.net/base/15" title="undefined" target="_blank" style="color:#df3434; font-weight:bold;">Android妯?#37815;熷櫒涓?#26473;?#29723;?#37832;蹇殑锛?#28003;嗘槸鏈?#37716;?#37731;?#37734;?#37733;?#28598;?#38171;孉ndroid闀滃儚涓嬭浇璧?#37833;?#38317;?#28051;?#38315;熷害灏?#28051;嶆兂璇翠簡銆?/strong>

Add new device鍚?#28051;嬭浇閫熷害澶?#37809;簡锛屽鏄撳?#36779;触


瑙?#37712;?#37826;规硶濡?#28051;?#38171;?/strong>

鏂规硶涓锛?/strong>

1銆?#29825;?#32515;瓾TTP?#21804;鐞?#38171;屽湪Setting->Network锛岃嚜宸?#29825;?#32515;瓾TTP proxy?#25328;Port锛?/strong>

 

鏂规硶浜?#38171;?/strong>

1銆佹?#24807;埌涓嬭浇?#25724;鎺?#38171;岀?#23384;帴?#25956;杩?#38342;锋嫋涓?#37833;?/strong>

     閬囧埌涓嬭浇澶辫触鎴?#38000;?#28051;嬭浇澶?#37809;?#38171;寃in+R鎵撳紑杩?#29723;?#22935;?#38171;岃?#25779;叆 %appdata%锛?鍐嶇偣鍑?#28051;?#28051;姝ワ紙Alt+↑ 锛?#38171;?#37813;惧埌local?#26499;浠跺す閲岀殑Genymobile,鎵撳紑 鏌?#37930;?#38322;岄潰?#27537;genymotion.log?#26499;浠?#38171;?/span>

鎵惧埌绫?#28028;?#28051;?#38344;㈢殑?#26499;瀛?/strong>

[Genymotion] [Debug] Downloading file

"http://files2.genymotion.com/dists/4.1.1/ova/genymotion_vbox86p_4.1.1_151117_133208.ova"


灏唄ttp://file........ova 杩?#28051;?#38095;氭?#29116;?#28355;儚鍦板潃鐩存帴?#25956;杩?#38342;?#37835;侀?#37911;?#28051;嬭浇锛?#37812;?#38000;?#28003;跨敤杩?#38342;风?#33636;嚎涓嬭浇绛?#37716;熻?#34249;緢蹇兘瀹屾垚涓嬭浇


2銆佹妸涓嬭浇?#27537;?#26499;浠跺鍒跺埌C:\Users\?#25956;鎴?#28051;?#37929;?#35120;?span style="margin:0px; padding:0px; border:0px">\AppData\Local\Genymobile\Genymotion\ova 涓?#29781;?#37929;?#38322;岄潰浠?#38341;?#37832;烘暟鍛?#37722;嶇殑瀵瑰簲闀滃儚銆?/span>瀹?#38340;?#28051;?#28751;?#37828;?#37714;?#37813;嶇湅?#22476;genymotion杞?#28000;跺?#27692;?#27697;偣鍑?#28051;嬭浇?#27537;閭?#28051;?#38336;滃儚锛?/strong>


3銆?/span>閲?#37826;版?#22796;収鍒氬垰涓嬭浇?#25655;浣淕UI?#27537;涓嬭浇姝?#26976;?#38171;?#28003;犱細鍙戠幇瀵瑰簲?#27537;闀滃儚宸?#32513;?#37721;?#28000;?#28003;跨敤?#31777;涓?#38343;瑕?#28051;嬭?#25106;簡锛岄?#23683;瘉瀹?#29761;呭?#24225;?#20805;細鏄剧ず鍦?#29825;?#28598;?#37714;?#29723;?#28051;?#37510;?/span>

?#20579;鍑籹tart 锛屽惎鍔?#22959;?#37815;熷櫒锛屽紑濮?#28003;跨敤



]]>
ubuntu mate 涓嬬殑sublime text 3?#30367;?#25956;涓枃杈撳叆娉曠殑淇敼http://www.8634070.com/wangxinsh55/archive/2016/08/19/431643.htmlSIMONESIMONEFri, 19 Aug 2016 09:53:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/08/19/431643.htmlhttp://www.8634070.com/wangxinsh55/comments/431643.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/08/19/431643.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431643.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431643.html

鏈?#32513;?#26976;岀洰鍓?#37734;?#23480;?#37832;夋?#28357;嫍杈撳叆娉?for Linux?#25328;Sublime Text 3?#27537;鎯?#37712;?#28051;?#28729;?#29761;呮垚鍔?#37510;?/p>


淇?#28699;?#28051;?#38344;㈢殑?#21804;鐮佸埌?#26499;浠秙ublime_imfix.c(浣?#27996;巭鐩?#35120;?


#include <gtk/gtkimcontext.h>

void gtk_im_context_set_client_window (GtkIMContext *context,

         GdkWindow    
*window)

{

 GtkIMContextClass 
*klass;

 g_return_if_fail (GTK_IS_IM_CONTEXT (context));

 klass 
= GTK_IM_CONTEXT_GET_CLASS (context);

 
if (klass->set_client_window)

   klass
->set_client_window (context, window);

 g_object_set_data(G_OBJECT(context),
"window",window);

 
if(!GDK_IS_WINDOW (window))

   
return;

 
int width = gdk_window_get_width(window);

 
int height = gdk_window_get_height(window);

 
if(width != 0 && height !=0)

   gtk_im_context_focus_in(context);

}



灏?#28051;?#28051;姝ョ殑?#21804;鐮佺紪璇戞垚鍏?#27996;?#25652;?em>libsublime-imfix.so锛屽懡浠?/p>

cd ~

gcc -shared -o libsublime-imfix.so sublime_imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC


濡傛灉杩?#29723;?#28051;嶆垚鍔?#38171;屽彲?#20824;鏄?#37836;?#27996;?#25652;撴病鏈夊畨瑁?#38171;屾墽琛?#28051;嬭竟?#27537;鍛?#28000;?#37833;?#28729;?#29761;?#32514;?#28598;辩殑搴?br />




sudo apt-get install build-essential
sudo apt-get install libgtk2.0-dev

鐒跺悗灏?em>libsublime-imfix.so鎷疯?#28614;埌sublime_text鎵鍦ㄦ枃浠跺す

sudo mv libsublime-imfix.so /opt/sublime_text/



淇敼sublime-text-2.desktop
娉ㄦ剰锛歴ublime_text.desktop涓?#37722;岀増鏈?#37832;?#37813;涓?#37722;?#38171;岃?#30127;皟鏁?#28051;鸿嚜宸卞畨瑁?#37911;?#37832;殑璺?#23536;?/div>
sudo vim /usr/share/applications/sublime_text.desktop

[Desktop Entry]
Version
=1.0
Type
=Application
Name
=Sublime Text
GenericName
=Text Editor
Comment
=Sophisticated text editor for code, markup and prose
Exec
=/usr/bin/subl %F        #杩?#38322;?#28103;敼?#22717;琛岃矾寰?#28051;?usr/bin/subl,subl?#26499;浠跺垰鎵?#23480;?#32513;?#28103;敼杩?#38171;屽ぇ瀹跺簲璇?#29825;板緱
Terminal
=false
MimeType
=text/plain;
Icon=sublime-text
Categories
=TextEditor;Development;
StartupNotify=true
Actions
=Window;Document;

[Desktop Action Window]
Name
=New Window
Exec
=/usr/bin/subl -n       #杩?#38322;?#28103;敼?#22717;琛岃矾寰?#28051;?usr/bin/subl,subl?#26499;浠跺垰鎵?#23480;?#32513;?#28103;敼杩?#38171;屽ぇ瀹跺簲璇?#29825;板緱
OnlyShowIn
=Unity;

[Desktop Action Document]
Name
=New File
Exec
=/usr/bin/subl new_file    #杩?#38322;?#28103;敼?#22717;琛岃矾寰?#28051;?usr/bin/subl,subl?#26499;浠跺垰鎵?#23480;?#32513;?#28103;敼杩?#38171;屽ぇ瀹跺簲璇?#29825;板緱
OnlyShowIn
=Unity;

濡傛?#28355;湪鍛?#28000;?#29723;?#28051;墽琛?usr/bin/subl鎵撳紑sublime text鍚?#38171;?#28751;卞簲璇?#37721;?#28000;?#28003;跨敤涓枃杈撳叆娉曚簡銆?br />鍙?#28598;?#37734;?#37721;?#38335;?#37813;撳紑?#26499;浠?#38171;?#26473;?#28051;嶈兘浣跨敤涓枃杈撳叆娉?#38171;岄渶瑕佸仛濡?#28051;?#23005;?#26976;ゆ搷浣?br />鎵撳紑“鎺?#37714;?#28051;?#36423;?#8221;-銆?#37813;撳紑“涓昏?#28355;崟”-銆?#8220;搴旂敤绋?#25652;?#8221;鏍慿鐩?#35120;?#28051;?#37813;惧埌“缂栫▼”锛?#37813;惧埌“sublime text”锛屽?#23677;嚮淇敼閲岃竟?#27537;鍛?#28000;?#28051;?div>/usr/bin/subl %F


]]>
Java ?#25328; HTTP ?#27537;閭?#27996;?#27996;嬶紙浜?#38171;?浣跨敤?#21804;鐞?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/08/02/431421.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Tue, 02 Aug 2016 06:11:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/08/02/431421.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/431421.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/08/02/431421.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/431421.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/431421.html</trackback:ping><description><![CDATA[<div>http://www.aneasystone.com/archives/2015/12/java-and-http-using-proxy.html</div><br /><div><p>鍦?#28051;?#28051;绡囧?#27692;<a href="http://www.aneasystone.com/archives/2015/12/java-and-http-one.html">銆?#22959;℃嫙 HTTP 璇锋眰銆?/a>涓?#38171;?#37812;?#28000;?#37714;嗗埆浠?#32513;嶄簡涓?#32457;?#37826;规硶鏉?#26473;?#29723;?HTTP ?#27537;妯℃嫙璇锋眰锛?code>HttpURLConnection</code> ?#25328; <code>HttpClient</code> 锛屽埌鐩?#37715;?#28051;?#23005;?#26473;?#28051;?#32457;?#37826;规硶閮?#23480;?#28003;滅殑寰堝ソ锛屽熀鏈?#28051;?#37721;?#28000;?#28729;炵幇鎴?#28000;?#38343;瑕佺殑 GET/POST 鏂规硶?#27537;妯℃嫙銆?#28725;?#27996;?#28051;涓埇铏?#37833;?#29831;?#38171;岃兘鍙?#38315;?HTTP 璇锋眰锛岃兘鑾峰彇椤?#38344;㈡暟鎹?#38171;岃兘瑙?#37835;?#32515;?#26916;?#37712;呭锛?#26473;?#37929;稿綋浜?#23480;?#32513;?#28729;屾垚 80% ?#27537;宸?#28003;滀簡銆?#37721;?#28051;?#26473;?#28725;?#27996;庡墿涓嬬殑杩?20% ?#27537;宸?#28003;?#38171;?#26473;?#23536;?#38010;辫垂鎴?#28000;?#37721;?#28598;?80% ?#27537;鏃?#38338;? :-)</p> <p>鍦?#26473;?#32481;囧?#27692;閲?#38171;?#37812;?#28000;?#28751;?#28000;?#32513;嶅墿涓?20% ?#27537;宸?#28003;?#28051;?#37832;涓?#38322;嶈?#20346;殑涓椤?#38171;氬浣?#37734;?Java 涓?#28003;跨敤 HTTP ?#21804;鐞?#38171;屼唬鐞?#28052;熸槸?#22471;铏妧鏈?#28051;殑閲嶈涓椤?#37510;?#28003;?#28641;傛灉瑕?#28598;?#29785;勬ā?#27537;?#22471;鍒?#27996;?#32515;?#26916;?#28051;婄殑鍐呭锛屽繀鐒朵細瀵?#27996;?#28729;剁殑缃?#32468;欓犳垚褰卞搷锛屽?#20635;灉浣?#28598;?#37815;间簡锛?#28751;变細閬?#27996;?#37836;?#28751;併?#29781;侀槻姝?#37714;?#27996;?#37836;?#28751;佹垜 浠?#38171;?#37812;?#28000;?#29781;佷箞灏嗚嚜宸辩殑绋?#25652;?#37714;嗗竷?#22476;澶?#38322;?#37832;?#37731;?#28051;?#37720;?#38171;?#28003;嗘槸瀵?#27996;?#29863;?#38322;?#37724;岃?#21228;簮鏈?#38340;愮殑鎴?#28000;?#37833;?#29831;?#26473;?#37828;?#23536;堝ア渚堢殑锛?#29781;佷箞灏?#28003;跨敤?#21804;鐞嗘妧鏈?#38171;?#28000;?#32515;?#28051;?#37817;?#28051;鎵逛唬鐞?#38171;屽厤璐圭殑涔熷?#33445;敹璐圭殑 涔熷ソ锛?#37812;?#38000;?#29840;?#28052;?#28051;鎵瑰粔浠风殑 VPS 鏉?#37820;?#23524;鸿嚜宸辩殑?#21804;鐞嗘湇鍔?#37731;?#37510;傚叧浜?#28641;?#28003;?#37820;?#23524;鸿嚜宸辩殑?#21804;鐞嗘湇鍔?#37731;?#38171;屽悗闈?#37832;?#37827;?#38338;寸殑璇?#37812;?#37712;?#37712;?#28051;绡囧叧浜?#26473;?#28051;?#29831;?#26864;樼殑鍗氬銆傜幇鍦?#37832;変簡涓澶?#37813;逛唬鐞嗘湇鍔?#37731;?#28052;?#37722;?#38171;?#28751;?#37721;?#28000;?#28003;跨敤鎴?#28000;?#26473;?绡囧?#27692;鎵浠?#32513;嶇殑?#22951;鏈簡銆?/p> <h2>涓銆佺畝鍗曠殑 HTTP ?#21804;鐞?/h2> <p>鎴?#28000;?#37711;?#28000;?#37832;?#30045;鍗曠殑寮濮?#38171;?#32515;?#28051;?#37832;夊緢澶氬厤璐逛唬鐞?#38171;岀?#23384;帴涓婄櫨搴?#37820;滅储 “?#21412;璐逛唬鐞?#8221; 鎴?#38000;?“HTTP ?#21804;鐞?#8221; 灏辫兘鎵惧埌寰?#28598;氾紙铏界劧缃?#28051;婅兘鎵惧埌澶?#38322;忕殑?#21412;璐逛唬鐞?#38171;?#28003;嗗畠浠殑瀹?#37711;?#37804;?#23480;?#32513;?#37832;夊緢澶氭枃绔?#29825;?#29825;?#26473;囦簡锛?#28052;熸湁涓?#38338;ㄧ殑?#26499;绔?#28725;?#23005;?#26473;?#29723;岃皟鐮旂殑锛岃濡?a href="http://www.freebuf.com/news/70733.html">杩?#32481;囨枃绔?/a>锛?#37812;?#37734;?#26473;?#38322;?#28751;?#28051;?#28598;?#28003;?#29831;存槑锛屽?#20635;灉浣犵殑?#22471;铏埇鍙栫殑淇?#37805;?#39582;舵病鏈?#28000;涔?#37911;?#37714;殑闅?#32457;侀棶棰?#38171;屽彲浠?#36423;界暐涔?#38171;屽?#20635;灉浣犵殑?#22471;铏秹鍙?#28051;浜?#28186;?#28641;?#22959;℃嫙?#27365;褰?#28052;?#32491;荤殑鍔熻兘锛岃冭檻?#22476;瀹?#37711;?#37804;?#38171;?#37812;?#23524;?#29825;?#28003;?#26473;?#37828;?#28051;嶈浣跨敤缃?#28051;?#37711;?#23534;?#27537;?#21412;璐逛唬鐞?#38171;岃?#37828;?#37820;?#23524;鸿嚜宸辩殑?#21804;鐞嗘湇鍔?#37731;?#23011;?#26440;?#38344;?#29835;?#38171;?#37510;?/p> <h4>1.1 HttpURLConnection 浣跨敤?#21804;鐞?/h4> <p>HttpURLConnection ?#27537; <code>openConnection()</code> 鏂规硶鍙?#28000;?#28028;?#37711;?#28051;涓?Proxy 鍙傛暟锛屽涓?#38171;?/p> <div><div id="highlighter_961651" java"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0="" alt2"="">1</div><div number2="" index1="" alt1"="">2</div><div number3="" index2="" alt2"="">3</div></td><td><div><div number1="" index0="" alt2"=""><code plain"="">Proxy proxy = </code><code keyword"="">new</code> <code plain"="">Proxy(Proxy.Type.HTTP, </code><code keyword"="">new</code> <code plain"="">InetSocketAddress(</code><code string"="">"127.0.0.1"</code><code plain"="">, </code><code value"="">9876</code><code plain"="">));</code></div><div number2="" index1="" alt1"=""><code plain"="">URL obj = </code><code keyword"="">new</code> <code plain"="">URL(url);</code></div><div number3="" index2="" alt2"=""><code plain"="">HttpURLConnection con = (HttpURLConnection) obj.openConnection(proxy);</code></div></div></td></tr></tbody></table></div></div> <p>OK ?#31777;锛?#28751;?#26473;欎箞?#30045;鍗?#38171;?/p> <p>涓?#28000;呭姝?#38171;?#37812;?#28000;?#23049;?#37808;忓埌 Proxy 鏋勯?#37713;芥暟?#27537;绗?#28051;涓?#37721;傛暟涓?#37835;?#28051;?#32491;诲瀷 <code>Proxy.Type.HTTP</code> 锛岄偅涔?#23536;?#37828;剧劧锛屽?#20635;灉灏嗗叾淇敼涓?<code>Proxy.Type.SOCKS</code> 鍗?#37721;?#28000;?#28003;跨敤 SOCKS ?#21804;鐞?#37510;?/p> <h4>1.2 HttpClient 浣跨敤?#21804;鐞?/h4> <p>鐢?#27996;?<code>HttpClient</code> 闈炲?#21733;?#22443;椿锛?#28003;跨敤 HttpClient 鏉?#26473;?#37818;ヤ唬鐞嗘?#22794;緢澶?#28051;?#37722;岀殑鏂规?#26328;?#37832;?#30045;鍗曠殑鏂规硶鑾?#26473;囦簬涓?#38344;?#26473;?#37837;?#38171;?/p> <div><div id="highlighter_384787" java"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0="" alt2"="">1</div><div number2="" index1="" alt1"="">2</div><div number3="" index2="" alt2"="">3</div><div number4="" index3="" alt1"="">4</div></td><td><div><div number1="" index0="" alt2"=""><code plain"="">HttpHost proxy = </code><code keyword"="">new</code> <code plain"="">HttpHost(</code><code string"="">"127.0.0.1"</code><code plain"="">, </code><code value"="">9876</code><code plain"="">, </code><code string"="">"HTTP"</code><code plain"="">);</code></div><div number2="" index1="" alt1"=""><code plain"="">CloseableHttpClient httpclient = HttpClients.createDefault();</code></div><div number3="" index2="" alt2"=""><code plain"="">HttpGet request = </code><code keyword"="">new</code> <code plain"="">HttpGet(url);</code></div><div number4="" index3="" alt1"=""><code plain"="">CloseableHttpResponse response = httpclient.execute(proxy, request);</code></div></div></td></tr></tbody></table></div></div> <p>?#25328;涓?#28051;绡?#28051;?#28003;跨敤 HttpClient 鍙?#38315;?#29831;锋眰?#27537;?#21804;鐮?#37713;犱箮涓鏍?#38171;屽彧鏄?<code>httpclient.execute()</code> 鏂规硶澶?#37716;犱簡涓涓?#37721;傛暟锛?#32471;?#28051;鍙傛暟涓?<code>HttpHost</code> 绫诲瀷锛?#37812;?#28000;?#26473;?#38322;岃缃垚鎴?#28000;殑?#21804;鐞嗗嵆鍙?#37510;?/p> <p>杩?#38322;岃?#20345;敞?#21104;涓鐐圭殑鏄?#38171;岃?#30028;劧杩?#38322;岀殑 <code>new HttpHost()</code> ?#25328;涓?#38344;㈢殑 <code>new Proxy()</code> 涓鏍?#38171;?#28052;熸槸鍙?#28000;ユ寚瀹氬崗璁?#32491;诲?#23340;殑锛?#28003;嗘槸閬楁?#21095;殑鏄?HttpClient ?#31919;璁?#37828;?#28051;嶆敮鎸?SOCKS 鍗?#29825;殑锛屽?#20635;灉鎴?#28000;?#28003;跨敤涓?#38344;㈢殑?#21804;鐮?#38171;?/p> <div><div id="highlighter_676139" java"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0="" alt2"="">1</div></td><td><div><div number1="" index0="" alt2"=""><code plain"="">HttpHost proxy = </code><code keyword"="">new</code> <code plain"="">HttpHost(</code><code string"="">"127.0.0.1"</code><code plain"="">, </code><code value"="">1080</code><code plain"="">, </code><code string"="">"SOCKS"</code><code plain"="">);</code></div></div></td></tr></tbody></table></div></div> <p>灏嗕細鐩存帴鎶?#37719;?#29825;?#28051;嶆敮鎸?#23534;?#29999;?#38171;?/p> <blockquote> <p>org.apache.http.conn.UnsupportedSchemeException: socks protocol is not supported</p> </blockquote> <p>濡傛?#28355;笇鏈?HttpClient 鏀?#37816;?SOCKS ?#21804;鐞?#38171;屽彲浠?#37721;?#37930;?#26473;?#38322;?#38171;?a href="http://stackoverflow.com/questions/22937983/how-to-use-socks-5-proxy-with-apache-http-client-4">How to use Socks 5 proxy with Apache HTTP Client 4?</a> 閫?#26473;?HttpClient 鎻?#28186;涚殑 ConnectionSocketFactory 绫?#37833;?#28729;炵幇銆?/p> <p>铏界劧浣跨敤杩?#32457;?#37826;瑰紡寰堢畝鍗?#38171;屽彧闇瑕?#37716;?#28051;?#37721;傛暟灏?#37721;?#28000;ヤ簡锛?#28003;嗘槸鍏跺疄鐪?HttpClient ?#27537;?#21804;鐮佹敞閲?#38171;屽涓?#38171;?/p> <div><div id="highlighter_710103" java"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0="" alt2"="">1</div><div number2="" index1="" alt1"="">2</div><div number3="" index2="" alt2"="">3</div><div number4="" index3="" alt1"="">4</div><div number5="" index4="" alt2"="">5</div><div number6="" index5="" alt1"="">6</div><div number7="" index6="" alt2"="">7</div></td><td><div><div number1="" index0="" alt2"=""><code comments"="">/*</code></div><div number2="" index1="" alt1"=""><code comments"="">* @param target    the target host for the request.</code></div><div number3="" index2="" alt2"=""><code comments"="">*                  Implementations may accept <code>null</code></code></div><div number4="" index3="" alt1"=""><code comments"="">*                  if they can still determine a route, for example</code></div><div number5="" index4="" alt2"=""><code comments"="">*                  to a default target or by inspecting the request.</code></div><div number6="" index5="" alt1"=""><code comments"="">* @param request   the request to execute</code></div><div number7="" index6="" alt2"=""><code comments"="">*/</code></div></div></td></tr></tbody></table></div></div> <p>鍙?#28000;?#37930;嬪埌绗?#28051;涓?#37721;傛暟 target 骞?#28051;嶆槸?#21804;鐞?#38171;屽?#20906;殑鐪熷疄浣滅敤鏄?<strong>?#22717;琛岃?#38155;眰?#27537;鐩?#37837;?#28051;?#37832;?/strong>锛?#26473;?#28051;?#29785;?#38322;?#37832;夌偣妯?#32495;?#38171;?#28000;涔?#37721;仛 <strong>?#22717;琛岃?#38155;眰?#27537;鐩?#37837;?#28051;?#37832;?/strong>锛熶唬鐞嗙畻涓嶇畻<strong>?#22717;琛岃?#38155;眰?#27537;鐩?#37837;?#28051;?#37832;?/strong>鍛?#38171;熷洜涓?#37816;夊父鐞嗘潵璁?#38171;?strong>?#22717;琛岃?#38155;眰?#27537;鐩?#37837;?#28051;?#37832;?/strong> 搴?#29831;?#37828;?#29781;?#29831;锋眰 URL 瀵瑰簲?#27537;绔欑偣鎵?#28725;?#37510;?#28641;傛灉涓嶇?#26947;殑璇?#38171;?#28051;?#28000;涔?#26473;?#38322;?#28751;?target 璁?#32515;垚?#21804;鐞?#28052;熻兘姝?#29999;稿伐浣?#38171;?#26473;?#28051;?#37812;戜篃涓嶆?#21614;锛?#26473;?#38343;瑕?#26473;?#28051;姝?#37934;旂┒涓?HttpClient ?#27537;婧?#37934;佹潵娣卞叆?#31777;瑙?#28051;?#37510;?/p> <p>?#27342;?#31777;涓?#38344;?#28000;?#32513;嶇殑杩?#32457;?#37826;瑰紡?#32025;?#22172;宸?#37712;欑殑锛?#28051;嶆帹?#23832;浣跨敤锛夋潵浣跨敤?#21804;鐞?#28052;?#28598;?#38171;孒ttpClient 瀹?#32515;?#26473;?#37819;?#28186;?#27996;嗗嚑涓?#32448;轰緥锛?#37812;?#28751;嗗叾浣?#28051;?#37818;ㄨ?#24876;啓娉?#29825;?#35120;?#37734;?#23005;?#37510;?/p> <p>绗?#28051;绉?#37712;?#23049;?#37828;?#28003;跨敤 <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientExecuteProxy.java">RequestConfig 绫?/a>锛屽涓?#38171;?/p> <div><div id="highlighter_305592" java"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0="" alt2"="">1</div><div number2="" index1="" alt1"="">2</div><div number3="" index2="" alt2"="">3</div><div number4="" index3="" alt1"="">4</div><div number5="" index4="" alt2"="">5</div><div number6="" index5="" alt1"="">6</div><div number7="" index6="" alt2"="">7</div><div number8="" index7="" alt1"="">8</div><div number9="" index8="" alt2"="">9</div><div number10="" index9="" alt1"="">10</div></td><td><div><div number1="" index0="" alt2"=""><code plain"="">CloseableHttpClient httpclient = HttpClients.createDefault();       </code></div><div number2="" index1="" alt1"=""><code plain"="">HttpGet request = </code><code keyword"="">new</code> <code plain"="">HttpGet(url);</code></div><div number3="" index2="" alt2"=""> </div><div number4="" index3="" alt1"=""><code plain"="">request.setConfig(</code></div><div number5="" index4="" alt2"=""><code spaces"="">    </code><code plain"="">RequestConfig.custom()</code></div><div number6="" index5="" alt1"=""><code spaces"="">        </code><code plain"="">.setProxy(</code><code keyword"="">new</code> <code plain"="">HttpHost(</code><code string"="">"45.32.21.237"</code><code plain"="">, </code><code value"="">8888</code><code plain"="">, </code><code string"="">"HTTP"</code><code plain"="">))</code></div><div number7="" index6="" alt2"=""><code spaces"="">        </code><code plain"="">.build()</code></div><div number8="" index7="" alt1"=""><code plain"="">);</code></div><div number9="" index8="" alt2"=""><code spaces"="">        </code> </div><div number10="" index9="" alt1"=""><code plain"="">CloseableHttpResponse response = httpclient.execute(request);</code></div></div></td></tr></tbody></table></div></div> <p>绗?#27996;岀鍐?#23049;?#37828;?#28003;跨敤 <a href="http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/connmgmt.html#d5e485">RoutePlanner 绫?/a>锛屽涓?#38171;?/p> <div><div id="highlighter_875054" java"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0="" alt2"="">1</div><div number2="" index1="" alt1"="">2</div><div number3="" index2="" alt2"="">3</div><div number4="" index3="" alt1"="">4</div><div number5="" index4="" alt2"="">5</div><div number6="" index5="" alt1"="">6</div><div number7="" index6="" alt2"="">7</div></td><td><div><div number1="" index0="" alt2"=""><code plain"="">HttpHost proxy = </code><code keyword"="">new</code> <code plain"="">HttpHost(</code><code string"="">"127.0.0.1"</code><code plain"="">, </code><code value"="">9876</code><code plain"="">, </code><code string"="">"HTTP"</code><code plain"="">);</code></div><div number2="" index1="" alt1"=""><code plain"="">DefaultProxyRoutePlanner routePlanner = </code><code keyword"="">new</code> <code plain"="">DefaultProxyRoutePlanner(proxy); </code></div><div number3="" index2="" alt2"=""><code plain"="">CloseableHttpClient httpclient = HttpClients.custom()</code></div><div number4="" index3="" alt1"=""><code spaces"="">        </code><code plain"="">.setRoutePlanner(routePlanner)</code></div><div number5="" index4="" alt2"=""><code spaces"="">        </code><code plain"="">.build();</code></div><div number6="" index5="" alt1"=""><code plain"="">HttpGet request = </code><code keyword"="">new</code> <code plain"="">HttpGet(url);</code></div><div number7="" index6="" alt2"=""><code plain"="">CloseableHttpResponse response = httpclient.execute(request);</code></div></div></td></tr></tbody></table></div></div> <h2>浜?#37510;?#28003;跨敤?#37108;缁熶唬鐞嗛厤缃?/h2> <p>鎴?#28000;?#37734;ㄨ皟璇?HTTP ?#22471;铏?#32459;?#25652;?#37827;?#38171;屽?#31295;父闇瑕?#37714;?#37817;唬鐞嗘潵娴嬭瘯锛?#37832;?#37827;跺?#37929;存帴浣跨敤?#37108;缁熻嚜甯︾殑?#21804;鐞嗛厤缃?#28751;嗘槸涓绉嶇畝鍗曠殑鏂规?#26328;?#28000;?#37715;?#37734;ㄥ仛 .Net 椤?#37929;?#37827;?#38171;岀▼搴忛粯璁?#28003;跨敤 Internet 缃?#32513;?#29825;?#32515;?#28051;厤?#27537;?#21804;鐞?#38171;岄?#26945;?#21095;殑鏄?#38171;?#37812;?#26473;?#38322;岃?#23544;殑?#37108;缁熶唬鐞嗛厤缃?#37816;囩殑 JVM ?#37108;缁?#38171;岃?#28051;嶆槸?#25655;浣滅郴缁?#38171;?#37812;?#26473;樻病鎵惧埌?#30045;鍗曠殑鏂规硶鏉?#29825;?Java 绋?#25652;?#37929;存帴浣跨敤 Windows ?#37108;缁?#28051;嬬殑?#21804;鐞嗛厤缃?#37510;?/p> <p>灏界濡?#23005;?#38171;岀郴缁熶唬鐞?#28003;跨敤璧?#37833;?#26473;?#37828;?#23536;堢畝鍗曠殑銆?#28051;?#22471;鏈?#28051;?#38344;?#28051;?#32457;?#37826;瑰紡鍙?#28000;?#29825;?#32515;?JVM ?#27537;?#21804;鐞嗛厤缃?#38171;?/p> <h4>2.1 System.setProperty</h4> <p>Java 涓殑 <code>System</code> 绫?#28051;?#28000;?#28000;呮槸?#25956;鏉?#32513;?#37812;?#28000;?<code>System.out.println()</code> 鎵撳嵃淇?#37805;殑锛屽畠鍏跺疄杩?#37832;夊緢澶?#38344;?#37804;佹?#35268;硶鍜屽睘鎬?#37721;?#28000;ョ敤銆?#37711;?#28051;?<code>System.setProperty()</code> 灏?#37828;?#23011;?#26440;?#29999;哥敤?#27537;涓涓?#37510;?/p> <p>鍙?#28000;?#38315;?#26473;?#28051;?#38344;㈢殑鏂瑰紡鏉?#37714;嗗埆璁?#32515;?HTTP ?#21804;鐞?#38171;孒TTPS ?#21804;鐞嗗拰 SOCKS ?#21804;鐞?#38171;?/p> <div><div id="highlighter_319495" java"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0="" alt2"="">1</div><div number2="" index1="" alt1"="">2</div><div number3="" index2="" alt2"="">3</div><div number4="" index3="" alt1"="">4</div><div number5="" index4="" alt2"="">5</div><div number6="" index5="" alt1"="">6</div><div number7="" index6="" alt2"="">7</div><div number8="" index7="" alt1"="">8</div><div number9="" index8="" alt2"="">9</div><div number10="" index9="" alt1"="">10</div><div number11="" index10="" alt2"="">11</div><div number12="" index11="" alt1"="">12</div></td><td><div><div number1="" index0="" alt2"=""><code comments"="">// HTTP ?#21804;鐞?#38171;屽彧鑳戒唬鐞?HTTP 璇锋眰</code></div><div number2="" index1="" alt1"=""><code plain"="">System.setProperty(</code><code string"="">"http.proxyHost"</code><code plain"="">, </code><code string"="">"127.0.0.1"</code><code plain"="">);</code></div><div number3="" index2="" alt2"=""><code plain"="">System.setProperty(</code><code string"="">"http.proxyPort"</code><code plain"="">, </code><code string"="">"9876"</code><code plain"="">);</code></div><div number4="" index3="" alt1"=""> </div><div number5="" index4="" alt2"=""><code comments"="">// HTTPS ?#21804;鐞?#38171;屽彧鑳戒唬鐞?HTTPS 璇锋眰</code></div><div number6="" index5="" alt1"=""><code plain"="">System.setProperty(</code><code string"="">"https.proxyHost"</code><code plain"="">, </code><code string"="">"127.0.0.1"</code><code plain"="">);</code></div><div number7="" index6="" alt2"=""><code plain"="">System.setProperty(</code><code string"="">"https.proxyPort"</code><code plain"="">, </code><code string"="">"9876"</code><code plain"="">);</code></div><div number8="" index7="" alt1"=""> </div><div number9="" index8="" alt2"=""><code comments"="">// SOCKS ?#21804;鐞?#38171;屾敮鎸?HTTP ?#25328; HTTPS 璇锋眰</code></div><div number10="" index9="" alt1"=""><code comments"="">// 娉ㄦ剰锛氬?#20635;灉璁?#32515;簡 SOCKS ?#21804;鐞?#28751;?#28051;嶈璁?HTTP/HTTPS ?#21804;鐞?/code></div><div number11="" index10="" alt2"=""><code plain"="">System.setProperty(</code><code string"="">"socksProxyHost"</code><code plain"="">, </code><code string"="">"127.0.0.1"</code><code plain"="">);</code></div><div number12="" index11="" alt1"=""><code plain"="">System.setProperty(</code><code string"="">"socksProxyPort"</code><code plain"="">, </code><code string"="">"1080"</code><code plain"="">);</code></div></div></td></tr></tbody></table></div></div> <p>杩?#38322;?#37832;?#28051;夌?#30789;璇存槑锛?/p> <ol><li>?#37108;缁熼粯璁?#37711;?#28003;跨敤 HTTP/HTTPS ?#21804;鐞?#38171;屽?#20635;灉鏃?#29825;?#32515;簡 HTTP/HTTPS ?#21804;鐞?#38171;屽張璁?#32515;簡 SOCKS ?#21804;鐞?#38171;孲OCKS ?#21804;鐞嗕細璧?#28051;嶅埌浣滅敤</li><li>鐢?#27996;?#37720;嗗彶鍘熷洜锛?#23049;ㄦ剰 <code>socksProxyHost</code> ?#25328; <code>socksProxyPort</code> 涓?#38338;存病鏈?#28751;忔暟?#20579;</li><li>HTTP ?#25328; HTTPS ?#21804;鐞嗗彲浠?#37722;?#29863;?#37833;ョ缉鍐?#38171;屽涓?#38171;?/li></ol> <div><div id="highlighter_705545" java"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0="" alt2"="">1</div><div number2="" index1="" alt1"="">2</div><div number3="" index2="" alt2"="">3</div></td><td><div><div number1="" index0="" alt2"=""><code comments"="">// 鍚?#37827;舵敮鎸佷唬鐞?HTTP/HTTPS 璇锋眰</code></div><div number2="" index1="" alt1"=""><code plain"="">System.setProperty(</code><code string"="">"proxyHost"</code><code plain"="">, </code><code string"="">"127.0.0.1"</code><code plain"="">);</code></div><div number3="" index2="" alt2"=""><code plain"="">System.setProperty(</code><code string"="">"proxyPort"</code><code plain"="">, </code><code string"="">"9876"</code><code plain"="">);</code></div></div></td></tr></tbody></table></div></div> <h4>2.2 JVM 鍛?#28000;?#29723;屽?#20635;暟</h4> <p>鍙?#28000;?#28003;跨敤 <code>System.setProperty()</code> 鏂规硶鏉?#29825;?#32515;郴缁熶唬鐞?#38171;?#28052;熷彲浠?#37929;存帴灏?#26473;欎簺鍙傛暟閫?#26473;?JVM ?#27537;鍛?#28000;?#29723;屽?#20635;暟鏉ユ寚瀹?#37510;?#28641;傛灉浣?#28003;跨敤?#27537;鏄?Eclipse 锛屽彲浠?#37816;?#28051;?#38344;㈢殑姝?#26976;?#37833;?#29825;?#32515;?#38171;?/p> <ol><li>鎸夐『搴?#37813;撳紑锛歐indow -> Preferences -> Java -> Installed JREs -> Edit</li><li>鍦?Default VM arguments 涓?#28617;?#37712;欏?#20635;暟锛?<code>-DproxyHost=127.0.0.1 -DproxyPort=9876</code></li></ol> <p><img src="http://www.aneasystone.com/usr/uploads/2015/12/946315741.jpg" alt="jvm-arguments.jpg" /></p> <h4>2.3 浣跨敤?#37108;缁熶唬鐞?/h4> <p>涓?#38344;?#28051;?#32457;?#37826;规硶閮?#37721;?#28000;?#29825;?#32515;郴缁?#38171;?#28051;?#38344;?#29781;佹?#28052;?#37734;?#32459;?#25652;?#28051;嚜鍔?#28003;跨敤?#37108;缁熶唬鐞嗗憿锛?/p> <p>瀵?#27996;?<code>HttpURLConnection</code> 绫?#37833;?#29831;?#38171;岀▼搴?#28051;嶇敤?#20187;浠?#28003;?#37721;?#37716;?#38171;屽畠?#32048;?#31919;璁?#28003;跨敤?#37108;缁熶唬鐞?#37510;?#28003;嗘槸 <code>HttpClient</code> ?#31919;璁?#37828;?#28051;?#28003;跨敤?#37108;缁熶唬鐞嗙殑锛屽?#20635;灉鎯?#29825;?#28729;冮粯璁?#28003;跨敤?#37108;缁熶唬鐞?#38171;屽彲浠?#38315;?#26473;?<code>SystemDefaultRoutePlanner</code> ?#25328; <code>ProxySelector</code> 鏉?#29825;?#32515;?#37510;傜?#36720;?#23337;唬鐮?#28641;?#28051;?#38171;?/p> <div><div id="highlighter_319464" java"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0="" alt2"="">1</div><div number2="" index1="" alt1"="">2</div><div number3="" index2="" alt2"="">3</div><div number4="" index3="" alt1"="">4</div><div number5="" index4="" alt2"="">5</div><div number6="" index5="" alt1"="">6</div></td><td><div><div number1="" index0="" alt2"=""><code plain"="">SystemDefaultRoutePlanner routePlanner = </code><code keyword"="">new</code> <code plain"="">SystemDefaultRoutePlanner(ProxySelector.getDefault());</code></div><div number2="" index1="" alt1"=""><code plain"="">CloseableHttpClient httpclient = HttpClients.custom()</code></div><div number3="" index2="" alt2"=""><code spaces"="">        </code><code plain"="">.setRoutePlanner(routePlanner)</code></div><div number4="" index3="" alt1"=""><code spaces"="">        </code><code plain"="">.build();</code></div><div number5="" index4="" alt2"=""><code plain"="">HttpGet request = </code><code keyword"="">new</code> <code plain"="">HttpGet(url);     </code></div><div number6="" index5="" alt1"=""><code plain"="">CloseableHttpResponse response = httpclient.execute(request);</code></div></div></td></tr></tbody></table></div></div> <h2>鍙?#38000;?/h2> <ol><li><a href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/">HttpClient Tutorial</a></li><li><a href="http://www.freebuf.com/news/70733.html">璇勬祴鍛?#29831;?#28003;?#38171;?#38317;?#27996;涘厤璐逛唬鐞嗘?#21228;?#21227;仛?#27537;榫岃箟浜嬪効</a></li><li><a href="http://stackoverflow.com/questions/22937983/how-to-use-socks-5-proxy-with-apache-http-client-4">How to use Socks 5 proxy with Apache HTTP Client 4?</a></li><li><a href="http://book.51cto.com/art/200809/89230.htm">浣跨敤ProxySelector閫夋嫨?#21804;鐞嗘湇鍔?#37731;?/a></li><li><a href="http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html">Java Networking and Proxies</a></li></ol></div><img src ="http://www.8634070.com/wangxinsh55/aggbug/431421.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-08-02 14:11 <a href="http://www.8634070.com/wangxinsh55/archive/2016/08/02/431421.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title>姝?#37714;?#29723;?#26440;惧紡澶嶆潅搴旂敤http://www.8634070.com/wangxinsh55/archive/2016/07/26/431335.htmlSIMONESIMONETue, 26 Jul 2016 10:04:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/07/26/431335.htmlhttp://www.8634070.com/wangxinsh55/comments/431335.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/07/26/431335.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431335.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431335.htmlclass ReqParam(queryString: String, val encode: String = "GBK") : HashMap<String, String>() {
        
        init {
            queryString.split(
"&+".toRegex()).filter { it.contains("=") }.forEach {
                val kv 
= it.split("(?<!=+)=".toRegex())
                put(kv[
0], URLDecoder.decode(kv[1], encode))
            }
            
        }
    }

浠?#28051;?#37828;痥otlin?#21804;鐮?鏄?#28751;嗛摼鎺?#37734;板潃涓茬殑 queryString 鎷嗗垎k=v褰㈠紡,骞?#37819;愬彇鍊?br />
fun main(args: Array<String>) {
    val domain 
= "fu.area.duxiu.com"
    val subdomain 
= domain.replace(Regex(""".+((\.\w+){2})"""), "$1")
    println(subdomain)
}

?#24391;涓?#37737;熷悕

public static String cookieDomain(String domain) {
        
if (domain.matches("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)")) {// 濡傛灉鏄疘P鍦板潃锛?#28051;?#37737;熷悕灏?#37828;疘P鍦板潃
            return domain;
        }
        
return domain.substring(domain.indexOf("."));// 鍐?#28699;愬?#29111;悕锛屽?#29111;?#23941;?#23944;竟鍔?#28051;?."
    }

鍒?#37826;璉P


]]>
maven 渚濊禆鎵撳寘鎻?#28000;?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/07/20/431252.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Wed, 20 Jul 2016 01:42:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/07/20/431252.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/431252.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/07/20/431252.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/431252.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/431252.html</trackback:ping><description><![CDATA[<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000FF; "><</span><span style="color: #800000; ">plugin</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                 </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">groupId</span><span style="color: #0000FF; ">></span><span style="color: #000000; ">org.apache.maven.plugins</span><span style="color: #0000FF; "></</span><span style="color: #800000; ">groupId</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                 </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">artifactId</span><span style="color: #0000FF; ">></span><span style="color: #000000; ">maven-shade-plugin</span><span style="color: #0000FF; "></</span><span style="color: #800000; ">artifactId</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                 </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">version</span><span style="color: #0000FF; ">></span><span style="color: #000000; ">2.4.2</span><span style="color: #0000FF; "></</span><span style="color: #800000; ">version</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                 </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">configuration</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                     </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">createDependencyReducedPom</span><span style="color: #0000FF; ">></span><span style="color: #000000; ">false</span><span style="color: #0000FF; "></</span><span style="color: #800000; ">createDependencyReducedPom</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                 </span><span style="color: #0000FF; "></</span><span style="color: #800000; ">configuration</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                 </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">executions</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                     </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">execution</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                         </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">phase</span><span style="color: #0000FF; ">></span><span style="color: #000000; ">package</span><span style="color: #0000FF; "></</span><span style="color: #800000; ">phase</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                         </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">goals</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                             </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">goal</span><span style="color: #0000FF; ">></span><span style="color: #000000; ">shade</span><span style="color: #0000FF; "></</span><span style="color: #800000; ">goal</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                         </span><span style="color: #0000FF; "></</span><span style="color: #800000; ">goals</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                         </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">configuration</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                             </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">artifactSet</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                                 </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">includes</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                                     </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">include</span><span style="color: #0000FF; ">></span><span style="color: #000000; ">org.apache.activemq:activemq-mqtt</span><span style="color: #0000FF; "></</span><span style="color: #800000; ">include</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                                 </span><span style="color: #0000FF; "></</span><span style="color: #800000; ">includes</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                             </span><span style="color: #0000FF; "></</span><span style="color: #800000; ">artifactSet</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                            <div><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:10.5pt;"><<span style="color:#cc7832;font-weight:bold;">transformers</span>><br /> <<span style="color:#cc7832;font-weight:bold;">transformer<br /></span> implementation<span style="color:#6a8759;">="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"</span>><br /> <<span style="color:#cc7832;font-weight:bold;">mainClass</span>>com.duxiu.demo.app.ApplicationKt</<span style="color:#cc7832;font-weight:bold;">mainClass</span>><br /> </<span style="color:#cc7832;font-weight:bold;">transformer</span>><br /></<span style="color:#cc7832;font-weight:bold;">transformers</span>></pre></div><br />                         </span><span style="color: #0000FF; "></</span><span style="color: #800000; ">configuration</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                     </span><span style="color: #0000FF; "></</span><span style="color: #800000; ">execution</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />                 </span><span style="color: #0000FF; "></</span><span style="color: #800000; ">executions</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />             </span><span style="color: #0000FF; "></</span><span style="color: #800000; ">plugin</span><span style="color: #0000ff;">><br /> </span></div> <br /><br />姝ら厤缃?#37826;瑰紡灏?#37813;鏈夌殑渚濊禆?#23512;?#27537;婧?#37934;侀兘瑙?#37720;?#37813;撳寘杩?#37720;?<br />濡傛灉鏄痺ar?#23512;,?#32048;灏嗘暣涓?#32468;?#37904;硅В鍘?#37813;撳寘杩?#37720;?<br />鍚?#37827;?#37813;撳寘杩囩▼涓?#28641;傛灉鏈塩lasspath?#25328;渚濊?#26667;殑jar?#23512;鏈?#37929;稿?#23680;殑绫??#32048;灏哻lasspath閲岀殑绫绘浛鎹?#37818;?#28186;濊禆?#23512;閲岀殑绫?br /><br /><br /><div><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:10.5pt;"><<span style="color:#cc7832;font-weight:bold;">plugin</span>><br /> <<span style="color:#cc7832;font-weight:bold;">groupId</span>>org.apache.maven.plugins</<span style="color:#cc7832;font-weight:bold;">groupId</span>><br /> <<span style="color:#cc7832;font-weight:bold;">artifactId</span>>maven-assembly-plugin</<span style="color:#cc7832;font-weight:bold;">artifactId</span>><br /> <<span style="color:#cc7832;font-weight:bold;">configuration</span>><br /> <<span style="color:#cc7832;font-weight:bold;">descriptorRefs</span>><br /> <<span style="color:#cc7832;font-weight:bold;">descriptorRef</span>>jar-with-dependencies</<span style="color:#cc7832;font-weight:bold;">descriptorRef</span>><br /> </<span style="color:#cc7832;font-weight:bold;">descriptorRefs</span>><br /> <span style="color:#808080;"><!--<descriptors><br /></span><span style="color:#808080;"> <descriptor>assembly.xml</descriptor><br /></span><span style="color:#808080;"> </descriptors>--><br /></span><span style="color:#808080;"> <!--<finalName>employees-app-${project.version}</finalName>--><br /></span> <<span style="color:#cc7832;font-weight:bold;">archive</span>><br /> <<span style="color:#cc7832;font-weight:bold;">manifest</span>><br /> <<span style="color:#cc7832;font-weight:bold;">mainClass</span>>com.duxiu.demo.app.ApplicationKt</<span style="color:#cc7832;font-weight:bold;">mainClass</span>><br /> </<span style="color:#cc7832;font-weight:bold;">manifest</span>><br /> </<span style="color:#cc7832;font-weight:bold;">archive</span>><br /> </<span style="color:#cc7832;font-weight:bold;">configuration</span>><br /> <<span style="color:#cc7832;font-weight:bold;">executions</span>><br /> <<span style="color:#cc7832;font-weight:bold;">execution</span>><br /> <<span style="color:#cc7832;font-weight:bold;">phase</span>>package</<span style="color:#cc7832;font-weight:bold;">phase</span>><br /> <<span style="color:#cc7832;font-weight:bold;">goals</span>><br /> <<span style="color:#cc7832;font-weight:bold;">goal</span>>single</<span style="color:#cc7832;font-weight:bold;">goal</span>><br /> </<span style="color:#cc7832;font-weight:bold;">goals</span>><br /> </<span style="color:#cc7832;font-weight:bold;">execution</span>><br /> </<span style="color:#cc7832;font-weight:bold;">executions</span>><br /></<span style="color:#cc7832;font-weight:bold;">plugin</span>></pre></div>鍙?#28751;?#28186;濊?#26667;殑jar?#23512;瑙?#37720;?#37813;撳寘,瀵?#27996;?#38344;?#37804;佹枃浠剁瓑鏄?#28051;嶄細鎵撳寘?#27537;<br /><br /><br /><div><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:10.5pt;"><<span style="color:#cc7832;font-weight:bold;">plugin</span>><br /> <<span style="color:#cc7832;font-weight:bold;">groupId</span>>org.codehaus.mojo</<span style="color:#cc7832;font-weight:bold;">groupId</span>><br /> <<span style="color:#cc7832;font-weight:bold;">artifactId</span>>appassembler-maven-plugin</<span style="color:#cc7832;font-weight:bold;">artifactId</span>><br /> <<span style="color:#cc7832;font-weight:bold;">version</span>>1.10</<span style="color:#cc7832;font-weight:bold;">version</span>><br /> <<span style="color:#cc7832;font-weight:bold;">configuration</span>><br /> <span style="color:#808080;"><!-- </span><span style="color:#808080;font-family:'瀹?#28003;?;">?#25939;?#22426;</span><span style="color:#808080;">linux, Windows</span><span style="color:#808080;font-family:'瀹?#28003;?;">涓?#32457;?#39582;?#37721;扮殑?#22717;琛岃剼鏈?</span><span style="color:#808080;">--><br /></span> <<span style="color:#cc7832;font-weight:bold;">platforms</span>><br /> <<span style="color:#cc7832;font-weight:bold;">platform</span>>windows</<span style="color:#cc7832;font-weight:bold;">platform</span>><br /> <<span style="color:#cc7832;font-weight:bold;">platform</span>>unix</<span style="color:#cc7832;font-weight:bold;">platform</span>><br /> </<span style="color:#cc7832;font-weight:bold;">platforms</span>><br /> <span style="color:#808080;"><!-- </span><span style="color:#808080;font-family:'瀹?#28003;?;">鏍?#37929;?#35120;?</span><span style="color:#808080;">--><br /></span> <<span style="color:#cc7832;font-weight:bold;">assembleDirectory</span>>${project.build.directory}/mall</<span style="color:#cc7832;font-weight:bold;">assembleDirectory</span>><br /> <span style="color:#808080;"><!-- </span><span style="color:#808080;font-family:'瀹?#28003;?;">鎵撳寘?#27537;</span><span style="color:#808080;">jar</span><span style="color:#808080;font-family:'瀹?#28003;?;">锛?#28000;?#37721;?/span><span style="color:#808080;">maven</span><span style="color:#808080;font-family:'瀹?#28003;?;">渚濊?#26667;殑</span><span style="color:#808080;">jar</span><span style="color:#808080;font-family:'瀹?#28003;?;">?#25985;?#22476;杩?#28051;?#37929;?#35120;?#38322;岄潰 </span><span style="color:#808080;">--><br /></span> <<span style="color:#cc7832;font-weight:bold;">repositoryName</span>>lib</<span style="color:#cc7832;font-weight:bold;">repositoryName</span>><br /> <span style="color:#808080;"><!-- </span><span style="color:#808080;font-family:'瀹?#28003;?;">鍙墽琛岃剼鏈殑鐩?#35120;?</span><span style="color:#808080;">--><br /></span> <<span style="color:#cc7832;font-weight:bold;">binFolder</span>>bin</<span style="color:#cc7832;font-weight:bold;">binFolder</span>><br /> <span style="color:#808080;"><!-- </span><span style="color:#808080;font-family:'瀹?#28003;?;">?#21412;缃枃浠剁殑鐩?#37837;?#37929;?#35120;?</span><span style="color:#808080;">--><br /></span> <<span style="color:#cc7832;font-weight:bold;">configurationDirectory</span>>conf</<span style="color:#cc7832;font-weight:bold;">configurationDirectory</span>><br /> <span style="color:#808080;"><!-- </span><span style="color:#808080;font-family:'瀹?#28003;?;">鎷疯礉?#21412;缃枃浠跺埌涓?#38344;㈢殑鐩?#35120;?#28051;?</span><span style="color:#808080;">--><br /></span> <<span style="color:#cc7832;font-weight:bold;">copyConfigurationDirectory</span>>true</<span style="color:#cc7832;font-weight:bold;">copyConfigurationDirectory</span>><br /> <span style="color:#808080;"><!-- </span><span style="color:#808080;font-family:'瀹?#28003;?;">浠庡摢閲屾?#30127;礉?#21412;缃枃浠?/span><span style="color:#808080;"> (</span><span style="color:#808080;font-family:'瀹?#28003;?;">?#31919;璁?/span><span style="color:#808080;">src/main/config) --><br /></span> <<span style="color:#cc7832;font-weight:bold;">configurationSourceDirectory</span>>src/main/resources</<span style="color:#cc7832;font-weight:bold;">configurationSourceDirectory</span>><br /> <span style="color:#808080;"><!-- lib</span><span style="color:#808080;font-family:'瀹?#28003;?;">鐩?#35120;?#28051;?/span><span style="color:#808080;">jar</span><span style="color:#808080;font-family:'瀹?#28003;?;">?#27537;瀛樻斁瑙勫垯锛岄粯璁?#37828;?/span><span style="color:#808080;">${groupId}/${artifactId}</span><span style="color:#808080;font-family:'瀹?#28003;?;">?#27537;鐩?#35120;?#37837;煎紡锛?/span><span style="color:#808080;">flat</span><span style="color:#808080;font-family:'瀹?#28003;?;">琛?#32448;?#37929;存帴?#22968;</span><span style="color:#808080;">jar</span><span style="color:#808080;font-family:'瀹?#28003;?;">?#25985;?#22476;</span><span style="color:#808080;">lib</span><span style="color:#808080;font-family:'瀹?#28003;?;">鐩?#35120;?</span><span style="color:#808080;">--><br /></span> <<span style="color:#cc7832;font-weight:bold;">repositoryLayout</span>>flat</<span style="color:#cc7832;font-weight:bold;">repositoryLayout</span>><br /> <<span style="color:#cc7832;font-weight:bold;">encoding</span>>UTF-8</<span style="color:#cc7832;font-weight:bold;">encoding</span>><br /> <<span style="color:#cc7832;font-weight:bold;">logsDirectory</span>>logs</<span style="color:#cc7832;font-weight:bold;">logsDirectory</span>><br /> <<span style="color:#cc7832;font-weight:bold;">tempDirectory</span>>tmp</<span style="color:#cc7832;font-weight:bold;">tempDirectory</span>><br /> <<span style="color:#cc7832;font-weight:bold;">programs</span>><br /> <<span style="color:#cc7832;font-weight:bold;">program</span>><br /> <<span style="color:#cc7832;font-weight:bold;">id</span>>mall</<span style="color:#cc7832;font-weight:bold;">id</span>><br /> <span style="color:#808080;"><!-- </span><span style="color:#808080;font-family:'瀹?#28003;?;">鍚?#37716;?#32491;?</span><span style="color:#808080;">--><br /></span> <<span style="color:#cc7832;font-weight:bold;">mainClass</span>>com.duxiu.demo.app.ApplicationKt</<span style="color:#cc7832;font-weight:bold;">mainClass</span>><br /> <<span style="color:#cc7832;font-weight:bold;">jvmSettings</span>><br /> <<span style="color:#cc7832;font-weight:bold;">extraArguments</span>><br /> <<span style="color:#cc7832;font-weight:bold;">extraArgument</span>>-server</<span style="color:#cc7832;font-weight:bold;">extraArgument</span>><br /> <<span style="color:#cc7832;font-weight:bold;">extraArgument</span>>-Xmx2G</<span style="color:#cc7832;font-weight:bold;">extraArgument</span>><br /> <<span style="color:#cc7832;font-weight:bold;">extraArgument</span>>-Xms2G</<span style="color:#cc7832;font-weight:bold;">extraArgument</span>><br /> </<span style="color:#cc7832;font-weight:bold;">extraArguments</span>><br /> </<span style="color:#cc7832;font-weight:bold;">jvmSettings</span>><br /> </<span style="color:#cc7832;font-weight:bold;">program</span>><br /> </<span style="color:#cc7832;font-weight:bold;">programs</span>><br /> </<span style="color:#cc7832;font-weight:bold;">configuration</span>><br /></<span style="color:#cc7832;font-weight:bold;">plugin</span>></pre></div><br />鎵撳寘搴旂敤绋?#25652;?骞朵細?#25939;?#22426;bat鎴杝h鍙墽琛屾枃浠?br /><br /><br /><br /><div><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:10.5pt;"><<span style="color:#cc7832;font-weight:bold;">plugin</span>><br /> <<span style="color:#cc7832;font-weight:bold;">artifactId</span>>maven-antrun-plugin</<span style="color:#cc7832;font-weight:bold;">artifactId</span>><br /> <<span style="color:#cc7832;font-weight:bold;">executions</span>><br /> <<span style="color:#cc7832;font-weight:bold;">execution</span>><br /> <<span style="color:#cc7832;font-weight:bold;">id</span>>move-main-class</<span style="color:#cc7832;font-weight:bold;">id</span>><br /> <<span style="color:#cc7832;font-weight:bold;">phase</span>>compile</<span style="color:#cc7832;font-weight:bold;">phase</span>><br /> <<span style="color:#cc7832;font-weight:bold;">configuration</span>><br /> <<span style="color:#cc7832;font-weight:bold;">tasks</span>><br /> <<span style="color:#cc7832;font-weight:bold;">move </span>todir<span style="color:#6a8759;">="${project.build.directory}/${project.artifactId}-${version}/com/duxiu/demo/app"</span>><br /> <<span style="color:#cc7832;font-weight:bold;">fileset </span>dir<span style="color:#6a8759;">="${project.build.directory}/classes/com/duxiu/demo/app"</span>><br /> <<span style="color:#cc7832;font-weight:bold;">include </span>name<span style="color:#6a8759;">="*.class" </span>/><br /> </<span style="color:#cc7832;font-weight:bold;">fileset</span>><br /> </<span style="color:#cc7832;font-weight:bold;">move</span>><br /> </<span style="color:#cc7832;font-weight:bold;">tasks</span>><br /> </<span style="color:#cc7832;font-weight:bold;">configuration</span>><br /> <<span style="color:#cc7832;font-weight:bold;">goals</span>><br /> <<span style="color:#cc7832;font-weight:bold;">goal</span>>run</<span style="color:#cc7832;font-weight:bold;">goal</span>><br /> </<span style="color:#cc7832;font-weight:bold;">goals</span>><br /> </<span style="color:#cc7832;font-weight:bold;">execution</span>><br /> </<span style="color:#cc7832;font-weight:bold;">executions</span>><br /></<span style="color:#cc7832;font-weight:bold;">plugin</span>></pre></div><br />鎵撳寘?#27537;鏃跺?#28751;嗗寘閲岀殑鏌?#28051;枃浠?#32457;?#37716;ㄥ埌?#23514;瀹氱殑浣?#32515;?img src ="http://www.8634070.com/wangxinsh55/aggbug/431252.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-07-20 09:42 <a href="http://www.8634070.com/wangxinsh55/archive/2016/07/20/431252.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title> 浣跨敤embeded tomcat杩?#29723;屽?#23677;叆?#32033;javaee寮鍙?#38171;?#37722;?#37716;╰omcathttp://www.8634070.com/wangxinsh55/archive/2016/07/18/431229.htmlSIMONESIMONEMon, 18 Jul 2016 06:42:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/07/18/431229.htmlhttp://www.8634070.com/wangxinsh55/comments/431229.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/07/18/431229.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431229.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431229.htmlhttps://www.iflym.com/index.php/code/use-embeded-tomcat-to-javaee-start-tomcat.html
鏄?#28598;?#37734;?#32515;?#28051;?#37934;旂?#26421;簡涓嬪叧浜?#28751;唗omcat宓屽叆?#22476;涓荤▼搴?#28051;?#26473;?#29723;?#26473;?#29723;?#38171;岃?#28051;嶆槸?#20762;浠?#37715;?#28751;?#28051;涓獁eb椤?#37929;甤opy?#22476;tomcat涓?#26473;?#29723;?#26473;?#29723;?#37510;?#28052;?#37813;浠?#26473;?#37837;峰仛?#27537;鍘?鍥?#38171;屽嵆鏄?#37733;?#28051;?#26916;?#37929;?#38318;?#32515;插埌瀹?#37812;?#37826;?#38171;屽湪杩?#29723;屾?#23384;柊?#27537;鏃跺?#38171;岄渶瑕佹墜鍔?#37734;?#26473;?#29723;屾?#23384;柊锛屽?#23942;妸鐩稿簲?#21804;鐮乧opy?#22476;tomcat锛岀?#36346;悗鍐?#26473;?#29723;?#37510;?#26473;愮敤embeded tomcat灏?#37721;?#28000;?#28751;嗛」鐩?#28051;巘omcat鍒嗗紑锛屽湪杩?#29723;屾?#23384;柊鏃?#38171;屽厛浣跨敤?#22172;瀹?#28052;夌殑绋?#25652;?#26473;?#29723;岃嚜鍔?#37718;?#37831;存柊锛屽?#21614;?#23384;柊瀹?#23011;?#28052;?#37722;?#38171;屽啀鍚?#37716;╰omcat?#32025;鎴?#37711;跺畠 javaee瀹瑰櫒锛?#26473;?#29723;岄」鐩?#26473;?#29723;?#37510;?

     杩?#37837;峰仛?#27537;鏈缁?#37825;堟灉灏?#37828;?#28103;敼?#31777;椤?#37929;殑杩?#29723;?#37826;瑰紡銆?#37720;熷?#22562;殑杩?#29723;?#37826;瑰紡鏄?#28000;omcat涓?#28051;?#36423;?#38171;岀?#30491;omcat鏉?#37722;?#37716;ㄥ拰缁?#23005;?#26916;?#37929;?#38171;岀幇鍦?#37828;?#37922;?#37812;?#28000;殑鍚?#37716;?#32459;?#25652;?涓?#28051;?#36423;?#38171;岀?#21342;惎鍔?#32459;?#25652;?#37833;?#29840;熻矗鍚?#37716;ㄥ拰缁?#23005;?#26916;?#37929;?#37510;?#28751;?#37929;稿綋浜庣幇鍦?#23092;?#29723;岀殑cs绋?#25652;?#28051;鏍?#38171;?#37832;夊?#26336;嫭?#27537;鍚?#37716;ㄨ剼鏈?#38171;屽湪鍚?#37716;?#37827;?#26473;?#29723;岀幆澧?#26864;勫垵濮?#37718;?#38171;屾?#23384;柊绋?#25652;?#28000;?#37721;?#37711;跺?#20905;搷浣?#38171;屽?#21613;?#23678;垚 涔?#37722;?#37712;?#26473;?#29723;?#37832;缁堢殑椤?#37929;?#37722;?#37716;?#37510;?/p>

     杩?#32481;?#28051;?#29781;?#29825;茶В濡?#28003;?#28003;跨敤embeded tomcat鍦ㄤ唬鐮?#28051;?#26473;?#29723;屽惎鍔ㄥ拰缁?#23005;?#37510;?#32515;?#28051;婄殑涓?#22471;?#26499;绔犲潎涓簍omca5.x鏉ュ仛锛?#26473;?#38322;?#28003;跨敤浜嗘渶鏂扮殑tomcat7锛屽洜涓簍omcat7涓篹mbeded寮 鍙?#38171;屽?#26336;嫭鍙?#29999;冧簡org.apache.tomcat.embed?#23512;锛?#28000;?#26473;?#29723;岀嫭绔嬬殑embed寮鍙?#37510;?#28000;?#28051;?#37828;?#37929;稿簲?#27537;maven?#23512;

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
<dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>7.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-util</artifactId>
            <version>7.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>7.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-logging-juli</artifactId>
            <version>7.0.2</version>
        </dependency>

    浣跨敤?#31777;embed?#23512;涓殑core?#23512;锛?#28000;?#37721;婄敤浜?#32514;?#29831;慾sp?#27537;jasper?#23512;锛岀?#36346;悗鏄?#23480;ュ叿绫?#28000;?#37721;?#26473;?#29723;?#28051;?#37734;?#29825;?#35120;曠殑logging-juli?#23512;銆?#23534;濮?#37712;欎唬鐮?#38171;?/p>

1
2
3
4
5
6
7
//璁?#32515;?#23480;?#28003;?#37929;?#35120;?/code>
        String catalina_home = "d:/";
        Tomcat tomcat = new Tomcat();
        tomcat.setHostname("localhost");
        tomcat.setPort(startPort);
        //璁?#32515;?#23480;?#28003;?#37929;?#35120;?鍏跺?#28852;病浠涔堢敤,tomcat闇瑕?#28003;跨敤杩?#28051;?#37929;?#35120;?#26473;?#29723;屽啓涓浜?#28051;滆タ
        tomcat.setBaseDir(catalina_home);

    涓?#38344;?#28003;跨敤?#31777;Tomcat绫?#37833;?#26473;?#29723;屽惎鍔?#32491;?#38171;屽湪tomcat7浠?#37715;嶅潎鏄?#28003;跨敤涓涓?#37721;獷mbed绫?#37833;?#26473;?#29723;屽惎鍔?#38171;屽湪tomcat7涔?#37722;?#38171;宔mbed绫昏涓嶅缓 璁?#28003;跨敤锛岃屽缓璁?#28003;跨敤鏂扮殑Tomcat绫?#37833;?#26473;?#29723;屽惎鍔ㄤ簡銆?#37906;跺悗璁?#32515;?#28051;?#37832;?#37722;?#38171;岀鍙?#38171;屽?#23944;缃?#28051;涓?#23480;?#28003;?#37929;?#35120;曘?#26473;?#28051;?#23480;?#28003;?#37929;?#35120;?#37721;?#28000;?#37828;?#28000;绘剰鐩?#35120;?#38171;?#28051;?#29781;佹槸tomcat闇瑕?#26473;?涓?#37929;?#35120;?#37833;?#29825;?#35120;?#28051;浜?#28051;滆タ锛?#23011;?#28641;?#29825;?#35120;晈ord淇?#37805;?#38171;?#37827;?#36423;?#28103;?#37805;紙濡傛灉?#21412;缃簡鏃?#36423;楃殑璇?#38171;?#38171;?#28000;?#37721;?#28051;?#37827;舵枃浠?#28699;樺偍绛?#37510;?/p>

1
2
3
4
5
6
7
8
//璁?#32515;?#32459;?#25652;忕殑鐩?#35120;?#28103;?#37805;?/code>
        tomcat.getHost().setAppBase("e:/");
        // Add AprLifecycleListener
        StandardServer server = (StandardServer) tomcat.getServer();
        AprLifecycleListener listener = new AprLifecycleListener();
        server.addLifecycleListener(listener);
        //娉?#37712;屽叧闂?#32468;?#37721;?#28000;?#26473;?#29723;屽叧闂?/code>
        tomcat.getServer().setPort(shutdownPort);

    涓?#38344;㈢殑?#21804;鐮?#38171;岄鍏?#29825;?#32515;?#37812;?#28000;殑椤?#37929;?#32459;?#25652;?#37813;鍦ㄧ殑appbase锛屽嵆?#25985;椤?#37929;唬鐮佺殑鍦?#37826;?#37510;?#37734;?#38315;氬?#21733;殑tomcat?#21412;缃?#28051;?#38171;?#26473;?#28051;?#37929;?#35120;?#28051;?#22471;鏄痺ebapps銆傛帴 ?#28483;璁?#32515;?#28051;涓猯istener锛?#26473;?#28051;猯istener涓?#29781;佹槸璐熻矗鍚?#37716;?#28051;浜?#23011;?#28641;俬tml native鏀?#37816;佺▼搴?#28000;?#37721;奿pv6绛?#28103;?#37805;厤缃紙鍙?#28000;?#36423;界暐锛?#37510;傛帴?#28483;鏄厤缃?#28051;涓叧闂殑娉?#37712;岀鍙?#38171;屽?#25779;悜杩?#28051;?#32468;?#37721;?#37721;?#38315;?#28103;?#37805;?#37827;?#38171;?#28751;?#37721;?#28000;?#26440;惧埌?#21479;闂璽omcat?#27537;鐩殑 ?#32025;鍚?#38344;細璁?#38171;?#37510;?/p>

1
2
3
4
5
6
7
8
9
//鍔犺浇涓?#28051;嬫枃
        StandardContext standardContext = new StandardContext();
        standardContext.setPath("/aa");//contextPath
        standardContext.setDocBase("aa");//?#26499;浠?#37929;?#35120;?#28003;?#32515;?/code>
        standardContext.addLifecycleListener(new Tomcat.DefaultWebXmlListener());
        //淇濊瘉宸?#32513;忛厤缃?#28610;戒簡銆?/code>
        standardContext.addLifecycleListener(new Tomcat.FixContextListener());
        standardContext.setSessionCookieName("t-session");
        tomcat.getHost().addChild(standardContext);

    鎴?#28000;?#37719;曠嫭浣跨敤?#31777;涓涓狢ontext鏉?#28051;?#26473;?#28051;猦ost娣?#37716;?#28051;?#28051;嬫枃锛宼omcat鏈韩鎻?#28186;?#28051;涓?#37826;规硶tomcat.addWeb鏂规硶鏉?#23075;?#37716;犻」鐩寘锛?#28051;?#26473;?鐢?#27996;?#26473;?#38322;岄渶瑕?#37719;曠嫭璁?#32515;?#28051;涓猼omcat?#27537;sessionName锛?#37813;浠?#28003;跨敤涓?#28051;巘omcat.addWeb瀹炵幇绫?#28028;肩殑鏂规硶鏉?#23075;?#37716;?#28051;涓?#26916;?#37929;寘銆?br />     浠?#28051;婁唬鐮?#28051;?#37832;?#28051;?#28051;?#38343;瑕佹敞鎰忕殑listener锛?#28051;涓?#37828;疍efaultWebXmlListener锛?#26473;?#28051;?#37828;?#37922;眛omcat鍔犺浇涓浜涢粯璁ょ殑?#21412;缃?#28103;?鎭?#38171;?#23011;?#28641;俲spServlet锛?#28000;?#37721;?#28051;浜涚箒澶嶇殑mime/type淇?#37805;?#38171;?#37716;?#28051;?#26473;?#28051;?#38171;?#28751;?#28051;?#38343;瑕佹垜浠嚜宸?#37720;?#37712;?#26473;欎箞澶氱殑?#21412;缃?#38171;屽洜涓?#23011;?#28051;?#26916;?#37929;?#38318;?#38343;瑕?#26473;欎簺銆?#26473;?#28051;厤缃?涓巘omcat鐩?#35120;?#28051;嬬殑conf/web.xml涓殑?#21412;缃?#28051;鏍?#38171;屽彧涓?#26473;?#26473;?#38322;?#37828;唬鐮?#37718;栦簡銆?#32471;?#27996;?#28051;?#37828;疐ixContextListener锛?#26473;?#28051;?#28051;?#29781;佹槸鍦?#26916;?#37929;?閮?#32515;?#28729;屽悗锛?#28751;?#26473;?#28051;?#28051;?#28051;嬫枃璁?#32515;?#28051;篶onfigured锛?#29723;?#32448;?#23480;?#32513;忛厤缃?#28610;戒簡?#32025;涓嶇劧锛宼omcat鍚?#37716;?#37827;朵細鎶?#38335;?#38171;屽嵆鐩稿簲涓?#28051;嬫枃杩?#37832;厤缃?#28610;?#38171;?#37510;?br />     ?#21412;缃甇K?#31777;涔?#37722;?#38171;?#28751;?#37828;?#37722;?#37716;╰omcat?#31777;锛?/p>

1
2
tomcat.start();
        tomcat.getServer().await();

    鍚?#37716;╰omcat锛屽苟璁﹖omcat鍦ㄥ叧闂?#32468;?#37721;?#28051;?#37929;?#37722;?#37510;?#28641;傛?#28356;病鏈?#37832;鍚?#28051;鍙?#38171;岀▼搴?#28751;?#37929;存帴缁?#37833;?#38171;?#28103;濊?#20346;洃鍚?#28052;?#37722;?#38171;宼omcat灏?#28051;鐩?#37929;?#37722;叧闂?#27996;?#28000;?#38171;屽?#21614;?#22794;叧闂?#27996;?#28000;朵箣鍚?#37813;?#32513;?#37833;熷?#25779;?#23943;▼搴?#37510;?#37813;浠?#28641;傛灉鎯?#29781;佸叧闂?#35120;撳?#23943;殑tomcat锛屽彧闇瑕?#37722;戝叧闂?#32468;?#37721;?#37721;?#38315;?#28051;浜?#28103;?#37805;?#37719;?#37721;?#38171;?/p>

1
2
3
4
5
6
7
8
9
private static void shutdown() throws Exception {
        Socket socket = new Socket("localhost", shutdownPort);
        OutputStream stream = socket.getOutputStream();
        for(int i = 0;i < shutdown.length();i++)
            stream.write(shutdown.charAt(i));
        stream.flush();
        stream.close();
        socket.close();
    }

    杩?#37837;峰嵆鍙?#26440;惧埌?#21479;闂璽omcat?#27537;鐩殑銆?/p>

     瀹?#38340;?#28051;?#37930;?#37825;?#28051;?#26916;?#37929;唬鐮?#38171;岄」鐩唬鐮佺殑杩?#29723;?#38171;?#28751;?#37828;?#28051;涓厤缃?#28051;涓?#37737;虹?#27537;server.xml?#32025;鍗硉omcat鐩?#35120;?#28051;嬬殑 conf/server.xml)锛屽?#22565;厤缃?#26473;?#29723;岀鍙?#38171;屽叧闂?#37929;?#37722;?#32468;?#37721;?#38171;涚?#36346;悗?#21412;缃?#26473;?#29723;岀殑host浠?#37721;婃坊鍔?#28051;涓?#28051;?#28051;嬫枃context锛?#37832;鍚?#28751;卞紑濮?#26473;?#29723;屽?#36346;紑濮?#37929;?鍚?#37510;?#28725;圭収杩?#28051;?#32459;?#25652;?#38171;屽?#23943;湅涓涓媠erver.xml涓殑?#21412;缃?#28103;?#37805;?#38171;?#28751;卞緢瀹?#37828;?#37828;庣櫧浠?#28051;?#26473;欐?#20856;唬鐮佷簡銆?/p>



]]>
Secure Kafka Java Producer with Kerberoshttp://www.8634070.com/wangxinsh55/archive/2016/07/05/431097.htmlSIMONESIMONETue, 05 Jul 2016 03:41:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/07/05/431097.htmlhttp://www.8634070.com/wangxinsh55/comments/431097.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/07/05/431097.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431097.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431097.html闃?#29831;?#37711;ㄦ枃

]]>
Kerberos ?#21412;缃?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/07/05/431095.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Tue, 05 Jul 2016 03:37:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/07/05/431095.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/431095.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/07/05/431095.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/431095.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/431095.html</trackback:ping><description><![CDATA[<div>https://www.zybuluo.com/xtccc/note/176298</div><br /><div><h1>?#25698;浜涢厤缃枃浠?#38171;?/h1><p data-anchor-id="aalv">鍦?#28729;?#29761;呭ソKerberos?#27537;杞?#28000;朵箣鍚?#38171;屼細?#25956;?#22476;鍑?#28051;厤缃枃浠?#38171;?#28186;?#28641;?<br /> + /etc/krb5.conf <br /> + /var/kerberos/krb5kdc/kdc.conf</p><p data-anchor-id="738z"><br /><br /></p><h1>?#21412;缃枃浠剁殑璇存槑</h1><h2>/etc/krb5.conf</h2><p data-anchor-id="lxq4">鍙?#28000;ョ敤鍛?#28000;?code>man krb5.conf</code>鏉?#37836;?#37930;嬪叧浜?#29831;ラ厤缃枃浠剁殑璇存槑</p><p data-anchor-id="vd2l">鍏?#37930;?#28051;涓嬭?#26499;浠剁殑妯?#37833;?#38171;?/p><pre linenums="" prettyprinted"="" data-anchor-id="xnzw"><ol><li><code>[logging]</code></li><li><code>default = FILE:/var/log/krb5libs.log</code></li><li><code>kdc = FILE:/var/log/krb5kdc.log</code></li><li><code>admin_server = FILE:/var/log/kadmind.log</code></li><li><code></code><br /></li><li><code>[libdefaults]</code></li><li><code>default_realm = EXAMPLE.COM</code></li><li><code>dns_lookup_realm = false</code></li><li><code>dns_lookup_kdc = false</code></li><li><code>ticket_lifetime = 24h</code></li><li><code>renew_lifetime = 7d</code></li><li><code>forwardable = true</code></li><li><code></code><br /></li><li><code>[realms]</code></li><li><code>EXAMPLE.COM = {</code></li><li><code>kdc = example.com</code></li><li><code>admin_server = example.com</code></li><li><code>}</code></li><li><code></code><br /></li><li><code>[domain_realm]</code></li><li><code>.example.com = EXAMPLE.COM</code></li><li><code>example.com = EXAMPLE.COM</code></li></ol></pre><p data-anchor-id="9hm0"><br /> <br /> ?#21479;浜?#37713;?#28051;?#38322;嶈?#20352;厤缃?#26916;圭殑璇存槑 <br /> + <code>[realms].kdc</code> : the name of the host running a KDC for that realm. <br /> + <code>[realms].admin_server</code> : identifies the host where the administration server is running. Typically this is the Master Kerberos server. <br /> + <code>[domain_realm]</code> : provides a translation from a hostname to the Kerberos realm name for the service provided by that host.</p></div><img src ="http://www.8634070.com/wangxinsh55/aggbug/431095.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-07-05 11:37 <a href="http://www.8634070.com/wangxinsh55/archive/2016/07/05/431095.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title>ubuntu kerberos?#21412;缃?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/07/05/431096.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Tue, 05 Jul 2016 03:37:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/07/05/431096.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/431096.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/07/05/431096.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/431096.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/431096.html</trackback:ping><description><![CDATA[<div>http://www.8634070.com/ivanwan/archive/2012/12/19/393221.html</div><br /><div>https://help.ubuntu.com/10.04/serverguide/kerberos.html<br /><h2>Kerberos</h2> <p> <strong>Kerberos</strong> is a network authentication system based on the principal of a trusted third party. The other two parties being the user and the service the user wishes to authenticate to. Not all services and applications can use Kerberos, but for those that can, it brings the network environment one step closer to being Single Sign On (SSO). </p> <p> This section covers installation and configuration of a Kerberos server, and some example client configurations. </p> <div title="Overview"> <div> <div> <div> <h3>Overview</h3> </div> </div> </div> <p> If you are new to Kerberos there are a few terms that are good to understand before setting up a Kerberos server. Most of the terms will relate to things you may be familiar with in other environments: </p> <div> <ul type="disc"><li> <p> <em>Principal:</em> any users, computers, and services provided by servers need to be defined as Kerberos Principals. </p> </li><li> <p> <em>Instances:</em> are used for service principals and special administrative principals. </p> </li><li> <p> <em>Realms:</em> the unique realm of control provided by the Kerberos installation. Usually the DNS domain converted to uppercase (EXAMPLE.COM). </p> </li><li> <p> <em>Key Distribution Center:</em> (KDC) consist of three parts, a database of all principals, the authentication server, and the ticket granting server. For each realm there must be at least one KDC. </p> </li><li> <p> <em>Ticket Granting Ticket:</em> issued by the Authentication Server (AS), the Ticket Granting Ticket (TGT) is encrypted in the user's password which is known only to the user and the KDC. </p> </li><li> <p> <em>Ticket Granting Server:</em> (TGS) issues service tickets to clients upon request. </p> </li><li> <p> <em>Tickets:</em> confirm the identity of the two principals. One principal being a user and the other a service requested by the user. Tickets establish an encryption key used for secure communication during the authenticated session. </p> </li><li> <p> <em>Keytab Files:</em> are files extracted from the KDC principal database and contain the encryption key for a service or host. </p> </li></ul> </div> <p> To put the pieces together, a Realm has at least one KDC, preferably two for redundancy, which contains a database of Principals. When a user principal logs into a workstation, configured for Kerberos authentication, the KDC issues a Ticket Granting Ticket (TGT). If the user supplied credentials match, the user is authenticated and can then request tickets for Kerberized services from the Ticket Granting Server (TGS). The service tickets allow the user to authenticate to the service without entering another username and password. </p> </div> <div title="Kerberos Server"> <div> <div> <div> <h3>Kerberos Server</h3> </div> </div> </div> <div title="Installation"> <div> <div> <div> <h4>Installation</h4> </div> </div> </div> <p> Before installing the Kerberos server a properly configured DNS server is needed for your domain. Since the Kerberos Realm by convention matches the domain name, this section uses the <em>example.com</em> domain configured in <a href="https://help.ubuntu.com/10.04/serverguide/dns-configuration.html#dns-primarymaster-configuration" title="Primary Master">the section called “Primary Master”</a>. </p> <p> Also, Kerberos is a time sensitive protocol. So if the local system time between a client machine and the server differs by more than five minutes (by default), the workstation will not be able to authenticate. To correct the problem all hosts should have their time synchronized using the <em>Network Time Protocol (NTP)</em>. For details on setting up NTP see <a href="https://help.ubuntu.com/10.04/serverguide/NTP.html" title="Time Synchronisation with NTP">the section called “Time Synchronisation with NTP”</a>. </p> <p> The first step in installing a Kerberos Realm is to install the <strong>krb5-kdc</strong> and <strong>krb5-admin-server</strong> packages. From a terminal enter: </p> <pre><strong>sudo apt-get install krb5-kdc krb5-admin-server</strong> </pre> <p> You will be asked at the end of the install to supply a name for the Kerberos and Admin servers, which may or may not be the same server, for the realm. </p> <p> Next, create the new realm with the <strong>kdb5_newrealm</strong> utility: </p> <pre><strong>sudo krb5_newrealm</strong> </pre> </div> <div title="Configuration"> <div> <div> <div> <h4>Configuration</h4> </div> </div> </div> <p> The questions asked during installation are used to configure the <code>/etc/krb5.conf</code> file. If you need to adjust the Key Distribution Center (KDC) settings simply edit the file and restart the <strong>krb5-kdc</strong> daemon. </p> <div> <ol type="1"><li title="Step 1"> <p> Now that the KDC running an admin user is needed. It is recommended to use a different username from your everyday username. Using the <strong>kadmin.local</strong> utility in a terminal prompt enter: </p> <pre><strong>sudo kadmin.local</strong> <code>Authenticating as principal root/admin@EXAMPLE.COM with password. kadmin.local:</code><strong><code> addprinc steve/admin</code></strong> <code>WARNING: no policy specified for steve/admin@EXAMPLE.COM; defaulting to no policy Enter password for principal "steve/admin@EXAMPLE.COM": Re-enter password for principal "steve/admin@EXAMPLE.COM": Principal "steve/admin@EXAMPLE.COM" created. kadmin.local:</code><strong><code> quit</code></strong> </pre> <p> In the above example steve is the <em>Principal</em>, /admin is an <em>Instance</em>, and @EXAMPLE.COM signifies the realm. The "every day" Principal would be <em>steve@EXAMPLE.COM</em>, and should have only normal user rights. </p> <div title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"> <table summary="Note" border="0"> <tbody><tr> <td rowspan="2" valign="top" width="25" align="center"> <img alt="[Note]" src="https://help.ubuntu.com/libs/admon/note.png" /> </td> <th align="left"><br /></th> </tr> <tr> <td valign="top" align="left"> <p> Replace <em>EXAMPLE.COM</em> and <em>steve</em> with your Realm and admin username. </p> </td> </tr> </tbody></table> </div> </li><li title="Step 2"> <p> Next, the new admin user needs to have the appropriate Access Control List (ACL) permissions. The permissions are configured in the <code>/etc/krb5kdc/kadm5.acl</code> file: </p> <pre>steve/admin@EXAMPLE.COM * </pre> <p> This entry grants <em>steve/admin</em> the ability to perform any operation on all principals in the realm. </p> </li><li title="Step 3"> <p> Now restart the <strong>krb5-admin-server</strong> for the new ACL to take affect: </p> <pre><strong>sudo /etc/init.d/krb5-admin-server restart</strong> </pre> </li><li title="Step 4"> <p> The new user principal can be tested using the <strong>kinit utility</strong>: </p> <pre><strong>kinit steve/admin</strong> <code>steve/admin@EXAMPLE.COM's Password:</code> </pre> <p> After entering the password, use the <strong>klist</strong> utility to view information about the Ticket Granting Ticket (TGT): </p> <pre><strong>klist</strong> <code>Credentials cache: FILE:/tmp/krb5cc_1000 Principal: steve/admin@EXAMPLE.COM Issued Expires Principal Jul 13 17:53:34 Jul 14 03:53:34 krbtgt/EXAMPLE.COM@EXAMPLE.COM</code> </pre> <p> You may need to add an entry into the <code>/etc/hosts</code> for the KDC. For example: </p> <pre>192.168.0.1 kdc01.example.com kdc01 </pre> <p> Replacing <em>192.168.0.1</em> with the IP address of your KDC. </p> </li><li title="Step 5"> <p> In order for clients to determine the KDC for the Realm some DNS SRV records are needed. Add the following to <code>/etc/named/db.example.com</code>: </p> <pre>_kerberos._udp.EXAMPLE.COM. IN SRV 1 0 88 kdc01.example.com. _kerberos._tcp.EXAMPLE.COM. IN SRV 1 0 88 kdc01.example.com. _kerberos._udp.EXAMPLE.COM. IN SRV 10 0 88 kdc02.example.com. _kerberos._tcp.EXAMPLE.COM. IN SRV 10 0 88 kdc02.example.com. _kerberos-adm._tcp.EXAMPLE.COM. IN SRV 1 0 749 kdc01.example.com. _kpasswd._udp.EXAMPLE.COM. IN SRV 1 0 464 kdc01.example.com. </pre> <div title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"> <table summary="Note" border="0"> <tbody><tr> <td rowspan="2" valign="top" width="25" align="center"> <img alt="[Note]" src="https://help.ubuntu.com/libs/admon/note.png" /> </td> <th align="left"><br /></th> </tr> <tr> <td valign="top" align="left"> <p> Replace <em>EXAMPLE.COM</em>, <em>kdc01</em>, and <em>kdc02</em> with your domain name, primary KDC, and secondary KDC. </p> </td> </tr> </tbody></table> </div> <p> See <a href="https://help.ubuntu.com/10.04/serverguide/dns.html" title="Chapter 7. Domain Name Service (DNS)">Chapter 7, <em>Domain Name Service (DNS)</em></a> for detailed instructions on setting up DNS. </p> </li></ol> </div> <p> Your new Kerberos Realm is now ready to authenticate clients. </p> </div> </div> <div title="Secondary KDC"> <div> <div> <div> <h3>Secondary KDC</h3> </div> </div> </div> <p> Once you have one Key Distribution Center (KDC) on your network, it is good practice to have a Secondary KDC in case the primary becomes unavailable. </p> <div> <ol type="1"><li title="Step 1"> <p> First, install the packages, and when asked for the Kerberos and Admin server names enter the name of the Primary KDC: </p> <pre><strong>sudo apt-get install krb5-kdc krb5-admin-server</strong> </pre> </li><li title="Step 2"> <p> Once you have the packages installed, create the Secondary KDC's host principal. From a terminal prompt, enter: </p> <pre><strong>kadmin -q "addprinc -randkey host/kdc02.example.com"</strong> </pre> <div title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"> <table summary="Note" border="0"> <tbody><tr> <td rowspan="2" valign="top" width="25" align="center"> <img alt="[Note]" src="https://help.ubuntu.com/libs/admon/note.png" /> </td> <th align="left"><br /></th> </tr> <tr> <td valign="top" align="left"> <p> After, issuing any <strong>kadmin</strong> commands you will be prompted for your <em>username/admin@EXAMPLE.COM</em> principal password. </p> </td> </tr> </tbody></table> </div> </li><li title="Step 3"> <p> Extract the <em>keytab</em> file: </p> <pre><strong>kadmin -q "ktadd -k keytab.kdc02 host/kdc02.example.com"</strong> </pre> </li><li title="Step 4"> <p> There should now be a <code>keytab.kdc02</code> in the current directory, move the file to <code>/etc/krb5.keytab</code>: </p> <pre><strong>sudo mv keytab.kdc02 /etc/krb5.keytab</strong> </pre> <div title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"> <table summary="Note" border="0"> <tbody><tr> <td rowspan="2" valign="top" width="25" align="center"> <img alt="[Note]" src="https://help.ubuntu.com/libs/admon/note.png" /> </td> <th align="left"><br /></th> </tr> <tr> <td valign="top" align="left"> <p> If the path to the <code>keytab.kdc02</code> file is different adjust accordingly. </p> </td> </tr> </tbody></table> </div> <p> Also, you can list the principals in a Keytab file, which can be useful when troubleshooting, using the <strong>klist</strong> utility: </p> <pre><strong>sudo klist -k /etc/krb5.keytab</strong> </pre> </li><li title="Step 5"> <p> Next, there needs to be a <code>kpropd.acl</code> file on each KDC that lists all KDCs for the Realm. For example, on both primary and secondary KDC, create <code>/etc/krb5kdc/kpropd.acl</code>: </p> <pre>host/kdc01.example.com@EXAMPLE.COM host/kdc02.example.com@EXAMPLE.COM </pre> </li><li title="Step 6"> <p> Create an empty database on the <em>Secondary KDC</em>: </p> <pre><strong>sudo kdb5_util -s create</strong> </pre> </li><li title="Step 7"> <p> Now start the <strong>kpropd</strong> daemon, which listens for connections from the <strong>kprop</strong> utility. <strong>kprop</strong> is used to transfer dump files: </p> <pre><strong>sudo kpropd -S</strong> </pre> </li><li title="Step 8"> <p> From a terminal on the <em>Primary KDC</em>, create a dump file of the principal database: </p> <pre><strong>sudo kdb5_util dump /var/lib/krb5kdc/dump</strong> </pre> </li><li title="Step 9"> <p> Extract the Primary KDC's <em>keytab</em> file and copy it to <code>/etc/krb5.keytab</code>: </p> <pre><strong>kadmin -q "ktadd -k keytab.kdc01 host/kdc01.example.com"</strong> <strong>sudo mv keytab.kdc01 /etc/kr5b.keytab</strong> </pre> <div title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"> <table summary="Note" border="0"> <tbody><tr> <td rowspan="2" valign="top" width="25" align="center"> <img alt="[Note]" src="https://help.ubuntu.com/libs/admon/note.png" /> </td> <th align="left"><br /></th> </tr> <tr> <td valign="top" align="left"> <p> Make sure there is a <em>host</em> for <em>kdc01.example.com</em> before extracting the Keytab. </p> </td> </tr> </tbody></table> </div> </li><li title="Step 10"> <p> Using the <strong>kprop</strong> utility push the database to the Secondary KDC: </p> <pre><strong>sudo kprop -r EXAMPLE.COM -f /var/lib/krb5kdc/dump kdc02.example.com</strong> </pre> <div title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"> <table summary="Note" border="0"> <tbody><tr> <td rowspan="2" valign="top" width="25" align="center"> <img alt="[Note]" src="https://help.ubuntu.com/libs/admon/note.png" /> </td> <th align="left"><br /></th> </tr> <tr> <td valign="top" align="left"> <p> There should be a <em>SUCCEEDED</em> message if the propagation worked. If there is an error message check <code>/var/log/syslog</code> on the secondary KDC for more information. </p> </td> </tr> </tbody></table> </div> <p> You may also want to create a <strong>cron</strong> job to periodically update the database on the Secondary KDC. For example, the following will push the database every hour: </p> <pre># m h dom mon dow command 0 * * * * /usr/sbin/kdb5_util dump /var/lib/krb5kdc/dump && /usr/sbin/kprop -r EXAMPLE.COM -f /var/lib/krb5kdc/dump kdc02.example.com </pre> </li><li title="Step 11"> <p> Back on the <em>Secondary KDC</em>, create a <em>stash</em> file to hold the Kerberos master key: </p> <pre><strong>sudo kdb5_util stash</strong> </pre> </li><li title="Step 12"> <p> Finally, start the <strong>krb5-kdc</strong> daemon on the Secondary KDC: </p> <pre><strong>sudo /etc/init.d/krb5-kdc start</strong> </pre> </li></ol> </div> <p> The <em>Secondary KDC</em> should now be able to issue tickets for the Realm. You can test this by stopping the <strong>krb5-kdc</strong> daemon on the Primary KDC, then use <strong>kinit</strong> to request a ticket. If all goes well you should receive a ticket from the Secondary KDC. </p> </div> <div title="Kerberos Linux Client"> <div> <div> <div> <h3>Kerberos Linux Client</h3> </div> </div> </div> <p> This section covers configuring a Linux system as a <strong>Kerberos</strong> client. This will allow access to any kerberized services once a user has successfully logged into the system. </p> <div title="Installation"> <div> <div> <div> <h4>Installation</h4> </div> </div> </div> <p> In order to authenticate to a Kerberos Realm, the <strong>krb5-user</strong> and <strong>libpam-krb5</strong> packages are needed, along with a few others that are not strictly necessary but make life easier. To install the packages enter the following in a terminal prompt: </p> <pre><strong>sudo apt-get install krb5-user libpam-krb5 libpam-ccreds auth-client-config</strong> </pre> <p> The <strong>auth-client-config</strong> package allows simple configuration of PAM for authentication from multiple sources, and the <strong>libpam-ccreds</strong> will cache authentication credentials allowing you to login in case the Key Distribution Center (KDC) is unavailable. This package is also useful for laptops that may authenticate using Kerberos while on the corporate network, but will need to be accessed off the network as well. </p> </div> <div title="Configuration"> <div> <div> <div> <h4>Configuration</h4> </div> </div> </div> <p> To configure the client in a terminal enter: </p> <pre><strong>sudo dpkg-reconfigure krb5-config</strong> </pre> <p> You will then be prompted to enter the name of the Kerberos Realm. Also, if you don't have DNS configured with Kerberos <em>SRV</em> records, the menu will prompt you for the hostname of the Key Distribution Center (KDC) and Realm Administration server. </p> <p> The <strong>dpkg-reconfigure</strong> adds entries to the <code>/etc/krb5.conf</code> file for your Realm. You should have entries similar to the following: </p> <pre>[libdefaults] default_realm = EXAMPLE.COM ... [realms] EXAMPLE.COM = } kdc = 192.168.0.1 admin_server = 192.168.0.1 } </pre> <p> You can test the configuration by requesting a ticket using the <strong>kinit</strong> utility. For example: </p> <pre><strong>kinit steve@EXAMPLE.COM</strong> <code>Password for steve@EXAMPLE.COM:</code> </pre> <p> When a ticket has been granted, the details can be viewed using <strong>klist</strong>: </p> <pre><strong>klist</strong> <code>Ticket cache: FILE:/tmp/krb5cc_1000 Default principal: steve@EXAMPLE.COM Valid starting Expires Service principal 07/24/08 05:18:56 07/24/08 15:18:56 krbtgt/EXAMPLE.COM@EXAMPLE.COM renew until 07/25/08 05:18:57 Kerberos 4 ticket cache: /tmp/tkt1000 klist: You have no tickets cached</code> </pre> <p> Next, use the <strong>auth-client-config</strong> to configure the <strong>libpam-krb5</strong> module to request a ticket during login: </p> <pre><strong>sudo auth-client-config -a -p kerberos_example</strong> </pre> <p> You will should now receive a ticket upon successful login authentication. </p> </div> </div> <div title="Resources"> <div> <div> <div> <h3>Resources</h3> </div> </div> </div> <div> <ul type="disc"><li> <p> For more information on Kerberos see the <a href="http://web.mit.edu/Kerberos/" target="_top">MIT Kerberos</a> site. </p> </li><li> <p> The <a href="https://help.ubuntu.com/community/Kerberos" target="_top">Ubuntu Wiki Kerberos</a> page has more details. </p> </li><li> <p> O'Reilly's <a href="http://oreilly.com/catalog/9780596004033/" target="_top">Kerberos: The Definitive Guide</a> is a great reference when setting up Kerberos. </p> </li><li> <p> Also, feel free to stop by the <em>#ubuntu-server</em> IRC channel on <a href="http://freenode.net/" target="_top">Freenode</a> if you have Kerberos questions. </p> </li></ul> </div> </div></div><img src ="http://www.8634070.com/wangxinsh55/aggbug/431096.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-07-05 11:37 <a href="http://www.8634070.com/wangxinsh55/archive/2016/07/05/431096.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title>kerberos瀹?#29761;呴厤缃?http://www.8634070.com/wangxinsh55/archive/2016/07/05/431094.htmlSIMONESIMONETue, 05 Jul 2016 03:36:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/07/05/431094.htmlhttp://www.8634070.com/wangxinsh55/comments/431094.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/07/05/431094.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431094.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431094.htmlhttp://blog.csdn.net/caizhongda/article/details/7947722
瀹?#29761;?#23005;?#26976;?#38171;?nbsp;
1.涓嬭浇krb5-1.9 
http://web.mit.edu/kerberos/dist/krb5/1.9/krb5-1.9-signed.tar 

2.瑙?#37720;?/span> 
tar -xvf krb5-1.9.signed.tar 
?#25939;?#22426;krb5-1.9.tar.gz ?#25328;krb5-1.9.tar.gz.asc 
缁?#32513;?#29785;?#37720;媡ar zxvf krb5-1.9.tar.gz 

3.缂?#29831;?/span> 
cd krb5-1.9/src 
./configure 
make 
make install 

4.?#21412;缃?etc/krb5.conf 
杩?#28051;?#37828;疜erberos鏈涓?#29781;佺殑?#21412;缃枃浠?#38171;岃?#28051;?#28051;瀹?#29781;佹斁鍦?etc涓?nbsp;
Xml?#21804;鐮?nbsp; 鏀惰?#24526;唬鐮? style=
  1. [libdefaults]  
  2.       default_realm = 360BUY.COM  
  3.   
  4. [realms]  
  5.       360BUY.COM = {  
  6.             kdc = m1.360buy.com  
  7.             admin_server = m1.360buy.com  
  8.             default_domain =360buy.com  
  9.       }  
  10.   
  11. [logging]  
  12.       kdc = FILE:/data/logs/krb5/krb5kdc.log  
  13.       admin_server = FILE:/data/logs/krb5/kadmin.log  
  14.       default = FILE:/data/logs/krb5/krb5lib.log  

[libdefaults]涓殑defalt_realm琛?#32448;?#37734;?#28051;?#32513;?#37713;?#37737;熺殑鏃跺?#38171;岄粯璁?#38322;囩敤杩?#28051;?nbsp;
[logging]涓殑鏄寚瀹?#37827;?#36423;楃殑浣?#32515;?nbsp;
[realms]鏄?#37832;閲嶈?#20346;殑涔熸槸Kerberos涓?#37832;闅剧殑姒?#36423;?#37510;?#38171;岀О涓簁erberos鍩?#38171;?#29723;?#32448;篕DC鎵绠?#26440;栫殑?#23510;鍥?#38171;屽彲浠ュ拰DNS鍩熷悕涓鏍?#38171;?#28052;熷彲浠?#28051;?#28051;鏍?nbsp;

5.?#21412;缃?usr/local/var/krb5kdc/kdc.conf 
鐢?#27996;?#28051;?#38344;?#28729;?#29761;?#37827;舵病鏈?#38315;夋嫨瀹?#29761;?#37929;?#35120;?#38171;?#37813;浠ラ粯璁ょ殑瀹?#29761;?#28003;?#32515;?#37734;?usr/local/var/krb5kdc 
Xml?#21804;鐮?nbsp; 鏀惰?#24526;唬鐮? style=
  1. [kdcdefaults]  
  2.       kdc_ports=750,88  
  3.   
  4. [realm]  
  5.       360BUY.COM ={  
  6.             database_name=/usr/local/var/krb5kdc/principal  
  7.             admin_keytab=/usr/local/var/krb5kdc/kadm5.keytab  
  8.             acl_file=/usr/local/var/krb5kdc/kadm5.acl  
  9.             key_stash_file=/usr/local/var/krb5kdc/.k5.360BUY.COM  
  10.             kdc_ports=750,88  
  11.             max_life=10h 0m 0s  
  12.             max_renewable_life=7d 0h 0m 0s  
  13.       }  



6.鍒?#23524;?#28051;涓猭erberos?#26271;鎹?#25652;?/span> 
Java?#21804;鐮?nbsp; 鏀惰?#24526;唬鐮? style=
  1. /usr/local/sbin/kdb5_util create -r 360BUY.COM -s  

?#32048;瑕佹眰鍒?#23524;烘暟鎹?#25652;撶殑瀵?#37934;併?nbsp;
骞?#28051;?#37714;?#23524;?/span>/usr/local/var/krb5kdc/principal淇?#28699;樻暟鎹?#25652;撴枃浠?nbsp;

7.?#27365;褰昸erberos 
Java?#21804;鐮?nbsp; 鏀惰?#24526;唬鐮? style=
  1. /usr/local/sbin/kadmin.local   


1锛夋煡鐪嬬敤鎴?nbsp;
listprincs 

2锛夋坊鍔犵敤鎴?nbsp;
addprinc admin/admin@360BUY.COM 

3锛?#37714;犻櫎?#25956;鎴?nbsp;
delprinc 

4锛?#37714;?#23524;簁eytab?#26499;浠? 
Java?#21804;鐮?nbsp; 鏀惰?#24526;唬鐮? style=
  1. ktadd -k /usr/local/var/krb5kdc/kadm5.keytab kadmin/admin kadmin/changepw  

鍙?#28000;ョ敤kadd鏉?#28583;?#37716;犵敤鎴风殑鏉?#38340;?nbsp;
娉ㄦ剰kadm5.keytab?#27537;璺?#23536;勮涓巏dc.conf涓殑璺?#23536;?#28051;鑷?nbsp;


8.閲?#37722;痥rb5kdc?#25328;kadmind杩涚▼ 
/usr/local/sbin/kadmind 
/usr/local/sbin/krb5kdc 

9.鏇存敼/etc/hosts?#26499;浠?/span> 
娣?#37716;?#28725;瑰簲?#27537;host 
192.168.101.201 m1.360buy.com kdc 
192.168.101.202 m2.360buy.com client 
骞?#28051;?#38343;瑕?#28103;敼瀵瑰簲?#27537;hostname 


10.鍦↘DC鏈?#37716;?#37731;?#28051;婃?#23341;瘯绁?#37817;?#29831;锋眰
 
/usr/local/sbin/kadmin.local 
kadmin.local:addprinc winston@360BUY.COM 
鎻愮ず鍒?#23524;?#28725;?#37934;?#38171;岀?#36346;悗閫鍑?nbsp;

su winston 
$ kinit winston@360BUY.COM 
鎻愮ず杈撳叆鍒氬?#27692;垱寤虹殑瀵?#37934;?nbsp;

$ klist 鏌?#37930;嬭嚜宸?#37922;?#29831;风殑绁?#37817;?nbsp;

11.鍦–lient绔?#28729;?#29761;卥erberos 
鍚?#37837;?#38343;瑕佺紪璇?#38171;?#28003;嗘槸鍙?#38343;瑕侀厤缃枃浠?etc/krb5.conf 
鍐呭?#29808;拰鏈?#37716;?#37731;ㄧ殑涓鑷?nbsp;

12.娴嬭瘯KDC鏈?#37716;?#37731;?#37922;?#29831;风エ鎹?/span> 
su winston 
$ kinit winston@360BUY.COM 
鎻愮ず杈撳叆鍒氬?#27692;垱寤虹殑瀵?#37934;?nbsp;


addprinc -randkey hdfs/sl.360buy.com@360BUY.COM 
ktadd -norandkey -k hdfs.keytab hdfs/s1.360buy.com host/master.360buy.com 


]]>
Apache Kafka Security 101http://www.8634070.com/wangxinsh55/archive/2016/06/30/431061.htmlSIMONESIMONEThu, 30 Jun 2016 11:16:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/06/30/431061.htmlhttp://www.8634070.com/wangxinsh55/comments/431061.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/06/30/431061.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431061.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431061.html闃?#29831;?#37711;ㄦ枃

]]>
MySQL鍒嗗?#25779;垎琛ㄧ殑鍏?#28766;?#25966;涓ID?#25939;?#22426;鍣?#37826;规http://www.8634070.com/wangxinsh55/archive/2016/06/28/431035.htmlSIMONESIMONETue, 28 Jun 2016 10:48:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/06/28/431035.htmlhttp://www.8634070.com/wangxinsh55/comments/431035.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/06/28/431035.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431035.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431035.htmlhttp://lztian.com/blog/5921.html

鍊熺敤MySQL ?#27537; auto_increment 鐗?#37804;?#37721;?#28000;?#27996;х敓?#25966;涓?#27537;鍙?#38344;營D銆?/p>

琛?#28729;?#28052;?#38171;屽叧閿?#37734;?#27996;巃uto_increment锛屽拰UNIQUE KEY?#27537;璁?#32515;?#38171;?/p>

1
2
3
4
5
6
CREATE TABLE `Tickets64` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `stub` char(1) NOT NULL default '',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `stub` (`stub`)
) ENGINE=MyISAM

闇瑕?#28003;跨敤鏃?#38171;屽阀?#25956;replace into璇?#23049;?#37833;?#38014;峰彇鍊?#38171;?#32513;撳悎琛?#28729;?#28052;夌殑UNIQUE KEY锛岀‘淇濅簡涓鏉?#29825;?#35120;?#28751;?#37721;?#28000;?#23114;¤冻ID?#25939;?#22426;鍣ㄧ殑闇?#30512;锛?/p>

1
2
REPLACE INTO Tickets64 (stub) VALUES ('a');
SELECT LAST_INSERT_ID();

浠?#28051;?#37826;瑰紡涓?#38171;岄?#26473;嘙ySQL?#27537;鏈?#37714;?#38171;屽彲浠?#32429;?#28103;?#23005;D?#27537;?#25966;涓鍜岃嚜澧?#38171;?#28051;?#38315;傜敤浜?#28598;?#39582;跺?#25120;殑鍦烘櫙銆?#28729;?#37826;?#28725;?#23005;ょ殑鎻?#26473;?#38171;歨ttps://dev.mysql.com/doc/refman/5.0/en/information-functions.html

1
2
3
It is multi-user safe because multiple clients can issue the UPDATE statement and
get their own sequence value with the SELECT statement (or mysql_insert_id()),
without affecting or being affected by other clients that generate their own sequence values.

闇瑕佹敞鎰忕殑鏄?#38171;岃嫢client閲囩敤PHP锛屽垯涓嶈兘浣跨敤mysql_insert_id()鑾峰彇ID锛屽?#29111;洜瑙併妋ysql_insert_id() 鍦╞igint?#28727;AI瀛楁閬囧埌?#27537;闂?#26864;?#37510;?#38171;歨ttp://kaifage.com/notes/99/mysql-insert-id-issue- with-bigint-ai-field.html銆?/p>

Flickr 閲囧彇浜嗘鏂规锛?http://code.flickr.net/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/

鐩稿叧锛?/p>

http://www.zhihu.com/question/30674667

http://my.oschina.net/u/142836/blog/174465



]]>
java淇敼static final甯?#38322;忓? http://www.8634070.com/wangxinsh55/archive/2016/06/28/431031.htmlSIMONESIMONETue, 28 Jun 2016 09:32:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/06/28/431031.htmlhttp://www.8634070.com/wangxinsh55/comments/431031.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/06/28/431031.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/431031.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/431031.htmlhttp://ljhzzyx.blog.163.com/blog/static/3838031220141011111435161/
java涓?#38171;宖inal鏍?#29831;嗙殑鍙?#38322;?#37828;?#28051;?#37721;?#28103;敼?#27537;锛?#28003;嗘槸閫?#26473;?#37721;?#28751;勭殑鏂瑰紡杈惧埌淇敼?#27537;鐩殑銆?#28103;敼?#27537;绀轰緥涔熷緢?#30045;鍗?#38171;屽湪杩?#38322;?http://stackoverflow.com/questions/2474017/using-reflection-to-change-static-final-file-separatorchar-for-unit-testing
public class EverythingIsTrue {
    static void setFinalStatic(Field field, Object newValue) throws Exception {
        field.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, newValue);
    }
    public static void main(String args[]) throws Exception {
        setFinalStatic(Boolean.class.getField("FALSE"), true);
        System.out.format("Everything is %s", false); // "Everything is true"
    }
}
    ?#21479;閿?#37904;瑰湪浜?span style="line-height: 28px;">.setAccessible(true)锛屽苟涓?#28103;敼modifiers鍘婚櫎final灞?#37804;?#37510;?#38014;峰緱淇?#26967;?#32471;︾殑鏂瑰紡鍙?#28000;?#38315;?#26473;?/span>java.lang.reflect.Modifier锛岃缁?#29831;存槑鍦?#26473;?#38322;?/div>
http://blog.csdn.net/xiao__gui/article/details/8141216
閫?#26473;嘙odifier?#27537;isPublic銆乮sPrivate銆乮sStatic绛?#37826;规硶锛屽彲浠?#37714;?#37826;?#37828;?#37722;﹀寘?#24776;鏌?#27996;?#28103;?#26967;?#32471;?/div>
boolean isStatic = Modifier.isStatic(field.getModifiers());
if(isStatic) {
    System.out.println(field.get(null).toString());
}
杩?#38322;岀殑field鏄?#38344;?#37804;佺?#35826;?#23340;殑锛屽洜姝?/span>field.get(null)鏂规硶?#27537;鍙傛暟锛屽彲浠?#37828;痭ull锛?#28052;熷彲浠?#37828;疉.class杩?#37837;风殑鐩?#37837;囩被锛?#28051;嶇敤鎻?#28186;?#28729;?#28186;?#28725;?#29854;?#37510;傛煡鐪?/span>java.lang.reflect.Modifier?#27537;?#21804;鐮?#38171;屽彲浠ョ煡閬撳淇?#26967;?#32471;︾殑瀹?#28052;?#37828;?#38315;?#26473;囦簩杩?#37714;?#28003;嶆潵瀹炵幇?#27537;銆?#28051;?#38344;㈡枃绔?#28051;?#37832;?#28051;?#28186;?/span>

public static锛屽?#29808;簲?#27537;鏁存暟灏?#37828;?#27996;?#26473;?#37714;剁殑锛?001锛?#28052;?#28751;?#37828;?銆?#28641;?#28051;?#38171;?/p>

……

native

transient

volatile

synchronized

final

static

protected

private

public

 

0

0

0

0

0

1

0

0

1


婧?#37934;?#28051;殑瀹?#37825;?#28729;?#28052;夊涓?/span>
public static final int PUBLIC           = 0x00000001;
public static final int PRIVATE          = 0x00000002;
public static final int PROTECTED        = 0x00000004;
public static final int STATIC           = 0x00000008;
public static final int FINAL            = 0x00000010;
public static final int SYNCHRONIZED     = 0x00000020;
public static final int VOLATILE         = 0x00000040;
public static final int TRANSIENT        = 0x00000080;
public static final int NATIVE           = 0x00000100;
public static final int INTERFACE        = 0x00000200;
public static final int ABSTRACT         = 0x00000400;
public static final int STRICT           = 0x00000800;
鏍?#37817;暟鍊?#38171;屽緱?#22476;瀹?#37825;寸殑椤?#25652;?#37828;?#26473;?#37837;风殑
strict,abstract,interface,native,transient,volatile,synchronized,final,static,protected,private,public
      鐢?#23005;?#28751;?#37721;?#28000;ヤ簡瑙?span style="line-height: 28px;">field.getModifiers() & ~Modifier.FINAL杩欓儴鍒嗙殑?#24776;涔夌殑锛屽厛~Modifier.FINAL灏唂inal鎵鍦ㄧ殑浣嶈缃?#28051;?锛屽叾浠?#37813;鏈?#28003;嶈缃?#28051;?銆?/span>field.getModifiers() & ~Modifier.FINAL涓庣殑?#25655;浣?#38171;?#28751;?#37828;?#28751;?/span>field?#27537;modifiers灞?#37804;?#28103;?#26967;?#32471;?#28051;璮inal缁?#37720;婚櫎鎺?#37510;?/span>
      浣嗘槸鍦ㄨ嚜宸?#28751;濊瘯?#27537;杩囩▼涓?#38171;屽?#25120;幇涓涓?#38338;?#26864;?#37510;?#29825;?#32515;甪inal鍙?#38322;忕殑鏂规硶鏄?span style="line-height: 28px;">field.set()锛屽?#20635;?#28355;湪杩?#28051;?#37826;规硶涔嬪?#23944;皟?#25956;?#31777;field.get()鏂规硶锛岄『搴忓涓?#38344;?#26473;?#37837;?/span>
field.get(null).toString();
...
field.set(null, newValue);
杩?#37827;?span style="line-height: 28px;">瀵筬inal鍙?#38322;忕殑璧嬪?#28751;变細鎶?#38335;?#38171;?#28751;辩畻.setAccessible(true);涔熸槸?#30149;鏈夌敤?#27537;銆傚叿浣撳?#29111;洜灏?#28051;嶆?#21614;锛?#28028;?#29825;?#38343;瑕佽?#29115;釜婧?#37934;佹?#23944;兘鏌?#23059;呮銆?/span>

      闇瑕佹敞鎰忕殑鏄?#38171;屽浜巌nt銆乴ong銆乥oolean浠?#37721;奡tring绛夊熀鏈?#32491;诲瀷锛岀敱浜?#32514;?#29831;?#37731;?#28028;?#37718;栫殑鍘熷洜锛屽緢澶?#28003;跨敤甯?#38322;忕殑鍦?#37826;圭殑鍊?#26473;?#37828;?#37720;熸潵?#27537;?#26271;鍊?#37510;?#28641;?/div>
if (index > maxFormatRecordsIndex) {
    index  =  maxFormatRecordsIndex;
}
maxFormatRecordsIndex涓篺inal锛屽垯琚?#32514;?#29831;?#37731;ㄦ敼?#22426;杩?#37837;?/span>
if (index > 100) {
    index = 100;
}
System.out.println(Bean.INT_VALUE);
//缂?#29831;?#37827;朵細琚?#28028;?#37718;栨垚涓?#38344;?#26473;?#37837;?#38171;?/div>
System.out.println(100);
鎵浠?#23005;?#29999;哥殑浣跨敤鏂瑰紡杩?#37828;?#38014;峰彇鍘熸潵?#27537;鍊?#38171;岃?#23792;緱淇敼鍚庣殑final甯?#38322;忕殑鍊?#38343;瑕佺敤field.get(null)杩?#37837;风殑鏂瑰紡銆?/div>
      鎬?#28003;?#37833;?#29825;?#38171;屾敼鍩?#37832;?#32491;诲?#23340;殑final甯?#38322;忕殑?#25956;澶?#26473;?#37828;?#28051;?#28598;?#38171;屽?#20635;灉鏄?#38344;炲熀鏈?#32491;诲瀷甯?#38322;?#38171;屽垯鏈夊疄闄呮?#24526;箟銆?/div>


]]>MYSQL涔?#29723;?#37714;嗗尯----鎸?#37827;?#37832;熷?#21975;尯 http://www.8634070.com/wangxinsh55/archive/2016/06/07/430822.htmlSIMONESIMONETue, 07 Jun 2016 10:06:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/06/07/430822.htmlhttp://www.8634070.com/wangxinsh55/comments/430822.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/06/07/430822.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/430822.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/430822.htmlhttp://blog.sina.com.cn/s/blog_888269b20100w7kf.html

mysql 5.1宸?#32513;忓埌?#31777;beta鐗?#38171;屽畼鏂?#32515;?#32468;?#28051;?#28052;熼檰缁?#37832;?#28051;浜涙枃绔?#28000;?#32513;?#38171;?#23011;?#28641;?#28051;婃鐪嬪埌?#27537;Improving Database Performance with Partitioning銆?#37734;?#28003;跨敤鍒嗗尯?#27537;鍓?#37819;?#28051;?#38171;屽彲浠ョ敤mysql瀹炵幇闈炲?#31295;ぇ?#27537;?#26271;鎹?#38322;?#28699;樺偍銆?#28000;?#28598;?#37734;╩ysql?#27537;绔?#28051;?#37721;?#37930;嬪埌涓绡?#26473;?#38339;剁殑?#26499;绔?—— 鎸?#37827;?#37832;熷?#21975;尯瀛樺偍銆?#28641;傛灉?#20824;澶熷疄鐜版寜鏃?#37832;熷?#21975;尯锛?#26473;?#28725;规煇浜?#37827;舵晥鎬?#23536;?#23534;虹殑?#26271;鎹?#28699;樺偍鏄?#37929;稿?#25779;疄?#25956;?#27537;鍔熻兘銆?#28051;?#38344;?#37828;?#28000;?#26473;?#32481;囨枃绔?#28051;?#37821;?#35120;曠殑涓浜?#37712;呭銆?/p>

閿?#29831;殑鎸?#37827;?#37832;熷?#21975;?#36720;緥瀛?/h3>

鏈鐩?#29785;傜殑鏂规硶锛?#28751;?#37828;?#37929;存帴?#25956;骞存湀鏃?#26473;?#32457;?#37827;?#37832;?#37837;煎紡鏉?#26473;?#29723;屽父瑙勭殑鍒嗗尯锛?/p>

CODE:
  1. mysql>  create table rms (d date)
  2.     ->  partition by range (d)
  3.     -> (partition p0 values less than ('1995-01-01'),
  4.     ->  partition p1 VALUES LESS THAN ('2010-01-01'));

 

涓?#38344;㈢殑渚?#28699;?#28051;?#38171;?#28751;?#37828;?#37929;存帴?#25956;"Y-m-d"?#27537;鏍煎紡鏉?#28725;?#28051;涓猼able杩?#29723;屽?#21975;尯锛屽彲鎯?#37807;?#35120;撶?#36346;線寰涓嶈?#34249;鏁?#38171;?#28028;氬緱?#22476;涓涓?#38335;?#29831;?#28103;?#37805;?

ERROR 1064 (42000): VALUES value must be of same type as partition function near '),
partition p1 VALUES LESS THAN ('2010-01-01'))' at line 3

涓?#26473;?#37714;嗗尯鏂瑰紡?#30149;鏈夋垚鍔?#38171;岃?#28051;?#37828;?#37828;剧殑涓?#32513;忔祹锛岃佺?#20906;殑DBA?#32048;?#25956;鏁?#37736;嬫暟鍊?#37833;?#26473;?#29723;屽?#21975;尯锛?/p>

CODE:
  1. mysql> CREATE TABLE part_date1
  2.     ->      (  c1 int default NULL,
  3.     ->  c2 varchar(30) default NULL,
  4.     ->  c3 date default NULL) engine=myisam
  5.     ->      partition by range (cast(date_format(c3,'%Y%m%d') as signed))
  6.     -> (PARTITION p0 VALUES LESS THAN (19950101),
  7.     -> PARTITION p1 VALUES LESS THAN (19960101) ,
  8.     -> PARTITION p2 VALUES LESS THAN (19970101) ,
  9.     -> PARTITION p3 VALUES LESS THAN (19980101) ,
  10.     -> PARTITION p4 VALUES LESS THAN (19990101) ,
  11.     -> PARTITION p5 VALUES LESS THAN (20000101) ,
  12.     -> PARTITION p6 VALUES LESS THAN (20010101) ,
  13.     -> PARTITION p7 VALUES LESS THAN (20020101) ,
  14.     -> PARTITION p8 VALUES LESS THAN (20030101) ,
  15.     -> PARTITION p9 VALUES LESS THAN (20040101) ,
  16.     -> PARTITION p10 VALUES LESS THAN (20100101),
  17.     -> PARTITION p11 VALUES LESS THAN MAXVALUE );
  18. Query OK, 0 rows affected (0.01 sec)

 

鎼?#28729;?#38171;熸帴?#28483;寰涓?#37714;嗘瀽

CODE:
  1. mysql> explain partitions
  2.     -> select count(*) from part_date1 where
  3.     ->      c3> date '1995-01-01' and c3 <date '1995-12-31'\G
  4. *************************** 1. row ***************************
  5.            id: 1
  6.   select_type: SIMPLE
  7.         table: part_date1
  8.    partitions: p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11
  9.          type: ALL
  10. possible_keys: NULL
  11.           key: NULL
  12.       key_len: NULL
  13.           ref: NULL
  14.          rows: 8100000
  15.         Extra: Using where
  16. 1 row in set (0.00 sec)

 

涓?#37805;剁殑mysql灞呯?#36346;涓?#38344;㈢殑sql浣跨敤鍏?#29723;?#37813;?#37819;?#38171;岃?#28051;嶆槸鎸夌収鎴?#28000;殑鏃?#37832;熷?#21975;尯鍒嗗潡鏌?#29831;?#37510;?#37720;熸枃涓?#29785;?#38322;婂埌MYSQL?#27537;浼?#37718;栧櫒骞?#28051;嶈杩?#32457;?#37827;?#37832;熷舰?#32033;?#27537;鍒嗗尯锛岃姳浜嗗ぇ閲忕殑绡?#39582;呮潵寮?#29831;?#28103;?#29863;?#28051;?#23005;?#29882;?#38171;?#26473;?#37714;?#37510;?/p>

姝?#32429;殑鏃?#37832;熷?#21975;?#36720;緥瀛?/h3>

mysql浼?#37718;栧櫒鏀?#37816;?#28000;?#28051;?#28051;?#32457;?#37712;?#32515;殑鏃?#37832;熷?#33445;暟杩?#29723;屽?#21975;尯锛?/p>

  • TO_DAYS()
  • YEAR()

鐪?#28051;?#28186;?#28699;?#38171;?/p>

CODE:
  1. mysql> CREATE TABLE part_date3
  2.     ->      (  c1 int default NULL,
  3.     ->  c2 varchar(30) default NULL,
  4.     ->  c3 date default NULL) engine=myisam
  5.     ->      partition by range (to_days(c3))
  6.     -> (PARTITION p0 VALUES LESS THAN (to_days('1995-01-01')),
  7.     -> PARTITION p1 VALUES LESS THAN (to_days('1996-01-01')) ,
  8.     -> PARTITION p2 VALUES LESS THAN (to_days('1997-01-01')) ,
  9.     -> PARTITION p3 VALUES LESS THAN (to_days('1998-01-01')) ,
  10.     -> PARTITION p4 VALUES LESS THAN (to_days('1999-01-01')) ,
  11.     -> PARTITION p5 VALUES LESS THAN (to_days('2000-01-01')) ,
  12.     -> PARTITION p6 VALUES LESS THAN (to_days('2001-01-01')) ,
  13.     -> PARTITION p7 VALUES LESS THAN (to_days('2002-01-01')) ,
  14.     -> PARTITION p8 VALUES LESS THAN (to_days('2003-01-01')) ,
  15.     -> PARTITION p9 VALUES LESS THAN (to_days('2004-01-01')) ,
  16.     -> PARTITION p10 VALUES LESS THAN (to_days('2010-01-01')),
  17.     -> PARTITION p11 VALUES LESS THAN MAXVALUE );
  18. Query OK, 0 rows affected (0.00 sec)

 

浠o_days()鍑芥暟鍒嗗尯?#22426;鍔?#38171;?#37812;?#28000;?#37714;嗘瀽涓涓?#37930;?#37930;?#38171;?/p>

CODE:
  1. mysql> explain partitions
  2.     -> select count(*) from part_date3 where
  3.     ->      c3> date '1995-01-01' and c3 <date '1995-12-31'\G
  4. *************************** 1. row ***************************
  5.            id: 1
  6.   select_type: SIMPLE
  7.         table: part_date3
  8.    partitions: p1
  9.          type: ALL
  10. possible_keys: NULL
  11.           key: NULL
  12.       key_len: NULL
  13.           ref: NULL
  14.          rows: 808431
  15.         Extra: Using where
  16. 1 row in set (0.00 sec)

 

鍙?#28000;?#37930;嬪埌锛?a href="http://www.ooso.net/index.php/archives/category/mysql/">mysql浼?#37718;栧櫒杩欐涓嶈礋浼?#37832;?#38171;?#28000;?#28000;?#37734;╬1鍒嗗尯杩?#29723;?#37836;?#29831;?#37510;?#37734;?#26473;?#32457;嶆儏鍐?#28051;?#37836;?#29831;?#38171;岀湡?#27537;?#20824;澶熷甫鏉?#37819;愬崌鏌?#29831;?#37825;?#37916;囦箞锛?#28051;?#38344;?#37714;嗗埆瀵?#26473;欐寤虹?#23340;殑part_date3?#25328;涔嬪墠鍒嗗尯澶辫触?#27537;part_date1?#20187;涓涓?#37836;?#29831;?#28725;规瘮锛?/p>

CODE:
  1. mysql> select count(*) from part_date3 where
  2.     ->      c3> date '1995-01-01' and c3 <date '1995-12-31';
  3. +----------+
  4. | count(*) |
  5. +----------+
  6.  805114 |
  7. +----------+
  8. 1 row in set (4.11 sec)
  9.  
  10. mysql> select count(*) from part_date1 where
  11.     ->      c3> date '1995-01-01' and c3 <date '1995-12-31';
  12. +----------+
  13. | count(*) |
  14. +----------+
  15.  805114 |
  16. +----------+
  17. 1 row in set (40.33 sec)

 

鍙?#28000;?#37930;嬪埌锛屽?#21975;尯姝?#32429;殑璇漲uery鑺辫垂鏃?#38338;?#28051;?绉?#38171;岃屽?#21975;尯閿?#29831;?#37714;?#38010;辫垂鏃?#38338;?0绉掞紙鐩稿綋浜庢病鏈?#37714;嗗尯锛?#38171;?#37825;?#37916;?#37832;?0锛呯殑鎻愬崌锛佹墍浠?#37812;?#28000;?#37719;?#28051;?#29781;佹纭殑浣跨敤鍒嗗尯鍔熻兘锛屽?#21975;尯鍚?#37716;?#36423;呯敤explain楠岃瘉锛?#26473;?#37837;?#37813;嶈兘鑾峰?#26947;湡姝g殑鎬ц兘鎻愬崌銆?/p>


娉ㄦ剰锛?/p>

鍦╩ysql5.1涓?#23524;虹珛鍒嗗尯琛ㄧ殑璇?#37721;?#28051;?#38171;屽彧鑳藉寘?#24776;涓?#37714;?#37713;芥暟锛?br /> ABS()
CEILING() and FLOOR() ?#32025;鍦?#28003;跨敤杩?涓?#37713;芥暟?#27537;寤虹珛鍒嗗尯琛ㄧ殑鍓?#37819;?#37828;?#28003;跨敤鍑芥暟?#27537;鍒嗗尯閿?#37828;疘NT绫诲瀷锛?#38171;?#28186;?#28641;?/p>

mysql> CREATE TABLE t (c FLOAT) PARTITION BY LIST( FLOOR(c) )(     -> PARTITION p0 VALUES IN (1,3,5),     -> PARTITION p1 VALUES IN (2,4,6)     -> );; ERROR 1491 (HY000): The PARTITION function returns the wrong type   mysql> CREATE TABLE t (c int) PARTITION BY LIST( FLOOR(c) )(     -> PARTITION p0 VALUES IN (1,3,5),     -> PARTITION p1 VALUES IN (2,4,6)     -> ); Query OK, 0 rows affected (0.01 sec) 

DAY()
DAYOFMONTH()
DAYOFWEEK()
DAYOFYEAR()
DATEDIFF()
EXTRACT()
HOUR()
MICROSECOND()
MINUTE()
MOD()
MONTH()
QUARTER()
SECOND()
TIME_TO_SEC()
TO_DAYS()
WEEKDAY()
YEAR()
YEARWEEK()



]]>Max MQTT connectionshttp://www.8634070.com/wangxinsh55/archive/2016/06/01/430732.htmlSIMONESIMONEWed, 01 Jun 2016 08:15:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/06/01/430732.htmlhttp://www.8634070.com/wangxinsh55/comments/430732.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/06/01/430732.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/430732.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/430732.htmlhttp://stackoverflow.com/questions/29358313/max-mqtt-connections?answertab=votes#tab-top


I have a need to create a server farm that can handle 5+ million connections, 5+ million topics (one per client), process 300k messages/sec.

I tried to see what various message brokers were capable so I am currently using two RHEL EC2 instances (r3.4xlarge) to make lots of available resources. So you do not need to look it up, it has 16vCPU, 122GB RAM. I am nowhere near that limit in usage.

I am unable to pass the 600k connections limit. Since there doesn't seem to be any O/S limitation (plenty of RAM/CPU/etc.) on either the client nor the server what is limiting me?

I have edited /etc/security/limits.conf as follows:

* soft  nofile  20000000 * hard  nofile  20000000  * soft  nproc  20000000 * hard  nproc  20000000  root  soft  nofile 20000000 root  hard  nofile 20000000 

I have edited /etc/sysctl.conf as follows:

net.ipv4.ip_local_port_range = 1024 65535   net.ipv4.tcp_tw_reuse = 1  net.ipv4.tcp_mem = 5242880  5242880 5242880  net.ipv4.tcp_tw_recycle = 1  fs.file-max = 20000000  fs.nr_open = 20000000  net.ipv4.tcp_syncookies = 0  net.ipv4.tcp_max_syn_backlog = 10000  net.ipv4.tcp_synack_retries = 3  net.core.somaxconn=65536  net.core.netdev_max_backlog=100000  net.core.optmem_max = 20480000 

For Apollo: export APOLLO_ULIMIT=20000000

For ActiveMQ:

ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS -Dorg.apache.activemq.UseDedicatedTaskRunner=false" ACTIVEMQ_OPTS_MEMORY="-Xms50G -Xmx115G" 

I created 20 additional private addresses for eth0 on the client, then assigned them: ip addr add 11.22.33.44/24 dev eth0

I am FULLY aware of the 65k port limits which is why I did the above.

  • For ActiveMQ I got to: 574309
  • For Apollo I got to: 592891
  • For Rabbit I got to 90k but logging was awful and couldn't figure out what to do to go higher although I know its possible.
  • For Hive I got to trial limit of 1000. Awaiting a license
  • IBM wants to trade the cost of my house to use them - nah!
asked Mar 30 '15 at 23:52
redboy
10311
   
Can't really tell how to increase the throughput. However, checkout kafka.apache.org . Not sure about the MQTT support, but it seems capable of extrem throughput / # clients. – Petter Nordlander Mar 31 '15 at 7:52
   
did you try mosquitto? (mosquitto.org) – Aleksey Izmailov Apr 2 '15 at 8:02
   
Trying Hive, Apollo, Mosquito, Active, Rabbit, mosquito – redboy Apr 2 '15 at 21:58

ANSWER: While doing this I realized that I had a misspelling in my client setting within /etc/sysctl.conf file for: net.ipv4.ip_local_port_range

I am now able to connect 956,591 MQTT clients to my Apollo server in 188sec.


More info: Trying to isolate if this is an O/S connection limitation or a Broker, I decided to write a simple Client/Server.

The server:

    Socket client = null;     server = new ServerSocket(1884);     while (true) {         client = server.accept();         clients.add(client);     } 

The Client:

    while (true) {         InetAddress clientIPToBindTo = getNextClientVIP();         Socket client = new Socket(hostname, 1884, clientIPToBindTo, 0);         clients.add(client);     } 

With 21 IPs, I would expect 65535-1024*21 = 1354731 to be the boundary. In reality I am able to achieve 1231734

[root@ip ec2-user]# cat /proc/net/sockstat sockets: used 1231734 TCP: inuse 5 orphan 0 tw 0 alloc 1231307 mem 2 UDP: inuse 4 mem 1 UDPLITE: inuse 0 RAW: inuse 0 FRAG: inuse 0 memory 0 

So the socket/kernel/io stuff is worked out.

I am STILL unable to achieve this using any broker.

Again just after my client/server test this is the kernel settings.

Client:

[root@ip ec2-user]# sysctl -p net.ipv4.ip_local_port_range = 1024     65535 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 5242880      5242880 15242880 net.ipv4.tcp_tw_recycle = 1 fs.file-max = 20000000 fs.nr_open = 20000000  [root@ip ec2-user]# cat /etc/security/limits.conf * soft  nofile  2000000 * hard  nofile  2000000     root  soft  nofile 2000000 root  hard  nofile 2000000 

Server:

[root@ ec2-user]# sysctl -p net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 5242880      5242880 5242880 net.ipv4.tcp_tw_recycle = 1 fs.file-max = 20000000 fs.nr_open = 20000000 net.ipv4.tcp_syncookies = 0 net.ipv4.tcp_max_syn_backlog = 1000000 net.ipv4.tcp_synack_retries = 3 net.core.somaxconn = 65535 net.core.netdev_max_backlog = 1000000 net.core.optmem_max = 20480000 


]]>HDFS?#21412;缃甂erberos璁?#29831;?http://www.8634070.com/wangxinsh55/archive/2016/05/31/430718.htmlSIMONESIMONETue, 31 May 2016 09:24:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/05/31/430718.htmlhttp://www.8634070.com/wangxinsh55/comments/430718.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/05/31/430718.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/430718.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/430718.html闃?#29831;?#37711;ㄦ枃

]]>
Spark History Server?#21412;缃?#28003;跨敤http://www.8634070.com/wangxinsh55/archive/2016/05/26/430665.htmlSIMONESIMONEThu, 26 May 2016 06:12:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/05/26/430665.htmlhttp://www.8634070.com/wangxinsh55/comments/430665.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/05/26/430665.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/430665.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/430665.htmlhttp://www.cnblogs.com/luogankun/p/3981645.html

Spark history Server浜х敓鑳屾櫙

浠tandalone杩?#29723;?#22959;″紡涓轰緥锛屽湪杩?#29723;孲park Application?#27537;鏃跺?#38171;孲park?#32048;鎻?#28186;?#28051;涓猈EBUI鍒?#37713;?#25652;旂敤绋?#25652;忕殑杩?#29723;?#37827;?#28103;?#37805;?#38171;?#28003;?#29831;EBUI闅忕潃Application?#27537;瀹屾垚(?#22426;鍔?澶??#35302;)鑰屽叧闂?#38171;?#28052;?#28751;?#37828;?#29831;?#38171;孲park Application杩?#29723;屽畬(?#22426;鍔?澶辫触)鍚?#38171;?#28751;?#37827;?#23049;?#37836;?#37930;婣pplication?#27537;鍘嗗?#33590;褰?#38171;?/p>

Spark history Server灏?#37828;?#28051;轰?#21975;簲瀵?#26473;?#32457;嶆儏鍐?#38000;?#27996;х敓?#27537;锛岄?#26473;囬厤缃?#37721;?#28000;?#37734;ˋpplication?#22717;琛岀殑杩囩▼涓?#29825;?#35120;?#28051;嬩簡鏃?#36423;?#27996;?#28000;?#28103;?#37805;?#38171;岄偅涔?#37734;ˋpplication?#22717;琛?缁?#37833;熷悗锛學EBUI灏辫兘閲?#37826;版覆鏌撶敓?#22426;UI鐣岄潰灞曠幇鍑?#29831;pplication鍦ㄦ墽琛?#26473;囩▼涓殑杩?#29723;?#37827;?#28103;?#37805;?#38171;?/p>

Spark杩?#29723;屽湪yarn鎴?#38000;卪esos涔?#28051;?#38171;岄?#26473;噑park?#27537;history server浠嶇?#36346;彲浠?#38322;嶆?#21227;嚭涓涓?#23480;?#32513;?#28729;屾垚?#27537;Application?#27537;杩?#29723;?#37827;跺?#20635;暟淇?#37805;紙鍋囧?#20395;pplication杩?#29723;岀殑浜?#28000;?#37827;?#36423;?#28103;?#37805;?#23480;?#32513;?#29825;?#35120;?#28051;?#37833;?#38171;?#38171;?/p>

 

?#21412;缃?amp;浣跨敤Spark History Server

浠ラ粯璁ら厤缃殑鏂瑰紡鍚?#37716;╯park history server锛?/p>

cd $SPARK_HOME/sbin start-history-server.sh

鎶?#38335;?#38171;?/p>

starting org.apache.spark.deploy.history.HistoryServer, logging to /home/spark/software/source/compile/deploy_spark/sbin/../logs/spark-spark-org.apache.spark.deploy.history.HistoryServer-1-hadoop000.out failed to launch org.apache.spark.deploy.history.HistoryServer:         at org.apache.spark.deploy.history.FsHistoryProvider.<init>(FsHistoryProvider.scala:44)         ... 6 more

闇瑕?#37734;?#37722;?#37716;?#37827;舵寚瀹?#37929;?#35120;?#38171;?/p>

start-history-server.sh hdfs://hadoop000:8020/directory

hdfs://hadoop000:8020/directory鍙?#28000;ラ厤缃?#37734;ㄩ厤缃枃浠?#28051;?#38171;岄偅涔?#37734;?#37722;?#37716;╤istory-server鏃?#28751;?#28051;?#38343;瑕佹寚瀹?#38171;屽悗缁?#28000;?#32513;?#37804;?#28052;堥厤缃?#38171;?/p>

娉?#38171;?#29831;?#37929;?#35120;?#38343;瑕佷簨鍏?#37734;╤dfs涓?#37714;?#23524;?#28610;?#38171;屽惁鍒檋istory-server鍚?#37716;?#37814;?#38335;?#37510;?/strong>

鍚?#37716;?#28729;屾垚涔?#37722;?#37721;?#28000;?#38315;?#26473;嘩EBUI璁?#38338;?#38171;岄粯璁?#32468;?#37721;?#37828;?8080锛歨ttp://hadoop000:18080

?#31919;璁?#37923;岄潰鍒?#29723;?#28103;?#37805;?#37828;?#32460;虹殑锛?#28051;?#38344;?#37812;?#37733;?#37828;?#37812;戣?#25116;?#21975;?#29363;spark-sql娴嬭瘯鍚?#37713;虹幇?#27537;銆?/p>

 

history server鐩稿叧?#27537;?#21412;缃?#37721;傛暟鎻?#26473;?/strong>

1锛?spark.history.updateInterval
銆銆?#31919;璁?#37706;?#38171;?0
銆銆浠?#32457;?#28051;?#37719;?#28003;?#38171;屾?#23384;柊鏃?#36423;楃?#31295;叧淇?#37805;殑鏃?#38338;?#38338;?#38341;?/p>

2锛塻park.history.retainedApplications
銆銆?#31919;璁?#37706;?#38171;?0
銆銆鍦?#37712;?#28699;?#28051;?#28103;?#28699;楢pplication鍘嗗?#33590;褰曠殑涓暟锛屽?#20635;灉瓒?#26473;?#26473;?#28051;?#37706;?#38171;?#37827;х殑搴旂敤绋?#25652;?#28103;?#37805;?#28751;嗚鍒犻櫎锛屽?#25779;?#23942;璁?#38338;?#23480;茶鍒犻櫎?#27537;搴旂敤淇?#37805;?#37827;?#38343;瑕?#38322;?#37826;版?#21227;缓椤?#38344;?#37510;?/p>

3锛?span style="color: #ff0000;">spark.history.ui.port
銆銆?#31919;璁?#37706;?#38171;?8080
銆銆HistoryServer?#27537;web绔?#37721;?/p>

4锛塻park.history.kerberos.enabled
銆銆?#31919;璁?#37706;?#38171;歠alse
銆銆鏄?#37722;?#28003;跨敤kerberos鏂瑰紡?#27365;褰?#29825;?#38338;瓾istoryServer锛屽浜?#37816;佷?#21613;眰浣?#27996;?#28729;?#37711;?#38342;嗙兢?#27537;HDFS涓?#37828;?#37832;夌敤?#27537;锛屽?#20635;灉璁?#32515;?#28051;簍rue锛?#28751;?#29781;侀厤缃?#28051;?#38344;㈢殑涓?#28051;?#28766;?#37804;?/p>

5锛塻park.history.kerberos.principal
銆銆?#31919;璁?#37706;?#38171;氱敤浜嶩istoryServer?#27537;kerberos涓?#28003;撳?#23943;О

6锛塻park.history.kerberos.keytab
銆銆?#25956;浜嶩istoryServer?#27537;kerberos keytab?#26499;浠?#28003;?#32515;?/p>

7锛塻park.history.ui.acls.enable
銆銆?#31919;璁?#37706;?#38171;歠alse
銆銆鎺堟?#20906;敤鎴?#37836;?#37930;?#25652;旂敤绋?#25652;?#28103;?#37805;殑鏃跺?#37828;?#37722;?#22955;鏌cl銆?#28641;傛?#28355;惎?#25956;锛屽彧鏈夊簲?#25956;绋?#25652;?#37813;鏈?#38000;呭拰spark.ui.view.acls?#23514;瀹氱殑?#25956;鎴?#37721;?#28000;?#37836;?#37930;?#25652;旂敤绋?#25652;?#28103;?#37805;?鍚?#37714;?#38171;?#28051;嶅仛浠?#28003;曟鏌?/p>

8锛?span style="color: #ff0000;">spark.eventLog.enabled
銆銆?#31919;璁?#37706;?#38171;歠alse
銆銆鏄?#37722;?#29825;?#35120;昐park浜?#28000;?#38171;岀敤浜?#25652;旂敤绋?#25652;?#37734;?#28729;屾垚鍚?#38322;嶆瀯webUI

9锛?span style="color: #ff0000;">spark.eventLog.dir
銆銆?#31919;璁?#37706;?#38171;歠ile:///tmp/spark-events
銆銆淇?#28699;?#37827;?#36423;楃?#31295;叧淇?#37805;殑璺?#23536;?#38171;屽彲浠?#37828;痟dfs://寮澶寸殑HDFS璺?#23536;?#38171;?#28052;熷彲浠?#37828;痜ile://寮澶寸殑鏈?#37734;拌矾寰?#38171;岄兘闇瑕佹?#24876;墠鍒?#23524;?/p>

10锛?span style="color: #ff0000;">spark.eventLog.compress
銆銆?#31919;璁?#37706;?#38171;歠alse
銆銆鏄?#37722;?#37720;嬬缉璁?#35120;昐park浜?#28000;?#38171;屽墠鎻恠park.eventLog.enabled涓簍rue锛岄粯璁?#28003;跨敤?#27537;鏄痵nappy

浠park.history寮澶寸殑闇瑕侀厤缃?#37734;╯park-env.sh涓殑SPARK_HISTORY_OPTS锛?#28000;park.eventLog寮澶寸殑?#21412;缃?#37734;╯park-defaults.conf

 

鎴?#37734;?#23092;嬭瘯杩囩▼涓殑?#21412;缃?#28641;?#28051;?#38171;?/p>

spark-defaults.conf

spark.eventLog.enabled  true spark.eventLog.dir      hdfs://hadoop000:8020/directory spark.eventLog.compress true

spark-env.sh

export SPARK_HISTORY_OPTS="-Dspark.history.ui.port=7777 -Dspark.history.retainedApplications=3 -Dspark.history.fs.logDirectory=hdfs://had oop000:8020/directory"

鍙傛暟鎻?#26473;?#38171;?/p>

spark.history.ui.port=7777  ?#30367;鏁碬EBUI璁?#38338;殑绔?#37721;?#37721;?#28051;?777

spark.history.fs.logDirectory=hdfs://hadoop000:8020/directory  ?#21412;缃簡璇?#28766;?#37804;?#37722;?#38171;屽湪start-history-server.sh鏃?#28751;?#37827;?#38343;鍐嶆?#21095;?#34425;殑?#23514;瀹?#29882;?#23536;?/p>

spark.history.retainedApplications=3   ?#23514;瀹?#28103;?#28699;楢pplication鍘嗗?#33590;褰曠殑涓暟锛屽?#20635;灉瓒?#26473;?#26473;?#28051;?#37706;?#38171;?#37827;х殑搴旂敤绋?#25652;?#28103;?#37805;?#28751;嗚鍒犻櫎

 

?#30367;鏁?#37721;傛暟鍚?#37722;?#37716;╯tart-history-server.sh

start-history-server.sh 

璁?#38338;甒EBUI锛?http://hadoop000:7777

 

鍦?#28003;跨敤spark history server?#27537;杩囩▼涓?#27996;х敓?#27537;鍑?#28051;枒闂?#38171;?/p>

?#26514;闂?锛歴park.history.fs.logDirectory?#25328;spark.eventLog.dir?#23514;瀹?#37929;?#35120;?#37832;夊暐鍖?#37714;?#38171;?/strong>

缁忔?#23341;瘯鍚?#37721;戠幇锛?/p>

spark.eventLog.dir锛欰pplication鍦?#26473;?#29723;?#26473;囩▼涓?#37813;鏈夌殑淇?#37805;潎璁?#35120;?#37734;?#29831;?#28766;?#37804;ф寚瀹氱殑璺?#23536;?#28051;?#38171;?/p>

spark.history.fs.logDirectory锛歋park History Server椤?#38344;?#37721;?#28766;曠ず璇ユ寚瀹?#29882;?#23536;?#28051;嬬殑淇?#37805;?#38171;?/p>

姣?#28641;?#38171;歴park.eventLog.dir鍒氬紑濮?#37827;舵寚瀹氱殑鏄痟dfs://hadoop000:8020/directory锛岃屽悗淇敼?#22426;hdfs://hadoop000:8020/directory2

閭?#28052;坰park.history.fs.logDirectory濡傛?#28356;寚瀹氱殑鏄痟dfs://hadoop000:8020/directory锛?#28751;?#37721;兘鏄剧ず鍑?#29831;?#37929;?#35120;?#28051;嬬殑鎵鏈堿pplication杩?#29723;岀殑鏃?#36423;?#28103;?#37805;?#38171;?#37721;?#28052;?#27996;?#37906;?#37510;?/p>

 

?#26514;闂?锛歴park.history.retainedApplications=3 璨?#28028;兼病?#25939;鏁?#38171;?#38171;?#38171;?#38171;?#38171;?#38171;?/strong>

The History Server will list all applications. It will just retain a max number of them in memory. That option does not control how many applications are show, it controls how much memory the HS will need.

娉ㄦ剰锛?#29831;?#37721;傛暟骞?#28051;嶆槸涔熼〉闈?#28051;?#37828;剧?#34425;殑application?#27537;璁?#35120;曟暟锛岃?#37828;?#28699;樻斁鍦?#37712;?#28699;?#28051;殑涓暟锛屽唴瀛?#28051;殑淇?#37805;?#37734;?#29825;?#38338;?#26916;?#38344;?#37827;?#37929;存帴璇诲彇?#35206;鏌?#37827;?#37721;?#38171;?/p>

姣?#28641;?#29831;?#29831;?#37721;傛暟?#21412;缃簡10涓?#38171;岄偅涔?#37712;?#28699;?#28051;?#28751;?#37832;澶?#37721;兘瀛樻斁10涓猘pplicaiton?#27537;鏃?#36423;?#28103;?#37805;?#38171;屽綋绗?1涓?#37716;?#37711;?#37827;?#38171;?#32471;?#28051;涓?#28751;变細琚?#38890;㈤櫎锛屽?#25779;?#23942;璁?#38338;?#32471;?涓猘pplication?#27537;椤?#38344;?#28103;?#37805;?#37827;?#28751;?span style="font-size: 14px; line-height: 1.5;">闇瑕?#38322;?#37826;?#29831;诲彇?#23514;瀹?#29882;?#23536;?#28051;婄殑鏃?#36423;?#28103;?#37805;?#37833;ユ覆鏌撳?#26336;ず椤?#38344;?#37510;?nbsp;

璇?#29785;?#28729;?#37826;规枃妗?#38171;歨ttp://spark.apache.org/docs/latest/monitoring.html



]]>Spark On Yarn涓璼park.yarn.jar灞?#37804;х殑浣跨敤http://www.8634070.com/wangxinsh55/archive/2016/05/26/430664.htmlSIMONESIMONEThu, 26 May 2016 06:11:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/05/26/430664.htmlhttp://www.8634070.com/wangxinsh55/comments/430664.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/05/26/430664.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/430664.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/430664.htmlhttp://www.cnblogs.com/luogankun/p/4191796.html

浠?#28598;?#37734;?#23092;嬭瘯spark-sql杩?#29723;屽湪yarn涓婄殑杩囩▼涓?#38171;?#37827;犳剰闂?#28000;?#37827;?#36423;?#28051;?#37721;戠?#39041;簡涓涓?#38338;?#26864;?#38171;?/p>

spark-sql --master yarn
14/12/29 15:23:17 INFO Client: Requesting a new application from cluster with 1 NodeManagers 14/12/29 15:23:17 INFO Client: Verifying our application has not requested more than the maximum memory capability of the cluster (8192 MB per container) 14/12/29 15:23:17 INFO Client: Will allocate AM container, with 896 MB memory including 384 MB overhead 14/12/29 15:23:17 INFO Client: Setting up container launch context for our AM 14/12/29 15:23:17 INFO Client: Preparing resources for our AM container 14/12/29 15:23:17 INFO Client: Uploading resource file:/home/spark/software/source/compile/deploy_spark/assembly/target/scala-2.10/spark-assembly-1.3.0-SNAPSHOT-hadoop2.3.0-cdh5.0.0.jar -> hdfs://hadoop000:8020/user/spark/.sparkStaging/application_1416381870014_0093/spark-assembly-1.3.0-SNAPSHOT-hadoop2.3.0-cdh5.0.0.jar 14/12/29 15:23:18 INFO Client: Setting up the launch environment for our AM container
澶?#37714;朵唬鐮? /></a></div> <pre><span style=14/12/29 15:24:03 INFO Client: Requesting a new application from cluster with 1 NodeManagers 14/12/29 15:24:03 INFO Client: Verifying our application has not requested more than the maximum memory capability of the cluster (8192 MB per container) 14/12/29 15:24:03 INFO Client: Will allocate AM container, with 896 MB memory including 384 MB overhead 14/12/29 15:24:03 INFO Client: Setting up container launch context for our AM 14/12/29 15:24:03 INFO Client: Preparing resources for our AM container 14/12/29 15:24:03 INFO Client: Uploading resource file:/home/spark/software/source/compile/deploy_spark/assembly/target/scala-2.10/spark-assembly-1.3.0-SNAPSHOT-hadoop2.3.0-cdh5.0.0.jar -> hdfs://hadoop000:8020/user/spark/.sparkStaging/application_1416381870014_0094/spark-assembly-1.3.0-SNAPSHOT-hadoop2.3.0-cdh5.0.0.jar 14/12/29 15:24:05 INFO Client: Setting up the launch environment for our AM container
drwx------   - spark supergroup          0 2014-12-29 15:23 hdfs://hadoop000:8020/user/spark/.sparkStaging/application_1416381870014_0093 drwx------   - spark supergroup          0 2014-12-29 15:24 hdfs://hadoop000:8020/user/spark/.sparkStaging/application_1416381870014_0094

姣?#28051;狝pplication閮戒細涓?#28028;?#28051;涓猻park-assembly-x.x.x-SNAPSHOT-hadoopx.x.x-cdhx.x.x.jar?#27537;jar?#23512;锛屽?#21342;搷HDFS?#27537;鎬ц兘浠?#37721;?#37719;犵敤HDFS?#27537;绌?#38338;?#37510;?/p>

 

鍦⊿park?#26499;妗?http://spark.apache.org/docs/latest/running-on-yarn.html)涓?#37721;戠幇spark.yarn.jar灞?#37804;?#38171;?#28751;唖park-assembly-xxxxx.jar瀛樻斁鍦╤dfs://hadoop000:8020/spark_lib/涓?/p>

鍦╯park-defaults.conf娣?#37716;犲睘鎬ч厤缃?#38171;?/p>

spark.yarn.jar hdfs://hadoop000:8020/spark_lib/spark-assembly-1.3.0-SNAPSHOT-hadoop2.3.0-cdh5.0.0.jar

鍐嶆鍚?#37716;╯park-sql --master yarn瑙?#28725;?#37827;?#36423;?#38171;?/p>

14/12/29 15:39:02 INFO Client: Requesting a new application from cluster with 1 NodeManagers 14/12/29 15:39:02 INFO Client: Verifying our application has not requested more than the maximum memory capability of the cluster (8192 MB per container) 14/12/29 15:39:02 INFO Client: Will allocate AM container, with 896 MB memory including 384 MB overhead 14/12/29 15:39:02 INFO Client: Setting up container launch context for our AM 14/12/29 15:39:02 INFO Client: Preparing resources for our AM container 14/12/29 15:39:02 INFO Client: Source and destination file systems are the same. Not copying hdfs://hadoop000:8020/spark_lib/spark-assembly-1.3.0-SNAPSHOT-hadoop2.3.0-cdh5.0.0.jar 14/12/29 15:39:02 INFO Client: Setting up the launch environment for our AM container


]]>Benchmarking Apache Kafka: 2 Million Writes Per Second (On Three Cheap Machines)http://www.8634070.com/wangxinsh55/archive/2016/05/26/430663.htmlSIMONESIMONEThu, 26 May 2016 05:53:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/05/26/430663.htmlhttp://www.8634070.com/wangxinsh55/comments/430663.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/05/26/430663.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/430663.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/430663.htmlhttps://engineering.linkedin.com/kafka/benchmarking-apache-kafka-2-million-writes-second-three-cheap-machines


wrote a blog post about how LinkedIn uses Apache Kafka as a central publish-subscribe log for integrating data between applications, stream processing, and Hadoop data ingestion.

To actually make this work, though, this "universal log" has to be a cheap abstraction. If you want to use a system as a central data hub it has to be fast, predictable, and easy to scale so you can dump all your data onto it. My experience has been that systems that are fragile or expensive inevitably develop a wall of protective process to prevent people from using them; a system that scales easily often ends up as a key architectural building block just because using it is the easiest way to get things built.

I've always liked the benchmarks of Cassandra that show it doing a million writes per second on three hundred machines onEC2 and Google Compute Engine. I'm not sure why, maybe it is a Dr. Evil thing, but doing a million of anything per second is fun.

In any case, one of the nice things about a Kafka log is that, as we'll see, it is cheap. A million writes per second isn't a particularly big thing. This is because a log is a much simpler thing than a database or key-value store. Indeed our production clusters take tens of millions of reads and writes per second all day long and they do so on pretty modest hardware.

But let's do some benchmarking and take a look.

Kafka in 30 seconds

To help understand the benchmark, let me give a quick review of what Kafka is and a few details about how it works. Kafka is a distributed messaging system originally built at LinkedIn and now part of the Apache Software Foundation and used by a variety of companies.

The general setup is quite simple. Producers send records to the cluster which holds on to these records and hands them out to consumers:

The key abstraction in Kafka is the topic. Producers publish their records to a topic, and consumers subscribe to one or more topics. A Kafka topic is just a sharded write-ahead log. Producers append records to these logs and consumers subscribe to changes. Each record is a key/value pair. The key is used for assigning the record to a log partition (unless the publisher specifies the partition directly).

Here is a simple example of a single producer and consumer reading and writing from a two-partition topic.

This picture shows a producer process appending to the logs for the two partitions, and a consumer reading from the same logs. Each record in the log has an associated entry number that we call the offset. This offset is used by the consumer to describe it's position in each of the logs.

These partitions are spread across a cluster of machines, allowing a topic to hold more data than can fit on any one machine.

Note that unlike most messaging systems the log is always persistent. Messages are immediately written to the filesystem when they are received. Messages are not deleted when they are read but retained with some configurable SLA (say a few days or a week). This allows usage in situations where the consumer of data may need to reload data. It also makes it possible to support space-efficient publish-subscribe as there is a single shared log no matter how many consumers; in traditional messaging systems there is usually a queue per consumer, so adding a consumer doubles your data size. This makes Kafka a good fit for things outside the bounds of normal messaging systems such as acting as a pipeline for offline data systems such as Hadoop. These offline systems may load only at intervals as part of a periodic ETL cycle, or may go down for several hours for maintenance, during which time Kafka is able to buffer even TBs of unconsumed data if needed.

Kafka also replicates its logs over multiple servers for fault-tolerance. One important architectural aspect of our replication implementation, in contrast to other messaging systems, is that replication is not an exotic bolt-on that requires complex configuration, only to be used in very specialized cases. Instead replication is assumed to be the default: we treat un-replicated data as a special case where the replication factor happens to be one.

Producers get an acknowledgement back when they publish a message containing the record's offset. The first record published to a partition is given the offset 0, the second record 1, and so on in an ever-increasing sequence. Consumers consume data from a position specified by an offset, and they save their position in a log by committing periodically: saving this offset in case that consumer instance crashes and another instance needs to resume from it's position.

Okay, hopefully that all made sense (if not, you can read a more complete introduction to Kafka here).

This Benchmark

This test is against trunk, as I made some improvements to the performance tests for this benchmark. But nothing too substantial has changed since the last full release, so you should see similar results with 0.8.1. I am also using our newly re-written Java producer, which offers much improved throughput over the previous producer client.

I've followed the basic template of this very nice RabbitMQ benchmark, but I covered scenarios and options that were more relevant to Kafka.

One quick philosophical note on this benchmark. For benchmarks that are going to be publicly reported, I like to follow a style I call "lazy benchmarking". When you work on a system, you generally have the know-how to tune it to perfection for any particular use case. This leads to a kind of benchmarketing where you heavily tune your configuration to your benchmark or worse have a different tuning for each scenario you test. I think the real test of a system is not how it performs when perfectly tuned, but rather how it performs "off the shelf". This is particularly true for systems that run in a multi-tenant setup with dozens or hundreds of use cases where tuning for each use case would be not only impractical but impossible. As a result, I have pretty much stuck with default settings, both for the server and the clients. I will point out areas where I suspect the result could be improved with a little tuning, but I have tried to resist the temptation to do any fiddling myself to improve the results.

I have posted my exact configurations and commands, so it should be possible to replicate results on your own gear if you are interested.

The Setup

For these tests, I had six machines each has the following specs

  • Intel Xeon 2.5 GHz processor with six cores
  • Six 7200 RPM SATA drives
  • 32GB of RAM
  • 1Gb Ethernet

The Kafka cluster is set up on three of the machines. The six drives are directly mounted with no RAID (JBOD style). The remaining three machines I use for Zookeeper and for generating load.

A three machine cluster isn't very big, but since we will only be testing up to a replication factor of three, it is all we need. As should be obvious, we can always add more partitions and spread data onto more machines to scale our cluster horizontally.

This hardware is actually not LinkedIn's normal Kafka hardware. Our Kafka machines are more closely tuned to running Kafka, but are less in the spirit of "off-the-shelf" I was aiming for with these tests. Instead, I borrowed these from one of our Hadoop clusters, which runs on probably the cheapest gear of any of our persistent systems. Hadoop usage patterns are pretty similar to Kafka's, so this is a reasonable thing to do.

Okay, without further ado, the results!

Producer Throughput

These tests will stress the throughput of the producer. No consumers are run during these tests, so all messages are persisted but not read (we'll test cases with both producer and consumer in a bit). Since we have recently rewritten our producer, I am testing this new code.

Single producer thread, no replication

821,557 records/sec
(78.3 MB/sec)

For this first test I create a topic with six partitions and no replication. Then I produce 50 million small (100 byte) records as quickly as possible from a single thread.

The reason for focusing on small records in these tests is that it is the harder case for a messaging system (generally). It is easy to get good throughput in MB/sec if the messages are large, but much harder to get good throughput when the messages are small, as the overhead of processing each message dominates.

Throughout this benchmark, when I am reporting MB/sec, I am reporting just the value size of the record times the request per second, none of the other overhead of the request is included. So the actually network usage is higher than what is reported. For example with a 100 byte message we would also transmit about 22 bytes of overhead per message (for an optional key, size delimiting, a message CRC, the record offset, and attributes flag), as well as some overhead for the request (including the topic, partition, required acknowledgements, etc). This makes it a little harder to see where we hit the limits of the NIC, but this seems a little more reasonable then including our own overhead bytes in throughput numbers. So, in the above result, we are likely saturating the 1 gigabit NIC on the client machine.

One immediate observation is that the raw numbers here are much higher than people expect, especially for a persistent storage system. If you are used to random-access data systems, like a database or key-value store, you will generally expect maximum throughput around 5,000 to 50,000 queries-per-second, as this is close to the speed that a good RPC layer can do remote requests. We exceed this due to two key design principles:

  1. We work hard to ensure we do linear disk I/O. The six cheap disks these servers have gives an aggregate throughput of 822 MB/sec of linear disk I/O. This is actually well beyond what we can make use of with only a 1 gigabit network card. Many messaging systems treat persistence as an expensive add-on that decimates performance and should be used only sparingly, but this is because they are not able to do linear I/O.
  2. At each stage we work on batching together small bits of data into larger network and disk I/O operations. For example, in the new producer we use a "group commit"-like mechanism to ensure that any record sends initiated while another I/O is in progress get grouped together. For more on understanding the importance of batching, check out this presentation by David Patterson on why "Latency Lags Bandwidth".

If you are interested in the details you can read a little more about this in our design documents.

Single producer thread, 3x asynchronous replication

786,980 records/sec
(75.1 MB/sec)

This test is exactly the same as the previous one except that now each partition has three replicas (so the total data written to network or disk is three times higher). Each server is doing both writes from the producer for the partitions for which it is a master, as well as fetching and writing data for the partitions for which it is a follower.

Replication in this test is asynchronous. That is, the server acknowledges the write as soon as it has written it to its local log without waiting for the other replicas to also acknowledge it. This means, if the master were to crash, it would likely lose the last few messages that had been written but not yet replicated. This makes the message acknowledgement latency a little better at the cost of some risk in the case of server failure.

The key take away I would like people to have from this is that replication can be fast. The total cluster write capacity is, of course, 3x less with 3x replication (since each write is done three times), but the throughput is still quite good per client. High performance replication comes in large part from the efficiency of our consumer (the replicas are really nothing more than a specialized consumer) which I will discuss in the consumer section.

Single producer thread, 3x synchronous replication

421,823 records/sec
(40.2 MB/sec)

This test is the same as above except that now the master for a partition waits for acknowledgement from the full set of in-sync replicas before acknowledging back to the producer. In this mode, we guarantee that messages will not be lost as long as one in-sync replica remains.

Synchronous replication in Kafka is not fundamentally very different from asynchronous replication. The leader for a partition always tracks the progress of the follower replicas to monitor their liveness, and we never give out messages to consumers until they are fully acknowledged by replicas. With synchronous replication we just wait to respond to the producer request until the followers have replicated it.

This additional latency does seem to affect our throughput. Since the code path on the server is very similar, we could probably ameliorate this impact by tuning the batching to be a bit more aggressive and allowing the client to buffer more outstanding requests. However, in spirit of avoiding special case tuning, I have avoided this.

Three producers, 3x async replication

2,024,032 records/sec
(193.0 MB/sec)

Our single producer process is clearly not stressing our three node cluster. To add a little more load, I'll now repeat the previous async replication test, but now use three producer load generators running on three different machines (running more processes on the same machine won't help as we are saturating the NIC). Then we can look at the aggregate throughput across these three producers to get a better feel for the cluster's aggregate capacity.

Producer Throughput Versus Stored Data

One of the hidden dangers of many messaging systems is that they work well only as long as the data they retain fits in memory. Their throughput falls by an order of magnitude (or more) when data backs up and isn't consumed (and hence needs to be stored on disk). This means things may be running fine as long as your consumers keep up and the queue is empty, but as soon as they lag, the whole messaging layer backs up with unconsumed data. The backup causes data to go to disk which in turns causes performance to drop to a rate that means messaging system can no longer keep up with incoming data and either backs up or falls over. This is pretty terrible, as in many cases the whole purpose of the queue was to handle such a case gracefully.

Since Kafka always persists messages the performance is O(1) with respect to unconsumed data volume.

To test this experimentally, let's run our throughput test over an extended period of time and graph the results as the stored dataset grows:

This graph actually does show some variance in performance, but no impact due to data size: we perform just as well after writing a TB of data, as we do for the first few hundred MBs.

The variance seems to be due to Linux's I/O management facilities that batch data and then flush it periodically. This is something we have tuned for a little better on our production Kafka setup. Some notes on tuning I/O are available here.

Consumer Throughput

Okay now let's turn our attention to consumer throughput.

Note that the replication factor will not effect the outcome of this test as the consumer only reads from one replica regardless of the replication factor. Likewise, the acknowledgement level of the producer also doesn't matter as the consumer only ever reads fully acknowledged messages, (even if the producer doesn't wait for full acknowledgement). This is to ensure that any message the consumer sees will always be present after a leadership handoff (if the current leader fails).

Single Consumer

940,521 records/sec
(89.7 MB/sec)

For the first test, we will consume 50 million messages in a single thread from our 6 partition 3x replicated topic.

Kafka's consumer is very efficient. It works by fetching chunks of log directly from the filesystem. It uses the sendfile API to transfer this directly through the operating system without the overhead of copying this data through the application. This test actually starts at the beginning of the log, so it is doing real read I/O. In a production setting, though, the consumer reads almost exclusively out of the OS pagecache, since it is reading data that was just written by some producer (so it is still cached). In fact, if you run I/O stat on a production server you actually see that there are no physical reads at all even though a great deal of data is being consumed.

Making consumers cheap is important for what we want Kafka to do. For one thing, the replicas are themselves consumers, so making the consumer cheap makes replication cheap. In addition, this makes handling out data an inexpensive operation, and hence not something we need to tightly control for scalability reasons.

Three Consumers

2,615,968 records/sec
(249.5 MB/sec)

Let's repeat the same test, but run three parallel consumer processes, each on a different machine, and all consuming the same topic.

As expected, we see near linear scaling (not surprising because consumption in our model is so simple).

Producer and Consumer

795,064 records/sec
(75.8 MB/sec)

The above tests covered just the producer and the consumer running in isolation. Now let's do the natural thing and run them together. Actually, we have technically already been doing this, since our replication works by having the servers themselves act as consumers.

All the same, let's run the test. For this test we'll run one producer and one consumer on a six partition 3x replicated topic that begins empty. The producer is again using async replication. The throughput reported is the consumer throughput (which is, obviously, an upper bound on the producer throughput).

As we would expect, the results we get are basically the same as we saw in the producer only case—the consumer is fairly cheap.

Effect of Message Size

I have mostly shown performance on small 100 byte messages. Smaller messages are the harder problem for a messaging system as they magnify the overhead of the bookkeeping the system does. We can show this by just graphing throughput in both records/second and MB/second as we vary the record size.

So, as we would expect, this graph shows that the raw count of records we can send per second decreases as the records get bigger. But if we look at MB/second, we see that the total byte throughput of real user data increases as messages get bigger:

We can see that with the 10 byte messages we are actually CPU bound by just acquiring the lock and enqueuing the message for sending—we are not able to actually max out the network. However, starting with 100 bytes, we are actually seeing network saturation (though the MB/sec continues to increase as our fixed-size bookkeeping bytes become an increasingly small percentage of the total bytes sent).

End-to-end Latency

2 ms (median)
3 ms (99th percentile)
14 ms (99.9th percentile)

We have talked a lot about throughput, but what is the latency of message delivery? That is, how long does it take a message we send to be delivered to the consumer? For this test, we will create producer and consumer and repeatedly time how long it takes for a producer to send a message to the kafka cluster and then be received by our consumer.

Note that, Kafka only gives out messages to consumers when they are acknowledged by the full in-sync set of replicas. So this test will give the same results regardless of whether we use sync or async replication, as that setting only affects the acknowledgement to the producer.

Replicating this test

If you want to try out these benchmarks on your own machines, you can. As I said, I mostly just used our pre-packaged performance testing tools that ship with Kafka and mostly stuck with the default configs both for the server and for the clients. However, you can see more details of the configuration and commands here.




]]>Kafka 楂?#37804;ц兘鍚炲悙鎻?#32457;?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/05/26/430662.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Thu, 26 May 2016 05:52:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/05/26/430662.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/430662.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/05/26/430662.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/430662.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/430662.html</trackback:ping><description><![CDATA[<div>http://umeng.baijia.baidu.com/article/227913</div><br /><div><div id="page" style="margin: 0px 0px 60px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;"><div style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; letter-spacing: 0.5px;"><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">鏈枃灏?#38333;?#28725;筀afka鎬ц兘鏂?#38344;?#26473;?#29723;岀畝鍗?#37714;嗘瀽锛岄鍏堢畝鍗?#28000;?#32513;?#28051;涓婯afka?#27537;鏋舵?#21227;拰?#31225;鍙婂埌?#27537;鍚嶈瘝锛?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">1.    Topic锛氱敤浜?#37714;?#37714;哅essage?#27537;閫?#26440;?#22994;?#36423;?#38171;?#28051;涓猅opic鍙?#28000;?#37714;嗗竷鍦?#28598;?#28051;狟roker涓?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">2.    Partition锛?#37828;疜afka涓?#22959;?#37722;?#37813;?#28766;曞拰涓鍒?#39582;?#29723;屽?#26667;殑鍩虹锛?#23011;?#28051;猅opic閮?#38007;?#28751;戣鍒?#37714;?#28051;?涓狿artition銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">3.    Offset锛氭秷鎭?#37734;≒artition涓殑缂?#37721;?#38171;岀紪鍙?#26916;?#25652;?#28051;嶈法Partition銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">4.    Consumer锛氱敤浜?#28000;嶣roker涓彇鍑?娑堣垂Message銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">5.    Producer锛氱敤浜庡線Broker涓?#37721;?#38315;??#25939;浜essage銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">6.    Replication锛欿afka鏀?#37816;?#28000;artition涓?#37719;?#28003;?#28725;筂essage杩?#29723;屽啑浣欏浠?#38171;?#23011;?#28051;狿artition閮?#37721;?#28000;ラ厤缃?#38007;?#28751;?涓猂eplication(褰?#28000;?涓猂eplication鏃跺嵆浠?#29831;artition鏈韩)銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">7.    Leader锛?#23011;?#28051;猂eplication闆嗗悎涓殑Partition閮戒細閫?#37713;?#28051;涓敮涓?#27537;Leader锛?#37813;鏈夌殑璇?#37712;?#29831;锋眰閮?#37922;盠eader澶?#37918;?#37510;?#37711;?#28000;朢eplicas浠嶭eader澶勬妸?#26271;鎹?#37831;存柊鍚?#23005;ュ埌鏈?#37734;?#38171;?#26473;囩▼绫?#28028;煎ぇ瀹剁?#29112;?#22796;殑MySQL涓殑Binlog鍚?#23005;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">8.    Broker锛欿afka涓?#28003;跨敤Broker鏉?#37818;?#37721;桺roducer?#25328;Consumer?#27537;璇锋眰锛屽?#33333;妸Message鎸佷箙鍖栧埌鏈?#37734;扮?#20346;洏銆傛瘡涓狢luster褰?#28051;細閫?#28051;?#37713;?#28051;涓狟roker鏉?#37815;?#28000;籆ontroller锛岃?#29115;矗澶?#37918;哖artition?#27537;Leader閫?#28051;?#38171;屽?#24538;皟Partition杩佺?#33636;瓑宸?#28003;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">9.    ISR(In-Sync Replica)锛?#37828;疪eplicas?#27537;涓涓?#28699;愰泦锛?#29723;?#32448;?#37929;?#37715;岮live涓?#28051;嶭eader?#20824;澶?#8220;Catch-up”?#27537;Replicas闆嗗悎銆?#37922;?#27996;?#29831;?#37712;欓兘鏄?#26851;?#37711;堣?#34249;埌Leader涓?#38171;?#37813;浠?#28051;?#22471;鏉?#29831;?#38315;?#26473;?#37722;?#23005;?#37832;?#37714;?#28000;嶭eader涓?#37815;夊彇?#26271;鎹殑Replica閮?#28028;氬拰Leader鏈?#28051;浜涘欢杩??#23512;?#23277;浜嗗欢杩?#37827;?#38338;?#37724;屽欢杩熸潯?#26271;涓?#28051;?#32513;?#25652;?锛?#28000;绘剰涓涓?#29906;?#26473;?#38339;堝?#38318;戒細?#22968;璇eplica韪?#37713;篒SR銆傛瘡涓狿artition閮?#37832;夊?#20909;嚜宸辩嫭绔嬬殑ISR銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">浠?#28051;?#37713;犱箮鏄?#37812;?#28000;?#37734;?#28003;跨敤Kafka?#27537;杩囩▼涓?#37721;?#38003;介?#22247;埌?#27537;鎵鏈夊?#23944;瘝锛屽悓鏃朵篃鏃?#28051;涓嶆槸鏈鏍稿?#20906;殑姒?#36423;?#37812;栫粍浠?#38171;屾劅瑙夊埌浠?#29825;?#29825;?#37832;韩鏉?#29831;?#38171;孠afka杩?#37828;冻澶熺畝娲佺殑銆?#26473;欐鏈枃鍥?#32513;昁afka浼?#23534;傜殑鍚炲悙鎬ц兘锛岄?#28051;?#28000;?#32513;?#28051;涓?#37711;?#29825;?#29825;?#28051;?#28729;炵幇褰?#28051;?#37813;浣跨敤?#27537;鍚勯」“榛?#32457;戞妧”銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;"><strong style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: 800; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;">Broker</strong></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">涓?#37722;?#27996;嶳edis?#25328;MemcacheQ绛?#37712;?#28699;樻秷鎭?#38339;熷垪锛孠afka?#27537;璁?#29825;?#37828;妸鎵鏈夌殑Message閮?#29781;?#37712;欏叆閫熷害浣?#28729;?#38322;?#28598;х殑纭?#37929;?#38171;?#28000;?#23005;?#37833;?#37817;㈠彇?#27967;寮虹殑瀛樺偍?#20824;鍔?#37510;?#28729;?#38340;?#28051;?#38171;孠afka浣跨敤纭?#37929;?#39582;舵病鏈夊甫鏉?#26473;?#28598;氱殑鎬ц兘鎹熷け锛?#8220;瑙勮?#21229;煩鐭?#8221;?#27537;鎶勪簡涓鏉?#8220;杩?#38316;?#8221;銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">棣?#37711;?#38171;岃“瑙勮?#21229;煩鐭?#8221;鏄?#37733;?#28051;篕afka鍦?#32446;佺洏涓?#37721;仛Sequence I/O锛岀敱浜庢秷鎭郴缁?#29831;?#37712;欑殑鐗规畩鎬?#38171;?#26473;?#39582;?#28051;?#28699;?#37734;?#28000;涔堥棶棰?#37510;傚叧浜庣?#20346;洏I/O?#27537;鎬ц兘锛屽?#26336;敤涓缁凨afka瀹?#37826;?#32513;?#37713;虹殑娴嬭瘯?#26271;鎹?Raid-5锛?200rpm)锛?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">Sequence I/O: 600MB/s</p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">Random I/O: 100KB/s</p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">鎵浠?#38315;?#26473;?#37721;仛Sequence I/O?#27537;闄愬埗锛岃?#21231;?#22840;簡纾佺洏璁?#38338;?#38315;熷害浣?#28051;?#28725;?#37804;ц兘鍙兘閫犳垚?#27537;褰卞搷銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">鎺?#28051;?#37833;?#37812;?#28000;?#37712;嶈亰涓鑱奒afka鏄?#28641;?#28003;?#8220;鎶?#26473;?#38316;撶殑”銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">棣?#37711;?#38171;孠afka閲嶅害渚濊禆搴曞?#20635;搷浣滅郴缁?#37819;?#28186;涚殑PageCache鍔熻兘銆?#35120;?#28051;婂眰鏈?#37712;欐搷浣?#37827;?#38171;屾搷浣滅郴缁熷彧鏄?#28751;嗘暟鎹?#37712;欏叆PageCache锛屽悓鏃?#37837;?#29825;癙age灞?#37804;?#28051;篋irty銆?#35120;?#29831;绘搷浣滃?#25120;敓鏃?#38171;屽厛浠嶱ageCache涓?#37836;?#37813;?#38171;屽?#20635;?#28355;?#25120;敓缂?#26916;?#37813;?#26473;?#29723;岀?#20346;?#27199;皟搴?#38171;?#37832;缁?#26473;?#37733;?#38343;瑕佺殑?#26271;鎹?#37510;?#28729;?#38340;?#28051;奝ageCache鏄妸灏?#37721;兘澶氱殑绌?#38338;?#37712;?#28699;?#38318;?#35120;撳仛?#31777;纾佺洏缂?#28699;?#37833;?#28003;跨敤銆?#37722;?#37827;跺?#20635;灉鏈?#37711;?#28000;?#26473;涚▼鐢?#29831;?#37712;?#28699;?#38171;屽?#28852;敹PageCache?#27537;?#21804;浠?#37721;?#23536;?#28751;?#38171;?#37813;浠?#37916;颁唬?#27537;OS閮芥敮鎸丳ageCache銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">浣跨敤PageCache鍔熻兘鍚?#37827;跺彲浠?#38316;垮厤鍦↗VM鍐?#38318;?#32514;?#28699;樻暟鎹?#38171;孞VM涓?#37812;?#28000;?#37819;?#28186;?#27996;嗗己澶х殑GC?#20824;鍔?#38171;屽悓鏃朵?#29111;紩鍏ヤ簡涓浜?#38338;?#26864;?#28051;?#38315;傜敤涓嶬afka?#27537;璁?#29825;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">·         濡傛?#28355;湪Heap鍐呯鐞嗙紦瀛?#38171;孞VM?#27537;GC绾跨?#23337;細棰戠?#20345;壂鎻廐eap绌?#38338;?#38171;屽甫鏉?#28051;嶅繀瑕佺殑寮閿銆?#28641;傛灉Heap杩?#28598;?#38171;屾墽琛?#28051;娆ull GC瀵圭郴缁熺殑鍙敤鎬?#37833;?#29831;?#28751;嗘槸鏋?#28598;х殑鎸?#37812;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">·         鎵鏈夊湪鍦↗VM鍐呯殑瀵?#29854;?#38318;?#28051;嶅厤甯?#37832;?#28051;涓狾bject Overhead(鍗?#28051;?#28051;?#37721;?#28751;?#29785;?锛屽唴瀛樼殑鏈?#37825;堢┖闂?#37714;╃敤鐜囦?#27692;洜姝?#38340;?#28003;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">·         鎵鏈夌殑In-Process Cache鍦∣S涓?#38318;?#37832;?#28051;浠?#37722;?#37837;风殑PageCache銆?#37813;浠?#38315;?#26473;?#28751;嗙紦瀛?#37721;斁鍦≒ageCache锛屽彲浠?#38007;?#28751;戣鍙敤缂?#28699;樼┖闂寸?#35826;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">·         濡傛灉Kafka閲?#37722;?#38171;?#37813;鏈夌殑In-Process Cache閮?#28028;氬け鏁?#38171;岃孫S绠?#37918;嗙殑PageCache渚?#37906;跺彲浠?#32513;?#32513;?#28003;跨敤銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">PageCache杩?#37721;?#37828;?#32471;?#28051;姝?#38171;孠afka涓轰簡杩?#28051;姝ョ殑浼?#37718;?#37804;ц兘杩?#38322;囩敤?#31777;Sendfile?#22951;鏈?#37510;?#37734;?#29785;?#38322;奡endfile涔嬪墠锛岄鍏?#28000;?#32513;?#28051;涓?#28028;?#32513;熺殑缃?#32513;淚/O?#25655;浣滄?#20346;▼锛屽ぇ浣?#28051;?#37714;?#28051;?#28000;?#28051;?姝?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">1.    OS 浠庣‖鐩樻妸?#26271;鎹?#29831;诲埌鍐?#37837;?#37718;虹殑PageCache銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">2.    ?#25956;鎴?#26473;涚?#23339;妸?#26271;鎹?#28000;?#37712;?#37837;?#37718;篊opy?#22476;?#25956;鎴?#37718;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">3.    鐒跺悗?#25956;鎴?#26473;涚▼鍐嶆妸?#26271;鎹?#37712;欏叆?#22476;Socket锛屾暟鎹?#23092;?#37711;?#37712;?#37837;?#37718;虹殑Socket Buffer涓?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">4.    OS 鍐嶆妸?#26271;鎹?#28000;嶣uffer涓瑿opy?#22476;缃?#37719;$殑Buffer涓?#38171;?#26473;?#37837;?#28729;屾垚涓娆?#37721;?#38315;併?/p><p style="margin: 0px 0px 20px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;"><img src="http://d.hiphotos.baidu.com/news/w%3D638/sign=1cbe97e976cf3bc7e800ceefe901babd/5366d0160924ab18ec9c8ad333fae6cd7a890b4f.jpg" style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; display: inline-block; max-width: 680px;" alt="" /></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">鏁?#28051;?#26473;囩▼鍏?#32513;?#37720;?#28051;?#23046;ontext Switch锛屽?#28057;System Call銆?#37722;?#28051;浠芥暟鎹?#37734;?#37712;?#37837;窧uffer涓庣敤鎴稡uffer涔?#38338;?#38322;?#28598;嶆?#30127;礉锛?#37825;?#37916;?#28003;?#28051;?#37510;?#37711;?#28051;?銆?涓?#23005;ユ病鏈夊繀瑕?#38171;屽?#23677;叏鍙?#28000;?#37929;存帴鍦?#37712;?#37837;?#37718;?#28729;屾垚?#26271;鎹?#37815;疯礉銆?#26473;欎篃姝?#37828;疭endfile鎵瑙?#37712;崇殑闂?#26864;?#38171;?#32513;?#26473;嘢endfile浼?#37718;?#37722;?#38171;?#37825;?#28051;狪/O杩囩▼灏?#37721;樻垚?#31777;涓?#38344;?#26473;?#28051;?#37837;?#28699;?#37510;?/p><p style="margin: 0px 0px 20px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;"><img src="http://h.hiphotos.baidu.com/news/w%3D638/sign=a1dfd456bf014a90813e45be91753971/1c950a7b02087bf4b5193c0df4d3572c10dfcf59.jpg" style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; display: inline-block; max-width: 680px;" alt="" /></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">閫?#26473;?#28000;?#28051;婄殑浠?#32513;?#28051;?#38341;?#37930;?#37713;?#38171;孠afka?#27537;璁?#29825;?#37714;?#29723;?#37828;?#28751;?#28051;鍒?#37716;?#37716;?#37734;?#37712;?#28699;?#28051;?#28729;屾垚?#26271;鎹?#27996;?#37817;?#38171;?#37827;?#29825;?#37828;?#28725;?#28598;?#28003;?#28051;?#28051;鏁?#28051;?#23057;?#37805;郴缁?#38171;?#37812;?#37828;?#37712;?#38318;?#37722;屽?#26334;?#20635;搷浣滅郴缁熺殑浜?#27996;?#37510;?#28641;傛灉Producer?#25328;Consumer涔?#38338;寸敓浜у拰娑堣垂杩?#25652;?#28051;婇厤鍚?#23536;?#35120;?#38171;屽?#23677;叏鍙?#28000;?#28729;?#37916;版暟鎹?#27996;?#37817;?#38342;禝/O銆?#26473;欎篃灏?#37828;?#37812;?#28051;?#28000;涔?#29831;碖afka浣跨敤“纭?#37929;?#8221;骞舵病鏈夊甫鏉?#26473;?#28598;?#37804;ц兘鎹熷?#36777;殑鍘熷洜銆?#28051;?#38344;?#37828;?#37812;?#37734;ㄧ敓浜?#37916;?#28583;?#28051;?#38322;囧埌?#27537;涓浜涙寚鏍?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">(20 Brokers, 75 Partitions per Broker, 110k msg/s)</p><p style="margin: 0px 0px 20px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;"><img src="http://e.hiphotos.baidu.com/news/w%3D638/sign=64b42cda0af3d7ca0cf63c75ca1ebe3c/3b87e950352ac65cecae0d97fdf2b21192138aaf.jpg" style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; display: inline-block; max-width: 680px;" alt="" /></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">姝?#37827;剁殑闆嗙兢鍙?#37832;?#37712;?#38171;屾病鏈?#29831;绘搷浣?#37510;?0M/s宸?#37721;崇殑Send?#27537;娴?#38322;?#37828;疨artition涔?#38338;?#26473;?#29723;孯eplicate鑰?#27996;х敓?#27537;銆?#28000;巖ecv?#25328;writ?#27537;閫熺巼姣?#26440;?#37721;?#28000;?#37930;?#37713;?#38171;屽啓鐩?#37828;?#28003;跨敤Asynchronous+Batch?#27537;鏂瑰紡锛屽?#26334;眰OS鍙兘杩樹細杩?#29723;岀?#20346;洏鍐欓『搴?#28028;?#37718;?#37510;?#38000;屽湪鏈塕ead Request杩?#37833;ョ殑鏃跺欏垎涓?#28051;?#32457;嶆儏鍐?#38171;?#32471;?#28051;绉嶆槸鍐?#28699;?#28051;?#28729;屾垚?#26271;鎹?#27996;?#37817;?#37510;?/p><p style="margin: 0px 0px 20px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;"><img src="http://e.hiphotos.baidu.com/news/w%3D638/sign=1615143928738bd4c421b132998a876c/f603918fa0ec08fa9d9c78435fee3d6d54fbdab2.jpg" style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; display: inline-block; max-width: 680px;" alt="" /></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">Send娴?#38322;?#28000;?#39582;冲潎10M/s澧?#37716;犲埌浜嗗埌骞冲潎60M/s锛岃岀?#20346;洏Read鍙?#37832;?#28051;嶈秴杩?0KB/s銆侾ageCache闄?#28003;庣?#20346;洏I/O鏁堟灉闈炲父鏄?#37828;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">鎺?#28051;?#37833;?#37828;?#29831;?#28051;浜?#37824;跺埌?#31777;涓娈?#37827;?#38338;?#38171;屽凡缁?#28000;?#37712;?#28699;?#28051;?#29722;?#37817;?#37713;?#37714;?#37712;欏埌纾佺洏涓婄殑鑰佹暟鎹?#37510;?/p><p style="margin: 0px 0px 20px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;"><img src="http://f.hiphotos.baidu.com/news/w%3D638/sign=00fc6907d339b6004dce0cb4d1513526/2fdda3cc7cd98d1014db66d5273fb80e7aec90c2.jpg" style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; display: inline-block; max-width: 680px;" alt="" /></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">鍏?#28000;栨寚鏍?#26473;?#37828;?#38000;佹牱瀛?#38171;岃岀?#20346;洏Read宸?#32513;忛?#27702;?#27194;埌40+MB/s銆?#23005;?#37827;跺叏閮ㄧ殑?#26271;鎹?#38318;?#23480;?#32513;?#37828;?#29863;扮‖鐩樹簡(瀵圭‖鐩樼殑椤?#25652;?#29831;诲彇OS灞備細杩?#29723;孭refill PageCache?#27537;浼?#37718;?銆?#28186;?#37906;舵病鏈?#28000;?#28003;?#37804;ц兘闂?#26864;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;"><strong style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: 800; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;">Tips</strong></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">1.    Kafka瀹?#37826;?#39582;?#28051;嶅缓璁?#38315;?#26473;嘊roker绔殑log.flush.interval.messages?#25328;log.flush.interval.ms鏉?#23534;?#37714;跺啓鐩?#38171;岃涓烘暟鎹殑鍙?#38344;?#37804;?#25652;?#29831;?#38315;?#26473;嘡eplica鏉?#28103;濊瘉锛岃屽己鍒禙lush?#26271;鎹埌纾佺?#27193;細瀵?#37825;?#28003;?#37804;ц兘浜х敓褰卞搷銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">2.    鍙?#28000;?#38315;?#26473;囪皟鏁?proc/sys/vm/dirty_background_ratio?#25328;/proc/sys/vm/dirty_ratio鏉ヨ皟浼?#37804;ц兘銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">a.    ?#21104;椤?#37916;囪秴杩?#32471;?#28051;涓寚鏍囦?#27692;惎鍔╬dflush寮濮婩lush Dirty PageCache銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">b.    ?#21104;椤?#37916;囪秴杩?#32471;?#27996;?#28051;寚鏍囦細闃诲鎵鏈夌殑鍐欐搷浣?#37833;?#26473;?#29723;孎lush銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">c.    鏍?#37817;?#28051;?#37722;岀殑涓?#37716;?#38343;?#30512;鍙?#28000;?#38315;?#35120;撶殑闄?#28003;巇irty_background_ratio?#25328;鎻愰珮dirty_ratio銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;"><strong style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: 800; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;">Partition</strong></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">Partition鏄疜afka鍙?#28000;?#23536;堝?#30028;殑妯?#37722;?#37813;?#28766;曞拰鎻?#28186;涢珮骞跺彂澶?#37918;?#28000;?#37721;?#28729;炵幇Replication?#27537;鍩虹銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">鎵?#28766;?#37804;?#37826;?#38344;?#37510;?#26851;?#37711;?#38171;孠afka鍏?#29825;窹artition鍦?#38342;嗙兢鍐呯殑Broker涔?#38338;?#28000;绘?#24533;Щ鍔?#38171;?#28000;?#23005;?#37833;ュ潎琛?#37721;兘瀛?#37734;ㄧ殑?#26271;鎹?#37706;?#37826;?#38338;?#26864;?#37510;?#37711;舵锛孭artition鏀?#37816;佽嚜瀹?#28052;夌殑鍒嗗尯?#30075;娉?#38171;?#28186;?#28641;?#37721;?#28000;?#28751;嗗悓涓涓狵ey?#27537;鎵鏈夋秷鎭?#38318;?#29882;?#37922;卞埌鍚?#28051;涓狿artition涓?#37720;?#37510;?鍚?#37827;禠eader涔熷彲浠?#37734;↖n-Sync?#27537;Replica涓?#26473;佺Щ銆?#37922;?#27996;?#38333;?#28725;规煇涓涓狿artition?#27537;鎵鏈?#29831;?#37712;?#29831;锋眰閮?#37828;?#37721;?#37922;盠eader鏉?#28598;?#37918;?#38171;?#37813;浠afka?#32048;灏?#38322;忔妸Leader?#28494;鍖?#27537;鍒嗘暎?#22476;闆嗙兢?#27537;鍚?#28051;?#38010;傜偣涓?#38171;?#28000;ュ厤閫犳垚缃?#32513;滄祦閲?#26473;囦簬闆?#28051;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">骞跺彂鏂?#38344;?#37510;?#28000;绘剰Partition鍦?#37836;?#28051;涓?#37827;跺埢鍙兘琚?#28051;涓狢onsumer Group鍐呯殑涓涓狢onsumer娑堣垂(鍙?#26473;?#37833;?#28051;涓狢onsumer鍒欏彲浠?#37722;?#37827;舵?#22563;垂澶?#28051;狿artition)锛孠afka闈炲?#21733;畝娲佺殑Offset鏈?#37714;?#37832;灏?#37718;栦簡Broker?#25328;Consumer涔?#38338;寸殑浜?#27996;?#38171;?#26473;?#28003;縆afka骞?#28051;?#28028;氬儚鍚岀被鍏?#28000;栨秷鎭?#38339;熷垪涓鏍?#38171;岄?#24533;潃涓嬫父Consumer?#26271;鐩殑澧?#37716;?#38000;屾垚姣?#28186;嬬殑闄?#28003;?#37804;ц兘銆?#23005;?#28598;?#38171;屽?#20635;?#28355;涓狢onsumer鎭?#23480;?#38318;?#37828;?#23057;堣垂鏃?#38338;?#25652;?#28051;?#23536;?#37929;?#26473;戠殑?#26271;鎹?#38171;屽彲浠?#26440;惧埌寰堥?#27196;殑PageCache鍛?#28051;?#37916;?#38171;屽洜鑰孠afka鍙?#28000;?#38344;炲父楂?#37825;堢殑鏀?#37816;侀珮骞跺?#25123;?#32472;搷浣?#38171;屽?#28854;返涓?#37737;?#37832;?#37721;?#28000;?#26440;惧埌鍗?#37832;?#32515;?#37719;?#28051;?#38340;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">涓?#26473;?#38171;孭artition?#27537;?#26271;閲?#39582;?#28051;嶆槸瓒?#28598;氳?#23106;ソ锛孭artition?#27537;?#26271;閲忚秺澶?#38171;屽?#20914;?#22247;埌姣?#28051;涓狟roker涓婄殑?#26271;閲忎篃灏辫秺澶?#37510;?#38000;冭檻?#22476;Broker瀹?#37832;?Network Failure, Full GC)?#27537;鎯?#37712;?#28051;?#38171;岄渶瑕佺敱Controller鏉?#28051;?#37813;鏈夊畷鏈虹殑Broker涓婄殑鎵鏈塒artition閲?#37826;?#38315;?#28051;綥eader锛屽亣璁?#23011;?#28051;狿artition?#27537;閫?#28051;炬秷鑰?0ms锛屽?#20635;灉Broker涓?#37832;?00涓狿artition锛岄偅涔?#37734;?#26473;?#29723;岄?#28051;剧殑5s?#27537;鏃?#38338;?#38322;?#38171;屽涓?#26473;癙artition?#27537;璇?#37712;欐搷浣?#38318;戒細瑙?#37721;慙eaderNotAvailableException銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">鍐?#26473;?#28051;姝?#38171;屽?#20635;灉鎸傛?#22796;殑Broker鏄?#37825;?#28051;?#38342;嗙兢?#27537;Controller锛岄偅涔堥鍏?#29781;?#26473;?#29723;岀殑鏄?#38322;?#37826;?#28000;?#37723;?#28051;涓狟roker浣?#28051;篊ontroller銆?#37826;?#28000;?#37723;界殑Controller瑕?#28000;嶼ookeeper涓?#38014;峰彇鎵鏈塒artition?#27537;Meta淇?#37805;?#38171;岃?#23792;彇姣?#28051;?#28103;?#37805;?#28598;?#22994;?-5ms锛岄偅涔?#28641;傛灉鏈?0000涓狿artition杩?#28051;?#37827;?#38338;?#28751;变細杈惧埌30s-50s銆?#38000;?#28051;?#28051;嶈?#20344;繕璁?#26473;欏彧鏄?#38322;?#37826;板惎鍔?#28051;涓狢ontroller鑺辫?#22317;殑鏃?#38338;?#38171;屽湪杩欏?#34425;涓?#26473;樿鍐?#37716;?#28051;婂墠闈?#29831;寸殑閫?#28051;綥eader?#27537;鏃?#38338;?-_-!!!!!!</p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">姝?#28598;?#38171;屽湪Broker绔?#38171;屽?#31558;roducer?#25328;Consumer閮?#28003;跨敤?#31777;Buffer鏈?#37714;?#37510;?#37711;?#28051;瑽uffer?#27537;澶?#28751;?#37828;?#32513;?#28051;?#21412;缃殑锛屾暟閲?#37714;?#28051;嶱artition涓暟鐩稿悓銆?#28641;傛灉Partition涓暟杩?#28598;?#38171;屼細瀵?#38007;碢roducer?#25328;Consumer?#27537;Buffer鍐?#28699;?#37719;犵敤杩?#28598;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;"><strong style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: 800; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;">Tips</strong></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">1.    Partition?#27537;?#26271;閲?#28751;?#38322;?#37819;愬?#23945;?#21227;?#21979;厤锛岃?#30028;?#36346;彲浠?#37734;?#37722;?#37832;?#37716;?#37804;佸鍔燩artition锛?#28003;嗘槸?#32048;鍐掔潃鍙兘鐮?#37735;廙essage Key?#25328;Partition涔?#38338;?#28725;瑰簲?#21479;绯荤殑椋?#38340;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">2.    Replica?#27537;?#26271;閲?#28051;嶈杩?#28598;?#38171;屽?#20635;灉鏉?#28000;跺厑璁?#28751;?#38322;忔妸Replica闆嗗悎鍐呯殑Partition鍒嗗埆?#30367;鏁村埌涓?#37722;岀殑Rack銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">3.    灏?#28051;鍒?#37716;?#37716;?#28103;濊?#20345;?#24532;鍋淏roker鏃?#38318;?#37721;?#28000;lean Shutdown锛屽惁鍒欓棶棰?#28751;?#28051;?#28000;?#28000;呮槸鎭?#28598;嶆湇鍔?#37813;闇鏃?#38338;?#38336;?#38171;?#26473;?#37721;兘鍑虹?#29256;暟鎹?#37817;熷潖鎴?#37711;?#28000;栧緢璇?#23534;傜殑闂?#26864;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;"><strong style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: 800; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;">Producer</strong></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">Kafka?#27537;鐮?#37721;?#37733;?#38339;?#29723;?#32448;?#37734;?.8鐗?#37832;?#38322;岀敤Java閲?#37712;欎?#21976;暣涓狿roducer锛?#37817;?#29831;?#37804;ц兘鏈?#27996;嗗緢澶?#37819;愬崌銆?#37812;?#26473;樻病鏈?#27996;茶嚜瀵规瘮璇曠敤杩?#38171;?#26473;?#38322;?#28751;?#28051;嶅仛?#26271;鎹?#28725;规?#26046;簡銆?#37832;枃缁?#28751;剧殑鎵?#28766;曢槄璇?#38322;?#37819;愬埌?#31777;涓濂?#37812;戣涓?#23011;?#26440;?#28610;界殑瀵圭収缁?#38171;?#37832;?#37711;磋叮?#27537;鍚?#28699;?#37721;?#28000;?#28751;濊瘯涓涓?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">鍏跺疄鍦≒roducer绔殑浼?#37718;?#28598;?#38318;?#37714;嗘秷鎭郴缁?#38322;?#37721;栫殑鏂瑰紡閮?#23011;?#26440;?#37719;?#28051;锛?#37827;?#38344;炰篃灏?#37718;?#38342;?#28051;?#37825;?#37510;?#37722;?#23005;?#37721;?#23534;?#23005;?#26473;欎箞鍑?#32457;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">Kafka?#37108;缁熼粯璁?#37824;?#37816;丮essageSet锛屾妸澶?#37833;essage?#22172;鍔?#37734;?#37813;撴垚涓涓狦roup鍚?#37721;?#38315;?#37713;?#37720;?#38171;屽潎鎽?#37722;?#37815;?#28003;?#27996;嗘?#24532;閫?#28103;$殑RTT銆?#38000;?#28051;?#37734;?#32513;?#32513;嘙essageSet?#27537;鍚?#37827;?#38171;?#26473;?#37721;?#28000;ユ妸?#26271;鎹?#38322;?#37826;版帓搴?#38171;?#28000;庣?#21975;彂娴佸紡?#27537;闅?#37832;?#37712;欏叆浼?#37718;栨垚杈?#28051;?#39582;?#32459;崇殑绾?#37804;?#37712;欏叆銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">姝?#28598;?#38171;?#26473;樿?#20346;潃閲?#28000;?#32513;嶇殑涓?#20579;鏄?#38171;孭roducer鏀?#37816;丒nd-to-End?#27537;鍘嬬缉銆傛暟鎹?#37734;?#37832;?#37734;?#37720;嬬缉鍚庢斁?#22476;缃?#32513;?#28051;?#28028;?#26440;?#38171;屽湪Broker涓?#22471;涓嶈В鍘??#27342;闈炴寚瀹?#29781;丏eep-Iteration)锛岀洿鑷虫秷鎭?#29722;獵onsume涔?#37722;?#37734;?#28729;?#37812;风瑙?#37720;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">褰撶?#21057;敤鎴蜂?#29111;彲浠?#38315;夋嫨?#22172;宸卞湪搴旂敤?#30512;涓婂仛鍘嬬缉鍜岃В鍘嬬殑宸?#28003;?姣?#32468;烱afka鐩?#37715;嶆敮鎸佺殑鍘嬬缉?#30075;娉?#37832;?#38340;?#38171;屽彧鏈塆ZIP?#25328;Snappy)锛?#28051;?#26473;?#26473;?#37837;峰仛鍙嶈屼細?#21104;澶栫殑闄?#28003;?#37825;?#37916;?#38171;?#38171;?#38171;?#38171;?Kafka?#27537;End-to-End鍘嬬缉涓嶮essageSet?#21412;鍚?#37734;?#28051;璧?#23480;?#28003;?#37825;堟灉鏈浣?#38171;?#28051;?#38344;㈢殑?#20187;娉?#37929;存帴鍓茶?#20633;簡涓?#38000;?#38338;?#38001;旂郴銆傝嚦浜?#38316;?#37918;嗗?#36346;?#28850;緢?#30045;鍗?#38171;屽?#23340;缉?#30075;娉?#28051;?#28051;鏉?#37737;?#37832;殑鍘?#37918;?#8220;閲?#28598;嶇殑?#26271;鎹?#38322;忚秺澶?#38171;屽?#23340;缉姣旇?#23111;珮”銆?#37827;犲叧浜庢秷鎭?#28003;撶殑鍐呭锛?#37827;犲叧浜庢秷鎭?#28003;撶殑?#26271;閲?#38171;屽ぇ澶氭暟鎯?#37712;?#28051;嬭?#25779;叆?#26271;鎹?#38322;?#28598;?#28051;浜?#28028;氬彇寰楁洿濂界殑鍘嬬缉姣?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">涓?#26473;嘖afka閲囩敤MessageSet涔熷鑷?#37734;?#37721;敤鎬?#28051;?#28051;瀹氱▼搴︾殑濡?#37719;?#37510;傛?#24532;鍙?#38315;佹暟鎹?#37827;?#38171;孭roducer閮?#37828;痵end()涔?#37722;?#28751;?#29825;?#28051;?#23480;?#32513;?#37721;?#38315;?#37713;?#37720;讳簡锛?#28003;嗗?#36346;疄澶?#28598;氭暟鎯?#37712;?#28051;嬫秷鎭?#26473;?#37734;?#37712;?#28699;樼殑MessageSet褰?#28051;?#38171;?#28751;?#37832;?#37721;?#38315;佸埌缃?#32513;?#38171;?#26473;?#37827;跺欏?#20635;灉Producer鎸傛帀锛岄偅灏变細鍑虹幇涓㈡暟鎹殑鎯?#37712;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">涓轰簡瑙?#37712;?#26473;?#28051;?#38338;?#26864;?#38171;孠afka鍦?.8鐗?#37832;殑璁?#29825;?#37706;熼?#32736;簡缃?#32513;滃綋涓殑ack鏈?#37714;?#37510;?#28641;傛灉瀵?#37804;ц兘瑕佹眰杈冮珮锛屽?#22563;兘鍦?#28051;瀹氱▼搴?#28051;?#37711;?#29825;窶essage?#27537;涓?#28598;?#38171;岄偅灏?#37721;?#28000;?#29825;?#32515;畆equest.required.acks=0 鏉ュ叧闂璦ck锛?#28000;?#37711;?#38315;熷彂閫併?#28641;傛灉闇瑕?#28725;?#37721;?#38315;佺殑娑?#37805;?#26473;?#29723;岀‘璁?#38171;?#28751;?#38343;瑕?#29825;?#32515;畆equest.required.acks涓?鎴?1锛岄偅涔??#25328;-1鍙?#37832;?#28000;涔?#37718;?#37714;?#37723;?#38171;?#26473;?#38322;屽張瑕佹?#24876;埌鍓?#38344;?#38001;婄殑鏈夊叧Replica?#26271;閲?#38338;?#26864;?#37510;?#28641;傛灉?#21412;缃?#28051;?锛?#29723;?#32448;烘秷鎭?#37721;?#38343;瑕佽Leader鎺?#37824;跺?#21057;‘璁?#37719;?#37721;?#38171;屽叾浠栫殑Replica鍙?#28000;?#26473;?#29723;屽紓姝?#37815;夊彇鏃?#38343;绔?#37719;?#26473;?#29723;岀‘璁?#38171;屽湪淇濊瘉鍙?#38344;?#37804;х殑鍚?#37827;跺張涓嶄細?#22968;鏁?#37916;?#37815;夊緱寰?#28003;?#37510;?#28641;傛灉璁?#32515;?#28051;?1锛?#29723;?#32448;烘秷鎭?#29781;丆ommit?#22476;璇artition?#27537;ISR闆嗗悎涓殑鎵鏈塕eplica鍚?#38171;?#37813;?#37721;?#28000;?#26473;?#37733;瀉ck锛屾秷鎭殑鍙?#38315;佷細?#27967;瀹?#37711;?#38171;岃?#37825;?#28051;?#26473;囩?#23340;殑?#27426;杩熶細闅忕潃Replica?#27537;?#26271;閲?#23005;?#23011;?#28583;?#38336;?#38171;?#26473;?#38322;?#28751;?#38343;瑕佹牴鎹?#28051;?#37722;岀殑闇?#30512;?#20187;鐩稿簲?#27537;浼?#37718;?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;"><strong style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: 800; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;">Tips</strong></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">1.    Producer?#27537;绾跨▼涓嶈?#20352;厤缃?#26473;?#28598;?#38171;?#28751;?#37711;舵槸鍦∕irror鎴?#38000;匨igration涓?#28003;跨敤?#27537;鏃跺?#38171;屼細鍔犲墽鐩?#37837;?#38342;嗙兢Partition娑?#37805;?#28052;卞?#24533;殑鎯?#37712;?濡傛灉浣犵殑搴旂敤鍦烘櫙瀵规秷鎭?#26916;?#25652;忓緢鏁忔?#29114;殑璇?銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">2.    0.8鐗?#37832;殑request.required.acks?#31919;璁?#37828;?(鍚?.7)銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;"><strong style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: 800; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;">Consumer</strong></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">Consumer绔殑璁?#29825;?#28598;?#28003;?#28051;?#26473;樼畻鏄?#23011;?#26440;?#29999;?#29785;勭殑銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">·         閫?#26473;嘋onsumer Group锛屽彲浠?#37824;?#37816;佺敓浜?#38000;呮秷璐硅呭拰闃熷垪璁?#38338;?#28051;?#32457;嶆ā?#32033;銆?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">·         Consumer API鍒?#28051;篐igh level?#25328;Low level涓?#32457;?#37510;?#37715;?#28051;绉?#38322;嶅害渚濊禆Zookeeper锛?#37813;浠?#37804;ц兘宸?#28051;浜?#28051;?#28051;嶈嚜鐢?#38171;?#28003;嗘槸瓒?#37930;佸績銆?#32471;?#27996;岀涓?#28186;濊禆Zookeeper鏈?#37716;?#38171;?#37827;?#29825;?#28000;庤嚜鐢卞害?#25328;鎬ц兘涓?#38318;?#37832;夋洿濂界殑琛ㄧ幇锛?#28003;嗘槸鎵鏈夌殑寮?#29999;?Leader杩佺Щ銆丱ffset瓒婄晫銆丅roker瀹?#37832;虹瓑)?#25328;Offset?#27537;缁存姢閮?#38343;瑕佽嚜琛屽鐞?#37510;?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">·         澶?#28729;跺彲浠ュ叧娉?#28051;?#28051;?#37827;?#37721;?#29999;冪殑0.9 Release銆?#23534;鍙戜汉鍛?#37721;堢敤Java閲?#37712;欎簡涓濂桟onsumer銆傛妸涓?#28610;桝PI鍚?#39582;跺湪涓璧?#38171;屽悓鏃跺幓鎺?#27996;嗗Zookeeper?#27537;渚濊禆銆?#37817;?#29831;?#37804;ц兘鏈?#28598;?#39582;呭害鎻愬?#22247;摝~~</p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;"><strong style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: 800; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;">Tips</strong></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">寮虹?#22559;帹?#23832;浣跨敤Low level API锛岃?#30028;?#21057;?#20346;悙涓浜?#38171;?#28003;嗘槸鐩?#37715;?#37721;?#37832;?#26473;?#28051;狝PI鍙?#28000;?#28725;笶rror?#26271;鎹?#26473;?#29723;岃嚜瀹?#28052;?#28598;?#37918;?#38171;?#28751;?#37711;舵槸澶?#37918;咮roker寮?#29999;?#37812;栫敱浜嶶nclean Shutdown瀵?#38007;寸殑Corrupted Data鏃?#38171;屽惁鍒?#37827;?#23049;昐kip鍙兘绛夌潃“鍧忔秷鎭?#8221;鍦˙roker涓婅?#29574;otate鎺?#38171;屽湪姝?#37832;熼棿璇eplica灏嗕細涓鐩?#28598;?#27996;?#28051;?#37721;敤鐘?#37804;併?/p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;"><strong style="margin: 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: 800; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased;">鎵?#28766;曢槄璇?/strong></p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">Sendfile: https://www.ibm.com/developerworks/cn/java/j-zerocopy/</p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">So what’s wrong with 1975 programming: https://www.varnish-cache.org/trac/wiki/ArchitectNotes</p><p style="margin: 25px 0px; padding: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 14px; line-height: 24px; font-family: inherit; vertical-align: baseline; -webkit-font-smoothing: antialiased; text-indent: 28px; color: #333333;">Benchmarking: https://engineering.linkedin.com/kafka/benchmarking-apache-kafka-2-million-writes-second-three-cheap-machines</p></div></div><br /> </div><img src ="http://www.8634070.com/wangxinsh55/aggbug/430662.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-05-26 13:52 <a href="http://www.8634070.com/wangxinsh55/archive/2016/05/26/430662.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title>JAVA瀹炵幇gif鍥?#37911;囩缉?#25985;涓庡壀鍒?#37716;熻兘http://www.8634070.com/wangxinsh55/archive/2016/05/23/430621.htmlSIMONESIMONEMon, 23 May 2016 06:40:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/05/23/430621.htmlhttp://www.8634070.com/wangxinsh55/comments/430621.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/05/23/430621.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/430621.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/430621.htmlhttp://www.open-open.com/lib/view/open1394859355853.html

package com.pinker.util;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
 
/**
 * 鍥惧?#24538;?#20344;壀浠?#37721;?#37720;嬬缉澶?#37918;嗗伐?#21503;绫?br /> *
 * 涓?#29781;?#38333;?#28725;?#37716;?#37804;佺殑GIF鏍煎紡鍥?#37911;?#29761;佸壀涔?#37722;?#38171;屽彧鍑虹幇涓甯?#37716;?#37804;佹?#22559;灉?#27537;?#24135;璞?#37819;?#28186;?#29785;?#37712;?#37826;规
 *
 * 鎻?#28186;?#28186;濊禆涓?#37826;瑰寘瑙?#37712;?#37826;规?#22572;紙閽?#28725;笹IF鏍煎紡?#26271;鎹?#37911;瑰緛涓涓瑙?#37835;?#38171;?#26473;?#29723;岀紪鐮?#29785;?#37934;佹搷浣?#38171;?br /> * 鎻?#28186;涘?#36720;簬JDK Image I/O ?#27537;瑙?#37712;?#37826;规(JDK鎺?#32497;?#28598;辫触)
 */
public class ImageUtil2 {
 
    public enum IMAGE_FORMAT{
        BMP("bmp"),
        JPG("jpg"),
        WBMP("wbmp"),
        JPEG("jpeg"),
        PNG("png"),
        GIF("gif");
         
        private String value;
        IMAGE_FORMAT(String value){
            this.value = value;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }
     
     
    /**
     * 鑾峰彇鍥?#37911;?#37837;煎紡
     * @param file   鍥?#37911;囨枃浠?br />     * @return    鍥?#37911;?#37837;煎紡
     */
    public static String getImageFormatName(File file)throws IOException{
        String formatName = null;
         
        ImageInputStream iis = ImageIO.createImageInputStream(file);
        Iterator<ImageReader> imageReader =  ImageIO.getImageReaders(iis);
        if(imageReader.hasNext()){
            ImageReader reader = imageReader.next();
            formatName = reader.getFormatName();
        }
 
        return formatName;
    }
     
    /*************************  鍩轰簬涓?#37826;瑰寘瑙?#37712;?#37826;规    *****************************/
    /**
     * 鍓?#37714;囧浘鐗?br />     *
     * @param source        寰呭壀鍒囧浘鐗?#29882;?#23536;?br />     * @param targetPath    瑁佸壀鍚?#28103;?#28699;樿矾寰勶紙?#31919;璁?#28051;烘?#24879;矾寰?#38171;?br />     * @param x                璧峰妯?#37735;?#37837;?br />     * @param y                璧峰绾?#37735;?#37837;?br />     * @param width            鍓?#37714;?#28729;藉害
     * @param height        鍓?#37714;?#26946;?#25652;?nbsp;        
     *
     * @returns            瑁佸壀鍚?#28103;?#28699;樿矾寰勶紙鍥?#37911;?#37722;?#32514;鏍?#37817;?#37733;?#37911;?#37832;韩绫诲?#23340;敓?#22426;锛?nbsp;   
     * @throws IOException
     */
    public static String cutImage(String sourcePath , String targetPath , int x , int y , int width , int height) throws IOException{
        File file = new File(sourcePath);
        if(!file.exists()) {
            throw new IOException("not found the image锛? + sourcePath);
        }
        if(null == targetPath || targetPath.isEmpty()) targetPath = sourcePath;
         
        String formatName = getImageFormatName(file);
        if(null == formatName) return targetPath;
        formatName = formatName.toLowerCase();
         
        // 闃?#23005;?#37733;?#37911;?#37722;?#32514;涓庡浘鐗?#37832;韩绫诲瀷涓?#28051;鑷寸殑鎯?#37712;?br />        String pathPrefix = getPathWithoutSuffix(targetPath);
        targetPath = pathPrefix + formatName;
         
        // GIF闇瑕佺?#35268;畩澶?#37918;?br />        if(IMAGE_FORMAT.GIF.getValue() == formatName){
            GifDecoder decoder = new GifDecoder();  
            int status = decoder.read(sourcePath);  
            if (status != GifDecoder.STATUS_OK) {  
                throw new IOException("read image " + sourcePath + " error!");  
            }
 
            AnimatedGifEncoder encoder = new AnimatedGifEncoder();
            encoder.start(targetPath);
            encoder.setRepeat(decoder.getLoopCount());  
            for (int i = 0; i < decoder.getFrameCount(); i ++) {  
                encoder.setDelay(decoder.getDelay(i));  
                BufferedImage childImage = decoder.getFrame(i);
                BufferedImage image = childImage.getSubimage(x, y, width, height);
                encoder.addFrame(image);  
            }  
            encoder.finish();
        }else{
            BufferedImage image = ImageIO.read(file);
            image = image.getSubimage(x, y, width, height);
            ImageIO.write(image, formatName, new File(targetPath));
        }
        //鏅?#38315;氬浘鐗?br />        BufferedImage image = ImageIO.read(file);
        image = image.getSubimage(x, y, width, height);
        ImageIO.write(image, formatName, new File(targetPath));
        
        return targetPath;
    }
     
    /**
     * 鍘嬬缉鍥?#37911;?br />     * @param sourcePath       寰?#37720;嬬缉?#27537;鍥?#37911;?#29882;?#23536;?br />     * @param targetPath    鍘嬬缉鍚庡浘鐗?#29882;?#23536;勶紙?#31919;璁?#28051;?#37714;?#28654;嬭矾寰?#38171;?br />     * @param width            鍘嬬缉瀹藉害
     * @param height        鍘嬬缉楂?#25652;?br />     *
     * @returns                   瑁佸壀鍚?#28103;?#28699;樿矾寰勶紙鍥?#37911;?#37722;?#32514;鏍?#37817;?#37733;?#37911;?#37832;韩绫诲?#23340;敓?#22426;锛?nbsp;   
     * @throws IOException
     */
    public static String zoom(String sourcePath , String targetPath, int width , int height) throws IOException{
        File file = new File(sourcePath);
        if(!file.exists()) {
            throw new IOException("not found the image 锛? + sourcePath);
        }
        if(null == targetPath || targetPath.isEmpty()) targetPath = sourcePath;
         
        String formatName = getImageFormatName(file);
        if(null == formatName) return targetPath;
        formatName = formatName.toLowerCase();
         
        // 闃?#23005;?#37733;?#37911;?#37722;?#32514;涓庡浘鐗?#37832;韩绫诲瀷涓?#28051;鑷寸殑鎯?#37712;?br />        String pathPrefix = getPathWithoutSuffix(targetPath);
        targetPath = pathPrefix + formatName;
         
        // GIF闇瑕佺?#35268;畩澶?#37918;?br />        if(IMAGE_FORMAT.GIF.getValue() == formatName){
            GifDecoder decoder = new GifDecoder();  
            int status = decoder.read(sourcePath);  
            if (status != GifDecoder.STATUS_OK) {  
                throw new IOException("read image " + sourcePath + " error!");  
            }
 
            AnimatedGifEncoder encoder = new AnimatedGifEncoder();
            encoder.start(targetPath);
            encoder.setRepeat(decoder.getLoopCount());  
            for (int i = 0; i < decoder.getFrameCount(); i ++) {  
                encoder.setDelay(decoder.getDelay(i));  
                BufferedImage image = zoom(decoder.getFrame(i), width , height);
                encoder.addFrame(image);  
            }  
            encoder.finish();
        }else{
            BufferedImage image = ImageIO.read(file);
            BufferedImage zoomImage = zoom(image , width , height);
            ImageIO.write(zoomImage, formatName, new File(targetPath));
        }
        BufferedImage image = ImageIO.read(file);
        BufferedImage zoomImage = zoom(image , width , height);
        ImageIO.write(zoomImage, formatName, new File(targetPath));
         
        return targetPath;
    }
     
    /*********************** 鍩轰簬JDK 瑙?#37712;?#37826;规     ********************************/
     
    /**
     * 璇诲彇鍥?#37911;?br />     * @param file 鍥?#37911;囨枃浠?br />     * @return     鍥?#37911;囨暟鎹?br />     * @throws IOException
     */
    public static BufferedImage[] readerImage(File file) throws IOException{
        BufferedImage sourceImage = ImageIO.read(file);
        BufferedImage[] images = null;
        ImageInputStream iis = ImageIO.createImageInputStream(file);
        Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);
        if(imageReaders.hasNext()){
            ImageReader reader = imageReaders.next();
            reader.setInput(iis);
            int imageNumber = reader.getNumImages(true);
            images = new BufferedImage[imageNumber];
            for (int i = 0; i < imageNumber; i++) {
                BufferedImage image = reader.read(i);
                if(sourceImage.getWidth() > image.getWidth() || sourceImage.getHeight() > image.getHeight()){
                    image = zoom(image, sourceImage.getWidth(), sourceImage.getHeight());
                }
                images[i] = image;
            }
            reader.dispose();
            iis.close();
        }
        return images;
    }
     
    /**
     * 鏍?#37817;?#29781;佹眰澶?#37918;嗗浘鐗?br />     *
     * @param images    鍥?#37911;囨暟缁?br />     * @param x            妯?#37722;戣?#23792;浣?#32515;?br />     * @param y         绾?#37722;戣?#23792;浣?#32515;?br />     * @param width      瀹藉害    
     * @param height    瀹藉害
     * @return            澶?#37918;嗗悗?#27537;鍥?#37911;囨暟缁?br />     * @throws Exception
     */
    public static BufferedImage[] processImage(BufferedImage[] images , int x , int y , int width , int height) throws Exception{
        if(null == images){
            return images;
        }
        BufferedImage[] oldImages = images;
        images = new BufferedImage[images.length];
        for (int i = 0; i < oldImages.length; i++) {
            BufferedImage image = oldImages[i];
            images[i] = image.getSubimage(x, y, width, height);
        }
        return images;
    }
     
    /**
     * 鍐欏叆澶?#37918;嗗悗?#27537;鍥?#37911;囧埌file
     *
     * 鍥?#37911;?#37722;?#32514;鏍?#37817;?#37733;?#37911;?#37837;煎紡?#25939;?#22426;
     *
     * @param images        澶?#37918;嗗悗?#27537;鍥?#37911;囨暟鎹?br />     * @param formatName     鍥?#37911;?#37837;煎紡
     * @param file            鍐欏叆?#26499;浠跺璞?br />     * @throws Exception
     */
    public static void writerImage(BufferedImage[] images ,  String formatName , File file) throws Exception{
        Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName(formatName);
        if(imageWriters.hasNext()){
            ImageWriter writer = imageWriters.next();
            String fileName = file.getName();
            int index = fileName.lastIndexOf(".");
            if(index > 0){
                fileName = fileName.substring(0, index + 1) + formatName;
            }
            String pathPrefix = getFilePrefixPath(file.getPath());
            File outFile = new File(pathPrefix + fileName);
            ImageOutputStream ios = ImageIO.createImageOutputStream(outFile);
            writer.setOutput(ios);
             
            if(writer.canWriteSequence()){
                writer.prepareWriteSequence(null);
                for (int i = 0; i < images.length; i++) {
                    BufferedImage childImage = images[i];
                    IIOImage image = new IIOImage(childImage, null , null);
                    writer.writeToSequence(image, null);
                }
                writer.endWriteSequence();
            }else{
                for (int i = 0; i < images.length; i++) {
                    writer.write(images[i]);
                }
            }
             
            writer.dispose();
            ios.close();
        }
    }
     
    /**
     * 鍓?#37714;?#37837;煎紡鍥?#37911;?br />     *
     * 鍩轰簬JDK Image I/O瑙?#37712;?#37826;规
     *
     * @param sourceFile        寰呭壀鍒囧浘鐗囨枃浠跺璞?br />     * @param destFile                  瑁佸壀鍚?#28103;?#28699;樻枃浠跺璞?br />     * @param x                    鍓?#37714;?#22959;?#37722;戣?#23792;浣?#32515;?br />     * @param y                 鍓?#37714;囩旱鍚戣?#23792;浣?#32515;?br />     * @param width              鍓?#37714;?#28729;藉害    
     * @param height            鍓?#37714;?#28729;藉害
     * @throws Exception
     */
    public static void cutImage(File sourceFile , File destFile, int x , int y , int width , int height) throws Exception{
        // 璇诲彇鍥?#37911;?#28103;?#37805;?br />        BufferedImage[] images = readerImage(sourceFile);
        // 澶?#37918;嗗浘鐗?br />        images = processImage(images, x, y, width, height);
        // 鑾峰彇?#26499;浠跺悗缂
        String formatName = getImageFormatName(sourceFile);
        destFile = new File(getPathWithoutSuffix(destFile.getPath()) + formatName);
 
        // 鍐欏叆澶?#37918;嗗悗?#27537;鍥?#37911;囧埌?#26499;浠?br />        writerImage(images, formatName , destFile);
    }
     
     
     
    /**
     * 鑾?#37721;栫郴缁熸敮鎸佺殑鍥?#37911;?#37837;煎紡
     */
    public static void getOSSupportsStandardImageFormat(){
        String[] readerFormatName = ImageIO.getReaderFormatNames();
        String[] readerSuffixName = ImageIO.getReaderFileSuffixes();
        String[] readerMIMEType = ImageIO.getReaderMIMETypes();
        System.out.println("========================= OS supports reader ========================");
        System.out.println("OS supports reader format name :  " + Arrays.asList(readerFormatName));
        System.out.println("OS supports reader suffix name :  " + Arrays.asList(readerSuffixName));
        System.out.println("OS supports reader MIME type :  " + Arrays.asList(readerMIMEType));
         
        String[] writerFormatName = ImageIO.getWriterFormatNames();
        String[] writerSuffixName = ImageIO.getWriterFileSuffixes();
        String[] writerMIMEType = ImageIO.getWriterMIMETypes();
         
        System.out.println("========================= OS supports writer ========================");
        System.out.println("OS supports writer format name :  " + Arrays.asList(writerFormatName));
        System.out.println("OS supports writer suffix name :  " + Arrays.asList(writerSuffixName));
        System.out.println("OS supports writer MIME type :  " + Arrays.asList(writerMIMEType));
    }
     
    /**
     * 鍘嬬缉鍥?#37911;?br />     * @param sourceImage    寰?#37720;嬬缉鍥?#37911;?br />     * @param width          鍘嬬缉鍥?#37911;?#26946;?#25652;?br />     * @param heigt          鍘嬬缉鍥?#37911;?#28729;藉害
     */
    private static BufferedImage zoom(BufferedImage sourceImage , int width , int height){
        BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType());
        Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        Graphics gc = zoomImage.getGraphics();
        gc.setColor(Color.WHITE);
        gc.drawImage( image , 0, 0, null);
        return zoomImage;
    }
     
    /**
     * 鑾峰彇鏌?#28051;枃浠剁殑鍓嶇紑璺?#23536;?br />     *
     * 涓嶅寘?#24776;?#26499;浠跺?#23943;殑璺?#23536;?br />     *
     * @param file   褰撳?#23942;枃浠跺璞?br />     * @return
     * @throws IOException
     */
    public static String getFilePrefixPath(File file) throws IOException{
        String path = null;
        if(!file.exists()) {
            throw new IOException("not found the file !" );
        }
        String fileName = file.getName();
        path = file.getPath().replace(fileName, "");
        return path;
    }
     
    /**
     * 鑾峰彇鏌?#28051;枃浠剁殑鍓嶇紑璺?#23536;?br />     *
     * 涓嶅寘?#24776;?#26499;浠跺?#23943;殑璺?#23536;?br />     *
     * @param path   褰撳?#23942;枃浠?#29882;?#23536;?br />     * @return       涓嶅寘?#24776;?#26499;浠跺?#23943;殑璺?#23536;?br />     * @throws Exception
     */
    public static String getFilePrefixPath(String path) throws Exception{
        if(null == path || path.isEmpty()) throw new Exception("?#26499;浠?#29882;?#23536;?#28051;虹┖锛?);
        int index = path.lastIndexOf(File.separator);
        if(index > 0){
            path = path.substring(0, index + 1);
        }
        return path;
    }
     
    /**
     * 鑾峰彇涓嶅寘?#24776;鍚?#32514;?#27537;?#26499;浠?#29882;?#23536;?br />     *
     * @param src
     * @return
     */
    public static String getPathWithoutSuffix(String src){
        String path = src;
        int index = path.lastIndexOf(".");
        if(index > 0){
            path = path.substring(0, index + 1);
        }
        return path;
    }
     
    /**
     * 鑾峰彇?#26499;浠跺悕
     * @param filePath        ?#26499;浠?#29882;?#23536;?br />     * @return                ?#26499;浠跺悕
     * @throws IOException
     */
    public static String getFileName(String filePath) throws IOException{
        File file = new File(filePath);
        if(!file.exists()) {
            throw new IOException("not found the file !" );
        }
        return file.getName();
    }
     
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // 鑾?#37721;栫郴缁熸敮鎸佺殑鍥?#37911;?#37837;煎紡
        //ImageCutterUtil.getOSSupportsStandardImageFormat();
         
        try {
            // 璧峰?#23338;潗鏍?#38171;屽壀鍒?#28598;?#28751;?br />            int x = 100;
            int y = 75;
            int width = 100;
            int height = 100;
            // 鍙?#38000;?#37733;惧儚澶?#28751;?br />            int clientWidth = 300;
            int clientHeight = 250;
             
             
            File file = new File("C:\\1.jpg");
            BufferedImage image = ImageIO.read(file);
            double destWidth = image.getWidth();
            double destHeight = image.getHeight();
             
            if(destWidth < width || destHeight < height)
                throw new Exception("婧愬浘澶?#28751;?#28751;?#27996;?#37812;彇鍥?#37911;?#28598;?#28751;?");
             
            double widthRatio = destWidth / clientWidth;
            double heightRatio = destHeight / clientHeight;
             
            x = Double.valueOf(x * widthRatio).intValue();
            y = Double.valueOf(y * heightRatio).intValue();
            width = Double.valueOf(width * widthRatio).intValue();
            height = Double.valueOf(height * heightRatio).intValue();
             
            System.out.println("瑁佸壀澶?#28751;?nbsp; x:" + x + ",y:" + y + ",width:" + width + ",height:" + height);
 
            /************************ 鍩轰簬涓?#37826;瑰寘瑙?#37712;?#37826;规 *************************/
            String formatName = getImageFormatName(file);
            String pathSuffix = "." + formatName;
            String pathPrefix = getFilePrefixPath(file);
            String targetPath = pathPrefix  + System.currentTimeMillis() + pathSuffix;
            targetPath = ImageUtil2.cutImage(file.getPath(), targetPath, x , y , width, height);
             
            String bigTargetPath = pathPrefix  + System.currentTimeMillis() + pathSuffix;
            ImageUtil2.zoom(targetPath, bigTargetPath, 100, 100);
             
            String smallTargetPath = pathPrefix  + System.currentTimeMillis() + pathSuffix;
            ImageUtil2.zoom(targetPath, smallTargetPath, 50, 50);
             
            /************************ 鍩轰簬JDK Image I/O 瑙?#37712;?#37826;规(JDK鎺?#32497;?#28598;辫触) *************************/
//                File destFile = new File(targetPath);
//                ImageCutterUtil.cutImage(file, destFile, x, y, width, height);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


]]>鍩轰簬Redis瀹炵幇鍒嗗竷?#32033;閿?http://www.8634070.com/wangxinsh55/archive/2016/05/12/430470.htmlSIMONESIMONEThu, 12 May 2016 09:52:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/05/12/430470.htmlhttp://www.8634070.com/wangxinsh55/comments/430470.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/05/12/430470.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/430470.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/430470.htmlhttp://chaopeng.me/blog/2014/01/26/redis-lock.html

http://blog.csdn.net/ugg/article/details/41894947

http://www.jeffkit.info/2011/07/1000/

Redis鏈?#28051;?#37108;鍒楃殑鍛?#28000;?#38171;岀?#22317;偣鏄?#28000;X缁?#28751;?#38171;孨X鏄疦ot eXists?#27537;?#32521;鍐?#38171;屽?#20418;ETNX鍛?#28000;?#28751;卞簲璇?#37918;?#29785;?#28051;?#38171;歋ET if Not eXists銆?#26473;欑郴鍒楃殑鍛?#28000;?#38344;炲父鏈夌敤锛?#26473;?#38322;岃?#36461;?#36328;敤SETNX鏉?#28729;炵幇鍒嗗竷?#32033;閿併?/p>

?#25956;SETNX瀹炵幇鍒嗗竷?#32033;閿?/h3>

鍒╃敤SETNX闈炲?#21733;畝鍗?#37734;?#28729;炵幇鍒嗗竷?#32033;閿併?#28186;?#28641;?#38171;?#37836;愬鎴风瑕?#38014;峰緱涓涓?#37722;?#28699;梖oo?#27537;閿?#38171;屽鎴风浣跨敤涓?#38344;㈢殑鍛?#28000;?#26473;?#29723;岃?#23792;彇锛?/p>

SETNX lock.foo <current Unix time + lock timeout + 1>

  •  濡?#26473;?#37733;?锛屽垯璇?#28729;?#37812;风鑾峰緱閿?#38171;屾妸lock.foo?#27537;閿?#37706;?#29825;?#32515;?#28051;?#37827;?#38338;?#37706;?#29723;?#32448;?#29831;?#38335;?#23480;茶閿?#28729;?#38171;岃瀹?#37812;风鏈鍚?#37721;?#28000;?#38315;?#26473;嘍EL lock.foo鏉?#38322;婃斁璇?#38335;併?/li>
  •  濡?#26473;?#37733;?锛?#29723;?#37828;?#29831;?#38335;?#23480;茶鍏?#28000;?#28729;?#37812;风?#24391;寰?#38171;?#26473;?#37827;?#37812;?#28000;?#37721;?#28000;?#37711;?#26473;?#37733;?#37812;?#26473;?#29723;?#38322;嶈瘯绛?#28725;?#37826;瑰?#23678;垚鎴栫?#22794;緟閿佽秴鏃?#37510;?/li>

瑙?#37712;?#23005;?#38335;?/h3>

涓?#38344;㈢殑閿?#28729;?#38315;?#26440;?#37832;?#28051;涓?#38338;?#26864;?#38171;氬?#20635;灉涓涓?#37816;佹湁閿佺殑瀹?#37812;风澶辫触鎴栧穿婧冧簡涓嶈兘閲婃斁閿?#38171;岃鎬?#28052;?#29785;?#37712;?#38171;?#37812;?#28000;?#37721;?#28000;?#38315;?#26473;?#38335;佺殑閿?#28725;瑰簲?#27537;鏃?#38338;?#37812;?#37833;?#37714;?#37826;?#26473;?#32457;嶆儏鍐?#37828;?#37722;?#37721;戠敓?#31777;锛屽?#20635;?#28355;?#25779;?#23943;殑鏃?#38338;?#23480;?#32513;?#28598;?#27996;巐ock.foo?#27537;鍊?#38171;岃?#23384;槑璇?#38335;?#23480;?#28598;?#37825;?#38171;屽彲浠?#29722;?#38322;?#37826;?#28003;跨敤銆?/p>

鍙戠敓杩?#32457;嶆儏鍐?#37827;?#38171;屽彲涓嶈?#30028;畝鍗曠殑閫?#26473;嘍EL鏉?#37714;犻櫎閿?#38171;岀?#36346;悗鍐峉ETNX涓娆?#38171;屽?#25779;涓?#28729;?#37812;风妫娴嬪埌閿佽秴鏃跺悗閮戒細灏濊瘯鍘?#38322;婃斁瀹?#38171;?#26473;?#38322;?#28751;?#37721;兘鍑虹幇涓涓?#32468;?#37804;佹潯浠?璁?#37812;?#28000;?#22959;℃嫙涓涓?#26473;?#28051;?#37734;烘櫙锛?/p>

  1.  C0?#25655;浣滆秴鏃朵簡锛?#28003;嗗畠杩?#37816;佹?#22796;潃閿?#38171;孋1?#25328;C2璇诲彇lock.foo妫鏌?#37827;?#38338;?#37812;?#38171;屽厛鍚?#37721;戠?#25292;秴鏃朵簡銆?/li>
  2.  C1 鍙?#38315;丏EL lock.foo
  3.  C1 鍙?#38315;丼ETNX lock.foo 骞?#28051;旀垚鍔熶簡銆?/li>
  4.  C2 鍙?#38315;丏EL lock.foo
  5.  C2 鍙?#38315;丼ETNX lock.foo 骞?#28051;旀垚鍔熶簡銆?/li>

杩?#37837;?#28051;鏉?#38171;孋1锛孋2閮芥?#22446;埌?#31777;閿?#38171;侀棶棰?#28598;т簡锛?/p>

骞稿ソ杩?#32457;?#38338;?#26864;?#37828;?#37721;?#28000;?#38316;垮厤D锛岃鎴?#28000;?#37833;?#37930;?#37930;婥3杩?#28051;?#28729;?#37812;风鏄?#37804;?#37837;峰仛?#27537;锛?/p>

  1. C3鍙?#38315;丼ETNX lock.foo 鎯?#29781;?#38014;峰緱閿?#38171;岀敱浜嶤0杩?#37816;佹湁閿?#38171;?#37813;浠edis杩?#37733;?#32513;機3涓涓?
  2. C3鍙?#38315;丟ET lock.foo 浠?#22955;鏌?#38335;佹槸鍚?#29906;?#37827;朵簡锛屽?#20635;?#28356;病瓒?#37827;?#38171;屽垯绛夊緟鎴?#38322;嶈?#26328;?/li>
  3. 鍙?#28052;?#38171;屽?#20635;?#28355;?#33590;秴鏃?#38171;孋3閫?#26473;?#28051;?#38344;㈢殑?#25655;浣?#37833;?#28751;濊瘯鑾峰緱閿?#38171;?br /> GETSET lock.foo <current Unix time + lock timeout + 1>
  4. 閫?#26473;嘒ETSET锛孋3鎷垮埌?#27537;鏃?#38338;?#37812;?#28641;傛灉浠嶇?#33333;槸瓒?#37827;剁殑锛岄偅灏?#29831;存槑锛孋3濡傛効浠?#37707;?#37815;垮埌閿佷簡銆?/li>
  5. 濡傛?#28355;湪C3涔嬪墠锛?#37832;?#28051;?#37721;獵4?#27537;瀹?#37812;风姣擟3蹇?#28051;姝ユ墽琛屼簡涓?#38344;㈢殑?#25655;浣?#38171;岄偅涔圕3鎷垮埌?#27537;鏃?#38338;?#37812;?#37828;?#28051;?#37832;?#29906;?#37827;剁殑鍊?#38171;?#26473;?#37827;?#38171;孋3?#30149;鏈夊鏈?#38014;峰緱閿?#38171;岄渶瑕?#37712;嶆绛夊緟鎴?#38322;嶈?#26328;傜?#27408;剰涓涓?#38171;?#28751;界C3?#30149;鎷垮埌閿?#38171;?#28003;嗗?#20905;敼鍐欎簡C4璁?#32515;殑閿佺殑瓒?#37827;跺?#38171;?#28051;?#26473;?#26473;?#28051;?#20579;闈炲?#31295;井灏忕殑璇?#23480;?#29999;?#37833;ョ殑褰卞搷鍙?#28000;?#36423;界暐涓嶈銆?/li>

娉ㄦ剰锛?/strong>涓轰簡璁?#37714;嗗竷?#32033;閿佺殑?#30075;娉曟洿绋?#38335;?#27996;?#38171;?#37816;佹湁閿佺殑瀹?#37812;风鍦?#29785;?#38335;佷?#23338;?#23941;簲璇?#37712;嶆鏌?#28051;娆¤嚜宸辩殑閿佹槸鍚?#23480;?#32513;忚秴鏃?#38171;屽啀鍘诲仛DEL?#25655;浣?#38171;屽洜涓?#37721;兘瀹?#37812;风鍥?#28051;?#37836;?#28051;?#38000;?#37827;剁殑?#25655;浣?#38000;?#37816;?#29863;?#38171;屾搷浣滃?#23680;殑鏃跺欓攣鍥?#28051;鸿秴鏃跺凡缁忚鍒?#27996;?#38014;峰緱锛?#26473;?#37827;?#28751;?#28051;嶅繀瑙?#38335;佷簡銆?/p>

绀轰緥浼唬鐮?/h3>

鏍?#37817;?#28051;?#38344;㈢殑?#21804;鐮?#38171;?#37812;?#37712;欎簡涓灏忔Fake?#21804;鐮佹潵鎻?#26473;?#28003;跨敤鍒嗗竷?#32033;閿佺殑鍏?#26473;囩▼锛?/p>

  1. # get lock
  2. lock = 0
  3. while lock != 1:
  4.     timestamp = current Unix time + lock timeout + 1
  5.     lock = SETNX lock.foo timestamp
  6.     if lock == 1 or (now() > (GET lock.foo) and now() > (GETSET lock.foo timestamp)):
  7.         break;
  8.     else:
  9.         sleep(10ms)
  10.  
  11. # do your job
  12. do_job()
  13.  
  14. # release
  15. if now() < GET lock.foo:
  16.     DEL lock.foo

鏄殑锛岃?#20345;兂杩欐閫?#26440;?#37721;?#28000;?#38322;嶇敤锛?#28003;跨敤python?#27537;浣犻┈涓?#28751;?#37807;冲埌?#31777;Decorator锛岃岀敤Java?#27537;浣?#37828;?#28051;嶆槸涔熸?#20914;埌?#31777;閭?#29835;?#38171;烝OP + annotation锛?#29723;?#38171;?#37804;?#37837;疯垝鏈?#37804;?#37837;风敤鍚?#38171;屽埆閲?#28598;嶄唬鐮?#28751;?#29723;?#37510;?/p>



鑳屾櫙
鍦?寰?#28598;?#27996;?#38001;?#32515;戜骇鍝?#25652;旂敤涓?#38171;?#37832;?#27996;?#37734;烘櫙闇瑕?#37716;犻攣澶?#37918;?#38171;?#23011;?#28641;?#38171;?#32457;?#37833;锛屽叏灞閫?#28583;濱D锛屾?#29006;眰?#25939;?#22426;绛夌瓑銆?#28598;?#38318;?#37714;嗙殑瑙?#37712;?#37826;规鏄?#37737;轰簬DB瀹炵幇?#27537;锛孯edis涓?#37719;?#26473;涚▼鍗?#32510;跨▼妯??#32033;锛?#38322;囩敤闃熷垪妯″紡灏嗗?#36346;?#25123;闂?#37721;樻垚涓?#29723;岃闂?#38171;?#28051;?#28598;氬鎴风瀵筊edis?#27537;杩?#37818;?#39582;?#28051;?#28699;?#37734;?#32468;炰?#22794;叧?#37108;銆?#37711;舵Redis鎻?#28186;?#28051;浜涘懡浠ETNX锛孏ETSET锛屽彲浠?#37826;?渚?#28729;炵幇鍒嗗竷?#32033;閿佹満鍒?#37510;?/p>

Redis鍛?#28000;?#28000;?#32513;?br />浣跨敤Redis瀹炵幇鍒嗗竷?#32033;閿?#38171;?#37832;?#28051;?#28051;?#38322;嶈鍑芥暟闇瑕?#28000;?#32513;?br />
SETNX鍛?#28000;わ紙SET if Not eXists锛?/span>
璇?#23049;?#38171;?br />SETNX key value
鍔熻兘锛?br />褰?#28051;?#28000;?#35120;?key 涓?#28699;?#37734;?#38171;?#28751;?key ?#27537;鍊?#29825;?#28051;?value 锛屽苟杩?#37733;?锛涜嫢缁欏?#27697;殑 key 宸?#32513;?#28699;?#37734;?#38171;屽垯 SETNX 涓嶅仛浠?#28003;?#37716;?#28003;?#38171;屽苟杩?#37733;?銆?/p>

GETSET鍛?#28000;?/span>
璇?#23049;?#38171;?br />GETSET key value
鍔熻兘锛?br />灏?#32513;欏畾 key ?#27537;鍊?#29825;?#28051;?value 锛屽苟杩?#37733;?key ?#27537;鏃?#37706;?(old value)锛屽綋 key 瀛?#37734;?#28003;?#28051;嶆槸瀛楃涓?#32491;诲瀷鏃?#38171;?#26473;?#37733;?#28051;涓?#38335;?#29831;?#38171;屽綋key涓?#28699;?#37734;?#37827;?#38171;?#26473;?#37733;瀗il銆?/p>

GET鍛?#28000;?/span>
璇?#23049;?#38171;?br />GET key
鍔熻兘锛?br />杩?#37733;?key 鎵?#21479;鑱旂殑瀛楃涓?#37706;?#38171;屽?#20635;灉 key 涓?#28699;?#37734;?#38317;?#28052;?#26473;?#37733;?#37911;规?#23106;?nil 銆?/p>

DEL鍛?#28000;?/span>
璇?#23049;?#38171;?br />DEL key [KEY …]
鍔熻兘锛?br />鍒犻櫎缁欏?#27697;殑涓涓?#37812;?#28598;?#28051;?key ,涓?#28699;?#37734;ㄧ殑 key ?#32048;琚?#36423;界暐銆?/p>

鍏?#29840;?#32494;?#38171;?#28051;?#37734;?#28598;?#37510;?#37714;嗗竷?#32033;閿?#38171;?#37812;?#28000;?#28751;?#28186;?#38344;?#26473;欏洓涓?#37723;?#28000;?#37510;?#28003;嗗湪?#21503;浣撳疄?#24135;锛?#26473;?#37832;夊緢澶?#32513;?#38010;?#38171;岄渶瑕?#28000;?#32513;嗘枱閰?#38171;屽洜涓?#37734;?#37714;嗗竷?#32033;骞跺彂澶?#26473;涚▼涓?#38171;?#28000;?#28003;?#28051;?#20579;鍑虹幇宸?#38335;?#38171;岄?#25106;細瀵?#38007;?#23005;?#38335;?#38171;宧old浣?#37813;鏈?#26473;涚▼銆?/p>

鍔犻攣瀹炵幇

SETNX 鍙?#28000;?#37929;存帴鍔犻?#20345;搷浣?#38171;?#23011;?#28641;?#29831;?#28725;规煇涓叧閿?#29831;峟oo鍔犻攣锛屽鎴风鍙?#28000;?#28751;濊瘯
SETNX foo.lock <current unix time>

濡傛灉杩?#37733;?锛?#29723;?#32448;?#28729;?#37812;风宸?#32513;?#38014;峰彇閿?#38171;屽彲浠?#23536;涓嬫搷浣?#38171;屾搷浣滃?#23678;垚鍚?#38171;岄?#26473;?br />DEL foo.lock

鍛?#28000;?#37833;?#38322;婃斁閿併?br />濡傛灉杩?#37733;?锛岃?#23384;槑foo宸?#32513;忚鍏?#28000;?#28729;?#37812;风涓?#38335;?#38171;屽?#20635;灉閿佹槸闈炲牭濉炵殑锛屽彲浠?#38315;夋嫨杩?#37733;炶皟?#25956;銆?#28641;傛灉鏄?#37739;?#28617;炶皟?#25956;?#30367;?#25956;锛?#28751;?#38343;瑕?#26473;?#37711;?#28000;?#28051;?#28051;?#38322;嶈瘯寰?#37916;?#38171;岀洿鑷虫垚鍔?#38014;峰緱閿佹垨鑰?#38322;嶈瘯瓒?#37827;?#37510;?#37918;嗘兂鏄?#32519;庡?#30028;殑锛岀幇瀹?#37828;?#23048;?#38320;风殑銆?#28000;?#28000;?#28003;跨敤SETNX鍔犻攣甯?#37832;夌?#28848;?#22795;潯浠剁殑锛屽湪鏌?#27996;涚?#29808;?#27697;殑鎯?#37712;典細閫犳垚姝?#38335;侀敊璇?#37510;?/p>

澶?#37918;嗘閿?/span>

鍦?涓?#38344;㈢殑澶?#37918;嗘?#29808;紡涓?#38171;屽?#20635;灉鑾峰彇閿佺殑瀹?#37812;风绔墽琛?#37827;?#38338;?#26473;?#38336;?#38171;?#26473;涚?#23341;?#29608;ill鎺?#38171;?#37812;?#38000;呭洜涓?#37711;?#28000;?#23534;?#29999;稿穿婧?#38171;屽鑷?#37827;?#23049;?#38322;婃斁閿?#38171;?#28751;变細閫犳垚姝?#38335;併?#37813;浠?#38171;岄渶瑕?#28725;?#37716;犻攣瑕佸仛鏃?鏁?#37804;?#22955;娴?#37510;?#37733;?#23005;?#38171;?#37812;?#28000;?#37734;?#37716;犻?#20345;椂锛屾妸褰撳墠鏃?#38338;?#37812;?#28003;?#28051;簐alue瀛?#37711;?#23005;?#38335;?#28051;?#38171;岄?#26473;?#35120;撳墠鏃?#38338;?#37812;冲拰Redis涓殑鏃?#38338;?#37812;?#26473;?#29723;屽?#35268;瘮锛屽?#20635;灉瓒?#26473;?#28051;瀹氬樊鍊?#38171;岃涓?#38335;?#23480;?#32513;?#37827;?鏁?#38171;岄槻姝?#38335;佹?#29371;檺鏈熺殑閿?#28051;?#37720;?#38171;?#28003;嗘槸锛屽湪澶?#39582;跺彂鎯?#37712;?#38171;屽?#20635;?#28355;悓鏃舵娴?#38335;?#28598;?#37825;?#38171;屽?#21057;畝鍗曠?#26945;?#23544;殑鍒犻櫎姝?#38335;?#38171;屽啀閫?#26473;嘢ETNX涓?#38335;?#38171;屽彲鑳戒細瀵?#38007;?#32468;炰?#22795;潯浠剁殑浜х敓锛屽嵆澶?#28051;?#28729;?鎴风鍚?#37827;?#38014;峰彇閿併?/p>

C1鑾峰彇閿?#38171;屽?#36346;穿婧?#37510;侰2?#25328;C3?#30367;?#25956;SETNX涓?#38335;?#26473;?#37733;?鍚?#38171;岃?#23792;緱foo.lock?#27537;鏃?#38338;?#37812;?#38171;岄?#26473;?#23011;?#28725;?#37827;?#38338;?#37812;?#38171;屽?#25120;幇閿佽秴鏃?#37510;?br />C2 鍚慺oo.lock鍙?#38315;丏EL鍛?#28000;?#37510;?br />C2 鍚慺oo.lock鍙?#38315;丼ETNX鑾峰彇閿併?br />C3 鍚慺oo.lock鍙?#38315;丏EL鍛?#28000;?#38171;?#23005;?#37827;禖3鍙?#38315;丏EL鏃?#38171;屽?#36346;疄DEL鎺夌殑鏄疌2?#27537;閿併?br />C3 鍚慺oo.lock鍙?#38315;丼ETNX鑾峰彇閿併?/p>

姝?#37827;禖2?#25328;C3閮?#38014;峰彇?#31777;閿?#38171;?#27996;х敓绔炰?#22795;潯浠?#38171;屽?#20635;?#28355;湪?#27967;楂?#39582;跺?#25120;殑鎯?#37712;?#38171;屽彲鑳戒細鏈夋洿澶氬鎴风鑾峰彇閿併?#37813;浠?#38171;孌EL閿佺殑?#25655;浣?#38171;?#28051;嶈兘鐩存帴浣跨敤鍦?#38335;佽秴鏃剁殑鎯?#37712;?#28051;?#38171;屽?#31295;ソ鎴?#28000;?#37832;塆ETSET鏂规硶锛屽亣璁?#37812;?#28000;幇鍦?#37832;?#37721;?#28598;?#28051;涓?#28729;?#37812;风?#30092;4锛岀湅鐪?#28641;?#28003;?#28003;跨敤GETSET鏂瑰紡锛岄?#22446;厤杩?#32457;嶆儏鍐?#27996;х敓銆?/p>

C1鑾峰彇閿?#38171;屽?#36346;穿婧?#37510;侰2?#25328;C3?#30367;?#25956;SETNX涓?#38335;?#26473;?#37733;?鍚?#38171;岃皟?#25956;GET鍛?#28000;?#38014;峰緱foo.lock?#27537;鏃?#38338;?#37812;砊1锛岄?#26473;?#23011;?#28725;?#37827;?#38338;?#37812;?#38171;屽?#25120;幇閿佽秴鏃?#37510;?br />C4 鍚慺oo.lock鍙?#38315;丟ESET鍛?#28000;?#38171;?br />GETSET foo.lock <current unix time>
骞跺緱?#22476;foo.lock涓?#38000;佺殑鏃?#38338;?#37812;砊2

濡傛灉T1=T2锛岃?#23384;槑C4鑾峰緱鏃?#38338;?#37812;?#37510;?br />濡傛灉T1!=T2锛岃?#23384;槑C4涔嬪?#23942;湁鍙?#28598;?#28051;涓?#28729;?#37812;风?#30092;5閫?#26473;囪皟?#25956;GETSET鏂瑰紡鑾峰彇?#31777;鏃?#38338;?#37812;?#38171;孋4鏈?#38014;峰緱閿併?#37721;兘sleep涓?#38171;?#26473;?#37711;?#28051;嬫寰?#37916;?#28051;?#37510;?/p>

?#24135;鍦ㄥ敮涓?#27537;闂?#26864;?#37828;?#38171;孋4璁?#32515;甪oo.lock?#27537;鏂?#37827;?#38338;?#37812;?#38171;?#37828;?#37722;︿細瀵?#38335;佷骇?#25939;褰卞搷銆?#37711;跺疄鎴?#28000;?#37721;?#28000;?#37930;嬪埌C4?#25328;C5?#22717;琛岀殑鏃?#38338;?#23480;?#37706;?#37835;?#28751;?#38171;屽苟涓?#37712;欏叆foo.lock涓殑閮?#37828;?#37832;?#37825;?#37827;?#38338;?#38335;?#38171;?#37813;浠?#28725;?#38335;?#39582;舵病鏈?#35120;卞搷銆?br />涓??#31777;璁?#26473;?#28051;?#38335;佹洿鍔?#23534;哄.锛岃?#23792;彇閿佺殑瀹?#37812;风锛屽簲璇?#37734;ㄨ皟?#25956;?#21479;閿?#28051;?#37716;?#37827;?#38171;屽?#23942;?#30367;?#25956;GET鏂规硶鑾峰彇T1锛屽?#23677;?#27407;叆?#27537;T0鏃?#38338;?#37812;?#26473;?#29723;屽?#35268;瘮锛?#28000;ュ厤閿?#37733;?#37711;?#28000;?#37807;?#37712;?#29722;墽琛孌EL?#21104; 澶?#29785;?#23534;鑰?#28051;嶇煡銆?#28000;?#28051;?#23005;?#26976;ゅ拰鎯?#37712;?#38171;屽緢瀹?#37828;?#28000;?#37711;?#28000;?#37721;?#38000;?#29863;勬枡涓?#37930;嬪埌銆?#28729;?#37812;风澶?#37918;嗗?#23677;?#36779;触?#27537;鎯?#37712;?#38344;炲?#31295;?#23942;潅锛?#28051;?#28000;?#28000;呮槸?#31359;婧?#26473;欎箞?#30045;鍗?#38171;?#26473;?#37721;兘鏄?#28729;?#37812;风鍥?#28051;?#37836;?#27996;涙搷浣滆闃诲 ?#31777;鐩稿綋闀?#37827;?#38338;?#38171;岀揣鎺ョ潃 DEL 鍛?#28000;?#29722;?#28751;濊瘯?#22717;琛?浣?#26473;?#37827;?#38335;?#37719;?#37734;?#37721;?#28598;栫殑瀹?#37812;风鎵?#28051;?銆?#28052;熷彲鑳藉洜涓?#28598;?#37918;?#28051;?#35120;?#38171;屽鑷?#23005;?#38335;併?#26473;?#37832;?#37721;?#38003;藉洜涓簊leep璁?#32515;?#28051;?#37722;?#37918;?#38171;屽鑷碦edis鍦?#28598;?#39582;跺彂涓嬭鍘嬪灝銆?鏈涓?#29999;?#29785;佺殑闂?#26864;?#26473;?#37832;?/p>

GET杩?#37733;瀗il鏃跺簲璇?#29863;?#38317;?#32457;?#38315;?#26440;?#38171;?/span>

绗?#28051;绉嶈?#25292;秴鏃?#38315;?#26440;?br />C1瀹?#37812;风鑾峰彇閿?#38171;屽苟涓?#28598;?#37918;嗗?#23677;悗锛孌EL鎺?#38335;?#38171;屽湪DEL閿佷?#23338;墠銆侰2閫?#26473;嘢ETNX鍚慺oo.lock璁?#32515;?#37827;?#38338;?#37812;砊0 鍙戠幇鏈夊鎴风鑾峰彇閿?#38171;?#26473;?#37711;ET?#25655;浣?#37510;?br />C2 鍚慺oo.lock鍙?#38315;丟ET鍛?#28000;?#38171;岃?#23792;彇杩?#37733;炲糡1(nil)銆?br />C2 閫?#26473;嘥0>T1+expire瀵规瘮锛?#26473;?#37711;ETSET娴佺▼銆?br />C2 ?#30367;?#25956;GETSET鍚慺oo.lock鍙?#38315;乀0鏃?#38338;?#37812;?#38171;?#26473;?#37733;瀎oo.lock?#27537;鍘熷糡2
C2 濡傛灉T2=T1鐩哥瓑锛岃?#23792;緱閿?#38171;屽?#20635;灉T2!=T1锛?#37832;?#38014;峰緱閿併?/p>

绗?#27996;岀?#23942;儏鍐?#29863;板惊鐜?#29863;皊etnx閫?#26440;?br />C1瀹?#37812;风鑾峰彇閿?#38171;屽苟涓?#28598;?#37918;嗗?#23677;悗锛孌EL鎺?#38335;?#38171;屽湪DEL閿佷?#23338;墠銆侰2閫?#26473;嘢ETNX鍚慺oo.lock璁?#32515;?#37827;?#38338;?#37812;砊0 鍙戠幇鏈夊鎴风鑾峰彇閿?#38171;?#26473;?#37711;ET?#25655;浣?#37510;?br />C2 鍚慺oo.lock鍙?#38315;丟ET鍛?#28000;?#38171;岃?#23792;彇杩?#37733;炲糡1(nil)銆?br />C2 寰?#37916;?#38171;?#26473;?#37711;?#28051;?#28051;娆ETNX閫?#26440;?/p>

涓?绉?#38315;?#26440;戣矊浼?#38318;?#37828;疧K锛?#28003;嗘槸浠?#38315;?#26440;?#28598;?#37918;?#28051;?#37833;?#29831;?#38171;?#32471;?#28051;绉嶆儏鍐?#28699;?#37734;?#38338;?#26864;?#37510;?#35120;揋ET杩?#37733;瀗il琛?#32448;?#38171;岄?#20345;槸琚?#37714;犻櫎?#27537;锛岃?#28051;嶆槸瓒?#37827;?#38171;屽簲璇?#29863;癝ETNX閫?#26440;?#37716;犻?#20341;?#29863;?#32471;?#28051; 绉嶆儏鍐电殑闂?#26864;?#37828;?#38171;?#23005;?#29999;哥殑鍔犻?#20352;?#26440;?#25652;?#29831;?#29863;癝ETNX锛岃岀幇鍦?#35120;?#38335;佽瑙i櫎鍚?#38171;岃?#25198;殑鏄疓ETST锛屽?#20635;?#28355;垽鏂?#37833;?#28000;?#28051;?#35120;?#38171;?#28751;?#28028;氬紩璧?#23005;?#38335;?#38171;屽?#22559;偛鍌?#38171;?#37812;?#37734;ㄥ仛?#27537;鏃跺?#28751;辩?#26495;埌 ?#31777;锛屽叿浣?#37804;?#28052;堢?#26495;埌?#27537;鐪?#28051;?#38344;㈢殑闂?#26864;?/p>

GETSET杩?#37733;瀗il鏃跺簲璇?#37804;?#28052;?#28598;?#37918;?#38171;?/span>

C1?#25328;C2瀹?#37812;风?#30367;?#25956;GET鎺?#37721;?#38171;孋1杩?#37733;濼1锛?#23005;?#37827;禖3缃?#32513;?#37807;?#37712;垫洿濂?#38171;屽揩閫?#26473;?#37711;?#38014;峰彇閿?#38171;屽?#33333;墽琛孌EL鍒犻櫎閿?#38171;孋2杩?#37733;濼2(nil)锛孋1?#25328;C2閮?#26473;?#37711;?#29906;?#37827;跺鐞嗛?#26440;?#37510;?br />C1 鍚慺oo.lock鍙?#38315;丟ETSET鍛?#28000;?#38171;岃?#23792;彇杩?#37733;炲糡11(nil)銆?br />C1 姣?#28725;笴1?#25328;C11鍙戠幇涓?#38000;?#28051;?#37722;?#38171;屽鐞嗛?#26440;戣涓?#37832;?#38014;峰彇閿併?br />C2 鍚慺oo.lock鍙?#38315;丟ETSET鍛?#28000;?#38171;岃?#23792;彇杩?#37733;炲糡22(C1鍐欏叆?#27537;鏃?#38338;?#37812;?銆?br />C2 姣?#28725;笴2?#25328;C22鍙戠幇涓?#38000;?#28051;?#37722;?#38171;屽鐞嗛?#26440;戣涓?#37832;?#38014;峰彇閿併?/p>

姝?鏃禖1?#25328;C2閮?#29825;?#28051;?#37832;?#38014;峰彇閿?#38171;屽?#36346;疄C1鏄?#23480;?#32513;?#38014;峰彇閿佷簡锛?#28003;嗘槸浠栫殑澶?#37918;嗛?#26440;戞病鏈?#38000;冭檻GETSET杩?#37733;瀗il?#27537;鎯?#37712;?#38171;屽彧鏄?#37719;?#32510;殑?#25956;GET?#25328;GETSET鍊?#28751;?#29723;?瀵规瘮锛岃嚦浜?#28051;?#28000;涔堜細鍑虹幇杩?#32457;嶆儏鍐?#38171;?#28051;绉嶆槸澶氬鎴风鏃?#38171;?#23011;?#28051;?#28729;?#37812;风杩?#37818;edis?#27537;鍚?#38171;屽彂鍑虹殑鍛?#28000;?#39582;?#28051;嶆槸杩?#32513;殑锛屽鑷?#28000;庡崟瀹?#37812;风鐪嬪埌?#27537;濂藉儚杩?#32513;殑鍛?#28000;?#38171;屽埌 Redis server鍚?#38171;?#26473;?#28051;?#37833;?#37723;?#28000;?#28052;?#38338;?#37721;兘宸?#32513;?#37819;?#37711;?#28598;?#38322;忕殑鍏?#28000;?#28729;?#37812;风鍙?#37713;虹殑鍛?#28000;?#38171;?#23011;?#28641;侱EL,SETNX绛?#37510;?#32471;?#27996;岀?#23942;儏鍐?#38171;屽?#27692;鎴风涔?#38338;?#37827;?#38338;?#28051;?#37722;?#23005;?#38171;?#37812;?#38000;?#28051;嶆槸涓?#37837;?鎰忎?#22796;殑鍚?#23005;?#37510;?/p>

鏃?#38338;?#37812;崇殑闂?#26864;?/span>

鎴?#28000;?#37930;嬪埌foo.lock?#27537;value鍊?#28051;?#37827;?#38338;?#37812;?#38171;?#37813;浠?#29781;?#37734;?#28598;氬鎴风鎯?#37712;?#28051;?#38171;?#28103;濊?#20352;?#20345;湁鏁?#38171;?#28051;瀹?#29781;?#37722;?#23005;?#37722;勬湇鍔?#37731;ㄧ殑鏃?#38338;?#38171;屽?#20635;?#28355;?#21228;湇鍔?#37731;?#38338;?#38171;?#37827;?#38338;存湁宸?#23534;?#37510;?#37827;?#38338;?#28051;?#28051;鑷寸殑瀹?#37812;风锛屽湪鍒?#37826;?#38335;佽秴鏃?#38171;?#28751;变細鍑虹幇鍋?#23480;?#38171;?#28000;?#38000;?#27996;х敓绔炰?#22795;潯浠?#37510;?br />閿佺殑瓒?#37827;?#28051;?#37722;?#38171;?#28051;?#37837;间?#28618;禆鏃?#38338;?#37812;?#38171;?#37827;?#38338;?#37812;?#37832;韩涔熸槸鏈夌簿搴?#38340;愬埗锛屽?#22247;鎴?#28000;殑鏃?#38338;寸簿搴?#28051;?#32457;?#38171;?#28000;?#37716;犻?#20344;埌?#22717;琛屾搷浣滃?#23941;埌瑙?#38335;?#38171;?#28051;?#22471;?#25655;浣滆偗瀹?#38318;借兘鍦?#28051;绉?#37712;呭?#23678;垚銆?#26473;?#37837;风殑璇?#38171;?#37812;?#28000;?#28051;?#38344;㈢殑CASE锛?#28751;卞緢瀹?#37828;撳嚭?#24135;銆?#37813;浠?#38171;?#37832;濂芥妸鏃?#38338;寸簿搴?#37819;愬?#22247;埌姣?#32457;?#32510;?#37510;?#26473;?#37837;风殑璇?#38171;屽彲浠?#28103;濊?#20345;绉?#32510;?#37714;殑閿佹槸瀹?#37711;ㄧ殑銆?/p>

鍒嗗竷?#32033;閿佺殑闂?#26864;?/span>

1锛氬繀瑕佺殑瓒?#37827;?#37832;?#37714;?#38171;?#38014;峰彇閿佺殑瀹?#37812;风涓鏃﹀穿婧?#38171;?#28051;瀹?#29781;佹湁杩?#37832;熸満鍒?#38171;屽惁鍒欏叾浠?#28729;?#37812;风閮?#38340;?#37827;?#23049;?#38014;峰彇閿?#38171;岄犳垚姝?#38335;侀棶棰?#37510;?br />2锛氬?#21975;竷?#32033;閿?#38171;屽?#27692;鎴风?#27537;鏃?#38338;?#37812;?#28051;嶈兘淇濊瘉涓?#37837;?#37808;忎?#22796;殑涓鑷?#37804;?#38171;?#37813;浠?#37734;?#37836;?#27996;涚?#29808;?#27692;洜绱?#28051;?#38171;?#37832;?#37721;兘瀛?#37734;?#38335;?#28051;茬殑鎯?#37712;?#37510;?#29781;侀?#25652;︾殑鏈?#37714;?#38171;屽彲浠?#37813;?#37721;?#28751;忔鐜囩殑浜?#28000;朵骇?#25939;銆?br />3锛?#37721;?#28725;瑰叧閿?#28598;?#37918;?#38010;傜偣鍔犻攣锛岃壇濂界殑涔?#37807;?#37828;?#38171;屾妸鐩稿叧?#27537;璧勬?#24876;?#21975;?#22247;ソ锛?#23011;?#28641;?#26473;?#37818;ユ暟鎹?#25652;撳悗锛岃皟?#25956;鍔犻?#20345;満鍒?#38014;峰彇閿?#38171;岀?#23384;帴杩?#29723;屾搷浣?#38171;岀?#36346;悗閲婃斁锛?#28751;?#38322;?#37713;?#28751;?#37816;佹湁閿佺殑鏃?#38338;?#37510;?br />4锛氬湪鎸佹湁閿佹?#29116;棿瑕?#28051;嶈CHECK閿?#38171;屽?#20635;灉闇瑕?#28051;?#37837;间?#28618;禆閿佺殑鐘?#37804;?#38171;?#37832;濂?#37734;ㄥ叧閿?#23005;?#26976;?#28051;仛閿佺殑CHECK妫鏌?#37832;?#37714;?#38171;?#28003;嗘槸鏍?#37817;?#37812;?#28000;殑娴嬭瘯鍙戠幇锛屽湪澶?#39582;跺彂鏃?#38171;?#23011;?#28051;娆HECK閿佹搷浣?#38171;岄兘瑕佹秷鑰?#37818;?#37713;?#28051;?#23011;?#32457;?#38171;岃?#37812;?#28000;殑鏁?#28051;?#37816;侀攣澶?#37918;嗛?#26440;?#37813;?#28051;嶅埌10姣?#32457;?#38171;岀帺瀹㈡病鏈?#38315;夋嫨?#20187;閿佺殑妫鏌?#37510;?br />5锛歴leep瀛?#38338;?#38171;?#28051;轰?#21975;噺灏?#28725;筊edis?#27537;鍘?#37716;?#38171;岃?#23792;彇閿?#28751;濊瘯鏃?#38171;屽惊鐜?#28052;?#38338;?#28051;瀹?#29781;佸仛sleep?#25655;浣?#37510;?#28003;嗘槸sleep鏃?#38338;存槸澶?#28751;?#37828;?#38338;?#28699;?#38338;?#37510;?#38343;瑕佹牴鎹嚜宸辩殑Redis?#27537;QPS锛?#37716;?#28051;?#37816;侀攣澶?#37918;?#37827;?#38338;?#32475;?#26473;?#29723;屽悎鐞?#29825;$畻銆?br />6锛?#38007;?#27996;?#28051;?#28000;涔?#28051;?#28003;跨敤Redis?#27537;muti锛宔xpire锛寃atch绛?#37832;?#37714;?#38171;屽彲浠?#37836;?#28051;鍙?#38000;?#29863;勬枡锛?#37813;?#28051;?#37720;熷洜銆?/p>

閿佹?#23341;瘯?#26271;鎹?/span>

鏈?#28003;跨敤sleep
绗?#28051;绉?#38171;岄攣閲嶈瘯鏃?#37832;仛sleep銆?#37719;曟璇锋眰锛?#37716;犻攣锛屾墽琛?#38171;岃В閿佹椂闂?nbsp;


鍙?#28000;?#37930;嬪埌鍔犻攣鍜岃В閿佹椂闂?#38318;藉緢蹇?#38171;屽綋鎴?#28000;?#28003;跨敤

ab -n1000 -c100 'http://sandbox6.wanke.etao.com/test/test_sequence.php?tbpm=t'
AB 骞跺彂100绱?#29825;?000娆?#29831;锋眰锛屽杩?#28051;?#37826;规硶杩?#29723;屽?#23339;祴鏃?#37510;?


鎴?#28000;細鍙戠幇锛岃?#23792;彇閿佺殑鏃?#38338;?#37721;樻垚锛屽悓鏃舵?#20345;湁閿?#37722;?#38171;屾墽琛?#37827;?#38338;?#28052;熷?#27195;垚锛岃宒elete閿佺殑鏃?#38338;?#38171;?#28751;?#26473;?0ms鏃?#38338;?#38171;?#28051;?#28000;涔堜細杩?#37837;?#38171;?br />1锛?#37816;佹湁閿?#37722;?#38171;?#37812;?#28000;殑?#22717;琛岄?#26440;?#28051;寘?#24776;浜嗗?#23942;?#30367;?#25956;Redis?#25655;浣?#38171;屽湪澶?#39582;跺彂鎯?#37712;?#28051;?#38171;孯edis?#22717;琛?#37828;?#37828;?#37721;樻參銆?br />2锛?#38335;佺殑鍒犻櫎鏃?#38338;?#37721;?#38336;?#38171;?#28000;?#28052;嬪?#23943;殑0.2ms锛屽?#27195;垚9.8ms锛?#37804;ц兘涓?#38340;?#26473;?0鍊?#37510;?br />鍦?#26473;?#32457;嶆儏鍐?#28051;?#38171;?#37812;?#28000;?#37720;嬫?#23340;殑QPS涓?9锛?#37832;缁?#37721;戠幇QPS鍜屽?#23339;祴鎬?#38322;?#37832;夊叧锛屽綋鎴?#28000;?#39582;跺彂100鎬?#37711;?00娆?#29831;锋眰鏃?#38171;孮PS寰楀埌110澶?#37510;?#35120;?#37812;?#28000;?#28003;跨敤sleep鏃?/p>

浣跨敤Sleep鏃?/strong>

鍗曟?#22717;琛岃?#38155;眰鏃?br />

鎴?#28000;?#37930;嬪埌锛屽拰涓?#28003;跨敤sleep鏈?#37714;?#37827;?#38171;?#37804;ц兘鐩稿綋銆?#35120;?#37827;剁敤鐩稿?#23680;殑鍘嬫祴鏉?#28000;?#26473;?#29723;屽?#23340;缉鏃?nbsp;

鑾峰彇閿佺殑鏃?#38338;存槑鏄?#37721;?#38336;?#38171;岃岄?#20346;殑閲婃斁鏃?#38338;存槑鏄?#37721;樼煭锛?#28000;呮槸涓?#38322;囩敤sleep鏈?#37714;剁殑涓鍗?#37510;?#35120;撶?#33333;墽琛?#37827;?#38338;?#37721;樻垚灏?#37828;?#37733;?#28051;?#38171;?#37812;?#28000;?#37734;ㄦ墽琛?#26473;囩▼涓?#38171;?#38322;?#37826;?#37714;?#23524;烘暟鎹?#25652;?#26473;?#37818;?#38171;屽鑷?#37827;?#38338;?#37721;?#38336;跨殑銆?#37722;?#37827;?#37812;?#28000;?#37721;?#28000;?#28725;规瘮涓婻edis?#27537;鍛?#28000;ゆ墽琛屽帇鍔?#37807;?#37712;?nbsp;

涓?鍥?#28051;?#32513;嗛珮閮?#37714;嗘槸涓?#37832;?#38322;囩敤sleep鏈?#37714;剁殑鏃剁殑鍘嬫祴鍥?#38171;岀煯鑳?#38318;?#37714;?#28051;?#38322;囩敤sleep鏈?#37714;剁殑鍘嬫祴鍥?#38171;岄?#28051;?#37733;?#37930;嬪埌鍘?#37716;?#37713;?#28751;?0%宸?#37721;?#38171;屽綋鐒?#38171;宻leep杩?#32457;?#37826;瑰紡杩?鏈?#28051;?#32514;虹偣QPS涓?#38340;嶆槑鏄?#38171;屽湪鎴?#28000;殑鍘嬫祴鏉?#28000;?#28051;?#38171;?#28000;?#28051;?5锛屽苟涓?#37832;?#38318;?#37714;?#29831;锋眰鍑虹?#25292;秴鏃舵儏鍐?#37510;?#28051;?#26473;?#32513;煎悎鍚勭?#23942;儏鍐?#37722;?#38171;?#37812;?#28000;?#26473;?#37828;?#37712;?#28729;?#38322;囩敤sleep鏈?#37714;?#38171;?#28051;?#29781;佹槸涓轰簡 闃?#23005;?#37734;?#28598;?#39582;跺彂鎯?#37712;?#28051;嬫妸Redis鍘嬪灝锛屽緢涓?#29723;?#38171;?#37812;?#28000;?#28052;嬪?#23943;?#26495;埌杩?#38171;?#37813;浠ヨ偗瀹氫細閲囩敤sleep鏈?#37714;?#37510;?/p>

鍙?#38000;?#29863;勬枡

http://www.worlduc.com/FileSystem/18/2518/590664/9f63555e6079482f831c8ab1dcb8c19c.pdf
http://redis.io/commands/setnx
SETNX 杩?#28051;椤?#32513;欎簡涓涓?#28729;炵幇銆?/p>

  • C4 sends SETNX lock.foo in order to acquire the lock
  • The crashed client C3 still holds it, so Redis will reply with 0 to C4.
  • C4 sends GET lock.foo to check if the lock expired. If it is not, it will sleep for some time and retry from the start.
  • Instead, if the lock is expired because the Unix time at lock.foo is older than the current Unix time, C4 tries to perform: GETSET lock.foo (current Unix timestamp + lock timeout + 1)
  • Because of the GETSET semantic, C4 can check if the old value stored at key is still an expired timestamp. If it is, the lock was acquired.
  • If another client, for instance C5, was faster than C4 and acquired the lock with the GETSET operation, the C4 GETSET operation will return a non expired timestamp. C4 will simply restart from the first step. Note that even if C4 set the key a bit a few seconds in the future this is not a problem.

浣嗘槸浣跨敤瀹?#37826;规帹鑽愮殑getset瀹炵幇?#27537;璇?#38171;?#37832;?#32468;炰?#22794;埌閿佺殑涓鏂圭‘瀹?#37721;?#28000;?#37714;?#37826;埌?#22172;宸?#37832;兘绔炰?#22794;埌閿?#38171;?#28003;嗗嵈灏嗘?#20345;湁閿?#28051;鏂圭殑鏃?#38338;?#28103;敼?#31777;锛?#26473;?#37837;风殑鐩存帴鍚?#37835;?#28751;?#37828;?#38171;?#37816;佹湁閿佺殑涓鏂?#37827;?#23049;?#29785;?#38335;?#38171;?#38171;?#38171;?/p>

鍩轰簬lua?#27537;瀹炵幇

鍏跺疄瀹?#37826;瑰疄?#24135;鍑虹幇?#27537;闂?#26864;?#38171;?#37828;?#37733;?#28051;?#28003;跨敤redis?#23277;绔嬬殑鍛?#28000;?#28051;嶈兘灏唃et-check-set杩?#28051;?#26473;囩▼杩?#29723;屽師瀛?#37718;?#38171;?#37813;浠?#37812;?#37712;?#28729;氬紩鍏edis-lua锛?#28751;唃et-check-set杩?#28051;?#26473;囩▼浣跨敤lua?#21116;鏈?#37833;?#28729;炵幇銆?/p>

鍔犻攣锛?/p>

  • script params: lock_key, current_timestamp, lock_timeout
  • setnx lock_key (current_timestamp + lock_timeout). if not success, set lock_key (current_timestamp + lock_timeout) if current_timestamp > value
  • client save current_timestamp(lock_create_timestamp)

瑙?#38335;?#38171;?/p>

  • script params: lock_key, lock_create_timestamp, lock_timeout
  • delete if lock_create_timestamp + lock_timeout == value

?#21503;浣撶殑瀹炵幇:

LUA
  1. ---lock

  2. local now = tonumber(ARGV[1])
  3. local timeout = tonumber(ARGV[2])
  4. local to = now + timeout
  5. local locked = redis.call('SETNX', KEYS[1], to)
  6. if (locked == 1) then
  7. return 0
  8. end
  9. local kt = redis.call('type', KEYS[1]);
  10. if (kt['ok'] ~= 'string') then
  11. return 2
  12. end
  13. local keyValue = tonumber(redis.call('get', KEYS[1]))
  14. if (now > keyValue) then
  15. redis.call('set', KEYS[1], to)
  16. return 0
  17. end
  18. return 1

  19. ---unlock

  20. local begin = tonumber(ARGV[1])
  21. local timeout = tonumber(ARGV[2])
  22. local kt = redis.call('type', KEYS[1]);
  23. if (kt['ok'] == 'string') then
  24. local keyValue = tonumber(redis.call('get', KEYS[1]))
  25. if ((keyValue - begin) == timeout) then
  26. redis.call('del', KEYS[1])
  27. return 0
  28. end
  29. end
  30. return 1

宸茬煡闂?#26864;?/h2>

redis?#27537;鍒嗗竷?#32033;閿佷細鏈夊崟鐐圭殑闂?#26864;?#37510;?#35120;撶劧鎴?#28000;殑涓?#37716;?#38322;忎?#29112;病鏈?#26440;惧埌鎸傛帀涓?#38338;ㄥ仛閿佺殑redis鍗?#37904;圭殑姘?#39582;?#37510;?/p>



]]>Spring 鍔?#37804;佹敞鍐岀被http://www.8634070.com/wangxinsh55/archive/2016/03/23/429774.htmlSIMONESIMONEWed, 23 Mar 2016 02:47:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/03/23/429774.htmlhttp://www.8634070.com/wangxinsh55/comments/429774.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/03/23/429774.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/429774.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/429774.htmlimport com.duxiu.modules.beetlsql.BeetlSQLDao;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.util.Objects;
import java.util.stream.Stream;

public class DaoFactoryBean implements ApplicationContextAware {
    
    
    @Override
    
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        
        ConfigurableApplicationContext context 
= (ConfigurableApplicationContext) applicationContext;
        DefaultListableBeanFactory beanFactory 
= (DefaultListableBeanFactory) context.getBeanFactory();
        ResourcePatternResolver rpr 
= new PathMatchingResourcePatternResolver(applicationContext);
        SQLManager sqlManager 
= applicationContext.getBean(SQLManager.class);
        
try {
            Resource[] resources 
= rpr.getResources("classpath:com/duxiu/**/*.class");
            Stream.of(resources).map(f 
-> {
                
try {
                    
return f.getURI().getPath().split("(classes/)|(!/)")[1].replace("/"".").replace(".class""");
                } 
catch (IOException e) {
                    e.printStackTrace();
                    
return null;
                }
            }).filter(Objects::nonNull).forEach(f 
-> {
                
try {
                    Class
<?> aClass = Class.forName(f);
                    
boolean match = Stream.of(aClass.getAnnotations()).anyMatch(c -> c instanceof BeetlSQLDao);
                    
if (match && !beanFactory.containsBean(aClass.getSimpleName())) {
                        System.out.println(sqlManager.getMapper(aClass));
                        
//beanFactory.registerSingleton(aClass.getSimpleName(),sqlManager.getMapper(aClass));
                    }
                } 
catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            });
        } 
catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(applicationContext.getBean(SQLManager.
class));
        
/*if(!beanFactory.containsBean(beanName)){
            BeanDefinitionBuilder beanDefinitionBuilder= BeanDefinitionBuilder.rootBeanDefinition(beanClass);
            beanDefinitionBuilder.addPropertyValue("host", host);
            beanDefinitionBuilder.addPropertyValue("port", port);
            beanDefinitionBuilder.addPropertyValue("database", database);
            beanDefinitionBuilder.setInitMethodName("init");
            beanDefinitionBuilder.setDestroyMethodName("destroy");
            beanFactory.registerBeanDefinition(beanName, beanDefinitionBuilder.getBeanDefinition());
            logger.info("Add {} to bean container.", beanName);
        }
*/
    }
}


]]>妯″潡鍖?#37714;?#37731;? 涓绡囨枃绔?#37818;?#37819;equireJS甯哥敤?#29025;璇?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/03/04/429538.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Fri, 04 Mar 2016 07:27:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/03/04/429538.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/429538.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/03/04/429538.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/429538.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/429538.html</trackback:ping><description><![CDATA[<div>http://www.cnblogs.com/lyzg/p/4865502.html</div><br /><div> <div> <div id="cnblogs_post_body"><div id="navCategory" style="background-color: #BDBDBD;padding:10px 5px;"><p style="font-size:18px;margin:0;line-height:30px;"><strong>闃?#29831;?#37929;?#35120;?/strong></p><ul><li><a href="http://www.cnblogs.com/lyzg/p/4865502.html#_label0">1. 妯″潡鍖?/a></li><li><a href="http://www.cnblogs.com/lyzg/p/4865502.html#_label1">2. AMD瑙勮寖</a></li><li><a href="http://www.cnblogs.com/lyzg/p/4865502.html#_label2">3. JavaScript?#27537;寮?#23005;?#37716;?#26462;藉拰鎸?#38343;鍔犺浇</a></li><li><a href="http://www.cnblogs.com/lyzg/p/4865502.html#_label3">4. RequireJS甯哥敤?#25956;娉?#37804;?#32513;?/a></li><li><a href="http://www.cnblogs.com/lyzg/p/4865502.html#_label4">5. RequireJS甯哥敤?#21412;缃?#37804;?#32513;?/a></li><li><a href="http://www.cnblogs.com/lyzg/p/4865502.html#_label5">6. 閿?#29831;?#28598;?#37918;?/a></li></ul></div><p>閫?杩?#37832;枃锛?#28003;?#37721;?#28000;?#28725;?#22959;″潡鍖?#23534;鍙戝拰AMD瑙勮寖鏈?#28051;涓?#26440;?#37929;?#29785;傜殑璁?#29831;?#38171;屽苟璇?#32513;嗗湴瀛?#28052;燫equireJS杩?#28051;?#22959;″潡鍖?#23534;鍙?#23480;ュ叿?#27537;甯?#29785;佺敤娉曘?#37832;枃閲囧彇寰?#25652;忔笎杩涚殑鏂瑰紡锛?#28000;?鐞?#29825;哄埌瀹炶返锛?#28000;嶳equireJS瀹?#37826;笰PI?#26499;妗?#28051;?#38171;?#37804;?#32513;撳嚭鍦?#28003;跨敤RequireJS杩囩▼涓?#37832;甯哥敤?#27537;涓浜涚敤娉?#38171;屽?#36346;?#35268;枃妗?#28051;?#28051;?#28598;熸?#21614;?#26495;叿浣撶殑鍐呭锛?#37716;?#28000;?#28186;嬭瘉?#25328; 鍒嗘瀽锛屽笇鏈?#37832;枃?#27537;鍐呭瀵?#28003;犵殑?#20824;鍔?#37819;愬崌鏈夊?#28854;川鎬х殑甯?#37716;?#37510;?/p> <div style="text-align: right; text-align: right;height: 0;padding: 0;overflow: hidden;visibility: hidden;"><a name="_label0"></a></div><h2>1. 妯″潡鍖?/h2> <p>鐩?#28103;?#23011;?#28051;?#37715;嶇寮鍙戜汉鍛?#37734;?#37714;氬紑濮?#37818;?#29785;s缂栫▼鏃?#38171;岄兘鍐?#26473;囩被浼?#28051;?#38344;?#26473;?#37837;烽鏍肩殑?#21804;鐮?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><script type="text/javascript"> <span style="color: #0000ff;">var</span> a = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">var</span> b = 2<span style="color: #000000;">; </span><span style="color: #0000ff;">var</span> c = a * a + b *<span style="color: #000000;"> b; </span><span style="color: #0000ff;">if</span>(c> 1<span style="color: #000000;">) { alert(</span>'c > 1'<span style="color: #000000;">); } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> add(a, b) { </span><span style="color: #0000ff;">return</span> a +<span style="color: #000000;"> b; } c </span>=<span style="color: #000000;"> add(a,b); </span></script></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><a href="javascript:;" onclick="click(this);" title="">璇风偣鍑?lt;/a></pre> </div> <p>杩欎?#28055;唬鐮佺殑鐗圭偣鏄?#38171;?/p> <ul><li><strong>?#22476;澶勫彲瑙佺殑鍏?#28766;鍙?#38322;?/strong></li><li><strong>澶?#38322;忕殑鍑芥暟</strong></li><li><strong>鍐呭?#23677;湪html鍏冪礌涓婄殑鍚勭?#23783;s?#30367;?#25956;</strong></li></ul> <p>褰撶劧杩欎?#28055;唬鐮佹湰?#38889;鍦?#28729;炵幇鍔熻兘涓?#39582;舵病鏈?#38335;?#29831;?#38171;?#28003;嗘槸浠庝唬鐮佺殑鍙?#38322;嶇敤鎬?#38171;屽仴澹?#37804;?#28000;?#37721;?#37721;?#32513;存姢鎬?#37833;?#29831;?#38171;?#26473;?#32457;嶇?#26667;▼鏂瑰紡鏄?#37832;夐棶棰樼殑锛?#28751;?#37711;舵槸鍦?#26916;?#38344;?#38315;?#26440;?#26440;?#28051;?#28598;嶆?#20636;殑搴旂敤涓?#38171;?#26473;欎簺闂?#26864;樹細鏆?#38343;?#37734;?#37911;?#37714;?#37828;?#37828;?#38171;?/p> <ul><li><strong>鍏?#28766;鍙?#38322;?#37835;佹槗閫犳垚鍛?#37722;?#37712;茬獊</strong></li><li><strong>鍑芥暟?#32033;缂栫▼闈炲父涓?#37714;?#27996;庝唬鐮佺殑缁?#32513;?#37724;岀鐞?/strong></li><li><strong>鍐呭?#23680;殑js?#30367;?#25956;寰?#28051;?#37714;?#27996;庝唬鐮佺殑缁存姢锛屽洜涓篽tml?#21804;鐮佹?#22796;殑鏃跺?#37828;?#37719;?#37714;嗚?#20909;偪鍜屽簽澶х殑</strong></li></ul> <p>鎵浠?#35120;?#26473;欎簺闂?#26864;?#37713;虹幇?#27537;鏃跺?#38171;宩s澶?#37911;?#28000;?#28751;卞紑濮?#28725;?#37813;?#37720;?#29785;?#37712;?#26473;欎簺闂?#26864;樼殑绌舵瀬鍔?#23049;?#38171;?#27996;?#37828;?#22959;″潡鍖?#23534;鍙?#28751;?#37713;虹?#39041;簡銆?#23005;?#28641;?#22959;″潡鍖?#26473;?#28051;?#22994;?#36423;电殑琛?#38344;㈡剰鎬?#28051;鏍?#38171;屽畠瑕佹眰鍦?缂?#37712;欎唬鐮佺殑鏃跺?#38171;?#37816;夊?#20635;锛?#37816;?#37716;熻兘锛?#28751;嗙嫭绔嬬殑閫?#26440;?#38171;?#28751;佽?#21614;垚鍙?#38322;嶇敤?#27537;妯″潡锛屽澶?#37819;?#28186;涚?#23384;帴鏄庝簡?#27537;?#30367;?#25956;鎺?#37721;?#38171;屽唴閮?#28729;炵幇缁?#38010;?#28729;屽叏绉佹湁锛屽苟涓?#22959;″潡涔?#38338;寸殑鍐?#38318;?#28729;炵幇鍦ㄦ墽琛?鏈熼棿浜?#28051;?#39582;?#37813;?#38171;?#37832;缁堢殑缁?#37835;?#28751;?#37828;?#37721;?#28000;?#29785;?#37712;?#37715;?#38344;?#28051;?#28186;?#37819;愬埌?#27537;闂?#26864;?#37510;?#28051;涓畝鍗?#38316;?#23536;?#22959;″潡鍖?#23534;鍙戣?#20345;眰缂?#37712;欑殑渚?#28699;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #008000;">//</span><span style="color: #008000;">module.js</span> <span style="color: #0000ff;">var</span> student = <span style="color: #0000ff;">function</span><span style="color: #000000;"> (name) { </span><span style="color: #0000ff;">return</span> name &&<span style="color: #000000;"> { getName: </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name; } }; }, course </span>= <span style="color: #0000ff;">function</span><span style="color: #000000;"> (name) { </span><span style="color: #0000ff;">return</span> name &&<span style="color: #000000;"> { getName: </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name; } } }, controller </span>= <span style="color: #0000ff;">function</span><span style="color: #000000;"> () { </span><span style="color: #0000ff;">var</span> data =<span style="color: #000000;"> {}; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> { add: </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (stu, cour) { </span><span style="color: #0000ff;">var</span> stuName = stu &&<span style="color: #000000;"> stu.getName(), courName </span>= cour &&<span style="color: #000000;"> cour.getName(), current, _filter </span>= <span style="color: #0000ff;">function</span><span style="color: #000000;"> (e) { </span><span style="color: #0000ff;">return</span> e ===<span style="color: #000000;"> courName; }; </span><span style="color: #0000ff;">if</span> (!stuName || !courName) <span style="color: #0000ff;">return</span><span style="color: #000000;">; current </span>= data[stuName] = data[stuName] ||<span style="color: #000000;"> []; </span><span style="color: #0000ff;">if</span> (current.filter(_filter).length === 0<span style="color: #000000;">) { current.push(courName); } }, list: </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (stu) { </span><span style="color: #0000ff;">var</span> stuName = stu &&<span style="color: #000000;"> stu.getName(), current </span>=<span style="color: #000000;"> data[stuName]; current </span>&& console.log(current.join(';'<span style="color: #000000;">)); } } }; </span><span style="color: #008000;">//</span><span style="color: #008000;">main.js</span> <span style="color: #0000ff;">var</span> stu = <span style="color: #0000ff;">new</span> student('lyzg'<span style="color: #000000;">), c </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> controller(); c.add(stu,</span><span style="color: #0000ff;">new</span> course('javascript'<span style="color: #000000;">)); c.add(stu,</span><span style="color: #0000ff;">new</span> course('html'<span style="color: #000000;">)); c.add(stu,</span><span style="color: #0000ff;">new</span> course('css'<span style="color: #000000;">)); c.list(stu);</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>浠?#28051;婁唬鐮?#28729;?#28052;変簡涓?#28051;?#22959;″潡鍒嗗埆琛?#32448;?#28699;︾敓锛岃?#21095;?#23338;拰鎺?#37714;跺櫒锛岀?#36346;悗鍦╩ain.js涓皟?#25956;?#31777;controller鎻?#28186;涚殑add?#25328;list鎺?#37721;?#38171;?#28051;簂yzg杩?#28051;?#28699;︾敓娣?#37716;犱簡涓夐棬璇剧▼锛岀?#36346;悗鍦?#37818;?#37714;跺彴鏄剧?#36720;?#21975;嚭鏉?#37510;?#26473;?#29723;?#32513;?#37835;滃涓?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>javascript;html;css</pre> </div> <p>閫?#26473;?#28051;?#28186;?#38171;屽彲浠?#37930;?#37713;?#22959;″潡鍖栫殑?#21804;鐮佺粨鏋勫拰閫?#26440;?#37719;?#37714;嗘?#21614;櫚锛屼唬鐮佺?#23341;捣鏉?#37719;?#37714;?#28028;?#38342;?#38171;屽彟澶栫敱浜?#38315;?#26440;?#38318;?#38315;?#26473;?#22959;″潡鎷嗗垎锛?#37813;浠?#26440;惧埌?#31777;瑙?#38000;︾殑鐩殑锛屼唬鐮佺殑鍔熻兘涔熶細姣?#26440;?鍋?#28601;?#37510;?#28051;?#26473;?#28051;?#28186;?#28003;跨敤?#27537;杩?#32457;嶆ā?#28513;鍖?#23534;鍙?#37826;瑰紡涔熷苟涓嶆槸?#30149;鏈夐棶棰?#38171;?#26473;?#28051;?#38338;?#26864;?#28751;?#37828;?#28729;?#26473;?#37828;妸妯″潡寮曠敤濡俿tudent杩欎?#28058;?#23384;帴娣?#37716;犲埌浜嗗叏灞绌?#38338;?#28051;?#38171;岃?#30028;劧閫?#26473;?#22959;″潡鍑?#28751;?浜嗗緢澶氬叏灞绌?#38338;寸殑鍙?#38322;?#37724;屽?#33445;暟锛?#28003;嗘槸妯″潡寮曠敤鏈韩杩?#37828;?#29781;?#28186;濊禆鍏?#28766;绌?#38338;?#38171;?#37813;嶈兘琚皟?#25956;锛屽綋妯″潡杈?#28598;?#38171;?#37812;?#38000;呮?#22794;紩鍏?#32471;?#28051;?#37826;?#22959;″潡搴?#37827;?#38171;?#28000;嶇?#36346;彲?#20824;閫犳垚鍛?#37722;?#37712;茬?#20346;殑闂?#26864;?#38171;?#37813; 浠?#26473;?#32457;?#37711;?#28766;绌?#38338;?#28051;嬬殑妯″潡鍖?#23534;鍙戠殑鏂瑰紡骞?#28051;嶆槸鏈瀹岀?#24227;殑鏂瑰紡銆?#37929;?#37715;?#29999;?#29785;佺殑妯″潡鍖?#23534;鍙?#37826;瑰紡锛屽叏灞绌?#38338;存?#29808;紡鏄?#37832;鍩?#37832;殑涓绉?#38171;屽彟澶?#29999;?#29785;佺殑杩?#37832;夐伒寰狝MD瑙勮?#20906;殑寮鍙?#37826;??#32033;锛岄伒寰狢MD瑙勮?#20906;殑寮鍙?#37826;瑰紡锛屽拰ECMAScript 6?#27537;寮鍙?#37826;瑰紡銆?#38343;瑕?#29831;存槑?#27537;鏄?#38171;孋MD?#25328;ES6璺熸湰?#26499;?#27537;鏍稿?#20905;病鏈夊叧?#37108;锛?#37813;浠?#28051;?#28028;氬湪姝?#28000;?#32513;?#38171;屽悗闈㈢殑鍐呭涓?#29781;?#28000;?#32513;岮MD浠?#37721;?#28729;?#37916;颁簡AMD瑙勮?#20906;殑 RequireJS銆?/p> <div style="text-align: right; text-align: right;height: 0;padding: 0;overflow: hidden;visibility: hidden;"><a name="_label1"></a></div><h2>2. AMD瑙勮寖</h2> <p>姝?#28641;?#28051;婃枃鎻愬埌锛屽疄?#24135;妯″潡鍖?#23534;鍙戠殑鏂瑰紡锛屽彟澶?#29999;?#29785;佺殑涓绉?#28751;?#37828;?#38316;?#23536;狝MD瑙勮?#20906;殑瀹炵幇鏂瑰紡锛?#28051;?#26473;嘇MD瑙勮寖骞?#28051;嶆槸?#21503;浣撶殑瀹炵幇鏂瑰紡锛岃?#28000;?#28000;呮槸妯″潡鍖?#23534;鍙戠殑涓 绉嶈В鍐?#37826;规锛?#28003;?#37721;?#28000;ユ妸瀹?#37918;?#29785;f垚妯″潡鍖?#23534;鍙戠殑涓浜?#37818;?#37721;?#28601;?#37828;?#38171;屽?#20635;灉浣?#29781;?#28729;炵幇涓涓?#38316;?#23536;?#29831;?#29785;勮?#20906;殑妯″潡鍖?#23534;鍙?#23480;ュ叿锛?#28751;卞繀椤?#28729;炵幇瀹?#26864;勫厛瀹?#28052;夌殑API銆傛瘮濡?#28729;?#29781;佹眰鍦?#37716;犺浇 妯″潡鏃?#38171;屽繀椤?#28003;跨敤濡?#28051;嬬殑API?#30367;?#25956;鏂瑰紡锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><span style="color: #000000;">require([module], callback) 鍏?#28051;?#38171;?[module]锛?#37828;?#28051;涓暟缁?#38171;?#38322;岄潰?#27537;?#22426;鍛?#28751;?#37828;?#29781;?#37716;犺浇?#27537;妯″潡; callback锛?#37828;?#22959;″潡鍔犺浇瀹屾垚涔?#37722;庣殑鍥炶皟鍑芥暟</span></pre> </div> <p>鎵鏈夐伒寰狝MD瑙勮?#20906;殑妯″潡鍖?#23480;ュ叿锛岄?#34249;繀椤?#37816;夌収瀹冪殑瑕佹眰鍘?#28729;炵幇锛?#23011;?#28641;俁equireJS杩?#28051;?#25652;?#38171;?#28751;?#37828;?#28729;屽叏閬?#23536;狝MD瑙勮寖瀹炵幇?#27537;锛?#37813;浠?#37734;?#37714;╃敤 RequireJS鍔犺浇鎴?#38000;呰皟?#25956;妯″潡鏃?#38171;屽?#20635;灉浣?#27996;?#37711;堢煡閬揂MD瑙勮?#20906;殑璇?#38171;?#28003;?#28751;辩煡閬?#29831;?#37804;?#28052;堢敤RequireJS?#31777;銆?#29785;勮?#20906;殑濂?#28598;勫湪浜?#38171;?#28051;?#37722;岀殑瀹?#37916;板?#23384;湁鐩稿?#23680;殑?#30367; ?#25956;鏂瑰紡锛屽緢瀹?#37828;撳垏鎹?#28051;?#37722;岀殑宸ュ叿浣跨敤锛岃嚦浜庡叿浣撶敤?#25698;涓涓?#28729;炵幇锛?#26473;?#28751;辫?#29111;悇涓?#23480;ュ叿?#27537;鍚勮嚜?#27537;浼?#37904;硅?#29116;」鐩殑鐗圭偣鏈夊叧?#37108;锛?#26473;欎簺閮?#37828;?#37734;?#26916;?#37929;?#23534;濮?#38315;夊?#23340;殑鏃跺欓渶瑕佺‘瀹氱殑銆?#37929;?鍓峈equireJS涓嶆槸?#25966;涓瀹?#37916;颁簡AMD瑙勮?#20906;殑搴?#38171;屽儚Dojo杩?#32457;嶆洿鍏?#38344;㈢殑js搴?#28052;熼兘鏈堿MD?#27537;瀹炵幇銆?/p> <p>鏈鍚?#28725;笰MD鍏?#32457;板仛涓涓?#29785;?#38322;?#38171;岃瘧涓?#38171;氬紓姝?#22959;″潡瀹?#28052;?#37510;?#23534;?#23005;?#23534;鸿皟?#27537;鏄?#38171;屽湪鍔犺浇妯″潡浠?#37721;?#22959;″潡鎵渚濊?#26667;殑鍏跺畠妯″潡鏃?#38171;岄兘閲囩敤寮?#23005;?#37716;犺浇?#27537;鏂瑰紡锛岄?#22446;厤妯″潡鍔犺浇闃诲?#28848;簡缃?#26916;电殑?#35206;鏌?#26473;?#25652;?#37510;?#37929;?#23011;?#28028;?#32513;熺殑寮?#23005;?#37716;犺浇锛孉MD宸ュ叿?#27537;寮?#23005;?#37716;犺浇?#27967;鍔犵畝渚?#38171;岃?#28051;?#26473;樿兘瀹?#37916;版寜闇鍔犺浇锛屽叿浣?#29785;?#38322;?#37734;?#28051;?#28051;閮?#37714;?#29831;存槑銆?/p> <div style="text-align: right; text-align: right;height: 0;padding: 0;overflow: hidden;visibility: hidden;"><a name="_label2"></a></div><h2>3. JavaScript?#27537;寮?#23005;?#37716;?#26462;藉拰鎸?#38343;鍔犺浇</h2> <p>html涓殑script鏍囩鍦?#37716;?#26462;藉拰?#22717;琛?#26473;囩▼涓細闃诲缃?#26916;电殑?#35206;鏌?#38171;?#37813;浠?#28051;?#22471;瑕佹眰灏?#38322;?#28751;唖cript鏍囩?#28844;斁缃?#37734;╞ody鍏冪?#29365;殑搴?#38318;?#38171;?#28000;?#28186;?#37716;?#36423;?#26916;?#38344;?#37828;剧?#34425;殑閫熷害锛?#26473;?#37832;?#28051;绉?#37826;瑰紡灏?#37828;?#38315;?#26473;?#23534;?#23005;?#37716;犺浇?#27537;鏂瑰紡鏉?#37716;犺浇js锛?#26473;?#37837;?#37721;?#28000;?#38316;垮厤js?#26499;浠跺?#31599;tml?#35206;鏌撶殑闃诲銆?/p> <p>绗?绉嶅紓姝?#37716;犺浇?#27537;鏂瑰紡鏄?#37929;存帴鍒╃敤?#21116;鏈敓?#22426;script鏍囩?#21095;殑鏂瑰紡锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre>(<span style="color: #0000ff;">function</span><span style="color: #000000;">() { var s </span>= document.createElement(<span style="color: #800000;">'</span><span style="color: #800000;">script</span><span style="color: #800000;">'</span><span style="color: #000000;">); s.type </span>= <span style="color: #800000;">'</span><span style="color: #800000;">text/javascript</span><span style="color: #800000;">'</span><span style="color: #000000;">; s.async </span>= <span style="color: #0000ff;">true</span><span style="color: #000000;">; s.src </span>= <span style="color: #800000;">'</span><span style="color: #800000;">http://yourdomain.com/script.js</span><span style="color: #800000;">'</span><span style="color: #000000;">; var x </span>= document.getElementsByTagName(<span style="color: #800000;">'</span><span style="color: #800000;">script</span><span style="color: #800000;">'</span>)[<span style="color: #800080;">0</span><span style="color: #000000;">]; x.parentNode.insertBefore(s, x); })();</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>杩欐?#20856;唬鐮?#38171;屾斁缃?#37734;╯cript鏍?#29825;?#37712;?#38318;?#38171;岀?#36346;悗璇cript鏍?#29825;版坊鍔犲埌body鍏冪?#29365;殑搴?#38318;?#37719;?#37721;?#37510;?/p> <p>绗?绉?#37826;瑰紡鏄?#37706;?#37716;﹕cript?#27537;灞?#37804;?#38171;歞efer?#25328;async锛宒efer杩?#28051;?#28766;?#37804;?#37734;↖E娴?#29785;?#37731;ㄥ拰鏃?#29863;风殑鐏?#37913;愭祻瑙?#37731;?#28051;?#37824;?#37816;?#38171;宎sync鍦?#37824;?#37816;?html5?#27537;娴?#29785;?#37731;?#28051;?#38318;芥敮鎸?#38171;屽彧瑕佹湁杩?#28051;?#28051;?#28766;?#37804;?#38171;宻cript灏变細浠?#23534;?#23005;ョ殑鏂瑰紡鏉?#37716;犺浇锛?#37813;浠cript鍦╤tml涓殑浣?#32515;?#28751;?#28051;?#38322;嶈?#20343;簡锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><script defer async=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> type=<span style="color: #800000;">"</span><span style="color: #800000;">text/javascript</span><span style="color: #800000;">"</span> src=<span style="color: #800000;">"</span><span style="color: #800000;">app/foo.js</span><span style="color: #800000;">"</span>></script> <script defer async=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> type=<span style="color: #800000;">"</span><span style="color: #800000;">text/javascript</span><span style="color: #800000;">"</span> src=<span style="color: #800000;">"</span><span style="color: #800000;">app/bar.js</span><span style="color: #800000;">"</span>></script></pre> <pre><script defer async=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> type=<span style="color: #800000;">"</span><span style="color: #800000;">text/javascript</span><span style="color: #800000;">"</span> src=<span style="color: #800000;">"</span><span style="color: #800000;">app/main.js</span><span style="color: #800000;">"</span>></script></pre> </div> <p>杩?#32457;?#37826;瑰紡涓?#38171;?#37813;鏈夊紓姝s鍦ㄦ墽琛岀殑鏃跺?#26473;?#37828;?#37816;夐『搴忔墽琛岀殑锛?#28051;嶇劧灏变細瀛?#37734;?#28186;濊禆闂?#26864;?#38171;?#23011;?#28641;?#28641;傛灉涓?#28186;?#28051;殑main.js渚濊禆foo.js?#25328;bar.js锛?浣嗘槸main.js鍏堟墽琛岀殑璇?#28751;变細鍑?#38335;欎簡銆傝?#30028;劧浠?#37833;?#37918;?#29825;?#28051;?#26473;?#32457;?#37826;瑰紡涔熺畻涓?#38335;欎簡锛?#28003;嗘槸涓?#28598;熷ソ锛屽洜涓?#28729;冪敤璧?#37833;?#23536;堢?#20346;悙锛岃?#28051;?#26473;?#37832;?#28051;?#38338;?#26864;?#28751;?#37828;?#26916;?#38344;?#38343;瑕佹坊鍔?#28598;?#28051;?script鏍?#29825;?#28000;?#37721;婃病鏈?#37716;?#23049;?#28729;屽叏?#20187;?#22476;鎸?#38343;鍔犺浇銆?/p> <p><strong>JS?#27537;鎸?#38343;鍔犺浇</strong>鍒?#28051;?#28051;?#28766;傛锛?#32471;?#28051;涓?#28766;傛鏄?#37721;?#37716;犺浇杩?#28051;?#26916;?#38344;?#37721;兘琚敤?#22476;?#27537;JS锛?#32471;?#27996;?#28051;?#28766;傛鏄?#37734;?#37721;?#37734;ㄧ敤?#22476;鏌?#28051;狫S?#27537;鏃?鍊?#37813;?#37720;?#37716;犺浇銆?#28028;?#32513;熷湴鏂瑰紡寰?#28729;?#37828;撳仛?#22476;绗?#28051;涓?#28766;傛锛?#28003;嗘槸涓?#28729;?#37828;撳仛?#22476;绗?#27996;?#28051;?#28766;傛锛岃?#30028;劧鎴?#28000;?#37721;?#28000;?#38315;?#26473;?#37722;?#39582;跺?#23677;?#23340;缉宸ュ叿锛?#28751;嗘煇涓?#26916;?#38344;?#37813;鏈夌殑JS閮芥坊鍔犲埌涓涓枃浠?#28051;?#37720;?#38171;?鏈澶?#32459;?#25652;?#37713;?#28751;戣?#21228;簮璇锋眰閲?#38171;?#28003;嗘槸杩?#28051;狫S璇锋眰?#22476;瀹?#37812;风浠?#37722;?#38171;屽叾涓?#37832;夊緢澶?#37712;呭鍙兘閮界敤涓?#28051;?#38171;岃?#20345;槸鏈?#28051;?#23480;ュ叿?#20824;澶熷仛?#22476;鍦?#38343;瑕佺殑鏃跺?#37813;?#37720;?#37716;犺浇鐩稿叧js灏卞?#23680;編瑙?#37712;?#38338;?#26864;??#31777;锛?#23011;?#28641;俁equireJS銆?/p> <div style="text-align: right; text-align: right;height: 0;padding: 0;overflow: hidden;visibility: hidden;"><a name="_label3"></a></div><h2>4. RequireJS甯哥敤?#25956;娉?#37804;?#32513;?/h2> <p>鍓嶆枃澶氭鎻愬強RequireJS锛?#37832;?#38318;?#37714;?#28751;嗗?#29808;?#20906;殑甯哥敤?#25956;娉?#29831;?#32513;?#29831;存槑锛屽?#20906;殑瀹?#37826;瑰?#26495;潃鏄?#38171;?a title="http://www.requirejs.cn/" href="http://www.requirejs.cn/">http://www.requirejs.cn/</a>锛?#28003;?#37721;?#28000;ュ埌璇?#37734;板潃鍘?#28051;嬭浇鏈鏂?#37911;圧equireJS?#26499;浠?#37510;俁equireJS浣?#28051;?#37929;?#37715;?#28003;跨敤鏈骞?#23049;涚殑AMD宸ュ叿锛屽?#20906;殑涓?#29781;佷?#27196;偣鏄?#38171;?/p> <ul><li>瀹屽叏鏀?#37816;佹ā?#28513;鍖?#23534;鍙?/li><li>?#20824;灏嗛潪AMD瑙勮?#20906;殑妯″潡寮?#37711;ュ埌RequireJS涓?#28003;跨敤</li><li>寮?#23005;?#37716;犺浇JS</li><li>瀹屽叏鎸?#38343;鍔犺浇渚濊禆妯″潡锛?#22959;″潡?#26499;浠跺彧闇瑕?#37720;嬬缉娣锋穯锛?#28051;?#38343;瑕?#37722;?#39582;?/li><li>閿?#29831;皟璇?/li><li>鎻?#28000;舵敮鎸?/li><li><br /></li></ul> <h3>4.01 濡?#28003;?#28003;跨敤RequireJS</h3> <p>浣跨敤鏂瑰紡寰堢畝鍗?#38171;屽彧瑕?#28051;涓猻cript鏍?#29825;?#28751;?#37721;?#28000;?#37734;?#32515;?#26916;?#28051;?#37716;犺浇RequireJS锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><script defer async=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> src=<span style="color: #800000;">"</span><span style="color: #800000;">/bower_components/requirejs/require.js</span><span style="color: #800000;">"</span>></script></pre> </div> <p>鐢?#27996;?#26473;?#38322;岀敤?#22476;?#31777;defer?#25328;async杩?#28051;?#28051;?#23534;?#23005;?#37716;犺浇?#27537;灞?#37804;?#38171;?#37813;浠equire.js鏄?#23534;?#23005;?#37716;犺浇?#27537;锛?#28003;犳妸杩?#28051;猻cript鏍?#29825;版斁缃?#37734;?#28000;?#28003;?#37734;?#37826;?#38318;芥病鏈夐棶棰?#37510;?/p> <p><strong>4.02 濡?#28003;?#37714;╃敤RequireJS鍔犺浇骞舵墽琛屽?#25779;墠缃?#26916;电殑閫?#26440;慗S</strong></p> <p>4.01瑙?#37712;崇殑浠?#28000;呮槸RequireJS?#27537;浣跨敤闂?#26864;?#38171;?#28003;嗗畠浠?#28000;呮槸涓涓狫S搴?#38171;?#37828;?#28051;涓?#29722;?#35120;撳?#23945;〉闈㈢殑閫?#26440;?#37813;鍒╃敤?#27537;宸ュ叿锛岀湡姝?#28729;炵幇缃?#26916;?#37716;熻兘閫?#26440;戠殑鏄?#37812;?#28000;?#29781;?鍒╃敤RequireJS缂?#37712;欑殑涓籎S锛?#26473;?#28051;?#28051;籎S?#32025;鍋?#29825;?#26473;欎?#28055;唬鐮侀兘?#25985;缃?#37734;╩ain.js?#26499;浠?#28051;?#38171;?#37721;?#29831;?#28641;?#28003;?#37714;╃敤RJ鏉?#37716;犺浇?#22717;琛屽憿锛?#37826;瑰紡濡?#28051;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><script data-main=<span style="color: #800000;">"scripts/main.js</span><span style="color: #800000;">" </span>defer async=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> src=<span style="color: #800000;">"</span><span style="color: #800000;">/bower_components/requirejs/require.js</span><span style="color: #800000;">"</span>></script></pre> </div> <p>瀵规瘮4.01锛?#28003;犱細鍙戠幇script鏍?#29825;?#28598;氫簡涓涓猟ata-main锛孯J?#25956;杩?#28051;厤缃?#35120;撳?#23945;〉闈㈢殑涓籎S锛?#28003;?#29781;佹妸閫?#26440;?#38318;?#37712;欏湪杩?#28051;猰ain.js閲岄潰銆?褰揜J?#22172;?#38889;鍔犺浇?#22717;琛屽悗锛?#28751;变細鍐嶆寮?#23005;?#37716;犺浇main.js銆?#26473;?#28051;猰ain.js鏄?#35120;撳墠缃?#26916;?#37813;鏈?#38315;?#26440;戠殑鍏?#37721;?#38171;岀?#21976;兂鎯?#37712;?#28051;?#38171;?#37825;?#28051;?#32515;?#26916;?#37721;?#38343;瑕?#26473;?#28051;涓猻cript鏍?璁?#38171;屽埄?#25956;RJ鍔犺浇渚濊?#26667;殑鍏跺?#20905;枃浠?#38171;屽?#20466;query绛?#37510;?/p> <h3> </h3> <h3>4.03 main.js鎬?#28052;?#37712;?/h3> <p>鍋?#29825;?#26916;?#37929;殑鐩?#35120;?#32513;?#37835;?#28051;?#38171;?/p> <p><a href="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233219237-874123725.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="clipboard_thumb" src="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233219674-790624569.png" alt="clipboard_thumb" border="0" width="287" height="326" /></a></p> <p>main.js鏄?#29882;熷?#25779;?#23945;〉闈?#37929;稿叧?#27537;涓籎S锛宎pp?#26499;浠跺す瀛樻斁鏈?#26916;?#37929;嚜瀹?#28052;夌殑妯″潡锛宭ib瀛樻斁绗?#28051;?#37826;瑰簱銆?/p> <p>html涓?#37816;?.02?#27537;鏂瑰紡?#21412;缃甊J銆俶ain.js?#27537;?#21804;鐮?#28641;?#28051;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>require(['lib/foo', 'app/bar', 'app/app'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(foo, bar, app) { </span><span style="color: #008000;">//</span><span style="color: #008000;">use foo bar app do sth</span> });</pre> </div> <p>鍦?#26473;欐?#31000;S涓?#38171;?#37812;?#28000;?#37714;╃敤RJ鎻?#28186;涚殑require鏂规硶锛?#37716;?#26462;戒簡涓?#28051;?#22959;″潡锛岀?#36346;悗鍦?#26473;?#28051;?#28051;?#28051;?#22959;″潡閮?#37716;犺浇?#22426;鍔?#28052;?#37722;庢墽琛岄〉闈?#38315;?#26440;?#37510;俽equire鏂规硶鏈?涓?鍙傛暟锛?#32471;?#28051;涓?#37721;傛暟鏄暟缁勭?#35826;?#23340;殑锛屽疄闄?#28003;跨敤鏃?#38171;屾暟缁勭殑姣?#28051;?#37711;冪礌閮?#37828;?#28051;涓?#22959;?#37735;楃殑module ID锛?#32471;?#27996;?#28051;?#37721;傛暟鏄?#28051;涓?#37733;炶皟鍑芥暟锛?#26473;?#28051;?#37713;芥暟鍦?#32471;?#28051;涓?#37721;傛暟瀹?#28052;夌殑鎵鏈?#22959;″潡閮?#37716;犺浇?#22426;鍔熷?#24225;?#28854;皟锛屽舰鍙傜殑涓暟?#25328;椤?#25652;?#37714;嗗埆涓?#32471;?#28051;涓?#37721;傛暟瀹?#28052;夌殑妯″潡瀵瑰簲锛?#23011;?#28641;?#32471;?#28051;涓?#22959;??#28513;鏃秎ib/foo锛岄偅涔?#26473;?#28051;?#37733;炶皟鍑芥暟?#27537;绗?#28051;涓?#37721;傛暟灏?#37828;痜oo杩?#28051;?#22959;?#37735;楃殑寮曠敤锛屽湪鍥炶皟鍑芥暟涓?#37812;?#28000;?#28003;跨敤杩欎簺褰?#37721;傛潵?#30367;?#25956;鍚?#28051;?#22959;?#37735;楃殑鏂规硶锛岀敱浜庡?#28854;皟鏄?#37734;?#37722;勬ā?#28513;鍔??#27975;涔?#37722;?#37813;嶈皟?#25956;?#27537;锛?#37813;浠?#26473;欎簺妯″潡寮曠敤?#20567;瀹?#38318;?#37828;?#37832;?#37825;堢殑銆?/p> <p>浠?#28000;?#28051;?#26473;?#28051;畝鐭殑?#21804;鐮?#38171;?#28003;?#25652;?#29831;?#23480;?#32513;忕煡閬?#29831;?#28641;?#28003;?#28003;跨敤RJ?#31777;銆?/p> <h3>4.04 RJ?#27537;baseUrl?#25328;module ID</h3> <p>鍦?#28000;?#32513;峈J濡?#28003;?#37720;?#29785;?#37835;?#28186;濊?#26667;殑閭?#27996;?#22959;″潡JS?#27537;璺?#23536;?#37827;?#38171;屽繀椤?#37711;?#23534;勬?#21614;?#27484;aseUrl?#25328;module ID杩?#28051;?#28051;?#22994;?#36423;?#37510;?/p> <p>html涓殑base鍏冪礌鍙?#28000;?#28729;?#28052;?#35120;撳?#23945;〉闈?#37712;?#38318;?#28000;?#28003;昲ttp璇锋眰?#27537;url鍓嶇紑閮?#37714;?#38171;孯J?#27537;baseUrl璺?#26473;?#28051;猙ase鍏冪礌璧风殑浣滅敤鏄?#32491;?#28028;肩殑锛岀敱浜?RJ鎬?#37828;?#37716;?#37804;?#37734;?#29831;锋眰渚濊?#26667;殑JS?#26499;浠?#38171;?#37813;浠?#36423;呯?#33333;秹鍙婂埌涓涓狫S?#26499;浠剁殑璺?#23536;勮В鏋愰棶棰?#38171;孯J?#31919;璁?#38322;囩敤涓绉峛aseUrl + moduleID?#27537;瑙?#37835;?#37826;瑰紡锛?#26473;?#28051;?#29785;?#37835;?#37826;瑰紡鍚?#32513;細涓?#28186;嬭?#23384;槑銆?#26473;?#28051;猙aseUrl闈炲父閲嶈锛孯J瀵瑰?#20906;殑澶?#37918;嗛伒寰?#28641;?#28051;嬭?#21227;垯锛?/p> <ul><li>鍦ㄦ病鏈?#28003;跨敤data-main?#25328;config?#27537;鎯?#37712;?#28051;?#38171;宐aseUrl?#31919;璁?#28051;?#35120;撳?#23945;〉闈㈢殑鐩?#35120;?/li><li>鍦?#37832;塪ata-main?#27537;鎯?#37712;?#28051;?#38171;宮ain.js鍓?#38344;㈢殑閮?#37714;?#28751;?#37828;痓aseUrl锛?#23011;?#28641;?#28051;?#38344;㈢殑scripts/</li><li>鍦?#37832;塩onfig?#27537;鎯?#37712;?#28051;?#38171;宐aseUrl浠onfig?#21412;缃殑涓?#37713;?/li></ul> <p>涓?#26473;?#28051;夌鏂瑰紡锛?#28028;?#37711;?#32510;?#37922;?#28003;庡埌楂?#37818;?#37714;?#37510;?/p> <p>data-main?#27537;浣跨敤鏂瑰紡锛?#28003;?#23480;?#32513;忕煡閬撲簡锛宑onfig璇?#28641;?#28003;曢厤缃?#38171;屽涓?#37813;绀?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><span style="color: #000000;">require.config({ baseUrl: </span><span style="color: #800000;">'</span><span style="color: #800000;">scripts</span><span style="color: #800000;">'</span><span style="color: #000000;"> });</span></pre> </div> <p>杩?#28051;厤缃?#36423;呴?#32472;斁缃?#37734;╩ain.js?#27537;鏈鍓?#38344;?#37510;俤ata-main涓巆onfig?#21412;缃?#37722;?#37827;?#28699;?#37734;ㄧ殑鏃跺?#38171;?#28000;onfig涓?#37713;?#38171;岀敱浜嶳J?#27537;鍏跺畠?#21412;缃?#28052;熸槸鍦?#26473;?#28051;?#28003;?#32515;厤缃殑锛?#37813;浠?.03涓殑main.js鍙?#28000;ユ敼?#22426;濡?#28051;?#32513;?#37835;?#38171;?#28000;?#28186;?#28751;嗘潵?#27537;鎵?#28766;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts'<span style="color: #000000;"> }); require([</span>'lib/foo', 'app/bar', 'app/app'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(foo, bar, app) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> use foo bar app do sth</span> });</pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <h3> </h3> <p>?#21479;浜巑odule ID锛?#28751;?#37828;?#37734;╮equire鏂规硶浠?#37721;?#37722;?#32513;殑define鏂规硶閲?#38171;岀敤鍦?#28186;濊禆?#26271;缁?#26473;?#28051;?#37721;傛暟閲?#38171;岀敤鏉?#37837;?#29831;?#28051;涓?#22959;?#37735;楃殑瀛楃涓?#37510;?#28051;?#38344;唬鐮?#28051;殑['lib/foo', 'app/bar', 'app/app']灏?#37828;?#28051;涓?#28186;濊禆?#26271;缁?#38171;屽叾涓殑姣?#28051;?#37711;冪礌閮?#37828;?#28051;涓猰odule ID銆傚煎緱娉?#37808;忕殑鏄?#38171;宮odule ID骞?#28051;?#28051;瀹?#37828;?#29831;odule 鐩稿叧JS璺?#23536;勭殑涓閮?#37714;?#38171;?#37832;夌殑module ID寰堢煭锛?#28003;嗗彲?#20824;璺?#23536;勫緢闀?#38171;?#26473;?#29882;烺J?#27537;瑙?#37835;?#29785;勫垯鏈夊叧銆?#28051;?#28051;鑺?#29831;?#32513;?#28000;?#32513;?#37510;?/p> <h3>4.05 RJ?#27537;?#26499;浠?#29785;?#37835;?#29785;勫垯</h3> <p>RJ?#31919;璁?#37816;塨aseUrl + module ID?#27537;瑙勫垯锛岃В鏋愭枃浠?#38171;屽苟涓?#28729;冮粯璁?#29781;?#37716;犺浇?#27537;?#26499;浠?#38318;?#37828;痡s锛?#37813;浠?#28003;犵殑module ID閲岄潰鍙?#28000;?#28051;嶅寘?#24776;.js?#27537;鍚?#32514;锛?#26473;?#28751;?#37828;?#28051;哄暐浣?#37930;嬪埌?#27537;module ID閮?#37828;痩ib/foo, app/bar杩?#32457;?#35120;㈠紡?#31777;銆?#37832;?#28051;夌module ID锛?#28051;?#38315;傜敤杩?#32457;嶈?#21227;垯锛?/p> <ul><li>浠?寮澶?#38171;屽/lib/jquey.js</li><li>浠?js缁?#28751;?#38171;屽test.js</li><li>?#23512;?#24776;http鎴杊ttps锛屽<a href="http://cdn.baidu.com/js/jquery.js">http://cdn.baidu.com/js/jquery.js</a></li></ul> <p>鍋囧?#20470;ain.js濡?#28051;?#28003;跨敤锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts'<span style="color: #000000;"> }); require([</span>'/lib/foo', 'test.js', 'http://cdn.baidu.com/js/jquery'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(foo, bar, app) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> use foo bar app do sth</span> });</pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>杩?#28051;?#28051;猰odule 閮?#28051;嶄細鏍?#37817;産aseUrl + module ID?#27537;瑙勫垯鏉?#29785;?#37835;?鑰?#37828;?#37929;存帴?#25956;module ID鏉?#29785;?#37835;?#38171;岀瓑鏁?#27996;?#28051;?#38344;㈢殑?#21804;鐮?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><span style="color: #0000ff;"><</span><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="/lib/foo.js"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="test.js"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://cdn.baidu.com/js/jquery.js"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span></pre> </div> <p><strong>鍚勭module ID瑙?#37835;?#28051;?#28186;?#38171;?/strong></p> <p>渚?锛岄」鐩?#32513;?#37835;勫涓?#38171;?/p> <p><a href="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233220003-2032273647.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="clipboard4_thumb" src="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233220331-2035716683.png" alt="clipboard4_thumb" border="0" width="287" height="326" /></a></p> <p>main.js濡?#28051;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts'<span style="color: #000000;"> }); require([</span>'lib/foo', 'app/bar', 'app/app'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(foo, bar, app) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> use foo bar app do sth</span> });</pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>baseUrl涓?#38171;歴cripts鐩?#35120;?/p> <p>moduleID涓?#38171;歭ib/foo, app/bar, app/app</p> <p>鏍?#37817;産aseUrl + moduleID锛?#28000;?#37721;婅嚜鍔?#29723;?#37722;?#32514;.js锛?#37832;缁?#26473;?#28051;?#28051;猰odule?#27537;js?#26499;浠?#29882;?#23536;?#28051;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><span style="color: #000000;">scripts/lib/foo.js scripts/app/bar.js scripts/app/app.js</span></pre> </div> <p>渚?锛岄」鐩?#32513;?#37835;勫悓渚?锛?/p> <p>main.js?#25980;涓?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts/lib'<span style="color: #000000;">, paths: { app: </span>'../app'<span style="color: #000000;"> } }); require([</span>'foo', 'app/bar', 'app/app'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(foo, bar, app) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> use foo bar app do sth</span> });</pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>杩?#38322;屽嚭鐜颁簡涓涓?#37826;扮殑?#21412;缃畃aths锛屽?#20906;殑浣滅敤鏄?#38333;?#28725;筸odule ID涓?#37911;瑰?#27697;殑閮?#37714;?#38171;?#26473;?#29723;岃浆涔?#38171;屽浠?#28051;婁唬鐮?#28051;?#28725;筧pp杩?#28051;?#38318;?#37714;?#38171;岃浆涔?#28051;?./app锛?#26473;?#29723;?#32448;?#28051;涓?#37929;?#28725;硅矾寰?#38171;岀浉瀵?#28003;?#32515;?#37828;痓aseUrl鎵?#23514;瀹氱殑鐩?#35120;?#38171;岀敱椤?#37929;?#32513;?鏋勫彲?#29025;锛?./app鍏跺疄瀵瑰簲?#27537;鏄痵cirpt/app鐩?#35120;曘?#23005;?#37733;?#28051;?#37832;?#26473;?#28051;浆涔夌殑瀛?#37734;?#38171;?#37813;浠?#28000;?#28051;婁唬鐮?#28051;殑app/bar鎵嶈兘琚?#23005;?#32429;?#29785;?#37835;?#38171;屽惁鍒?#26473;?#37816;?baseUrl + moduleID?#27537;瑙勫垯锛宎pp/bar涓嶆槸搴?#29831;?#29722;?#29785;?#37835;愭垚scripts/lib/app/bar.js鍚?#38171;?#28003;嗗疄闄?#39582;?#38344;炲姝?#38171;宎pp/bar琚?#29785;?#37835;愭垚 scripts/app/bar.js锛屽叾涓?#29863;峰叧閿?#28003;滅敤?#27537;灏?#37828;痯aths?#27537;?#21412;缃?#37510;?#38315;?#26473;?#26473;?#28051;?#28051;?#28186;?#38171;屽彲浠?#37930;?#37713;簃odule ID骞?#28051;?#28051;瀹?#37828;痡s?#26499;浠?#29882;?#23536;?#28051;殑涓閮?#37714;?#38171;宲aths?#27537;?#21412;缃?#28725;?#27996;?#29882;?#23536;?#26473;囩?#23340;殑js鐗?#37714;?#37832;?#37825;?#38171;屽洜涓?#37721;?#28000;ョ畝鍖?#28729;冪殑module ID銆?/p> <p>鍙?#28598;?#32471;?#28051;涓?#22959;?#37735;楃殑ID涓篺oo锛屽悓鏃舵病鏈塸aths?#27537;?#27974;涔?#38171;?#37813;浠?#37837;?#37817;?#29785;?#37835;?#29785;勫垯锛屽?#20906;殑?#26499;浠?#29882;?#23536;?#37827;?#38171;歴cripts/lib/foo.js銆?/p> <p>paths?#27537;?#21412;缃?#28051;?#37721;?#37832;?#35120;?#22959;″潡浣?#27996;巄aseUrl鎵?#23514;瀹氱殑?#26499;浠跺?#22317;殑鍚屽眰鐩?#35120;?#38171;?#37812;?#38000;呮洿涓婂眰?#27537;鐩?#35120;?#37827;?#38171;?#37813;嶄細?#25956;?#22476;../杩?#32457;?#37929;?#28725;硅矾寰?#37510;?/p> <p>渚?锛岄」鐩?#32513;?#37835;滃悓渚?锛宮ain.js鍚?#28186;?锛?/p> <p>杩?#38322;岃璇存槑?#27537;闂?#26864;?#32459;嶅井鐗规畩锛?#28051;?#28000;ain.js涓轰緥锛岃?#28000;pp.js涓轰緥锛?#28051;攁pp渚濊禆bar锛屽綋鐒禼onfig杩?#37828;?#38343;瑕?#37734;╩ain.js涓?#28729;?#28052;夌殑锛岀敱浜?#26473;?#28051;?#38338;?#26864;?#37734;?#28729;?#28052;?#22959;?#37735;楃殑鏃跺欐洿鍔?#29999;?#29785;?#38171;?#37813;浠ョ敤define鏉?#28051;?#28186;?#38171;屽亣璁綼pp.js妯″潡濡?#28051;?#28729;?#28052;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre>define(['./bar'], function(bar) { <span style="color: #0000ff;">return</span><span style="color: #000000;"> { doSth: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">() { bar.doSth(); } } });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>涓?#38344;㈢殑?#21804;鐮侀?#26473;嘾efine瀹?#28052;変簡涓涓?#22959;″潡锛?#26473;?#28051;猟efine鍑芥暟鍚?#38344;?#28000;?#32513;嶅浣?#28729;?#28052;?#22959;?#37735;楃殑鏃跺?#37712;嶆潵浠?#32513;?#38171;?#26473;?#38322;岀畝鍗曚簡瑙?#37510;?#26473;?#38322;?#26473;?#32457;嶇敤娉曠殑绗?#28051;涓?#37721;傛暟璺?require鍑芥暟涓鏍?#38171;?#37828;?#28051;涓?#28186;濊禆?#26271;缁?#38171;?#32471;?#27996;?#28051;?#37721;傛暟鏄?#28051;涓?#37733;炶皟锛?#28052;熸槸鍦?#37813;鏈?#28186;濊禆鍔犺浇?#22426;鍔?#28052;?#37722;庤皟?#25956;锛?#26473;?#28051;?#37733;炶皟?#27537;杩?#37733;炲间細?#22426;涓?#26473;?#28051;?#22959;?#37735;楃殑寮曠敤琚?#37711;跺畠妯″潡鎵浣??#25956;銆?/p> <p>杩?#38322;岃璇寸殑闂?#26864;?#26473;?#37828;?#29882;?#29785;?#37835;?#29785;勫垯鐩稿叧?#27537;锛屽?#20635;?#28355;?#23677;叏閬?#28729;圧J?#27537;瑙?#37835;?#29785;勫垯锛?#26473;?#38322;岀殑渚濊禆搴?#29831;ラ厤缃垚app/bar鎵嶆槸姝?#32429;殑锛?#28003;?#37922;?#27996;巃pp.js涓?bar.js浣?#27996;?#37722;?#28051;涓?#37929;?#35120;?#38171;?#37813;浠?#28729;屽叏鍙?#37714;╃敤./杩?#28051;?#37722;岀洰褰曠殑鐩?#28725;?#37837;?#29831;?#32471;?#37833;?#29785;?#37835;恓s锛?#26473;?#37837;风殑璇?#37721;?#29781;乤pp.js宸?#32513;?#37716;犺浇?#22426;鍔熶簡锛岄偅涔?#37720;?#37722;岀洰褰?#28051;?#37813;?bar.js灏辫偗瀹氳兘鎵惧埌?#31777;銆?#26473;?#32457;嶉厤缃?#37734;?#28729;?#28052;?#22959;?#37735;楃殑鏃跺欓?#28850;父鏈夋?#24526;箟锛?#26473;?#37837;?#28003;犵殑妯″潡灏?#28051;?#28186;濊禆浜庢斁缃?#26473;欎簺妯?#37735;楃殑?#26499;浠跺?#29808;?#23943;?#39041;簡銆?/p> <h3>4.06 RJ?#27537;寮?#23005;?#37716;犺浇</h3> <p>RJ涓嶇鏄痳equire鏂规硶杩?#37828;痙efine鏂规硶?#27537;渚濊禆妯″潡閮?#37828;?#23534;?#23005;?#37716;犺浇?#27537;锛?#37813;浠?#28051;?#38344;㈢殑?#21804;鐮?#28051;?#28051;瀹氳兘瑙?#37835;愬埌姝?#32429;殑JS?#26499;浠?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><script data-main="scripts/main" src="scripts/require.js"></script> <script src="scripts/other.js"></script></pre> </div> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><span style="color: #000000;">//main.js<br />require.config({ paths: { foo: </span>'libs/foo-1.1.3'<span style="color: #000000;"> } });</span></pre> </div> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>//other.js<br />require( ['foo'], <span style="color: #0000ff;">function</span><span style="color: #000000;">( foo ) { </span><span style="color: #008000;">//</span><span style="color: #008000;">foo is undefined</span> });</pre> </div> <p>鐢?#27996;巑ain.js鏄?#23534;?#23005;?#37716;犺浇?#27537;锛?#37813;浠ther.js?#32048;姣?#28729;?#37711;?#37716;犺浇锛?#28003;嗘槸RJ?#27537;?#21412;缃?#28699;?#37734;?#27996;巑ain.js閲岄潰锛?#37813;浠?#37734;?#37716;犺浇other.js璇?#28051;嶅埌RJ?#27537;?#21412;缃?#38171;屽湪other.js?#22717;琛岀殑鏃跺?#29785;?#37835;愬嚭鏉ョ殑foo?#27537;璺?#23536;?#28751;变細鍙樻垚scripts/foo.js锛岃?#23005;?#32429;?#29882;?#23536;勫簲璇?#37828;痵cripts/libs/foo-1.1.3.js銆?/p> <p>灏界RJ?#27537;渚濊禆鏄?#23534;?#23005;?#37716;犺浇?#27537;锛?#28003;嗘槸宸?#37716;犺浇?#27537;妯″潡鍦?#28598;氭渚濊?#26667;殑鏃跺?#38171;?#28051;嶄細鍐?#38322;?#37826;?#37716;犺浇锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>define(['require', 'app/bar', 'app/app'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(require) { </span><span style="color: #0000ff;">var</span> bar= require("app/bar"<span style="color: #000000;">); </span><span style="color: #0000ff;">var</span> app= require("app/app"<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">use bar and app do sth</span> });</pre> </div> <p>涓?#38344;㈢殑?#21804;鐮?#38171;屽湪callback瀹?#28052;夌殑鏃跺?#38171;屽彧?#25956;?#31777;涓涓?#35120;?#37721;?#38171;?#26473;?#28051;?#29781;佹槸涓轰?#21975;噺灏?#35120;?#37721;傜殑?#26271;閲?#38171;岄?#22446;厤鏁?#28051;?#37733;炶皟?#27537;绛?#37722;嶅緢闀?#37510;?#28186;濊?#26667;殑妯″潡鍦?#37733;炶皟鍐?#38318;?#37721;?#28000;?#37929;存帴?#25956;require(moduleID)?#27537;鍙傛暟寰楀埌锛岀敱浜?#37734;?#37733;炶皟?#22717;琛屽墠锛?#28186;濊?#26667;殑妯″潡宸?#32513;?#37716;犺浇锛?#37813;浠?#23005;?#28598;勮皟?#25956;涓嶄細鍐?#38322;?#37826;?#37716;犺浇銆?#28003;嗘槸濡傛灉姝?#28598;勮?#23792;彇涓涓?#39582;?#28051;?#37734;?#28186;濊禆?#26271;缁?#28051;?#37713;虹幇?#27537;module ID锛宺equire寰?#37832;?#37721;兘鑾峰彇涓嶅埌璇?#22959;″潡寮曠敤锛屽洜涓?#28729;?#37721;兘闇瑕?#38322;?#37826;?#37716;犺浇锛屽?#20635;?#28355;?#20905;病鏈夊湪鍏跺畠妯″潡涓?#29722;?#37716;犺浇杩囩殑璇?#37510;?/p> <h3>4.07 RJ瀹?#37826;规帹鑽愮殑JS?#26499;浠?#32513;?#32513;?#32513;?#37835;?/h3> <p>RJ寤?#29825;?#38171;屾枃浠?#32513;?#32513;?#28751;?#38322;?#37813;?#39582;?#38171;?#28051;嶈澶氬眰宓屽锛?#37832;鐞嗘兂?#27537;鏄?#29882;熼」鐩?#37929;稿叧?#27537;?#25985;鍦?#28051;涓枃浠跺す锛?#32471;?#28051;?#37826;瑰簱?#25985;鍦?#28051;涓枃浠跺す锛屽涓?#37813;绀?#38171;?/p> <p><a href="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233220643-21928324.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image_thumb" src="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233220971-668031612.png" alt="image_thumb" border="0" width="269" height="191" /></a></p> <h3>4.08 浣跨敤define瀹?#28052;?#22959;″潡</h3> <p>AMD瑙勫?#27697;殑妯″潡瀹?#28052;?#29785;勮寖涓?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>define(id?, dependencies?<span style="color: #000000;">, factory); 鍏?#28051;?#38171;?id: 妯″潡鏍?#29831;?#38171;屽彲浠?#37930;佺暐銆?dependencies: 鎵渚濊?#26667;殑妯″潡锛屽彲浠?#37930;佺暐銆?factory: 妯?#37735;楃殑瀹炵幇锛?#37812;?#38000;?#28051;涓狫avaScript瀵?#29854;?/span></pre> </div> <p>?#21479;浜?#32471;?#28051;涓?#37721;傛暟锛?#37832;枃涓嶄細?#31225;鍙?#38171;屽洜涓篟J寤?#29825;?#37813;鏈?#22959;″潡閮?#28051;嶈浣跨敤绗?#28051;涓?#37721;傛暟锛屽?#20635;灉浣跨敤绗?#28051;涓?#37721;傛暟瀹?#28052;夌殑妯″潡?#22426;涓?#37723;?#37722;嶆ā?#28513;锛?#28051;?#38315;傜敤绗?#28051;涓?#37721;傛暟?#27537;妯″潡?#22426;涓?#37718;?#37722;嶆ā?#28513;锛屽懡鍚嶆ā?#28513;濡傛?#28356;洿鍚?#38171;?#37813;鏈?#28186;濊禆瀹冪殑妯″潡閮藉緱淇敼锛?#32471;?#27996;?#28051;?#37721;傛暟鏄?#28186;濊禆?#26271;缁?#38171;岃窡require涓鏍?#38171;屽?#20635;?#28356;病鏈?#26473;?#28051;?#37721;傛暟锛岄偅涔?#28729;?#28052;夌殑灏?#37828;?#28051;涓?#37827;?#28186;濊?#26667;殑妯″潡锛?#37832;鍚?#28051;涓?#37721;傛暟鏄?#37733;炶皟鎴?#38000;呮槸涓涓畝鍗?#28725;?#29854;?#38171;屽湪妯″潡鍔犺浇瀹?#23011;?#37722;庤皟?#25956;锛屽綋鐒舵病鏈?#32471;?#27996;?#28051;?#37721;傛暟锛?#37832;鍚?#28051;涓?#37721;傛暟涔熶細?#30367;?#25956;銆?/p> <p>鏈?#38318;?#37714;?#37813;涓?#28186;?#38318;?#38322;囩敤濡?#28051;嬮」鐩?#32513;?#37835;?#38171;?/p> <p><a href="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233221284-525813259.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="clipboard1_thumb" src="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233221534-1421083426.png" alt="clipboard1_thumb" border="0" width="190" height="190" /></a></p> <p>1. 瀹?#28052;夌畝鍗?#28725;?#29854;?#22959;″潡锛?/p> <p>app/bar.js</p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre><span style="color: #000000;">define({ bar:</span>'I am bar.'<span style="color: #000000;"> });</span></pre> </div> <p>鍒╃敤main.js娴嬭瘯锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts/lib'<span style="color: #000000;">, paths: { app: </span>'../app'<span style="color: #000000;"> } }); require([</span>'app/bar'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(bar) { console.log(bar);</span><span style="color: #008000;">//</span><span style="color: #008000;"> {bar: 'I am bar.'}</span> });</pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>2. 瀹?#28052;?#37827;?#28186;濊?#26667;殑妯″潡锛?/p> <p>app/nodec.js锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>define(<span style="color: #0000ff;">function</span><span style="color: #000000;"> () { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> { nodec: </span>"yes, I don't need dependence."<span style="color: #000000;"> } });</span></pre> </div> <p>鍒╃敤main.js娴嬭瘯锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts/lib'<span style="color: #000000;">, paths: { app: </span>'../app'<span style="color: #000000;"> } }); require([</span>'app/nodec'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(nodec) { console.log(nodec);</span><span style="color: #008000;">//</span><span style="color: #008000;"> {nodec: yes, I don't need dependence.'}</span> });</pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>3. 瀹?#28052;?#28186;濊禆鍏跺畠妯?#37735;楃殑妯″潡锛?/p> <p>app/dec.js锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>define(['jquery'], <span style="color: #0000ff;">function</span><span style="color: #000000;">($){ </span><span style="color: #008000;">//</span><span style="color: #008000;">use $ do sth ...</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> { useJq: </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> } });</span></pre> </div> <p>鍒╃敤main.js娴嬭瘯锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts/lib'<span style="color: #000000;">, paths: { app: </span>'../app'<span style="color: #000000;"> } }); require([</span>'app/dec'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(dec) { console.log(dec);//{useJq: true} });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>4. 寰?#37916;?#28186;濊禆锛?/p> <p>褰?#28051;涓?#22959;″潡foo?#27537;渚濊禆?#26271;缁?#28051;?#28699;?#37734;╞ar锛宐ar妯?#37735;楃殑渚濊禆?#26271;缁?#28051;?#28699;?#37734;╢oo锛?#28751;变細褰㈡垚寰?#37916;?#28186;濊禆锛岀?#23941;井淇敼涓媌ar.js?#25328;foo.js濡?#28051;?#37510;?/p> <p>app/bar.js锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre>define(['foo'],<span style="color: #0000ff;">function</span><span style="color: #000000;">(foo){ </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> { name: </span>'bar'<span style="color: #000000;">, hi: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){ console.log(</span>'Hi! ' +<span style="color: #000000;"> foo.name); } } });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>lib/foo.js锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre>define(['app/bar'],<span style="color: #0000ff;">function</span><span style="color: #000000;">(bar){ </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> { name: </span>'foo'<span style="color: #000000;">, hi: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){ console.log(</span>'Hi! ' +<span style="color: #000000;"> bar.name); } } });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>鍒╃敤main.js娴嬭瘯锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts/lib'<span style="color: #000000;">, paths: { app: </span>'../app'<span style="color: #000000;"> } }); require([</span>'app/bar', 'foo'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(bar, foo) { bar.hi(); foo.hi(); });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>杩?#29723;?#32513;?#37835;?#38171;?/p> <p><a href="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233221846-1627729866.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="clipboard3_thumb1" src="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233222159-358112935.png" alt="clipboard3_thumb1" border="0" width="523" height="43" /></a></p> <p>濡傛?#28356;敼鍙榤ain.js涓璻equire閮?#37714;嗙殑渚濊禆椤?#25652;?#38171;?#32513;?#37835;?#38171;?/p> <p><a href="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233222596-1942446542.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="clipboard5_thumb1" src="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233223018-1645668833.png" alt="clipboard5_thumb1" border="0" width="518" height="41" /></a></p> <p>寰?#37916;?#28186;濊禆瀵?#38007;?#28051;?#28051;?#28186;濊?#26667;殑module涔?#38338;?#38171;屽缁堜細鏈?#28051;涓?#37734;?#38014;峰彇鍙?#28051;涓殑鏃跺?#38171;屽緱?#22476;undefined銆?#29785;?#37712;?#37826;规硶鏄?#38171;屽湪瀹?#28052;塵odule?#27537;鏃跺?#38171;屽?#20635;灉?#25956;?#22476;寰?#37916;?#28186;濊?#26667;殑鏃跺?#38171;屽湪define鍐?#38318;?#38315;?#26473;噐equire閲?#37826;?#38014;峰彇銆俶ain.js涓?#37721;?#38171;宐ar.js?#25980;?#22426;锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre>define(['require', 'foo'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(require, foo) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> { name: </span>'bar'<span style="color: #000000;">, hi: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">() { foo </span>= require('foo'<span style="color: #000000;">); console.log(</span>'Hi! ' +<span style="color: #000000;"> foo.name); } } });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>foo.js?#25980;?#22426;锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre>define(['require', 'app/bar'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(require, bar) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> { name: </span>'foo'<span style="color: #000000;">, hi: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">() { bar </span>= require('app/bar'<span style="color: #000000;">); console.log(</span>'Hi! ' +<span style="color: #000000;"> bar.name); } } });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>鍒╃敤涓?#26473;颁唬鐮?#38171;?#38322;?#37826;版墽琛?#38171;?#32513;?#37835;?#37828;?#38171;?/p> <p><a href="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233223362-257149020.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="clipboard7_thumb" src="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233223612-1459133655.png" alt="clipboard7_thumb" border="0" width="202" height="43" /></a></p> <p><strong>妯″潡瀹?#28052;?#37804;?#32513;?#38171;?/strong>涓嶇妯″潡鏄敤鍥炶皟鍑芥暟瀹?#28052;?#26473;?#37828;畝鍗?#28725;?#29854;?#28729;?#28052;?#38171;?#26473;?#28051;?#22959;″潡杈撳嚭?#27537;鏄?#28051;涓?#23534;曠敤锛?#37813;浠?#26473;?#28051;?#23534;曠敤蹇呴』鏄?#37832;?#37825;堢殑锛?#28003;犵殑鍥炶皟涓嶈兘杩?#37733;瀠ndefined锛?#28003;嗘槸涓嶅眬闄?#27996;?#28725;?#29854;?#32491;诲瀷锛?#26473;?#37721;?#28000;?#37828;暟缁?#38171;屽?#33445;暟锛岀敋鑷?#37828;?#37737;?#37832;?#32491;诲瀷锛屽彧涓?#26473;囧?#20635;灉杩?#37733;?#28725;?#29854;?#38171;?#28003;犺兘閫?#26473;?#26473;?#28051;?#28725;?#29854;?#32513;?#32513;囨洿澶氱殑鎺?#37721;?#37510;?/p> <h3>4.09 鍐?#32515;殑RJ妯″潡</h3> <p>鍐嶇湅鐪?#26473;?#28051;唬鐮?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre>define(['require', 'app/bar'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(require) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> { name: </span>'foo'<span style="color: #000000;">, hi: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">() { <span style="color: #0000ff;">var </span>bar </span>= require('app/bar'<span style="color: #000000;">); console.log(</span>'Hi! ' +<span style="color: #000000;"> bar.name); } } });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>渚濊禆?#26271;缁?#28051;殑require杩?#28051;猰oduleID瀵瑰簲?#27537;鏄?#28051;涓?#37712;?#32515;?#22959;″潡锛屽埄?#25956;瀹?#37716;犺浇妯″潡锛?#37804;?#28052;堢敤浣?#23480;?#32513;忕湅?#22476;?#31777;锛?#23011;?#28641;?#37734;╩ain.js涓?#38171;屽湪define涓?#37510;?#37721;?#28598;?#28051;涓?#37712;?#32515;?#22959;″潡鏄痬odule锛?#26473;?#28051;?#22959;″潡璺烺J?#27537;鍙?#28598;?#28051;涓厤缃?#37832;夊叧锛屽叿浣撶敤娉?#29831;?#37734;?#32471;?澶?#38318;?#37714;嗗?#35763;簡瑙?#37510;?/p> <h3>4.10 鍏跺畠RJ鏈夌敤鍔熻兘</h3> <p>1. ?#25939;?#22426;鐩?#28725;?#27996;?#22959;?#37735;楃殑URL鍦板潃</p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>define(["require"], <span style="color: #0000ff;">function</span><span style="color: #000000;">(require) { </span><span style="color: #0000ff;">var</span> cssUrl = require.toUrl("./style.css"<span style="color: #000000;">); });</span></pre> </div> <p>杩?#28051;?#37716;熻兘鍦?#28003;?#37807;?#29781;?#37716;?#37804;?#37734;?#37716;犺浇涓浜涙枃浠剁殑鏃跺?#37832;夌敤锛?#23049;ㄦ剰瑕?#28003;跨敤鐩?#28725;硅矾寰?#37510;?/p> <p>2. 鎺?#37714;跺?#25292;皟璇?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>require("module/name").callSomeFunction()</pre> </div> <p>鍋囧浣?#37807;?#37734;?#37818;?#37714;跺彴涓?#37836;?#37930;?#37836;?#28051;?#22959;″潡閮?#37832;夊摢浜?#37826;规硶鍙?#28000;ヨ皟?#25956;锛屽?#20635;灉杩?#28051;?#22959;″潡宸?#32513;?#37734;?#26916;?#38344;?#37716;犺浇?#27537;鏃跺欓?#26473;囦?#28618;禆琚?#37716;犺浇杩?#37722;?#38171;岄偅涔?#28751;?#37721;?#28000;ョ敤浠?#28051;婁唬鐮?#37734;?#37818;?#37714;跺彴涓仛鍚勭?#23942;?#23341;?#26330;簡銆?/p> <div style="text-align: right; text-align: right;height: 0;padding: 0;overflow: hidden;visibility: hidden;"><a name="_label4"></a></div><h2>5. RequireJS甯哥敤?#21412;缃?#37804;?#32513;?/h2> <p>鍦≧J?#27537;?#21412;缃?#28051;?#38171;屽墠闈?#23480;?#32513;?#37818;?#29785;﹀埌?#31777;baseUrl锛宲aths锛屽彟澶?#37713;?#28051;?#29999;哥敤?#27537;?#21412;缃?#37828;?#38171;?/p> <ul><li>shim</li><li>config</li><li>enforceDefine</li><li>urlArgs</li></ul> <h3>5.01 shim</h3> <p>涓?#38317;?#27996;涙病鏈?#28003;跨敤define()鏉?#28601;?#37828;?#28186;濊禆?#21479;?#37108;銆?#29825;?#32515;?#22959;?#37735;楃殑"娴?#29785;?#37731;?#37711;?#28766;鍙?#38322;?#23049;?#37711;?鍨嬭剼鏈仛渚濊禆鍜屽?#29006;嚭?#21412;缃?#37510;?/p> <p>渚?锛氬埄?#25956;exports灏嗘ā鍧楃殑鍏?#28766;鍙?#38322;?#23534;曠敤涓嶳equireJS?#21479;鑱?/p> <p>椤?#37929;?#32513;?#37835;勫鍥?#38171;?/p> <p><a href="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233223956-1171293057.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="clipboard3_thumb" src="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233224284-1258279487.png" alt="clipboard3_thumb" border="0" width="189" height="215" /></a></p> <p>main.js濡?#28051;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts/lib'<span style="color: #000000;">, paths: { app: </span>'../app'<span style="color: #000000;"> }, shim: { underscore: { exports: </span>'_'<span style="color: #000000;"> } } }); require([</span>'underscore'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(_) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> ?#24135;鍦?#37721;?#28000;?#38315;?#26473;嘷?#30367;?#25956;underscore?#27537;api?#31777;</span> });</pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>濡?#28003;?#37813;瑙?#38171;孯J鍦╯him涓?#23075;?#37716;犱簡涓涓?#28725;箄nderscore杩?#28051;?#22959;?#37735;楃殑?#21412;缃?#38171;屽苟閫?#26473;噀xports灞?#37804;ф寚瀹?#29831;?#22959;″潡鏆?#38343;茬殑鍏?#28766;鍙?#38322;?#38171;?#28000;?#28186;縍J?#20824;澶熷杩欎簺妯?#37735;楃粺涓绠?#37918;?#37510;?/p> <p>渚?锛氬埄?#25956;deps?#21412;缃甹s妯?#37735;楃殑渚濊禆</p> <p>椤?#37929;?#32513;?#37835;勫鍥?#38171;?/p> <p><a href="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233224581-1312226297.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="clipboard5_thumb" src="http://images2015.cnblogs.com/blog/459873/201510/459873-20151009233224940-550914652.png" alt="clipboard5_thumb" border="0" width="199" height="233" /></a></p> <p>main.js濡?#28051;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ baseUrl: </span>'scripts/lib'<span style="color: #000000;">, paths: { app: </span>'../app'<span style="color: #000000;"> }, shim: { backbone: { deps: [</span>'underscore', 'jquery'<span style="color: #000000;">], exports: </span>'Backbone'<span style="color: #000000;"> } } }); require([</span>'backbone'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(Backbone) { //use Backbone's API });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>鐢?#27996;巄ackbone杩?#28051;?#32513;?#28000;?#28186;濊禆jquery?#25328;underscore锛?#37813;浠?#37721;?#28000;?#38315;?#26473;嘾eps灞?#37804;ч厤缃?#28729;冪殑渚濊禆锛?#26473;?#37837;穊ackbone灏?#28028;氬湪鍙?#28598;?#28051;?#28051;?#22959;″潡鍔犺浇瀹?#23011;?#28052;?#37722;?#37813;嶄細鍔犺浇銆?/p> <p>渚?锛歫query绛夊簱鎻?#28000;堕厤缃?#37826;规硶</p> <p>?#21804;鐮?#28051;?#28186;?#28641;?#28051;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">requirejs.config({ shim: { </span>'jquery.colorize'<span style="color: #000000;">: { deps: [</span>'jquery'<span style="color: #000000;">], exports: </span>'jQuery.fn.colorize'<span style="color: #000000;"> }, </span>'jquery.scroll'<span style="color: #000000;">: { deps: [</span>'jquery'<span style="color: #000000;">], exports: </span>'jQuery.fn.scroll'<span style="color: #000000;"> }, </span>'backbone.layoutmanager'<span style="color: #000000;">: { deps: [</span>'backbone'<span style="color: #000000;">] exports: </span>'Backbone.LayoutManager'<span style="color: #000000;"> } } });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <h3> </h3> <h3>5.02 config</h3> <p>甯稿父闇瑕?#28751;嗛厤缃?#28103;?#37805;?#28028;?#32513;?#28051;涓?#22959;″潡銆?#26473;欎簺?#21412;缃?#23536;寰鏄痑pplication绾?#37714;殑淇?#37805;?#38171;岄渶瑕?#28051;涓?#37813;嬫灏嗗畠浠?#37722;?#28051;?#28028;?#38315;?#32513;?#22959;″潡銆?#37734;≧equireJS涓?#38171;屽?#36720;簬requirejs.config()?#27537;config?#21412;缃?#26916;规潵瀹炵幇銆?#29781;?#38014;峰彇杩欎簺淇?#37805;殑妯″潡鍙?#28000;?#37716;犺浇鐗规?#23108;殑渚濊禆“module”锛屽苟?#30367;?#25956;module.config()銆?/p> <p>渚?锛氬湪requirejs.config()涓?#28729;?#28052;塩onfig锛?#28000;?#28186;?#37711;跺畠妯″潡浣跨敤</p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">requirejs.config({ config: { </span>'bar'<span style="color: #000000;">: { size: </span>'large'<span style="color: #000000;"> }, </span>'baz'<span style="color: #000000;">: { color: </span>'blue'<span style="color: #000000;"> } } });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>濡?#28003;?#37813;瑙?#38171;宑onfig灞?#37804;?#28051;殑bar杩?#28051;鑺?#37828;?#37734;ㄧ敤浜巑odule ID涓篵ar杩?#28051;?#22959;?#37735;楃殑锛宐az杩?#28051;鑺?#37828;敤浜巑odule ID涓篵az杩?#28051;?#22959;?#37735;楃殑銆傚叿浣?#28003;跨敤浠ar.js涓?#28186;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>define(['module'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(module) { </span><span style="color: #008000;">//</span><span style="color: #008000;">Will be the value 'large'var size = module.config().size;</span> });</pre> </div> <p>鍓?#38344;?#37819;愬埌杩?#38171;孯J?#27537;鍐?#32515;?#22959;″潡?#27342;?#31777;require杩?#37832;?#28051;涓猰odule锛岀敤娉?#28751;卞湪姝?#28598;?#38171;岄?#26473;?#28729;?#37721;?#28000;?#37833;?#37716;犺浇config?#27537;鍐呭銆?/p> <h3>5.03 enforceDefine</h3> <p>濡傛灉璁?#32515;?#28051;簍rue锛屽垯褰?#28051;涓剼鏈?#28051;嶆槸閫?#26473;嘾efine()瀹?#28052;?#28051;?#28051;嶅叿澶?#37721;?#28186;涙鏌ョ殑shim瀵煎嚭瀛?#28051;?#37706;?#37827;?#38171;?#28751;变細鎶?#37713;?#38335;?#29831;?#37510;?#26473;?#28051;?#28766;?#37804;?#37721;?#28000;?#23534;?#37714;?#29781;佹眰鎵鏈塕J渚濊禆鎴?#37716;犺浇?#27537;妯″潡閮?#29781;侀?#26473;嘾efine鎴?#38000;卻him琚玆J鏉?#32480;?#37918;?#38171;屽悓鏃跺畠杩?#37832;?#28051;涓?#28610;?#28598;?#28751;?#37828;敤浜?#38335;?#29831;?#22955;娴?#37510;?/p> <h3>5.04 urlArgs</h3> <p>RequireJS鑾峰彇璧勬簮鏃堕檮鍔?#37734;║RL鍚?#38344;㈢殑棰?#28598;栫殑query鍙傛暟銆?#28003;?#28051;烘祻瑙?#37731;?#37812;?#37832;?#37716;?#37731;?#37832;?#23005;?#32429;厤缃?#37827;剁殑“cache bust”鎵嬫寰?#37832;夌敤銆?#28003;跨敤cache bust?#21412;缃殑涓涓?#32448;轰緥锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>urlArgs: "bust=" + (<span style="color: #0000ff;">new</span> Date()).getTime()</pre> </div> <div style="text-align: right; text-align: right;height: 0;padding: 0;overflow: hidden;visibility: hidden;"><a name="_label5"></a></div><h2>6. 閿?#29831;?#28598;?#37918;?/h2> <h3><strong>6.01 鍔犺浇閿?#29831;殑鎹?#38014;?/strong></h3> <p>IE涓?#37817;?#38014;?#37716;犺浇閿?#29831;?#28051;?#28729;岀編锛?/p> <ul><li>IE 6-8涓殑script.onerror鏃?#37825;?#37510;傛病鏈?#37716;?#23049;?#37714;?#37826;?#37828;?#37722;?#37716;犺浇涓涓剼鏈細瀵?#38007;?04閿?#38171;涙洿鐢氬湴锛屽湪404涓?#28186;?#37906;朵細瑙?#37721;憇tate涓篶omplete?#27537;onreadystatechange浜?#28000;?#37510;?/li><li>IE 9+涓璼cript.onerror鏈?#37825;?#38171;?#28003;嗘湁涓涓猙ug锛氬湪?#22717;琛岃剼鏈?#28052;?#37722;?#28729;?#39582;?#28051;嶈Е鍙憇cript.onload浜?#28000;跺彞鏌?#37510;?#37733;?#23005;?#28729;?#37827;?#23049;?#37824;?#37816;?#37718;?#37722;岮MD妯?#37735;楃殑鏍?#37713;嗘?#35268;?#26328;?#37813;浠cript.onreadystatechange浜?#28000;?#28000;嶈浣跨敤銆?#28003;嗘槸锛宻tate涓篶omplete?#27537;onreadystatechange浜?#28000;朵?#27692;湪script.onerror鍑芥暟瑙?#37721;戜?#23338;?#23944;Е鍙?#37510;?/li></ul> <p>鎵浠?#28051;轰?#21976;敮鎸?#37734;↖E涓?#37817;?#38014;?#37716;犺浇閿?#29831;?#38171;岄渶瑕侀厤缃甧nforceDefine涓簍rue锛?#26473;?#28051;嶅緱涓嶈?#20345;眰浣?#37813;鏈夌殑妯″潡閮界敤define瀹?#28052;?#38171;?#37812;?#38000;呯敤shim?#21412;缃甊J瀵瑰?#20906;殑寮曠敤銆?/p> <p><strong>娉ㄦ剰锛?/strong>濡傛灉浣?#29825;?#32515;簡enforceDefine: true锛岃?#28051;?#28003;?#28003;跨敤data-main=""鏉?#37716;犺浇浣犵殑涓籎S妯″潡锛屽垯璇?#28051;籎S妯″潡蹇呴?#26127;皟?#25956;define()鑰?#28051;嶆槸require()鏉?#37716;犺浇鍏?#37813;闇?#27537;?#21804;鐮併?#28051;籎S妯″潡浠嶇?#36346;彲?#30367;?#25956;require/requirejs鏉?#29825;?#32515;甤onfig鍊?#38171;?#28003;嗗浜?#22959;″潡鍔?#26462;藉繀椤?#28003;跨敤define()銆傛瘮濡?#37720;熸潵?#27537;杩欐灏变細鎶?#38335;?#38171;?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">require.config({ enforceDefine: </span><span style="color: #0000ff;">true</span><span style="color: #000000;">, baseUrl: </span>'scripts/lib'<span style="color: #000000;">, paths: { app: </span>'../app'<span style="color: #000000;"> }, shim: { backbone: { deps: [</span>'underscore', 'jquery'<span style="color: #000000;">], exports: </span>'Backbone'<span style="color: #000000;"> } } }); require([</span>'backbone'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(Backbone) { console.log(Backbone); });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>?#22968;鏈鍚?#28051;?#29723;屾敼?#22426;锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"> <pre>define(['backbone'], <span style="color: #0000ff;">function</span><span style="color: #000000;">(Backbone) { console.log(Backbone); });</span></pre> </div> <p>鎵?#28051;嶄細鎶?#38335;?#37510;?/p> <h3>6.02 paths澶?#38335;?/h3> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre><span style="color: #000000;">requirejs.config({ </span><span style="color: #008000;">//</span><span style="color: #008000;">To get timely, correct error triggers in IE, force a define/shim exports check.</span> enforceDefine: <span style="color: #0000ff;">true</span><span style="color: #000000;">, paths: { jquery: [ </span>'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min'<span style="color: #000000;">, </span><span style="color: #008000;">//</span><span style="color: #008000;">If the CDN location fails, load from this location</span> 'lib/jquery'<span style="color: #000000;"> ] } }); </span><span style="color: #008000;">//</span><span style="color: #008000;">Later</span> require(['jquery'], <span style="color: #0000ff;">function</span><span style="color: #000000;"> ($) { });</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>涓?#26473;颁唬鐮?#37711;?#28751;濊瘯鍔犺浇CDN鐗?#37832;?#38171;屽?#20635;?#28355;嚭閿?#38171;屽?#27411;鍥炲埌鏈?#37734;扮殑lib/jquery.js銆?/p> <p><strong>娉ㄦ剰</strong>: paths澶?#38335;?#28000;?#37734;?#22959;″潡ID绮剧‘鍖归厤鏃跺伐浣?#37510;?#26473;?#28051;?#37722;?#27996;?#29999;?#29785;勭殑paths?#21412;缃?#38171;屽父瑙勯厤缃?#37721;?#37718;归厤妯″潡ID?#27537;浠绘?#24531;?#23943;紑閮?#37714;?#37510;?#28598;?#38335;?#28051;?#29781;佺敤浜?#38344;炲?#21733;殑閿?#29831;?#37805;?#28598;?#38171;岃?#28051;嶆槸甯?#29785;勭殑path鏌?#37813;?#29785;?#37835;?#38171;屽洜涓?#38317;?#37734;?#23092;?#29785;?#37731;?#28051;?#37828;?#28003;?#37825;堢殑銆?/p> <h3>6.03 鍏?#28766; requirejs.onError</h3> <p>涓轰簡鎹?#38014;?#37734;?#28766;鍩熺殑errback涓?#37832;?#37817;?#38014;风殑寮?#29999;?#38171;?#28003;?#37721;?#28000;?#38322;嶈浇requirejs.onError()锛?/p> <div style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div> <pre>requirejs.onError = <span style="color: #0000ff;">function</span><span style="color: #000000;"> (err) { console.log(err.requireType); </span><span style="color: #0000ff;">if</span> (err.requireType === 'timeout'<span style="color: #000000;">) { console.log(</span>'modules: ' +<span style="color: #000000;"> err.requireModules); } </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> err; };</span></pre> <div><a title="澶?#37714;朵唬鐮?><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶?#37714;朵唬鐮? /></a></div></div> <p>?#32025;瀹?#38171;?/p></div></div></div><img src ="http://www.8634070.com/wangxinsh55/aggbug/429538.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-03-04 15:27 <a href="http://www.8634070.com/wangxinsh55/archive/2016/03/04/429538.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title>娴?#29835;圫park搴旂敤绋?#25652;忕殑鎬ц兘?#30367;浼?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/03/02/429506.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Wed, 02 Mar 2016 06:12:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/03/02/429506.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/429506.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/03/02/429506.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/429506.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/429506.html</trackback:ping><description><![CDATA[<div><div markdown_views"=""> <p>Spark鏄?#37737;轰簬鍐?#28699;樼殑鍒嗗竷?#32033;璁$畻寮?#37823;?#38171;?#28000;?#28598;?#37918;嗙殑楂?#37825;?#37724;岀ǔ瀹氳?#26947;О銆?#37906;?#38000;屽湪瀹?#38340;呯殑搴旂敤寮鍙?#26473;囩▼涓?#38171;屽紑鍙戣?#26473;?#37828;細閬囧埌绉嶇闂?#26864;?#38171;屽叾涓?#28051;澶?#32491;?#28751;?#37828;拰鎬ц兘鐩稿叧銆?#37734;?#37832;枃涓?#38171;?#32471;?#38000;?#28751;?#32513;撳?#22563;嚜?#38889;瀹炶返锛岃皥璋?#28641;?#28003;?#28751;?#37721;兘鍦?#37819;愰珮搴旂敤绋?#25652;?#37804;ц兘銆?/p><p>鍒嗗竷?#32033;璁$畻寮?#37823;?#37734;ㄨ皟浼?#37826;?#38344;?#37832;夊洓涓?#28051;?#29781;佸叧娉?#37826;瑰悜锛屽?#21975;埆鏄疌PU銆?#37712;?#28699;?#37510;佺綉缁滃紑閿?#25328;I/O锛屽?#36346;叿浣撹皟浼?#37929;?#37837;囧涓?#38171;?/p><ol><li>鎻愰珮CPU鍒╃敤鐜?#37510;?</li><li>閬垮厤OOM銆?</li><li>闄?#28003;?#32515;?#32513;滃紑閿銆?</li><li>鍑?#28751;慖/O?#25655;浣?#37510;?/li></ol><h2>绗?绔??#26271;鎹?#37706;?#37826;?/h2><p>?#26271;鎹?#37706;?#37826;滄?#24531;?#23815;潃鏌?#28051;涓?#37812;?#37836;愬嚑涓狿artition涓殑?#26271;鎹?#38322;忕壒鍒殑澶?#38171;?#26473;欐?#24531;?#23815;潃瀹屾垚閽?#28725;?#26473;?#37713;?#28051;狿artition?#27537;璁$畻闇瑕?#38000;楄垂鐩稿綋闀跨殑鏃?#38338;?#37510;?/p><p>濡?鏋滃ぇ閲忔暟鎹?#38342;?#28051;埌鏌?#28051;涓狿artition锛岄偅涔?#26473;?#28051;狿artition鍦?#29825;?#32480;楃殑鏃跺?#28751;变細?#22426;涓虹摱棰?#37510;?#37733;?鏄疭park搴旂敤绋?#25652;忔墽琛屽?#36346;?#25120;殑绀烘?#24531;浘锛屽湪 Spark涓?#38171;屽悓涓涓?#25652;旂敤绋?#25652;忕殑涓?#37722;孲tage鏄?#28051;?#29723;屾墽琛岀殑锛岃屽悓涓Stage涓殑涓?#37722;孴ask鍙?#28000;?#39582;跺彂?#22717;琛?#38171;孴ask?#26271;鐩?#37922;盤artition?#26271;鏉?#37712;?瀹?#38171;屽?#20635;灉鏌?#28051;涓狿artition?#27537;?#26271;鎹?#38322;忕壒鍒?#28598;?#38171;屽垯鐩稿簲?#27537;task瀹屾垚鏃?#38338;翠細鐗?#37714;?#38336;?#38171;岀敱姝?#28725;?#38007;存帴涓?#37833;ョ殑Stage鏃?#23049;?#23534;濮?#38171;?#37825;?#28051;狫ob瀹屾垚?#27537;鏃?#38338;?#28751;变細闈?甯?#38336;?#37510;?/p><p>瑕侀?#22446;厤?#26271;鎹?#37706;?#37826;滅殑鍑虹幇锛?#28051;绉?#37826;规硶灏?#37828;?#38315;夋嫨鍚?#38315;傜殑key锛?#37812;?#38000;呮槸?#22172;宸卞畾涔?#37929;稿叧?#27537;partitioner銆?#37734;⊿park涓瑽lock浣跨敤 ?#31777;ByteBuffer鏉?#28699;樺偍?#26271;鎹?#38171;岃孊yteBuffer?#20824;澶?#28699;樺偍?#27537;鏈澶ф暟鎹?#38322;?#28051;嶈秴杩?GB銆?#28641;傛灉鏌?#28051;涓猭ey鏈?#28598;?#38322;忕殑?#26271;鎹?#38171;岄偅涔?#37734;ㄨ皟?#25956;cache鎴?persist鍑芥暟鏃?#28751;变細纰板埌spark-1476杩?#28051;?#23534;?#29999;?#37510;?/p><p>涓?#38344;?#37714;?#37713;虹殑杩欎簺API?#32048;瀵?#38007;碨huffle?#25655;浣?#38171;?#37828;暟鎹?#37706;?#37826;滃彲?#20824;鍙戠敓?#27537;?#21479;閿偣鎵鍦?<br /> 1. groupByKey <br /> 2. reduceByKey <br /> 3. aggregateByKey <br /> 4. sortByKey <br /> 5. join <br /> 6. cogroup <br /> 7. cartesian <br /> 8. coalesce <br /> 9. repartition <br /> 10. repartitionAndSortWithinPartitions</p><p><img src="http://img.blog.csdn.net/20160119093312225" alt="鍥?#37911;?#37819;?#26473;? title="" /> <br /> </p><center>鍥?: Spark浠?#37716;?#39582;跺彂妯″瀷</center><br /><pre><code rddwrapper[t]="" {="" hljs="" python"=""> <span>def rdd:</span> RDD[T] } // TODO View bounds are deprecated, should use context bounds // Might need to change ClassManifest for ClassTag in spark 1.0.0 case <span>class DemoPairRDD[K <% Ordered[K] :</span> ClassManifest, V: ClassManifest]( rdd: RDD[(K, V)]) extends RDDWrapper[(K, V)] { // Here we use a single Long to try to ensure the sort is balanced, // but for really large dataset, we may want to consider // using a tuple of many Longs or even a GUID <span>def sortByKeyGrouped(numPartitions: Int):</span> RDD[(K, V)] = rdd.map(kv => ((kv._1, Random.nextLong()), kv._2)).sortByKey() .grouped(numPartitions).map(t => (t._1._1, t._2)) } case <span>class DemoRDD[T:</span> ClassManifest](rdd: RDD[T]) extends RDDWrapper[T] { <span>def grouped(size: Int):</span> RDD[T] = { // TODO Version where withIndex is cached val withIndex = rdd.mapPartitions(_.zipWithIndex) val startValues = withIndex.mapPartitionsWithIndex((i, iter) => Iterator((i, iter.toIterable.last))).toArray().toList .sortBy(_._1).map(_._2._2.toLong).scan(-1L)(_ + _).map(_ + 1L) withIndex.mapPartitionsWithIndex((i, iter) => iter.map { case (value, index) => (startValues(i) + index.toLong, value) }) .partitionBy(new Partitioner { <span>def numPartitions:</span> Int = size <span>def getPartition(key: Any):</span> Int = (key.asInstanceOf[Long] * numPartitions.toLong / startValues.last).toInt }) .map(_._2) } }</code></pre><p>瀹?#28052;夐?#24876;紡?#27537;?#27974;鎹?/p><pre><code rddconversions="" {="" hljs="" python"=""> implicit <span>def toDemoRDD[T:</span> ClassManifest](rdd: RDD[T]): DemoRDD[T] = new DemoRDD[T](rdd) implicit <span>def toDemoPairRDD[K <% Ordered[K] :</span> ClassManifest, V: ClassManifest]( rdd: RDD[(K, V)]): DemoPairRDD[K, V] = DemoPairRDD(rdd) implicit <span>def toRDD[T](rdd: RDDWrapper[T]):</span> RDD[T] = rdd.rdd }</code></pre><p>鍦╯park-shell涓?#28751;?#37721;?#28000;?#28003;跨敤?#31777;</p><pre><code hljs="" java"="">import RDDConversions._ yourRdd.grouped(5)</code></pre><h2>绗?绔? 鍑?#28751;?#32515;?#32513;?#38315;?#28103;?#23534;閿</h2><p>Spark ?#27537;Shuffle杩囩▼闈炲父娑?#38000;?#29863;勬簮锛孲huffle杩囩?#23339;?#24531;?#23815;潃鍦?#37929;稿簲?#27537;璁$畻鑺傜偣锛岃鍏?#28751;?#29825;?#32480;楃粨鏋?#28699;樺偍?#22476;纾佺洏锛屽悗缁殑Stage闇瑕?#28751;?#28051;?#28051;涓猄tage?#27537;缁?鏋滃?#23942;璇?#37711;?#37510;傛暟鎹殑鍐欏叆鍜岃?#35826;彇鎰忓?#23815;潃Disk I/O?#25655;浣?#38171;?#28051;?#37712;?#28699;樻搷浣?#37929;?#23011;?#38171;孌isk I/O?#25655;浣?#37828;?#38344;炲父浣?#37825;堢殑銆?/p><p>浣跨敤iostat鏉?#37836;?#37930;媎isk i/o?#27537;浣跨敤鎯?#37712;?#38171;宒isk i/o?#25655;浣?#26864;戠箒涓?#22471;?#32048;浼?#38341;忕潃cpu load寰堥珮銆?/p><p>濡傛?#28356;暟鎹?#37724;岃?#30075;鑺傜偣閮?#37734;?#37722;?#28051;鍙?#37832;?#37731;?#28051;?#38171;岄偅涔?#37721;?#28000;?#38316;垮厤缃?#32513;滃紑閿锛屽惁鍒?#26473;樿鍔?#28051;?#37929;稿簲?#27537;缃?#32513;滃紑閿銆?浣跨敤iftop鏉?#37836;?#37930;?#32515;?#32513;滃甫瀹?#28003;跨敤鎯?#37712;?#38171;岀湅?#25698;鍑?#28051;?#38010;傜偣涔?#38338;存湁澶?#38322;忕殑缃?#32513;?#28028;?#26440;?#37510;?<br /> 鍥?鏄疭park鑺傜偣闂存暟鎹?#28028;?#26440;撶殑绀烘?#24531;浘锛孲park Task?#27537;璁$畻鍑芥暟鏄?#38315;?#26473;嘇kka閫?#38316;?#37922;盌river鍙?#38315;佸埌Executor涓?#38171;岃孲huffle?#27537;?#26271;鎹?#37714;?#37828;?#38315;?#26473;嘚etty缃?#32513;?#37818;?#37721;?#37833;?#28729;炵幇銆?#37922;?#27996;嶢kka 閫?#38316;?#28051;?#37721;傛暟spark.akka.framesize鍐?#28729;氫簡?#20824;澶?#28028;?#26440;撴秷鎭殑鏈澶?#37706;?#38171;?#37813;浠?#25652;?#29831;?#38316;垮厤鍦⊿park Task涓?#23534;?#37711;?#29906;?#28598;х殑灞閮?#37721;?#38322;?#37510;?/p><p><img src="http://img.blog.csdn.net/20160119093459274" alt="鍥?#37911;?#37819;?#26473;? title="" /></p><center>鍥?: Spark鑺傜偣闂寸殑?#26271;鎹?#28028;?#26440;?/center><br /><strong>绗?鑺?閫夋嫨鍚?#38315;傜殑骞跺彂?#26271;</strong><p>涓轰?#21976;?#24880;珮Spark搴旂敤绋?#25652;忕殑鏁?#37916;?#38171;?#28751;?#37721;?#38003;界殑鎻愬崌CPU?#27537;鍒╃敤鐜?#37510;?#39582;跺彂?#26271;搴?#29831;?#37828;?#37721;敤CPU鐗?#37918;?#37837;告暟?#27537;涓?#37706;?#37510;?#37734;?#26473;?#38322;?#38171;屽?#36346;彂?#26271;杩?#28003;?#38171;孋PU寰?#28051;嶅埌鍏?#37714;嗙殑鍒╃敤锛屽?#36346;彂?#26271;杩?#28598;?#38171;岀敱浜巗park鏄?#23011;?#28051;涓猼ask閮?#29781;?#37714;嗗彂?#22476;璁?#32480;楃粨?#20579;锛?#37813;浠?#28000;?#37716;?#37722;?#37716;ㄧ殑寮閿?#32048;涓?#37719;?#37510;?/p><p>骞跺彂?#26271;?#27537;淇敼锛岄?#26473;囬厤缃?#37721;傛暟鏉ユ敼鍙榮park.default.parallelism锛屽?#20635;灉鏄痵ql?#27537;璇?#38171;屽彲?#20824;閫?#26473;?#28103;敼spark.sql.shuffle.partitions鏉?#28103;敼銆?/p><p><strong><em>绗?椤? Repartition vs. Coalesce</em></strong></p><p>repartition?#25328;coalesce閮借兘瀹?#37916;版暟鎹?#37714;嗗尯?#27537;鍔?#37804;佽皟鏁?#38171;?#28003;嗛渶瑕佹敞鎰忕殑鏄痳epartition?#32048;瀵?#38007;磗huffle?#25655;浣?#38171;岃宑oalesce涓嶄細銆?/p><p><strong>绗?鑺? reduceByKey vs. groupBy</strong></p><p>groupBy?#25655;浣滃簲璇?#28751;?#37721;?#38003;界殑閬垮厤锛?#32471;?#28051;鏄?#37832;?#37721;兘閫犳垚澶?#38322;忕殑缃?#32513;滃紑閿锛?#32471;?#27996;?#37828;?#37721;兘瀵?#38007;碠OM銆?#28000;ordCount涓轰緥鏉ユ?#26050;?#31753;educeByKey?#25328;groupBy?#27537;宸?#23534;?/p><pre><code hljs="" avrasm"="">reduceByKey sc.textFile(“README.md”).map(l=>l.split(“,”)).map(w=>(w,1)).reduceByKey(_ + _)</code></pre><p><img src="http://img.blog.csdn.net/20160119094034574" alt="鍥?#37911;?#37819;?#26473;? title="" /> <br /> </p><center>鍥?锛歳educeByKey?#27537;Shuffle杩囩▼</center><p>Shuffle杩囩▼濡?#37733;?鎵绀?/p><pre><code hljs="" avrasm"="">groupByKey sc.textFile(“README.md”).map(l=>l.split(“,”)).map(w=>(w,1)).groupByKey.map(r=>(r._1,r._2.sum))</code></pre><p><img src="http://img.blog.csdn.net/20160119094231652" alt="鍥?#37911;?#37819;?#26473;? title="" /> <br /> </p><center>鍥?锛歡roupByKey?#27537;Shuffle杩囩▼</center><br /><p>寤?#29825;? 灏?#37721;兘浣跨敤reduceByKey, aggregateByKey, foldByKey?#25328;combineByKey <br /> 鍋?#29825;?#37832;?#28051;RDD濡?#28051;?#37813;绀?#38171;屾?#20635;瘡涓猭ey?#27537;鍧囧?/p><p>val data = sc.parallelize( List((0, 2.), (0, 4.), (1, 0.), (1, 10.), (1, 20.)) )</p><p>鏂规硶涓锛歳educeByKey</p><pre><code hljs="" coffeescript"="">data.map<span>(r=>(r._1, (r<span>.2,1)))</span>.reduceByKey((a,b)=>(a._1 + b._1, a._2 + b._2)).map(r=>(r._1,(r._2._1/r._2._2)).foreach(println)</span></code></pre><p>鏂规硶浜?#38171;歝ombineByKey</p><pre><code hljs="" coffeescript"="">data.combineByKey(value=>(value,1), <span>(x:(Double, Int), value:Double)=></span> (x._1+value, x._2 + 1), <span>(x:(Double,Int), y:(Double, Int))=></span>(x._1 + y._1, x._2 + y._2))</code></pre><p><strong>绗?鑺? BroadcastHashJoin vs. ShuffleHashJoin</strong></p><p>鍦↗oin杩囩▼涓?#38171;?#32513;?#29999;镐細閬囧埌澶?#29723;ㄥ拰灏?#29723;ㄧ殑join. 涓轰?#21976;?#24880;珮鏁?#37916;?#37721;?#28000;?#28003;跨敤BroadcastHashJoin, 棰勫厛灏?#28751;?#29723;ㄧ殑鍐呭骞挎挱?#22476;鍚?#28051;狤xecutor, 杩?#37837;?#28751;嗛?#22446;厤閽?#28725;?#28751;?#29723;ㄧ殑Shuffle杩囩▼锛?#28000;?#38000;屾瀬澶х殑鎻愰珮杩?#29723;?#37825;?#37916;?#37510;?/p><p>鍏跺疄BroadCastHashJoin鏍稿績灏?#37828;?#37714;╃敤?#31777;BroadCast鍑芥暟锛屽?#20635;灉鐞?#29785;?#23059;呮?#27484;roadcast?#27537;浼樼偣锛?#28751;辫兘姣?#26440;?#28610;界殑鏄庣櫧BroadcastHashJoin?#27537;浼?#37716;?#37813;鍦?#37510;?/p><p>浠?#28051;?#37828;?#28051;涓畝鍗?#28003;跨敤broadcast?#27537;绀轰?#23340;▼搴?#37510;?/p><pre><code hljs="" avrasm"="">val lst = 1 to 100 toList val exampleRDD = sc.makeRDD(1 to 20 toSeq, 2) val broadcastLst = sc.broadcast(lst) exampleRDD.filter(i=>broadcastLst.valuecontains(i)).collect.foreach(println)</code></pre><p><strong>绗?鑺? map vs. mapPartitions</strong></p><p>鏈?#37827;?#38343;瑕?#28751;?#29825;?#32480;楃粨鏋?#28699;樺偍?#22476;澶?#38318;ㄦ暟鎹?#25652;?#38171;?#37716;垮繀浼氬缓绔嬪埌澶?#38318;ㄦ暟鎹?#25652;撶殑杩?#37818;?#37510;?#25652;?#29831;?#28751;?#37721;?#38003;界殑璁╂洿澶氱殑鍏冪礌鍏?#27996;?#37722;?#28051;涓暟鎹?#26473;?#37818;?#38000;?#28051;嶆槸姣?#28051;涓?#37711;冪?#29365;殑澶?#37918;?#37827;?#38318;?#37720;?#23524;虹?#23339;暟鎹?#25652;?#26473;?#37818;?#37510;?<br /> 鍦?#26473;?#32457;嶆儏鍐?#28051;?#38171;宮apPartitions?#25328;foreachPartitons灏嗘瘮map?#25655;浣?#26946;?#37825;堢殑澶?#37510;?/p><p><strong>绗?鑺? ?#26271;鎹?#28751;卞湴璇诲彇</strong></p><p>绉?#37716;?#29825;?#32480;楃殑寮閿杩?#26473;?#28003;?#27996;?#32457;?#37716;ㄦ暟鎹殑寮閿銆?/p><p>Spark涓?#23011;?#28051;猅ask閮?#38343;瑕佺?#31295;簲?#27537;杈撳叆?#26271;鎹?#38171;屽洜姝?#26440;撳叆?#26271;鎹殑浣?#32515;?#28725;?#27996;嶵ask?#27537;鎬ц兘鍙?#23536;?#23536;?#38322;嶈?#20341;傛?#22796;収?#26271;鎹?#38014;?#37721;栫殑閫熷害鏉?#37718;?#37714;?#38171;岀?#21342;揩?#22476;鎱?#37714;嗗埆鏄?#38171;?/p><p>1.PROCESS_LOCAL <br /> 2.NODE_LOCAL <br /> 3.RACK_LOCAL</p><p>Spark鍦═ask?#22717;琛岀殑鏃跺欎細灏?#28028;?#37711;?#38000;冭檻鏈蹇殑?#26271;鎹?#38014;峰彇鏂瑰紡锛屽?#20635;灉鎯?#28751;?#37721;?#38003;界殑鍦ㄦ洿澶氱殑鏈?#37731;?#28051;?#37722;?#37716;═ask锛岄偅涔?#37721;?#28000;?#38315;?#26473;囪皟浣巗park.locality.wait?#27537;鍊?#37833;?#28729;炵幇, ?#31919;璁?#37706;?#37828;?s銆?/p><p>?#27342; ?#31777;HDFS锛孲park?#20824;澶熸敮鎸佺殑?#26271;鎹?#23143;愯秺鏉?#29906;?#28598;?#38171;屽?#20400;assandra, HBase,MongoDB绛夌煡鍚嶇殑NoSQL?#26271;鎹?#25652;?#38171;岄?#24533;潃Elasticsearch?#27537;鏃?#23059;愬叴璧?#38171;宻park?#25328;elasticsearch缁勫悎璧?#37833;?#37819;?#28186;?楂?#38315;熺殑鏌?#29831;?#29785;?#37712;?#37826;规涔熸垚涓?#28051;绉嶆湁鐩婄殑灏濊?#26328;?/p><p>涓?#26473;?#37819;愬埌?#27537;澶?#38318;ㄦ暟鎹?#23143;愰潰涓寸殑涓涓?#37929;稿?#23684;棶棰?#28751;?#37828;?#28641;?#28003;?#29825;﹕park蹇?#38315;?#29831;诲彇鍏?#28051;殑?#26271;鎹?#38171;?灏?#37721;?#38003;界殑灏?#29825;?#32480;楃粨鐐瑰拰?#26271;鎹?#32513;撶偣閮?#32515;?#37734;?#28051;璧?#37828;?#26440;惧埌璇?#37929;?#37837;囩殑鍩?#37832;?#37826;规硶锛?#23011;?#28641;?#37734;?#38318;?#32515;睭adoop闆嗙兢?#27537;鏃跺?#38171;屽彲浠?#28751;咹DFS?#27537;DataNode?#25328;Spark Worker鍏?#27996;?#28051;鍙?#37832;?#37731;?#37510;?/p><p>浠assandra涓轰緥,濡傛灉Spark?#27537;閮?#32515;插拰Cassandra?#27537;鏈?#37731;?#37832;?#38318;?#37714;?#38322;?#37721;?#38171;岄偅涔?#37734;?#29831;诲彇Cassandra涓暟鎹殑鏃跺?#38171;岄?#26473;囪皟浣巗park.locality.wait灏?#37721;?#28000;?#37734;ㄦ病鏈?#38318;?#32515;睠assandra?#27537;鏈?#37731;?#28051;?#37722;?#37716;⊿park Task銆?/p><p>瀵?#27996;嶤assandra, 鍙?#28000;?#37734;?#38318;?#32515;睠assandra?#27537;鏈?#37731;?#28051;?#38318;?#32515;睸park Worker锛岄渶瑕佹敞鎰忕殑鏄疌assandra?#27537;compaction?#25655;浣滀細鏋?#28598;х殑娑?#38000;桟PU锛屽洜姝?#37734;?#28051;篠park Worker?#21412;缃瓹PU鏍告暟鏃?#38171;岄渶瑕?#28751;?#26473;欎?#28056;洜绱?#32513;煎悎鍦?#28051;璧?#26473;?#29723;岃冭檻銆?/p><p>杩?#28051;閮?#37714;嗙殑?#21804;鐮侀?#26440;?#37721;?#28000;?#37721;?#38000;冩簮鐮乀askSetManager::addPendingTask</p><pre><code hljs="" php"="">private def addPendingTask(index: Int, readding: Boolean = false) { // Utility method that adds `index` to a list only if readding=false or it's not already there def addTo(list: ArrayBuffer[Int]) { if (!readding || !list.contains(index)) { list += index } } for (loc <- tasks(index).preferredLocations) { loc match { case e: ExecutorCacheTaskLocation => addTo(pendingTasksForExecutor.getOrElseUpdate(e.executorId, new ArrayBuffer)) case e: HDFSCacheTaskLocation => { val exe = sched.getExecutorsAliveOnHost(loc.host) exe match { case Some(set) => { for (e <- set) { addTo(pendingTasksForExecutor.getOrElseUpdate(e, new ArrayBuffer)) } logInfo(s"Pending task $index has a cached location at ${e.host} " + ", where there are executors " + set.mkString(",")) } case None => logDebug(s"Pending task $index has a cached location at ${e.host} " + ", but there are no executors alive there.") } } case _ => Unit } addTo(pendingTasksForHost.getOrElseUpdate(loc.host, new ArrayBuffer)) for (rack <- sched.getRackForHost(loc.host)) { addTo(pendingTasksForRack.getOrElseUpdate(rack, new ArrayBuffer)) } } if (tasks(index).preferredLocations == Nil) { addTo(pendingTasksWithNoPrefs) } if (!readding) { allPendingTasks += index // No point scanning this whole list to find the old task there } }</code></pre><p>濡傛?#28355;?#21975;璁﹕park鏀?#37816;佹柊?#27537;瀛樺偍婧?#38171;?#26473;?#38000;屽紑鍙?#37929;稿簲?#27537;RDD锛?#28051;?#28003;?#32515;?#37929;稿叧?#27537;閮?#37714;?#28751;?#37828;嚜瀹?#28052;塯etPreferredLocations鍑芥暟锛?#28000;lasticsearch-hadoop涓殑EsRDD涓轰緥锛屽?#26421;唬鐮?#28729;?#37916;板涓?#37510;?/p><pre><code hljs="" python"="">override <span>def getPreferredLocations(split: Partition):</span> Seq[String] = { val esSplit = split.asInstanceOf[EsPartition] val ip = esSplit.esPartition.nodeIp if (ip != null) Seq(ip) else Nil }</code></pre><p><strong>绗?鑺? 搴?#37714;?#37718;?/strong></p><p>浣跨敤濂界殑搴?#37714;?#37718;栫畻娉曡兘澶?#37819;愰珮杩?#29723;岄熷害锛屽悓鏃惰兘澶熷噺灏?#37712;?#28699;樼殑浣跨敤銆?/p><p>Spark鍦⊿huffle?#27537;鏃跺?#29781;?#28751;嗘暟鎹?#37711;?#28699;樺偍?#22476;纾佺洏涓?#38171;?#28699;樺偍?#27537;鍐呭鏄?#32513;?#26473;囧簭鍒?#37718;栫殑銆?#25652;?#37714;?#37718;栫殑杩囩▼鐗垫?#22794;埌涓?#28598;?#37737;?#37832;?#38000;冭?#25120;殑鍥?#32497;?#38171;?#28051;鏄?#25652;?#37714;?#37718;栫殑閫熷害锛?#27996;?#37828;?#25652;?#37714;?#37718;?#37722;?#37712;呭鎵鍗犵敤?#27537;澶?#28751;?#37510;?/p><p>kryoSerializer涓庨粯璁ょ殑javaSerializer鐩?#23011;?#38171;屽湪搴?#37714;?#37718;?#38315;熷害鍜屽簭鍒?#37718;栫粨鏋滅殑澶?#28751;?#37826;?#38344;?#38318;藉叿鏈夋瀬澶х殑浼?#37716;?#37510;?#37813;浠?#23524;?#29825;?#37734;?#25652;旂敤绋?#25652;忛厤缃?#28051;?#28003;跨敤KryoSerializer.</p><pre><code hljs="" avrasm"="">spark.serializer org.apache.spark.serializer.KryoSerializer</code></pre><p>?#31919;璁ょ殑cache?#30149;鏈?#28725;圭紦瀛樼殑瀵?#29854;?#26473;?#29723;屽簭鍒?#37718;?#38171;?#28003;跨敤?#27537;StorageLevel鏄疢EMORY_ONLY,杩欐?#24531;?#23815;潃瑕?#37719;犵敤姣?#26440;?#28598;х殑鍐?#28699;?#37510;?#37721;?#28000;?#38315;?#26473;囨寚瀹歱ersist涓殑鍙傛暟鏉?#28725;圭紦瀛?#37712;呭杩?#29723;屽簭鍒?#37718;?#37510;?/p><pre><code hljs="" avrasm"="">exampleRDD.persist(MEMORY_ONLY_SER)</code></pre><p>闇瑕佺壒鍒寚鍑虹殑鏄痯ersist鍑芥暟鏄?#32475;夊埌job?#22717;琛岀殑鏃跺?#37813;嶄細灏嗘暟鎹?#32514;?#28699;樿捣鏉?#38171;屽?#28848;?#24225;欢杩熸墽琛? 鑰寀npersist鍑芥暟鍒?#37828;?#32468;?#37719;虫墽琛?#38171;岀紦瀛樹細琚?#32468;?#37719;虫?#21620;櫎銆?/p><blockquote> <p>浣?#38000;呯畝浠?#38171;?#29825;搁箯锛?銆夾pache Spark婧?#37934;佸墫鏋?#37510;?#28003;?#38000;?#38171;屽叧娉?#27996;?#28598;ф暟鎹?#28729;?#37827;舵?#28357;储鍜屽疄鏃舵?#20345;暟鎹?#28598;?#37918;?#38171;屽elasticsearch, storm鍙奷rools澶?#37832;?#37934;旂┒锛岀幇灏?#38001;?#27996;?#37820;虹▼銆?/p> </blockquote> </div></div><img src ="http://www.8634070.com/wangxinsh55/aggbug/429506.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-03-02 14:12 <a href="http://www.8634070.com/wangxinsh55/archive/2016/03/02/429506.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title>playframwork dist 鎵撳寘鏃?#28751;嗛?#28858;」鐩?#28051;殑?#26499;浠朵篃鎵撳寘杩?#37720;?/title><link>http://www.8634070.com/wangxinsh55/archive/2016/02/26/429448.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Fri, 26 Feb 2016 08:46:00 GMT</pubDate><guid>http://www.8634070.com/wangxinsh55/archive/2016/02/26/429448.html</guid><wfw:comment>http://www.8634070.com/wangxinsh55/comments/429448.html</wfw:comment><comments>http://www.8634070.com/wangxinsh55/archive/2016/02/26/429448.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/wangxinsh55/comments/commentRss/429448.html</wfw:commentRss><trackback:ping>http://www.8634070.com/wangxinsh55/services/trackbacks/429448.html</trackback:ping><description><![CDATA[<div>http://stackoverflow.com/questions/12231862/how-to-make-play-framework-dist-command-adding-some-files-folders-to-the-final</div><br /><div><p>Play uses <a href="http://www.scala-sbt.org/sbt-native-packager/" rel="nofollow">sbt-native-packager</a>, which supports the inclusion of arbitrary files by adding them to the mappings:</p> <pre><code>mappings in Universal ++= (baseDirectory.value / "scripts" * "*" get) map (x => x -> ("scripts/" + x.getName)) </code></pre> The syntax assumes Play 2.2.x</div><br /><div><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:10.5pt;"><span style="color:#cc7832;background-color:#344134;font-weight:bold;">val</span> jdk8 = <span style="color:#cc7832;background-color:#344134;font-weight:bold;">new </span><span style="background-color:#344134;">File(</span><span style="color:#6a8759;background-color:#344134;">"D:</span><span style="color:#cc7832;background-color:#344134;">\\</span><span style="color:#6a8759;background-color:#344134;">JDK</span><span style="color:#cc7832;background-color:#344134;">\\</span><span style="color:#6a8759;background-color:#344134;">JDK8</span><span style="color:#cc7832;background-color:#344134;">\\</span><span style="color:#6a8759;background-color:#344134;">jre1_8_0_40"</span><span style="background-color:#344134;">)</span><br /><span style="color:#9876aa;font-style:italic;">mappings </span>in <span style="color:#9876aa;font-style:italic;">Universal </span>++= (jdk8 ** <span style="color:#6a8759;">"*" </span>get) map (x => x -> (<span style="color:#6a8759;">"jre8/" </span>+ jdk8.relativize(x).getOrElse(x.getName)))</pre></div><img src ="http://www.8634070.com/wangxinsh55/aggbug/429448.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/wangxinsh55/" target="_blank">SIMONE</a> 2016-02-26 16:46 <a href="http://www.8634070.com/wangxinsh55/archive/2016/02/26/429448.html#Feedback" target="_blank" style="text-decoration:none;">鍙?#29723;?#29831;勮</a></div>]]></description></item><item><title>鍒╃敤涓枃?#26271;鎹?#29882;慓oogle寮婧愰」鐩畐ord2vechttp://www.8634070.com/wangxinsh55/archive/2016/01/13/429028.htmlSIMONESIMONEWed, 13 Jan 2016 05:49:00 GMThttp://www.8634070.com/wangxinsh55/archive/2016/01/13/429028.htmlhttp://www.8634070.com/wangxinsh55/comments/429028.htmlhttp://www.8634070.com/wangxinsh55/archive/2016/01/13/429028.html#Feedback0http://www.8634070.com/wangxinsh55/comments/commentRss/429028.htmlhttp://www.8634070.com/wangxinsh55/services/trackbacks/429028.htmlhttp://www.cnblogs.com/hebin/p/3507609.html

涓鐩?#37722;?#29831;磜ord2vec鍦?#28598;?#37918;?#29831;?#28051;?#29831;嶇殑鐩?#28028;煎害?#27537;闂?#26864;?#28051;?#37825;堟?#28355;崄鍒嗗ソ锛?#37832;杩戣嚜宸?#28052;?#28051;?#37813;嬭?#25116;簡璺慓oogle寮婧愮殑?#21804;鐮侊紙https://code.google.com/p/word2vec/锛?#37510;?/span>

1銆?#29831;?#37826;?/span>

棣?#37711;?#37713;嗗?#22248;暟鎹?#38171;?#38322;囩敤缃?#28051;?#37719;氬涓?#37818;ㄨ?#24878;殑鍏?#32515;?#37826;?#38338;绘暟鎹?SogouCA)锛屽ぇ灏?#28051;?.1G銆?nbsp;

浠巉tp涓?#28051;嬭浇?#26271;鎹寘SogouCA.tar.gz锛?/span>
1 wget ftp://ftp.labs.sogou.com/Data/SogouCA/SogouCA.tar.gz --ftp-user=hebin_hit@foxmail.com --ftp-password=4FqLSYdNcrDXvNDi -r

瑙?#37720;嬫暟鎹寘锛?/span>

1 gzip -d SogouCA.tar.gz 2 tar -xvf SogouCA.tar

鍐?#28751;嗙敓?#22426;?#27537;txt?#26499;浠跺綊骞跺埌SogouCA.txt涓?#38171;屽彇鍑?#37711;?#28051;寘?#24776;content?#27537;琛屽苟?#27974;鐮?#38171;屽緱?#22476;璇?#37826;檆orpus.txt锛屽ぇ灏?#28051;?.7G銆?/span>

1 cat *.txt > SogouCA.txt 2 cat SogouCA.txt | iconv -f gbk -t utf-8 -c | grep "<content>" > corpus.txt

2銆?#37714;?#29831;?/span>

?#25956;ANSJ瀵筩orpus.txt杩?#29723;屽垎璇?#38171;屽緱?#22476;鍒?#29831;?#32513;?#37835;渞esultbig.txt锛屽ぇ灏?#28051;?.1G銆?/span>

鍒?#29831;?#23480;ュ叿ANSJ鍙?#29785;?nbsp;http://blog.csdn.net/zhaoxinfan/article/details/10403917
鍦?#37714;?#29831;?#23480;ュ叿seg_tool鐩?#35120;?#28051;?#37711;?#32514;?#29831;?#37712;嶆墽琛屽緱?#22476;鍒?#29831;?#32513;?#37835;渞esultbig.txt锛屽?#21613;惈426221涓?#29831;?#38171;屾?#26271;鎬?#29825;?72308385涓?#37510;?/span>
 鍒?#29831;?#32513;?#37835;?#38171;?/span>
  
3銆佺敤word2vec宸ュ叿璁?#32513;?#29831;?#37722;?#38322;?/span>
1 nohup ./word2vec -train resultbig.txt -output vectors.bin -cbow 0 -size 200 -window 5 -negative 0 -hs 1 -sample 1e-3 -threads 12 -binary 1 &

vectors.bin鏄痺ord2vec澶?#37918;唕esultbig.txt鍚庣敓?#22426;?#27537;璇嶇殑鍚?#38322;忔枃浠?#38171;屽湪瀹為?#23677;?#27537;鏈?#37716;?#37731;?#28051;?#29825;?#32513;冧簡1涓?#37719;?#28751;?#37827;?#37510;?/span>

4銆?#37714;嗘瀽
4.1 璁?#32480;楃浉浼肩殑璇?#38171;?/span>
1 ./distance vectors.bin

 ./distance鍙?#28000;?#37930;嬫垚璁$畻璇?#28051;?#29831;?#28052;?#38338;寸殑璺濈锛屾妸璇嶇湅?#22426;鍚?#38322;忕┖闂?#28051;婄殑涓涓偣锛宒istance鐪嬫垚鍚?#38322;忕┖闂?#28051;婄偣涓?#37904;圭殑璺濈銆?/span>

涓?#38344;?#37828;?#28051;浜?#28186;?#28699;?#38171;?/span> 

4.2 娼滃湪?#27537;璇?#29815;瀛?#29785;勫緥

鍦?#28725;筪emo-analogy.sh淇敼鍚庡緱?#22476;涓?#38344;?#37713;?#28051;?#28186;?#28699;?#38171;?/span>
娉?#37733;界殑棣?#38318;?#37828;?#23480;?#27035;?#38171;岃?#21342;浗?#27537;棣?#38318;?#37828;?#28028;?#37825;?#38171;?nbsp;vector("娉?#37733;?) - vector("宸?#27035;? + vector("鑻卞浗") --> vector("浼?#37825;?)"

4.3 ?#20187;绫?/span>

灏?#32513;?#26473;?#37714;?#29831;?#37722;庣殑璇?#37826;檙esultbig.txt涓殑璇嶈仛绫?#39582;舵?#22796;収绫?#37714;?#37818;?#25652;?#38171;?/span>

1 nohup ./word2vec -train resultbig.txt -output classes.txt -cbow 0 -size 200 -window 5 -negative 0 -hs 1 -sample 1e-3 -threads 12 -classes 500  & 2 sort classes.txt -k 2 -n > classes_sorted_sogouca.txt  

渚?#28641;?#38171;?/span>

4.4 鐭?#29831;?#37714;嗘瀽

鍏?#37714;╃敤缁?#26473;?#37714;?#29831;嶇殑璇?#37826;檙esultbig.txt涓?#23536;?#37713;哄寘?#24776;璇?#37724;岀煭璇殑?#26499;浠秙ogouca_phrase.txt锛屽?#23944;缁?#29831;ユ枃浠?#28051;?#29831;?#28051;庣煭璇殑鍚?#38322;?#29723;?#32448;?#37510;?/span>

1 ./word2phrase -train resultbig.txt -output sogouca_phrase.txt -threshold 500 -debug 2 2 ./word2vec -train sogouca_phrase.txt -output vectors_sogouca_phrase.bin -cbow 0 -size 300 -window 10 -negative 0 -hs 1 -sample 1e-3 -threads 12 -binary 1

涓?#38344;?#37828;?#37713;?#28051;?#29825;?#32480;楃浉浼煎害?#27537;渚?#28699;?#38171;?/span>

5銆?#37721;?#38000;冮摼鎺?/strong>锛?/span>

1. word2vec锛歍ool for computing continuous distributed representations of words锛?a href="https://code.google.com/p/word2vec/">https://code.google.com/p/word2vec/

2. ?#25956;涓枃?#22968;鐜〨oogle寮婧愮殑Deep-Learning椤?#37929;畐ord2vec锛?a href="http://www.cnblogs.com/wowarsenal/p/3293586.html">http://www.cnblogs.com/wowarsenal/p/3293586.html

3. 鍒╃敤word2vec瀵瑰叧閿?#29831;?#26473;?#29723;岃仛绫?#38171;?a href="http://blog.csdn.net/zhaoxinfan/article/details/11069485">http://blog.csdn.net/zhaoxinfan/article/details/11069485

6銆?#37722;?#32513;?#37713;嗗浠?#32513;嗛槄璇荤殑?#26499;?#23582;锛?/span>

[1] Tomas Mikolov, Kai Chen, Greg Corrado, and Jeffrey Dean. Efficient Estimation of Word Representations in Vector Space. In Proceedings of Workshop at ICLR, 2013.
[2] Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado, and Jeffrey Dean. Distributed Representations of Words and Phrases and their Compositionality. In Proceedings of NIPS, 2013.
[3] Tomas Mikolov, Wen-tau Yih, and Geoffrey Zweig. Linguistic Regularities in Continuous Space Word Representations. In Proceedings of NAACL HLT, 2013.

[4] Collobert R, Weston J, Bottou L, et al. Natural language processing (almost) from scratch[J]. The Journal of Machine Learning Research, 2011, 12: 2493-2537.

 



]]> 双色球直播吧
<div id="k0jz6"><tr id="k0jz6"></tr></div>
    <sup id="k0jz6"><menu id="k0jz6"><small id="k0jz6"></small></menu></sup>
    <div id="k0jz6"><tr id="k0jz6"><mark id="k0jz6"></mark></tr></div><em id="k0jz6"></em>
    <dl id="k0jz6"><ins id="k0jz6"></ins></dl><dl id="k0jz6"></dl>
      <div id="k0jz6"></div>

      <sup id="k0jz6"></sup><dl id="k0jz6"><ins id="k0jz6"><thead id="k0jz6"></thead></ins></dl>
        <div id="k0jz6"><tr id="k0jz6"></tr></div>
          <sup id="k0jz6"><menu id="k0jz6"><small id="k0jz6"></small></menu></sup>
          <div id="k0jz6"><tr id="k0jz6"><mark id="k0jz6"></mark></tr></div><em id="k0jz6"></em>
          <dl id="k0jz6"><ins id="k0jz6"></ins></dl><dl id="k0jz6"></dl>
            <div id="k0jz6"></div>

            <sup id="k0jz6"></sup><dl id="k0jz6"><ins id="k0jz6"><thead id="k0jz6"></thead></ins></dl>
              莱万特17款跟18款怎么区分 pk10开奖 2012多特蒙德阵容 好多怪兽在线客服 范特西篮球巨星卡 毕尔巴鄂竞技vs韦斯卡 川崎前锋亚冠积分 cf手游焕蝴蝶刀觉醒石 迷你世界海洋更新鲨鱼 云从龙 楚留香