文章列表

Java优势有哪些?

作者:cdhqyj

Java优势有哪些? 从事IT行业的人对于java一定不陌生,从当前就业状况及企业用人情况来讲,java人才的薪水很高,企业对该类人才需求旺盛。那么java语言有哪些优势呢?它到底是一种什么样的程序设计语言?现在就让我们来共同了解java相关的内容。 Java优势有哪些? 先来看看java的基本概念: java是一种可以撰写跨平台应用程序的面向对象的程序设计语言。java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球大的开发者专业社群。 java自面世后就非常流行,发展迅速,对C++语言形成有力冲击。在全球云计算和移动互联网的产业环境下,java更具备了显著优势和广阔前景。 java的优势: 与传统程序不同,Sun公司在推出 java之际就将其作为一种开放的技术。全球数以万计的 java 开发公司被要求所设计的java软件必须相互兼容。 java 平台是基于java 语言的平台。这样的平台非常流行。因此微软公司推出了与之竞争的.NET平台以及模仿java 的C#语言。 java 是功能完善的通用程序设计语言,可以用来开发可靠的、要求严格的应用程序。 java的就业方向: 1.互联网 目前采用java开发互联网网站及应用的公司越来越多,这也增加了java开发工程师的需求。像京东的一些后端都是使用的java。 2.手机开发 智能手机的流行是java的一个推进剂,Android手机应用都是使用java开发。Android系统开发里面也涉及到java。 3.java应用开发 java除了做后端和手机应用,还可以做PC端的应用。很多公司的OA系统,邮箱,股票金融系统都是使用java 开发。java开发者在这方面的需求也非常大,待遇也相当不错,很容易在一些大型企业就业。 以上就是Java的一些优势。从以上几点内容来看,java的前景是相当不错的。

一键帮你生成所需代码--EasyCode试试这个工具吧

作者:微信小助手

<section style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"> <br> </section> <p data-mpa-powered-by="yiban.io" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(62, 71, 83);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 15px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-family: Optima-Regular, PingFangTC-light;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(127, 127, 127);font-size: 14px;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;">点击上方“</span><span style="max-width: 100%;font-size: 14px;line-height: 1.75em;color: rgb(0, 176, 240);box-sizing: border-box !important;overflow-wrap: break-word !important;">服务端思维</span><span style="max-width: 100%;color: rgb(127, 127, 127);font-size: 14px;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;">”,选择“</span></span><span style="max-width: 100%;color: rgb(136, 136, 136);font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">设为星标</span><span style="max-width: 100%;color: rgb(127, 127, 127);letter-spacing: 0.544px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">”</span></span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;color: rgb(62, 62, 62);font-size: 15px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-family: Optima-Regular, PingFangTC-light;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(127, 127, 127);font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">回复”</span><span style="max-width: 100%;letter-spacing: 0.544px;font-size: 14px;color: rgb(0, 122, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;">669</span><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(127, 127, 127);font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">“获取独家整理的精选资料集</span></span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;color: rgb(62, 62, 62);font-size: 15px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-family: Optima-Regular, PingFangTC-light;letter-spacing: 0.544px;color: rgb(127, 127, 127);font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">回复”</span><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(0, 122, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;">加群</span><span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">“加入全国服务端高端社群「后端圈」</span></span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;color: rgb(62, 62, 62);font-size: 15px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-family: Optima-Regular, PingFangTC-light;letter-spacing: 0.544px;color: rgb(127, 127, 127);font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></span></span></p> <section> <img data-backh="340" data-backw="509" data-ratio="0.6666666666666666" src="/upload/633c04dc28a6b4e23d72da19001ca5a4.jpg" data-type="jpeg" data-w="510" style="width: 100%;height: auto;"> <br> </section> <h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 22px;max-width: 100%;font-family: Optima-Regular, Optima, PingFangTC-Light, PingFangSC-light, PingFangTC-light;letter-spacing: 2px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: black;border-bottom: 4px solid rgb(64, 184, 250);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="margin-left: 25px;max-width: 100%;display: flex;color: rgb(64, 184, 250);font-size: 20px;box-sizing: border-box !important;overflow-wrap: break-word !important;">EasyCode</span><span style="margin-top: -10px;max-width: 100%;display: flex;width: 200px;height: 10px;border-top-left-radius: 20px;background: rgba(64, 184, 250, 0.5);color: rgb(255, 255, 255);font-size: 16px;letter-spacing: 0.544px;justify-content: flex-end;float: right;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></h2> <p data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;padding-top: 8px;padding-bottom: 8px;max-width: 100%;min-height: 1em;color: rgb(43, 43, 43);font-family: Optima-Regular, Optima, PingFangTC-Light, PingFangSC-light, PingFangTC-light;font-size: 14px;letter-spacing: 2px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;word-spacing: 2px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(89, 89, 89);box-sizing: border-box !important;overflow-wrap: break-word !important;">EasyCode</span><sup style="max-width: 100%;line-height: 0;color: rgb(89, 89, 89);box-sizing: border-box !important;overflow-wrap: break-word !important;">[1]</sup>&nbsp;是基于 IntelliJ IDEA Ultimate 版开发的一个代码生成插件,主要通过自定义模板(基于 velocity)来生成各种你想要的代码。通常用于生成 Entity、Dao、Service、Controller。如果你动手能力强还可以用于生成 HTML、JS、PHP 等代码。理论上来说只要是与数据有关的代码都是可以生成的。</p> <h1 style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-size: 24px;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;line-height: 32px;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);margin-top: 28px !important;">1、安装(EasyCode)</h1> <p><img src="/upload/9bbd2fe60c772c6832e7df8de576ede5.other" data-cropx1="0" data-cropx2="640" data-cropy1="0" data-cropy2="400.1381692573402" data-ratio="0.6234375" src="https://mmbiz.qpic.cn/mmbiz_jpg/hUzEz6Bmcao8ukhV2D2mraxVGu2eRVJEbk3K3yddNdGyNKicm2QTBfmWBrofHSLdFiaWZVMt4lsp6oxmQ46Xia2Bw/640?wx_fmt=jpeg" data-type="jpeg" data-w="640" style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;margin: 10px auto;display: block;width: 578px;height: 361px;"></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;"><span style="background-color: rgb(255, 255, 255);color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;">我这里的话是已经那装好了。</span><br></p> <ul class="list-paddingleft-2" style="-webkit-tap-highlight-color: transparent;margin: 20px 30px;padding-left: 0px;list-style-position: outside;list-style-image: initial;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"> <li style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;list-style: inherit;"><p>建议大家在安装一个插件,叫做Lombok。Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。出现的神奇就是在源码中没有getter和setter方法,但是在编译生成的字节码文件中有getter和setter方法。</p></li> </ul> <section powered-by="xiumi.us" style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-size: 16px;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);text-align: center;color: rgb(62, 71, 83);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <h1 style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-size: 24px;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;line-height: 32px;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);margin-top: 28px !important;">2. 创建一个springboot项目</h1> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">如果不会创建的话可以翻看我前面写过的文章。初识 Springboot</p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;">创建完成后的目录结构</span>:</p> <p><img data-ratio="1.5374149659863945" src="/upload/e05929cc9c0ce3ef848608082515746.png" data-type="png" data-w="294" style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;margin: 10px auto;display: block;"></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;"><br></p> <h1 style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-size: 24px;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;line-height: 32px;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);margin-top: 28px !important;">3. 准备数据库表</h1> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;">表结构:</span></p> <pre style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-family: Monaco;font-size: 16px;white-space: pre-wrap;margin-top: 1em;margin-bottom: 1em;padding: 15px;border-width: 1px;border-style: solid;border-color: rgb(232, 232, 232);line-height: 1.5;color: rgb(34, 34, 34);background: rgb(250, 250, 250);overflow-x: auto;border-radius: 3px;text-align: left;box-shadow: rgba(217, 217, 217, 0.498) 0px 0px 0px 1px inset;"><code style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-family: Consolas, Menlo, Courier, monospace;font-size: 1em;border-width: 0px;border-style: initial;border-color: initial;"><span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">CREATE</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">TABLE</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(0, 117, 59);">`user`</span> (<br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(0, 117, 59);">`id`</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">int</span>(<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(168, 46, 46);">11</span>) <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">NOT</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(138, 115, 4);">NULL</span>,<br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(0, 117, 59);">`username`</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">varchar</span>(<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(168, 46, 46);">255</span>) <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">DEFAULT</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(138, 115, 4);">NULL</span>,<br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(0, 117, 59);">`gender`</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">varchar</span>(<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(168, 46, 46);">11</span>) <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">DEFAULT</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(138, 115, 4);">NULL</span>,<br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(0, 117, 59);">`age`</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">int</span>(<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(168, 46, 46);">11</span>) <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">DEFAULT</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(138, 115, 4);">NULL</span>,<br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(0, 117, 59);">`address`</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">varchar</span>(<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(168, 46, 46);">255</span>) <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">DEFAULT</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(138, 115, 4);">NULL</span>,<br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> PRIMARY <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">KEY</span> (<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(0, 117, 59);">`id`</span>)<br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;">) <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">ENGINE</span>=<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">InnoDB</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">DEFAULT</span> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">CHARSET</span>=utf8;</code></pre> <h1 style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-size: 24px;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;line-height: 32px;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);margin-top: 28px !important;">4. 在IDEA配置连接数据库</h1> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">① 按如下方式:选择数据源、选择mysql数据库(你也可以选择自己使用的数据库)</p> <p><img data-ratio="1.056930693069307" src="/upload/60312619ba0710bf522d25b5d964faf9.png" data-type="png" data-w="404" style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;margin: 10px auto;display: block;"></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;"><br></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">② 填写数据连接信息,点击测试。如果测试成功,点击OK。</p> <p><img data-ratio="0.7823691460055097" src="/upload/4db155359db6982a37d4842c4582e72e.png" data-type="png" data-w="726" style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;margin: 10px auto;display: block;"></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;"><br></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;">注意</span>:</p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">idea关联mysql时报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon'</p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;">解决办法:</span></p> <ul style="-webkit-tap-highlight-color: transparent;margin: 20px 30px;list-style-position: outside;list-style-image: initial;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"> <li style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;list-style: inherit;"><p>进入cmd命令窗口,连接数据库 mysql -uroot -p,回车, 输入密码,回车,输入:show variables like’%time_zone’;</p></li> <li style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;list-style: inherit;"><p>设置北京时区,set global time_zone=’+8:00’;</p></li> </ul> <h1 style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-size: 24px;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;line-height: 32px;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);margin-top: 28px !important;">二、生成代码</h1> <h1 style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-size: 24px;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;line-height: 32px;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);margin-top: 28px !important;">1. IDEA中开始生成代码</h1> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">① 找到所需要生成代码的表,然后点击Generate Code。</p> <p><img data-ratio="1.0625" src="/upload/96239a9d5f0d4ee54893a3b76f84d595.png" data-type="png" data-w="480" style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;margin: 10px auto;display: block;"></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;"><br></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">② 选择包路径以及生成的模板</p> <p><img data-ratio="0.6742209631728046" src="/upload/db648e256e505dd1305a93b81ee48ae5.png" data-type="png" data-w="353" style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;margin: 10px auto;display: block;"></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;"><br></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">③ 生成的代码结构</p> <p><img data-ratio="1.5117845117845117" src="/upload/494271356fdf811aa4722347a7dd1e7f.png" data-type="png" data-w="297" style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;margin: 10px auto;display: block;"></p> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;"><br></p> <h1 style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-size: 24px;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;line-height: 32px;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);margin-top: 28px !important;">2. 导入需要的依赖</h1> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;margin-top: 16px;margin-bottom: 16px;border-width: 0px;border-style: initial;border-color: initial;color: rgb(34, 34, 34);font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-weight: 700;border-width: 0px;border-style: initial;border-color: initial;">pom.xml</span></p> <pre style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-family: Monaco;font-size: 16px;white-space: pre-wrap;margin-top: 1em;margin-bottom: 1em;padding: 15px;border-width: 1px;border-style: solid;border-color: rgb(232, 232, 232);line-height: 1.5;color: rgb(34, 34, 34);background: rgb(250, 250, 250);overflow-x: auto;border-radius: 3px;text-align: left;box-shadow: rgba(217, 217, 217, 0.498) 0px 0px 0px 1px inset;"><code style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;font-family: Consolas, Menlo, Courier, monospace;font-size: 1em;border-width: 0px;border-style: initial;border-color: initial;"><span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">properties</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">java.version</span>&gt;</span>1.8<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">java.version</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"><span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">properties</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"><span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependencies</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependency</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">groupId</span>&gt;</span>org.springframework.boot<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">groupId</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">artifactId</span>&gt;</span>spring-boot-starter-web<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">artifactId</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependency</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependency</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">groupId</span>&gt;</span>org.springframework.boot<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">groupId</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">artifactId</span>&gt;</span>spring-boot-starter<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">artifactId</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependency</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependency</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">groupId</span>&gt;</span>org.springframework.boot<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">groupId</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">artifactId</span>&gt;</span>spring-boot-devtools<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">artifactId</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">optional</span>&gt;</span>true<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">optional</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependency</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(153, 153, 153);">&lt;!-- springboot与mybatis的整合包 --&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependency</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">groupId</span>&gt;</span>org.mybatis.spring.boot<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">groupId</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">artifactId</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">version</span>&gt;</span>2.1.1<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">version</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;/<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependency</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(153, 153, 153);">&lt;!-- mysql驱动包 --&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;"> <span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;color: rgb(17, 75, 166);">&lt;<span style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">dependency</span>&gt;</span><br style="-webkit-tap-highlight-color: transparent;box-sizing: border-

七夕之夜,如何保证私密信息不泄露?

作者:微信小助手

<section style="line-height: 1.75em;" data-mpa-powered-by="yiban.io"> <span style="font-size: 15px;letter-spacing: 1px;">七夕之夜,想和另一半聊一些私密的话,如何保证聊天内容不被黑客窥探,看完此文,终于略知一二了。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">一、初级阶段:信息裸传</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <img data-ratio="0.38366336633663367" data-s="300,640" src="/upload/51e3b0ed971897ceb57144337d04f345.png" data-type="png" data-w="404"> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">特点:</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(1)在网络上传递明文;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">黑客定理一:</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;color: rgb(255, 76, 0);">网络上传递的数据是不安全的,网络属于黑客公共场所,能被截取。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">如何改进呢?</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">很容易想到,先加密,再传输。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">二、进阶阶段:传输密文</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <img data-ratio="0.3138401559454191" data-s="300,640" src="/upload/4d02e74957b3cadc3b5e1765af4b46d0.png" data-type="png" data-w="513"> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">特点:</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(1)服务端和客户端先约定好加密算法,加密密钥;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(2)客户端,传输前用约定好的密钥加密;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(3)传输密文;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(4)服务端,收到消息后用约定好的密钥解密;</span> </section> <section style="line-height: 1.75em;"> <br> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">黑客定理二:</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;color: rgb(255, 76, 0);">客户端是不安全的,属于黑客本地范畴,能被逆向工程。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">任何客户端与服务端提前约定好的算法与密钥都是不安全的,那如何改进呢?</span></strong> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">不能固定密钥,一个用户一个密钥。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">三、中级阶段:一人一密,服务端生成密钥</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <img data-ratio="0.3895216400911162" data-s="300,640" src="/upload/d72d1757bf6bc60983cf6f56002c1365.png" data-type="png" data-w="439"> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">特点:</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(1)客户端和服务端提前约定好加密算法,在传递消息前,先协商密钥;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(2)客户端,请求密钥;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(3)服务端,返回密钥;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(4)然后用协商密钥加密消息,传输密文;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">这么传输安全么?</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">答案是否定的。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(1)根据黑客定理一,网上传输的内容是不安全的,于是乎,黑客能得到加密key=X;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(2)根据客定理二,客户端和服务端提前约定的加密算法是不安全的,于是乎,黑客能得到加密算法;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(3)于是乎,黑客截取后续传递的密文,可以用对应的算法和密钥解密;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">应该如何优化呢?</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">根本上,密钥不能在网络上直接传输。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">四、再进阶阶段:一人一密,客户端确定密钥,密钥不再传输</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <img data-ratio="0.33030852994555354" data-s="300,640" src="/upload/3e996e8c90b008831877a327180add02.jpg" data-type="jpeg" data-w="551"> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">特点:</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(1)协商的密钥无需在网络传输;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(2)使用“具备用户特性的东西”作为加密密钥,例如:用户密码的散列值;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(3)一人一密,每个人的密钥不同;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(4)然后密钥加密消息,传输密文;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(5)服务端从db里获取这个“具备用户特性的东西”,解密;</span> </section> <section style="line-height: 1.75em;"> <br> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">黑客定理三:</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;color: rgb(255, 76, 0);">用户客户端内存是安全的,属于黑客远端范畴,认为是安全的。</span> </section> <section style="line-height: 1.75em;"> <span style="color: rgb(0, 128, 255);"><em><span style="color: rgb(0, 128, 255);font-size: 15px;letter-spacing: 1px;">画外音:中了木马,电脑被控制了另说。</span></em></span> <br> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">使用“具备用户特性的东西”作为加密密钥,一人一密,是安全的。但这仍不是最优方案。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">五、高级阶段:一次一密,密钥协商</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;color: rgb(255, 76, 0);">每次通信前,都进行密钥协商,一次一密。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">密钥协商过程,如下图所述,需要</span> <span style="font-size: 15px;letter-spacing: 1px;color: rgb(255, 76, 0);">随机生成三次动态密钥</span> <span style="font-size: 15px;letter-spacing: 1px;">:</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(1)两次非对称加密密钥(公钥,私钥);</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(2)一次对称加密密钥;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">此称为,<strong>安全信道建立的“三次握手”</strong>,安全信道建立之后,再进行密文发送。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <img data-ratio="0.3011456628477905" data-s="300,640" src="/upload/cf29b74eb3c4bc6126799dafd2311f59.jpg" data-type="jpeg" data-w="611" style="line-height: 25.6px;white-space: normal;"> </section> <section style="white-space: normal;line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">密钥交换的</span> <span style="font-size: 15px;letter-spacing: 1px;">步骤</span> <span style="font-size: 15px;letter-spacing: 1px;">为:</span> <br> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(1)服务端<strong>随机生成公私钥对</strong>(公钥pk1,私钥pk2),并将公钥pk1传给客户端;</span> </section> <section style="line-height: 1.75em;"> <span style="color: rgb(0, 128, 255);"><em><span style="color: rgb(0, 128, 255);font-size: 15px;letter-spacing: 1px;">画外音:此时黑客能截获pk1。</span></em></span> </section> <section style="line-height: 1.75em;"> <span style="color: rgb(0, 128, 255);"><em><span style="color: rgb(0, 128, 255);font-size: 15px;letter-spacing: 1px;"><br></span></em></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(2)客户端<strong>随机生成公私钥对</strong>(公钥pk11,私钥pk22),并将公钥pk11,通过pk1加密,传给服务端,<span style="font-size: 15px;letter-spacing: 1px;">服务端收到密文,用私钥</span><span style="font-size: 15px;letter-spacing: 1px;">pk2</span><span style="font-size: 15px;letter-spacing: 1px;">解密,得到</span><span style="font-size: 15px;letter-spacing: 1px;">pk11</span>;</span> </section> <section style="line-height: 1.75em;"> <span style="color: rgb(0, 128, 255);"><em><span style="color: rgb(0, 128, 255);font-size: 15px;letter-spacing: 1px;">画外音:此时黑客能截获密文,也知道是通过pk1加密的,但由于黑客不知道私钥pk2,是无法解密的。</span></em></span> </section> <section style="line-height: 1.75em;"> <span style="color: rgb(0, 128, 255);"><em><span style="color: rgb(0, 128, 255);font-size: 15px;letter-spacing: 1px;"><br></span></em></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(3)服务端<strong>随机生成对称加密密钥</strong>key=X,用pk11加密,传给客户端,<span style="font-size: 15px;letter-spacing: 1px;">客户端收到密文,用私钥</span><span style="font-size: 15px;letter-spacing: 1px;">pk22</span><span style="font-size: 15px;letter-spacing: 1px;">解密,可到</span><span style="font-size: 15px;letter-spacing: 1px;">key=X</span>;</span> </section> <section style="line-height: 1.75em;"> <span style="color: rgb(0, 128, 255);"><em><span style="color: rgb(0, 128, 255);font-size: 15px;letter-spacing: 1px;">画外音:同理,黑客由密文无法解密出key。</span></em></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">至此,安全信道建立完毕,后续通讯用</span> <span style="font-size: 15px;letter-spacing: 1px;">key=X</span> <span style="font-size: 15px;letter-spacing: 1px;">加密,以保证信息的安全性。</span> <br> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <strong><span style="font-size: 15px;letter-spacing: 1px;">六、总结</span></strong> <span style="font-size: 15px;letter-spacing: 1px;"></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">信息安全方案设计三大假设:</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(1)网络上传递的数据是不安全的,能被截取;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(2)用户客户端是不安全的</span> <span style="font-size: 15px;letter-spacing: 1px;">,属于黑客本地范畴,能被逆向工程;</span> <br> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(3)</span> <span style="font-size: 15px;letter-spacing: 1px;">客户端内存是安全的</span> <span style="font-size: 15px;letter-spacing: 1px;">,属于黑客远端范畴,可以认为是安全的;</span> <br> </section> <section style="line-height: 1.75em;"> <br> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">对于信息安全传输的不同阶段:</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(1)明文消息传递如同裸奔,不安全;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(2)客户端和服务端提前约定加密算法和密钥,不安全;</span> </section> <section style="line-height: 1.75em;"> <span style="color: rgb(0, 128, 255);"><em><span style="color: rgb(0, 128, 255);font-size: 15px;letter-spacing: 1px;">画外音:好多公司都是这么实现的=_=。</span></em></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(3)一人一密,服务端随机生成密钥,发送给客户端,不安全;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(4)一人一密,客户端使用“具备用户特性的东西”作为加密密钥,安全;</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">(5)一次一密,三次握手建立安全信道,安全;</span> </section> <section style="line-height: 1.75em;"> <br> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">好了,这下可以嘿嘿嘿了。</span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;"><br></span> </section> <section style="line-height: 1.75em;"> <span style="font-size: 15px;letter-spacing: 1px;">对了,很多公司说,我们绝不存储,绝不窥探用户聊天记录,你信不?</span> </section>

Redisson 是如何实现分布式锁的?

作者:微信小助手

<section style="display: none;" data-tools="新媒体管家" data-label="powered by xmt.cn" data-mpa-powered-by="yiban.io"> <br> </section> <p style="text-align: center;"><span style="font-size: 13px;color: rgb(136, 136, 136);font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;letter-spacing: 0.544px;">点击上方蓝色“</span><span style="font-size: 13px;font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;letter-spacing: 0.544px;color: rgb(0, 128, 255);">程序猿DD</span><span style="font-size: 13px;color: rgb(136, 136, 136);font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;letter-spacing: 0.544px;">”,选择“设为星标”</span><strong style="max-width: 100%;letter-spacing: 0.544px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 16px;white-space: pre-line;background-color: rgb(255, 255, 255);text-align: right;box-sizing: border-box !important;overflow-wrap: break-word !important;"></strong></p> <p style="max-width: 100%;min-height: 1em;white-space: normal;text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;font-size: 13px;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;">回复“</span><span style="max-width: 100%;font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;letter-spacing: 0.544px;color: rgb(0, 128, 255);overflow-wrap: break-word !important;box-sizing: border-box !important;">资源</span><span style="max-width: 100%;color: rgb(136, 136, 136);font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;">”获取独家整理的学习资料!</span></span></p> <p style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;min-height: 1em;white-space: normal;text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;font-size: 14px;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;"><img data-backh="34" data-backw="540" data-ratio="0.0625" data-s="300,640" data-type="jpeg" data-w="640" width="100%" src="/upload/8c292e55ba5a23cb6ebc11f2a2c4fece.jpg" style="font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;visibility: visible !important;width: 654px !important;"></span></p> <p style="max-width: 100%;min-height: 1em;white-space: normal;font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: left;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;letter-spacing: 0.544px;font-size: 13px;font-family: Optima-Regular, PingFangTC-light;color: rgb(178, 178, 178);overflow-wrap: break-word !important;box-sizing: border-box !important;">作者 |&nbsp;<span style="font-family: Optima-Regular, PingFangTC-light;font-size: 13px;max-width: 100%;letter-spacing: 0.544px;text-align: start;widows: 1;background-color: rgb(255, 255, 255);overflow-wrap: break-word !important;box-sizing: border-box !important;">bravoban</span><br style="ma

分布式秒杀实战之订单数据分表

作者:微信小助手

<section style="margin-right: 8px;margin-left: 8px;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;min-height: 1em;background-color: rgb(255, 255, 255);text-size-adjust: auto;font-size: 15px;word-spacing: 2px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;font-size: 14px;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">点击▲关注 “</span> <span style="max-width: 100%;font-size: 14px;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;letter-spacing: 0.544px;color: rgb(0, 128, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">爪哇笔记</span> <span style="max-width: 100%;font-size: 14px;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">”&nbsp; &nbsp;给公众号标星置顶</span> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> </section> <section style="margin-right: 8px;margin-left: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;min-height: 1em;font-size: 16px;background-color: rgb(255, 255, 255);text-align: center;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">更多精彩 第一时间直达</span> </section> <section style="margin-left: 8px;margin-right: 8px;"> <span style="font-size: 16px;"><img data-ratio="0.6666666666666666" src="/upload/5a979abca303d7404f975864308a7ae1.jpg" data-type="jpeg" data-w="1200"></span> <br> </section> <h4 style="box-sizing: border-box;font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica;font-weight: bold;line-height: 1.4;color: rgb(36, 41, 46);margin: 1em 8px 16px;font-size: 1.25em;-webkit-tap-highlight-color: transparent;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(0, 209, 0);"><strong><span style="color: rgb(0, 209, 0);font-size: 18px;">前言</span></strong></span></h4> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 一般来说电商的日订单都是百千万级甚至是亿万级别的了,小小的数据库肯定是撑不住的,这时候就要提前考虑分库分表了。 </section> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 国内一般大厂规则参考: </section> <ul style="margin-bottom: 16px;padding-left: 2em;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;" class="list-paddingleft-2"> <li style="box-sizing: border-box;"><p>单表500万条记录,正常水平</p></li> <li style="box-sizing: border-box;"><p>800万条警戒线</p></li> <li style="box-sizing: border-box;"><p>1000万条必须要分库分表</p></li> </ul> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 一般业界,对订单数据的分库分表,有两类思路:按照订单号来切分、按照用户id来切分,当然各有利弊,这里不细说。 </section> <h4 style="box-sizing: border-box;font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica;font-weight: bold;line-height: 1.4;color: rgb(36, 41, 46);margin: 1em 8px 16px;font-size: 1.25em;-webkit-tap-highlight-color: transparent;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="font-size: 18px;color: rgb(0, 209, 0);"><strong>手动分表</strong></span></h4> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 这个在秒杀一中已有体现,这里仅仅是分表而已,提供一种思路,供参考,测试的时候自行建表。 </section> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 按照用户&nbsp; <code style="box-sizing: border-box;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 3px;color: rgb(199, 37, 78);background: rgb(246, 246, 246);border-radius: 3px;margin-right: 4px;margin-left: 5px;border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);">ID</code>&nbsp;来做&nbsp; <code style="box-sizing: border-box;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 3px;color: rgb(199, 37, 78);background: rgb(246, 246, 246);border-radius: 3px;margin-right: 4px;margin-left: 5px;border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);">hash</code>&nbsp;分散订单数据。为了减少迁移的数据量,一般扩容是以倍数的形式增加。比如原来是8个库,扩容的时候,就要增加到16个库,再次扩容,就增加到32个库。这样迁移的数据量,就小很多了。 </section> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 这个问题不算很大问题,毕竟一次扩容,可以保证比较长的时间,而且使用倍数增加的方式,已经减少了数据迁移量。 </section> <pre style="box-sizing: border-box;overflow: auto;font-variant-numeric: normal;font-variant-east-asian: normal;font-stretch: normal;font-size: 11.9px;line-height: 1.45;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 1em;margin-bottom: 16px;word-break: break-all;overflow-wrap: break-word;background: 0% 0% / 30px, 0% 0% / 30px rgb(248, 248, 248);border-width: initial;border-style: none;border-color: initial;border-radius: 4px;-webkit-tap-highlight-color: transparent;max-height: 35em;text-align: left;"> <section style="margin-left: 8px;margin-right: 8px;"> <code style="box-sizing: border-box;font-family: &quot;Source Code Pro&quot;, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, &quot;Microsoft Yahei&quot;, monospace;font-size: 1.1em;color: inherit;background: transparent;border-radius: 3px;white-space: inherit;border-width: 0px;border-style: initial;border-color: initial;word-break: break-all;display: inline;max-width: initial;overflow: hidden auto;line-height: inherit;overflow-wrap: normal;">String table = <span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;color: rgb(221, 17, 68);"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">"success_killed_"</span></span></span>+userId%<span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;color: rgb(0, 128, 128);"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">8</span></span></span>;<br>nativeSql = <span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;color: rgb(221, 17, 68);"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">"INSERT INTO "</span></span></span>+table+<span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;color: rgb(221, 17, 68);"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">" (seckill_id, user_id,state,create_time)VALUES(?,?,?,?)"</span></span></span>;<br>Object[] <span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;font-weight: bold;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">params</span></span></span> = <span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;font-weight: bold;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">new</span></span></span> Object[]{seckillId,userId,(<span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;font-weight: bold;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">short</span></span></span>)<span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;color: rgb(0, 128, 128);"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">0</span></span></span>,<span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;font-weight: bold;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">new</span></span></span> Timestamp(System.currentTimeMillis())};<br>dynamicQuery.nativeExecuteUpdate(nativeSql,<span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;font-weight: bold;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">params</span></span></span>);</code> </section></pre> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 手动撕业务逻辑是不是很 <code style="box-sizing: border-box;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 3px;color: rgb(199, 37, 78);background: rgb(246, 246, 246);border-radius: 3px;margin-right: 4px;margin-left: 5px;border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);">low</code>?今天给大家推荐一款分表神器。 </section> <h4 style="box-sizing: border-box;font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica;font-weight: bold;line-height: 1.4;color: rgb(36, 41, 46);margin: 1em 8px 16px;font-size: 1.25em;-webkit-tap-highlight-color: transparent;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(0, 209, 0);"><strong><span style="color: rgb(0, 209, 0);font-size: 18px;">自动分表</span></strong></span></h4> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 墙裂推荐使用开源的分布式数据库中间件&nbsp; <code style="box-sizing: border-box;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 3px;color: rgb(199, 37, 78);background: rgb(246, 246, 246);border-radius: 3px;margin-right: 4px;margin-left: 5px;border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);">ShardingSphere</code>,资料丰富,入手简单, 并且已于2020年4月16日成为&nbsp; <code style="box-sizing: border-box;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 3px;color: rgb(199, 37, 78);background: rgb(246, 246, 246);border-radius: 3px;margin-right: 4px;margin-left: 5px;border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);">Apache</code>&nbsp;软件基金会的顶级项目,值得信赖。 </section> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> <img data-ratio="0.5610955056179775" src="/upload/97d80c40964b8fb2dd6113032e08aa20.png" data-type="png" data-w="1424" style="box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;vertical-align: middle;" title="null"> </section> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 适用于任何基于 <code style="box-sizing: border-box;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 3px;color: rgb(199, 37, 78);background: rgb(246, 246, 246);border-radius: 3px;margin-right: 4px;margin-left: 5px;border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);">JDBC</code>的 <code style="box-sizing: border-box;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 3px;color: rgb(199, 37, 78);background: rgb(246, 246, 246);border-radius: 3px;margin-right: 4px;margin-left: 5px;border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);">ORM</code>框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。 </section> <ul style="margin-bottom: 16px;padding-left: 2em;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;" class="list-paddingleft-2"> <li style="box-sizing: border-box;"><p>支持任何第三方的数据库连接池,如:DBCP, C3P0, BoneCP, Druid, HikariCP等。</p></li> <li style="box-sizing: border-box;"><p>支持任意实现JDBC规范的数据库。目前支持 MySQL,Oracle,SQLServer,PostgreSQL以及任何遵循SQL92标准的数据库。</p></li> </ul> <h5 style="box-sizing: border-box;font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica;font-weight: bold;line-height: 1.4;color: rgb(36, 41, 46);margin: 1em 8px 16px;font-size: 1em;-webkit-tap-highlight-color: transparent;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(0, 209, 0);"><strong><span style="color: rgb(0, 209, 0);font-size: 16px;">集成 SpringBoot</span></strong></span></h5> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 案例使用&nbsp; <code style="box-sizing: border-box;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 3px;color: rgb(199, 37, 78);background: rgb(246, 246, 246);border-radius: 3px;margin-right: 4px;margin-left: 5px;border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);">SpringBoot2.2.6.RELEASE + JPA</code>,部分坐标请自行引入。 </section> <section style="box-sizing: border-box;margin-bottom: 16px;-webkit-tap-highlight-color: transparent;color: rgb(36, 41, 46);font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;;font-size: 14px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);margin-left: 8px;margin-right: 8px;"> 引入坐标 <code style="box-sizing: border-box;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 3px;color: rgb(199, 37, 78);background: rgb(246, 246, 246);border-radius: 3px;margin-right: 4px;margin-left: 5px;border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);">pom.xml</code>: </section> <pre style="box-sizing: border-box;overflow: auto;font-variant-numeric: normal;font-variant-east-asian: normal;font-stretch: normal;font-size: 11.9px;line-height: 1.45;font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace;padding: 1em;margin-bottom: 16px;word-break: break-all;overflow-wrap: break-word;background: 0% 0% / 30px, 0% 0% / 30px rgb(248, 248, 248);border-width: initial;border-style: none;border-color: initial;border-radius: 4px;-webkit-tap-highlight-color: transparent;max-height: 35em;text-align: left;"> <section style="margin-left: 8px;margin-right: 8px;"> <code style="box-sizing: border-box;font-family: &quot;Source Code Pro&quot;, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, &quot;Microsoft Yahei&quot;, monospace;font-size: 1.1em;color: inherit;background: transparent;border-radius: 3px;white-space: inherit;border-width: 0px;border-style: initial;border-color: initial;word-break: break-all;display: inline;max-width: initial;overflow: hidden auto;line-height: inherit;overflow-wrap: normal;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;color: rgb(0, 0, 128);"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">&lt;</span></span><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">dependency</span></span></span></span></span><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">&gt;</span></span></span><br> <span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;color: rgb(0, 0, 128);"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">&lt;</span></span><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">groupId</span></span></span></span></span><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">&gt;</span></span></span>org.apache.shardingsphere<span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;color: rgb(0, 0, 128);"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">&lt;/</span></span><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">groupId</span></span></span></span></span><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">&gt;</span></span></span><br> <span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;color: rgb(0, 0, 128);"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;">&lt;</span></span><span style="box-sizing: border-box;-webkit-tap-highlight-color: transparent;"

我参与了两个接近100k+star的开源项目!聊聊开源项目贡献指南

作者:微信小助手

<section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="padding-right: 10px;padding-left: 10px;line-height: 1.6;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;font-size: 15px;letter-spacing: 0.05em;" data-mpa-powered-by="yiban.io"> <p data-tool="mdnice编辑器" style="font-size: 16px;padding-top: 8px;padding-bottom: 8px;line-height: 26px;color: black;margin: 1em 4px;">给 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">SkyWalking</code> 以及 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">JavaGuide</code> 项目贡献后的总结</p> <ol data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;" class="list-paddingleft-2"> <li> <section style="line-height: 26px;color: rgb(1, 1, 1);margin-top: 10px;margin-bottom: 10px;"> <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">JavaGuide</code>: <strong><span style="color: rgb(0, 128, 255);">https://github.com/Snailclimb/JavaGuide</span></strong> </section></li> <li> <section style="line-height: 26px;color: rgb(1, 1, 1);margin-top: 10px;margin-bottom: 10px;"> <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">SkyWalking</code>: <span style="color: rgb(0, 128, 255);"><strong>https://github.com/apache/skywalking</strong></span> </section></li> </ol> <h2 data-tool="mdnice编辑器" style="min-height: 32px;line-height: 28px;color: rgb(70, 51, 118);border-bottom: 1px solid rgb(70, 51, 118);border-top-color: rgb(70, 51, 118);border-right-color: rgb(70, 51, 118);border-left-color: rgb(70, 51, 118);font-size: 22px;margin: 1em auto;padding-top: 0.5em;padding-bottom: 0.5em;text-align: center;width: 85%;font-weight: bold;display: flex;flex-direction: column;justify-content: center;"><span style="display: none;"></span>1. 本地开发</h2> <p data-tool="mdnice编辑器" style="font-size: 16px;padding-top: 8px;padding-bottom: 8px;line-height: 26px;color: black;margin: 1em 4px;">以 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">SkyWalking</code> 举例。在本地编译源码前,先查看相关的文档:https://github.com/apache/skywalking/blob/v8.0.1/docs/en/guides/How-to-build.md 。大致了解后,我们就可以开始操作了。</p> <ol data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;" class="list-paddingleft-2"> <li> <section style="line-height: 26px;color: rgb(1, 1, 1);margin-top: 10px;margin-bottom: 10px;"> 在 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">Github</code> 上 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">fork</code> 你想要贡献的项目 </section></li> <li> <section style="line-height: 26px;color: rgb(1, 1, 1);margin-top: 10px;margin-bottom: 10px;"> 接着在本地拉取自己的项目: <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">git clone --recurse-submodules https://github.com/$Name/skywalking.git</code> </section></li> </ol> <blockquote data-tool="mdnice编辑器" style="font-size: 0.9em;overflow: auto;padding: 10px 10px 10px 20px;margin: 10px 5px;border-left-color: rgb(150, 84, 181);border-right: 1px solid rgb(150, 84, 181);color: rgb(97, 97, 97);quotes: none;background: rgb(251, 249, 253);"> <p style="font-size: 16px;padding-top: 8px;padding-bottom: 8px;color: black;line-height: 26px;">这是因为 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">SkyWalking</code> 它包含了子仓库,因此加入了 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">--recurse-submodules</code> 参数,它可以把主仓库和子仓库源码都同时拉取。</p> </blockquote> <p data-tool="mdnice编辑器" style="font-size: 16px;padding-top: 8px;padding-bottom: 8px;line-height: 26px;color: black;margin: 1em 4px;">代码拉到本地后,接着我们使用 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">idea</code> 打开该项目。&nbsp;但是可能我们网络不够给力或有“<em style="color: rgb(250, 157, 59);">奇怪的力量</em>”干扰,我们则需要改动一些配置以方便快速编译。&nbsp;对 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">maven</code> 来说一般都是设置 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">maven</code> 加速器,如果你拉的是 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">docker</code> 相关,还需要配置 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">docker</code> 容器阿里云地址加速。<br>而我们这里主要是设置 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">maven</code> 阿里云镜像以及 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(150, 84, 181);">npm</code> 设置淘宝镜像。</p> <h3 data-tool="mdnice编辑器" style="font-size: 20px;margin-top: 1.2em;margin-bottom: 1em;font-weight: bold;color: rgb(70, 51, 118);padding-left: 10px;border-left: 2px solid rgb(70, 51, 118);"><span style="display: none;"></span>1.1 设置 maven 加速<span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px;padding-top: 8px;padding-bottom: 8px;line-height: 26px;color: black;margin: 1em 4px;">当你执行 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot

Lambda表达式,真爽!

作者:微信小助手

<p data-lake-id="f47942fff38c686ed626a530b6d84219" style="text-align: center;font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;" data-mpa-powered-by="yiban.io"><span data-mce-style="font-size: 10px" style="font-size: 13px;"><span style="color: #888888;">点击上方蓝色“</span></span><span style="color: rgb(24, 144, 255);font-size: 13px;" data-mce-style="font-size: 10px">后端面试那些事儿</span><span data-mce-style="font-size: 10px" style="font-size: 13px;"><span style="color: #888888;">”,选择“设为星标”</span></span></p> <p data-lake-id="eca2a1864e13b3e1b84aafe9cb4abdff" style="text-align: center;font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="color: rgb(140, 140, 140);font-size: 13px;" data-mce-style="font-size: 10px">学最好的别人,做最好的我们</span></p> <p data-lake-id="0ce54dbe16877f3c73047d1e0add7e36" style="text-align: center;font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><img data-ratio="0.6666666666666666" src="/upload/a883191e7f6dc9fd9ee64a59721f9dae.jpg" data-type="jpeg" data-w="960"></p> <blockquote class="js_blockquote_wrap" data-type="2" data-url="" data-author-name="" data-content-utf8-length="37" data-source-title="" style="white-space: normal;"> <section class="js_blockquote_digest"> <section> <p style="text-align: left;"><span style="font-size: 12px;">作者:<span style="color: rgb(0, 0, 0);font-family: &quot;Helvetica Neue&quot;, Helvetica, Verdana, Arial, sans-serif;background-color: rgb(255, 255, 255);">&nbsp;</span>泡代码的小二黑&nbsp;</span></p> <p style="text-align: left;"><span style="font-size: 12px;">来源:https://urlify.cn/NnIJ73</span></p> </section> </section> </blockquote> <h2 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"># 简介</span></strong><br></h2> <p style="white-space: normal;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">JDK的升级的目的有以下几个:增加新的功能、修复bug、性能优化、简化代码等几个方面,Lambda表达式就是属于简化代码,用于简化匿名实现类,提供一种更加简洁的写法。Lambda表达式在Swift语言中称之为代码块,Lambda表达式可以认为是一种特殊的接口,该接口必须只有一个抽象方法。</span></p> <p style="white-space: normal;text-align: left;"><br></p> <h4 style="white-space: normal;text-align: left;"><span style="font-family: Optima-Regular, PingFangTC-light;background-color: rgb(255, 218, 169);">语法</span></h4> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer">(参数类型 参数名, 数参数类型 参数名<span class="code-snippet__number">2.</span>..) -&gt; { </span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// code</span></span></code><code><span class="code-snippet_outer">};</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">小括号()中的内容就是方法中的参数列表包括参数类型、参数名,其中参数类型是可以省略的,当参数个数只有一个时也可以省略掉小括号;</span></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">&nbsp;<br>花括号{}中的内容就是方法中的方法体,当方法体中只有一行代码时可以省略{},当方法体中只有一行代码并且需要返回值时也可以省略掉return;&nbsp;</span></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;"><br>由于Lambda表达式是匿名实现类的简写,是一种特殊的接口,当赋值给一个变量时也少不掉分号;</span><br></p> <p style="white-space: normal;text-align: left;"><br></p> <h4 style="white-space: normal;text-align: left;"><span style="font-family: Optima-Regular, PingFangTC-light;background-color: rgb(255, 218, 169);">Lambda表达式的作用</span></h4> <p style="white-space: normal;"><br></p> <ul class="list-paddingleft-2" style="width: 577.422px;white-space: normal;"> <li style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;"><p style="text-align: left;">简化匿名实现类的书写</p></li> <li style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;"><p style="text-align: left;">作为函数中的参数来传递</p></li> </ul> <h3 style="white-space: normal;text-align: left;"></h3> <h3 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"><br></span></strong></h3> <h3 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"># 示例</span></strong></h3> <p style="white-space: normal;"><br></p> <h4 style="white-space: normal;text-align: left;"><span style="font-family: Optima-Regular, PingFangTC-light;background-color: rgb(255, 218, 169);">示例1:两个参数,一个返回值</span></h4> <p style="white-space: normal;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">IHello 一个很普通的接口,但接口中只能有一个抽象方法</span></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="typescript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">interface</span> IHello {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> sayHello(<span class="code-snippet__built_in">String</span> name, <span class="code-snippet__built_in">String</span> msg);</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">Main</span></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="typescript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> Main {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__built_in">void</span> main(<span class="code-snippet__built_in">String</span>[] args) {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 将一个Lambda表达式赋值给一个接口,说明Lambda表达式就是一种接口数据类型,只不过该接口只能有一个抽象方法</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 参数列表可以省略参数类型,可以写成(name, msg),</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 在JDK1.8中有个叫做类型推断的东西,可以自动推断出参数的类型,</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 因为IHello中只有一个抽象方法,知道方法了就知道参数列表了,从而就能推出参数类型来</span></span></code><code><span class="code-snippet_outer">IHello iHello = (<span class="code-snippet__built_in">String</span> name, <span class="code-snippet__built_in">String</span> msg) -&gt; {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> hello = name + <span class="code-snippet__string">": "</span> + msg;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> hello;</span></code><code><span class="code-snippet_outer">};</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 调用接口的方法</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> content = iHello.sayHello(<span class="code-snippet__string">"mengday"</span>, <span class="code-snippet__string">"happy new year everyone!"</span>);</span></code><code><span class="code-snippet_outer">System.out.println(content);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <h4 style="white-space: normal;text-align: left;"><span style="font-family: Optima-Regular, PingFangTC-light;background-color: rgb(255, 218, 169);">示例2:一个参数,一个返回值</span></h4> <p style="white-space: normal;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="typescript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">interface</span> IHello {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> sayHello(<span class="code-snippet__built_in">String</span> name);</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">Main</span></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="cs"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> <span class="code-snippet__title">Main</span> {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">main</span>(<span class="code-snippet__params">String[] args</span>)</span> {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 参数列表可以省略参数类型,当只有一个参数时可以省略小括号 (String name) --&gt; (name) --&gt; name</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 当方法体中只有一行代码并且有返回值时可以同时省略花括号和return</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// { return name + ": " + "happy new year everyone!";} --&gt; name + ": " + "happy new year everyone!";</span></span></code><code><span class="code-snippet_outer">IHello iHello = name -&gt; name + <span class="code-snippet__string">": "</span> + <span class="code-snippet__string">"happy new year everyone!"</span>;</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer">String content = iHello.sayHello(<span class="code-snippet__string">"mengday"</span>);</span></code><code><span class="code-snippet_outer">System.<span class="code-snippet__keyword">out</span>.println(content);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <h4 style="white-space: normal;text-align: left;"><span style="font-family: Optima-Regular, PingFangTC-light;background-color: rgb(255, 218, 169);">示例3:没有参数,没有返回值</span></h4> <p style="white-space: normal;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="cs"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">interface</span> <span class="code-snippet__title">IHello</span> {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">void</span> <span class="code-snippet__title">sayHello</span>(<span class="code-snippet__params"></span>)</span>;</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">main</span></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="cs"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> <span class="code-snippet__title">Main</span> {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">main</span>(<span class="code-snippet__params">String[] args</span>)</span> {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 当表达式没有参数时一对小括号是不能省略掉的</span></span></code><code><span class="code-snippet_outer">IHello iHello = () -&gt; System.<span class="code-snippet__keyword">out</span>.println(<span class="code-snippet__string">"mengday: happy new year everyone!"</span>);</span></code><code><span class="code-snippet_outer">iHello.sayHello();</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-family: Optima-Regular, PingFangTC-light;font-size: 16px;">从这三个示例中我们发现我们只定义了接口,并没有定义实现类,而是通过Lambda表达式来代替了实现类。&nbsp;</span><br></p> <p style="white-space: normal;text-align: left;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">注意:Lambda接口只能有一个抽象方法,可以同时拥有多个静态方法和默认方法。</span></p> <p style="white-space: normal;text-align: left;"><br></p> <h4 style="white-space: normal;text-align: left;"><span style="font-family: Optima-Regular, PingFangTC-light;background-color: rgb(255, 218, 169);">示例4:Lambda表达式参数</span></h4> <p style="white-space: normal;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="cs"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">interface</span> <span class="code-snippet__title">IHello</span> { <span class="code-snippet__function"><span class="code-snippet__keyword">void</span> <span class="code-snippet__title">sayHello</span>(<span class="code-snippet__params">String name</span>)</span>;</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="cs"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> <span class="code-snippet__title">Main</span> {</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">sayHello</span>(<span class="code-snippet__params">IHello iHello, String name</span>)</span> {</span></code><code><span class="code-snippet_outer">iHello.sayHello(name);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">main</span>(<span class="code-snippet__params">String[] args</span>)</span> {</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer">IHello iHello = name -&gt; {</span></code><code><span class="code-snippet_outer">String content = name + <span class="code-snippet__string">": "</span> + <span class="code-snippet__string">"happy new year everyone!"</span>;</span></code><code><span class="code-snippet_outer">System.<span class="code-snippet__keyword">out</span>.println(content);</span></code><code><span class="code-snippet_outer">};</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 这里可以把iHelo看成一个匿名实现类来传递参数</span></span></code><code><span class="code-snippet_outer">sayHello(iHello, <span class="code-snippet__string">"mengday"</span>);</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 如果去掉变量的接收,直接将Lambda表达式传递到参数中,此时Lambda表达式更像是一个函数</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 也就是说JDK1.8竟然可以将一个函数作为参数传递到方法中,这是之前版本做不到的</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 将函数作为方法的参数传递,一般用于回调函数,将回调函数传递到方法中</span></span></code><code><span class="code-snippet_outer">sayHello(name -&gt; {</span></code><code><span class="code-snippet_outer">String content = name + <span class="code-snippet__string">": "</span> + <span class="code-snippet__string">"happy new year everyone!"</span>;</span></code><code><span class="code-snippet_outer">System.<span class="code-snippet__keyword">out</span>.println(content);</span></code><code><span class="code-snippet_outer">}, <span class="code-snippet__string">"mengday"</span>);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <h4 style="white-space: normal;text-align: left;"><span style="font-family: Optima-Regular, PingFangTC-light;background-color: rgb(255, 218, 169);">示例5:集合排序示例</span></h4> <p style="white-space: normal;text-align: left;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="typescript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__built_in">void</span> main(<span class="code-snippet__built_in">String</span>[] args) {</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 写法一:使用匿名内部类</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 好好学习,天天向上</span></span></code><code><span class="code-snippet_outer">List&lt;<span class="code-snippet__built_in">String</span>&gt; words = Arrays.asList(<span class="code-snippet__string">"good"</span>, <span class="code-snippet__string">"good"</span>, <span class="code-snippet__string">"study"</span>, <span class="code-snippet__string">"day"</span>, <span class="code-snippet__string">"day"</span>, <span class="code-snippet__string">"up"</span>);</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// public static &lt;T&gt; void sort(List&lt;T&gt; list, Comparator&lt;? super T&gt; c)</span></span></code><code><span class="code-snippet_outer">Collections.sort(words, <span class="code-snippet__keyword">new</span> Comparator&lt;<span class="code-snippet__built_in">String</span>&gt;() {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__meta">@Override</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> int compare(<span class="code-snippet__built_in">String</span> s1, <span class="code-snippet__built_in">String</span> s2) {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 降续排序</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> s2.compareTo(s1);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer">});</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer">System.out.println(words);</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 写法二:使用Lambda表达式</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 咱俩谁跟谁</span></span></code><code><span class="code-snippet_outer">words = Arrays.asList(<span class="code-snippet__string">"we"</span>, <span class="code-snippet__string">"two"</span>, <span class="code-snippet__string">"who"</span>, <span class="code-snippet__string">"and"</span>, <span class="code-snippet__string">"who"</span>);</span></code><code><span class="code-snippet_outer">Collections.sort(words, (<span class="code-snippet__built_in">String</span> s1, <span class="code-snippet__built_in">String</span> s2) -&gt; {<span class="code-snippet__keyword">return</span> s2.compareTo(s1);});</span></code><code><span class="code-snippet_outer">System.out.println(words);</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="swift"><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 写法三:使用Lambda表达式(简写)</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 有事起奏,无事退朝</span></span></code><code><span class="code-snippet_outer">words = <span class="code-snippet__type">Arrays</span>.asList(<span class="code-snippet__string">"if"</span>, <span class="code-snippet__string">"you"</span>, <span class="code-snippet__string">"have"</span>, <span class="code-snippet__string">"something"</span>, <span class="code-snippet__string">"to"</span>, <span class="code-snippet__string">"say"</span>, <span class="code-snippet__string">"then"</span>, <span class="code-snippet__string">"say!"</span>,</span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"if"</span>, <span class="code-snippet__string">"you"</span>, <span class="code-snippet__string">"have"</span>, <span class="code-snippet__string">"nothing"</span>, <span class="code-snippet__string">"to"</span>, <span class="code-snippet__string">"say"</span>, <span class="code-snippet__string">"go"</span>, <span class="code-snippet__string">"home!"</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__type">Collections</span>.<span class="code-snippet__built_in">sort</span>(words, (s1, s2) -&gt; s2.compareTo(s1));</span></code><code><span class="code-snippet_outer"><span class="code-snippet__type">System</span>.out.<span class="code-snippet__built_in">println</span>(words);</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <h2 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"># 函数式接口@FunctionalInterface</span></strong></h2> <p style="white-space: normal;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">从我们自定义的IHello示例来看,Lambda表达式其实是一种接口类型的数据类型,严格的说Lambda表达式的数据类型是:函数式接口,是一种特殊的接口,该接口使用@FunctionalInterface注解来标记(不是必须的,可以不用该注解标记,IHello接口就没有使用该注解标记, ),并且接口中只能有一个抽象方法,可以有多个静态方法或者默认方法, 每一个该类型的lambda表达式都会被匹配到这个抽象方法。</span></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="java"><code><span class="code-snippet_outer"><span class="code-snippet__meta">@FunctionalInterface</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">interface</span> <span class="code-snippet__title">Comparator</span>&lt;<span class="code-snippet__title">T</span>&gt; </span>{</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">int</span> <span class="code-snippet__title">compare</span><span class="code-snippet__params">(T o1, T o2)</span></span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 其它static、default方法</span></span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">@FunctionalInterface: 该注解没啥太大含义,该注解是给编译器做检查使用的,如果使用了该注解,编译器就会检查该接口中的抽象方法是不是只有一个,如果有多个就会报错:在接口Xxx中找到多个非覆盖抽象方法</span><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="kotlin"><code><span class="code-snippet_outer"><span class="code-snippet__meta">@Documented</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__meta">@Retention(RetentionPolicy.RUNTIME)</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__meta">@Target(ElementType.TYPE)</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__meta">@interface</span> FunctionalInterface {}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">我们完善一下我们的IHello, 使用@FunctionalInterface注解</span><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="typescript"><code><span class="code-snippet_outer"><span class="code-snippet__meta">@FunctionalInterface</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">interface</span> IHello {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">void</span> sayHello(<span class="code-snippet__built_in">String</span> name);</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">我们可以将lambda表达式当作任意只包含一个抽象方法的接口类型,也就是说我们的IHello接口无论叫什么名字,接口中的方法无论叫什么名字都无所谓(只是可读性更好些),因此可以再进行抽象化一下,JDK1.8中提供了这样的函数式接口,我们也不需要再定义IHello接口了,JDK1.8中提供了Supplier、Consumer、Function、BiFunction,这几个是比较常用的</span><br></p> <p style="white-space: normal;text-align: left;"><br></p> <h2 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"># Supplier&lt; T &gt; 供应商:没有参数,有返回值</span></strong></h2> <p style="white-space: normal;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="typescript"><code><span class="code-snippet_outer"><span class="code-snippet__meta">@FunctionalInterface</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">interface</span> Supplier&lt;T&gt; {</span></code><code><span class="code-snippet_outer">T <span class="code-snippet__keyword">get</span>();</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <h2 style="white-space: normal;text-align: left;"></h2> <h2 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"># Consumer&lt; T &gt; 消费者:只有一个参数,没有返回值</span></strong><br></h2> <p style="white-space: normal;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="java"><code><span class="code-snippet_outer"><span class="code-snippet__meta">@FunctionalInterface</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">interface</span> <span class="code-snippet__title">Consumer</span>&lt;<span class="code-snippet__title">T</span>&gt; </span>{</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">void</span> <span class="code-snippet__title">accept</span><span class="code-snippet__params">(T t)</span></span>;</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <h2 style="white-space: normal;text-align: left;"></h2> <h2 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"># Function&lt; T, R &gt; 函数:一个参数,一个返回值</span></strong><br></h2> <p style="white-space: normal;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="java"><code><span class="code-snippet_outer"><span class="code-snippet__meta">@FunctionalInterface</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">interface</span> <span class="code-snippet__title">Function</span>&lt;<span class="code-snippet__title">T</span>, <span class="code-snippet__title">R</span>&gt; </span>{</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function">R <span class="code-snippet__title">apply</span><span class="code-snippet__params">(T t)</span></span>;</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <h2 style="white-space: normal;text-align: left;"><br></h2> <h2 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"># BiFunction&lt; T, U, R &gt; 二元函数:两个参数,一个返回值</span></strong><br></h2> <p style="white-space: normal;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="java"><code><span class="code-snippet_outer"><span class="code-snippet__meta">@FunctionalInterfacepublic</span> <span class="code-snippet__class"><span class="code-snippet__keyword">interface</span> <span class="code-snippet__title">BiFunction</span>&lt;<span class="code-snippet__title">T</span>, <span class="code-snippet__title">U</span>, <span class="code-snippet__title">R</span>&gt; </span>{</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function">R <span class="code-snippet__title">apply</span><span class="code-snippet__params">(T t, U u)</span></span>;</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <h2 style="white-space: normal;text-align: left;"></h2> <h2 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"># Comparator&lt; T &gt; 比较器:接收两个参数,返回比较的结果</span></strong><br></h2> <p style="white-space: normal;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="java"><code><span class="code-snippet_outer"><span class="code-snippet__meta">@FunctionalInterface</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">interface</span> <span class="code-snippet__title">Comparator</span>&lt;<span class="code-snippet__title">T</span>&gt; </span>{</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">int</span> <span class="code-snippet__title">compare</span><span class="code-snippet__params">(T o1, T o2)</span></span>;</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">使用以上四大函数式接口来取代自定义的接口IHello</span><br></p> <p style="white-space: normal;text-align: left;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="typescript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> Main {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__built_in">String</span> end = <span class="code-snippet__string">"."</span>;</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__built_in">void</span> main(<span class="code-snippet__built_in">String</span>[] args) {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 直接使用JDK1.8提供的接口,不需要再定义IHello接口, 直接使用JDK提供的接口来接收Lambda表达式</span></span></code><code><span class="code-snippet_outer">Supplier&lt;<span class="code-snippet__built_in">String</span>&gt; supplier = () -&gt; <span class="code-snippet__string">"mengday: happy new year everyone!"</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> result = supplier.get();</span></code><code><span class="code-snippet_outer">System.out.println(result);</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer">Consumer&lt;<span class="code-snippet__built_in">String</span>&gt; consumer = (name) -&gt; System.out.println(name + <span class="code-snippet__string">": "</span> + <span class="code-snippet__string">"happy new year everyone!"</span>);</span></code><code><span class="code-snippet_outer">consumer.accept(<span class="code-snippet__string">"mengday"</span>);</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">Function</span>&lt;<span class="code-snippet__built_in">String</span>, <span class="code-snippet__built_in">String</span>&gt; func = (name) -&gt; name + <span class="code-snippet__string">": "</span> + <span class="code-snippet__string">"happy new year everyone!"</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> hi = func.apply(<span class="code-snippet__string">"mengday"</span>);</span></code><code><span class="code-snippet_outer">System.out.println(hi);</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 在代码块的内部可以访问静态全局变量</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 在代码块中可以访问外边局部变量</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 在代码块的内部可以修改全局静态变量</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 在代码块内部是不能访问接口中的其它方法的</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> split = <span class="code-snippet__string">": "</span>;</span></code><code><span class="code-snippet_outer">BiFunction&lt;<span class="code-snippet__built_in">String</span>, <span class="code-snippet__built_in">String</span>, <span class="code-snippet__built_in">String</span>&gt; biFunction = (<span class="code-snippet__built_in">String</span> name, <span class="code-snippet__built_in">String</span> msg) -&gt; {</span></code><code><span class="code-snippet_outer">end = <span class="code-snippet__string">"!"</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> hello = name + split + msg + end;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> hello;</span></code><code><span class="code-snippet_outer">};</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> hello = biFunction.apply(<span class="code-snippet__string">"mengday"</span>, <span class="code-snippet__string">"happy new year everyone"</span>);</span></code><code><span class="code-snippet_outer">System.out.println(hello);</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 根据字符串长度比较大小</span></span></code><code><span class="code-snippet_outer">Comparator&lt;<span class="code-snippet__built_in">String</span>&gt; comparator = (s1, s2) -&gt; s1.length() - s2.length();</span></code><code><span class="code-snippet_outer">int compare = comparator.compare(<span class="code-snippet__string">"abc"</span>, <span class="code-snippet__string">"ab"</span>);</span></code><code><span class="code-snippet_outer">System.out.println(compare);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <h2 style="white-space: normal;text-align: left;"><strong><span style="font-family: Optima-Regular, PingFangTC-light;"># Predicate&lt; T &gt; 断言 谓词: 用于测试一个条件的真假</span></strong></h2> <p style="white-space: normal;text-align: left;"><br></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="java"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">package</span> java.util.function;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">import</span> java.util.Objects;</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__meta">@FunctionalInterface</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">interface</span> <span class="code-snippet__title">Predicate</span>&lt;<span class="code-snippet__title">T</span>&gt; </span>{</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 在给定的参数上评估这个谓词</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">boolean</span> <span class="code-snippet__title">test</span><span class="code-snippet__params">(T t)</span></span>;</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 返回一个组合的谓词,表示该谓词与另一个谓词的短路逻辑AND</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">default</span> Predicate&lt;T&gt; <span class="code-snippet__title">and</span><span class="code-snippet__params">(Predicate&lt;? <span class="code-snippet__keyword">super</span> T&gt; other)</span> </span>{</span></code><code><span class="code-snippet_outer">Objects.requireNonNull(other);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> (t) -&gt; test(t) &amp;&amp; other.test(t);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 返回表示此谓词的逻辑否定的谓词,相当于not</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">default</span> Predicate&lt;T&gt; <span class="code-snippet__title">negate</span><span class="code-snippet__params">()</span> </span>{</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> (t) -&gt; !test(t);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 返回一个组合的谓词,表示该谓词与另一个谓词的短路逻辑或</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">default</span> Predicate&lt;T&gt; <span class="code-snippet__title">or</span><span class="code-snippet__params">(Predicate&lt;? <span class="code-snippet__keyword">super</span> T&gt; other)</span> </span>{</span></code><code><span class="code-snippet_outer">Objects.requireNonNull(other);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> (t) -&gt; test(t) || other.test(t);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 返回根据 Objects.equals(Object, Object)测试两个参数是否相等的谓词</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">static</span> &lt;T&gt; <span class="code-snippet__function">Predicate&lt;T&gt; <span class="code-snippet__title">isEqual</span><span class="code-snippet__params">(Object targetRef)</span> </span>{</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> (<span class="code-snippet__keyword">null</span> == targetRef) ? Objects::isNull : object -&gt; targetRef.equals(object);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <p style="white-space: normal;text-align: left;"><br></p> <p style="white-space: normal;text-align: left;"><span style="font-size: 16px;font-family: Optima-Regular, PingFangTC-light;">Main</span></p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="typescript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__built_in">void</span> main(<span class="code-snippet__built_in">String</span>[] args) {</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 可以构造复杂的条件:并且and、或者or、否negate</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> email = <span class="code-snippet__string">"mengday@gmal.com"</span>;</span></code><code><span class="code-snippet_outer">Predicate&lt;<span class="code-snippet__built_in">String</span>&gt; predicate = (str) -&gt; str.length() &gt; <span class="code-snippet__number">20</span>;</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 测试 emial.length &gt; 0 的boolean</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">boolean</span> result = predicate.test(email); <span class="code-snippet__comment">// false</span></span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 测试 !(emial.length &gt; 0) 的boolean</span></span></code><code><span class="code-snippet_outer">result = predicate.negate().test(email); <span class="code-snippet__comment">// true</span></span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer">Predicate&lt;<span class="code-snippet__built_in">String</span>&gt; orPredicate = (str) -&gt; str.contains(<span class="code-snippet__string">"@"</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">// 测试 emial.length &gt; 0 or emial.contains("@")</span></span></code><code><span class="code-snippet_outer">result = predicate.or(orPredicate).test(email); <span class="code-snippet__comment">// true</span></span></code><code><span class="code-snippet_outer">}</span></code></pre> </section> <section> <section style="box-sizing: border-box;font-size: 16px;"> <section style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="display: flex;flex-flow: row nowrap;margin: 10px 0%;box-sizing: border-box;"> <section style="display: inline-block;vertical-align: middle;width: auto;flex: 100 100 0%;align-self: center;height: auto;box-sizing: border-box;"> <section style="margin-top: 0.5em;margin-bottom: 0.5em;transform: translate3d(0px, 0px, 1px) rotateY(180deg);box-sizing: border-box;" powered-by="xiumi.us"> <section style="background-color: rgba(194, 150, 255, 0.45);height: 1px;box-sizing: border-box;line-height: 0;"> <br> </section> </section> </section> <section style="display: inline-block;vertical-align: middle;width: 60px;flex: 0 0 auto;align-self: center;height: auto;box-sizing: border-box;"> <section style="text-align: center;margin-right: 0%;margin-left: 0%;box-sizing: border-box;" powered-by="xiumi.us"> <section style="max-width: 100%;vertical-align: middle;display: inline-block;line-height: 0;width: 90%;height: auto;box-sizing: border-box;"> <img data-ratio="0.855" src="/upload/28943959e42a3cfb7892208f8254ade3.gif" data-type="gif" data-w="400" style="vertical-align: middle;width: 100%;box-sizing: border-box;"> </section> </section> </section> <section style="display: inline-block;vertical-align: middle;width: auto;flex: 100 100 0%;align-self: center;height: auto;box-sizing: border-box;"> <section style="margin-top: 0.5em;margin-bottom: 0.5em;box-sizing: border-box;" powered-by="xiumi.us"> <section style="background-color: rgba(194, 150, 255, 0.45);height: 1px;box-sizing: border-box;line-height: 0;"> <br> </section> </section> </section> </section> </section> </section> <p data-lake-id="098b923ca3e65e99528d2c30d6cf4c26" style="text-align: center;font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="color: #8C8C8C;">推荐阅读</span></p> <p data-lake-id="098b923ca3e65e99528d2c30d6cf4c26" style="text-align: center;font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="color: rgb(217, 33, 66);text-decoration: underline;">《</span><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247487241&amp;idx=2&amp;sn=510aa88cc55b12ebf27e05e56a35a23a&amp;chksm=97b79111a0c0180762f7b58cca2ce02abf9d345fdab6e50ba3cca065656644c83a3a8cf73dad&amp;scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" style="color: rgb(217, 33, 66);text-decoration: underline;" data-linktype="2"><span style="color: rgb(217, 33, 66);">架构师离职后,成为自由开发者的第 100 天</span></a><span style="color: rgb(217, 33, 66);text-decoration: underline;">》</span></p> <p data-lake-id="098b923ca3e65e99528d2c30d6cf4c26" style="text-align: center;font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="color: #8C8C8C;"><br></span></p> <p data-lake-id="098b923ca3e65e99528d2c30d6cf4c26" style="text-align: center;font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="color: #8C8C8C;">往期推荐</span></p> <section data-recommend-type="list-normal" data-recommend-tid="2" data-mpa-template="t" style="width: 100%;display: flex;justify-content: center;align-items: center;" data-mid="" data-from="yb-recommend"> <section style="width: 100%;padding: 14px 7px 6px;background: rgb(255, 255, 255);border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(232, 232, 235);" data-mid=""> <section data-mpa-template="t" data-recommend-article-type="list-normal" data-recomment-template-id="2" data-recommend-article-id="2247488000_1" data-recommend-article-time="1598401800" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/HmHDU48icAtbPiaKAKuVhnXuZuPZicJ6HgmcicQVicQECmRk2cWNBiaZibTJlBp9iaYEct7axnvaBMYCXtm5sKAiaFZEbAg/0?wx_fmt=jpeg" data-recommend-article-title="为什么阿里规定需要在事务注解@Transactional中指定rollbackFor?" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247488000&amp;idx=1&amp;sn=fec9543c91be538ea92e653df4107257&amp;chksm=97b78c18a0c0050e492cad7b975b3086d3bdca9d5d0bfeded1468fe8f5e424b329365194d5fa#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247488000&amp;idx=1&amp;sn=fec9543c91be538ea92e653df4107257&amp;chksm=97b78c18a0c0050e492cad7b975b3086d3bdca9d5d0bfeded1468fe8f5e424b329365194d5fa&amp;scene=21#wechat_redirect" data-linktype="1"> <section data-recommend-content="t" style="width: 100%;padding: 5px 10px;background: rgb(249, 249, 249);border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(239, 239, 239);margin-bottom: 8px;display: flex;justify-content: space-between;align-items: center;" data-mid=""> <section style="margin-right: 12px;flex: 1;" data-mid=""> <p data-recommend-title="t" style="font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;margin-bottom: 20px;max-width: 100%;overflow: hidden;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical;" data-mid="">为什么阿里规定需要在事务注解@Transactional中指定rollbackFor?</p> <p data-recommend-date="t" style="max-width: 100%;font-size: 11px;font-weight: 400;color: #999999;line-height: 16px;" data-mid="">2020-08-26</p> </section> <span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 0px;right: auto;bottom: auto;"><img data-ratio="0.4270833333333333" src="/upload/150ae5e45412e1c6a6e12948a7910dbb.jpg" data-type="jpeg" data-w="960" style="width: 51px;height: 51px;object-fit: cover;margin: 0px;"></span> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-normal" data-recomment-template-id="2" data-recommend-article-id="2247487985_1" data-recommend-article-time="1598314260" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/HmHDU48icAtYdyWGuGXD6YZMETubutIh0b3Yibz06icYWZB5SpicpM3S5kkJcFXeeK8T7p92LGl4GNpKZDAf7POicpQ/0?wx_fmt=jpeg" data-recommend-article-title="拼多多一面:Mybatis是如何实现SQL语句复用功能的?" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247487985&amp;idx=1&amp;sn=502362af53dcaf0d16ec8173bdc33891&amp;chksm=97b78fe9a0c006ff776bce771199b92bc2ed293327d7f50f85098c0b29e2cd03e1ef20c4fde8#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247487985&amp;idx=1&amp;sn=502362af53dcaf0d16ec8173bdc33891&amp;chksm=97b78fe9a0c006ff776bce771199b92bc2ed293327d7f50f85098c0b29e2cd03e1ef20c4fde8&amp;scene=21#wechat_redirect" data-linktype="1"> <section data-recommend-content="t" style="width: 100%;padding: 5px 10px;background: rgb(249, 249, 249);border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(239, 239, 239);margin-bottom: 8px;display: flex;justify-content: space-between;align-items: center;" data-mid=""> <section style="margin-right: 12px;flex: 1;" data-mid=""> <p data-recommend-title="t" style="font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;margin-bottom: 20px;max-width: 100%;overflow: hidden;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical;" data-mid="">拼多多一面:Mybatis是如何实现SQL语句复用功能的?</p> <p data-recommend-date="t" style="max-width: 100%;font-size: 11px;font-weight: 400;color: #999999;line-height: 16px;" data-mid="">2020-08-25</p> </section> <span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 0px;right: auto;bottom: auto;"><img data-ratio="0.42604166666666665" src="/upload/7177f62762429c20ffd6628ea4921e37.jpg" data-type="jpeg" data-w="960" style="width: 51px;height: 51px;object-fit: cover;margin: 0px;"></span> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-normal" data-recomment-template-id="2" data-recommend-article-id="2247487970_1" data-recommend-article-time="1598227860" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/HmHDU48icAtb0hz7PciaE9Slrrz4H8s4KX24PR01qBGDzglAkUBFUCTHjy3HV8xoyqqb3HN37qVX3LVj79OJCHTg/0?wx_fmt=jpeg" data-recommend-article-title="Mybatis的Sql编译策略及Executor答疑" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247487970&amp;idx=1&amp;sn=0283593d39686691f5bdbd289089e81b&amp;chksm=97b78ffaa0c006ec3930caaa8e4465b2a8221118a9889eedc951682fbfff8809bcc9ceb687a6#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247487970&amp;idx=1&amp;sn=0283593d39686691f5bdbd289089e81b&amp;chksm=97b78ffaa0c006ec3930caaa8e4465b2a8221118a9889eedc951682fbfff8809bcc9ceb687a6&amp;scene=21#wechat_redirect" data-linktype="1"> <section data-recommend-content="t" style="width: 100%;padding: 5px 10px;background: rgb(249, 249, 249);border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(239, 239, 239);margin-bottom: 8px;display: flex;justify-content: space-between;align-items: center;" data-mid=""> <section style="margin-right: 12px;flex: 1;" data-mid=""> <p data-recommend-title="t" style="font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;margin-bottom: 20px;max-width: 100%;overflow: hidden;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical;" data-mid="">Mybatis的Sql编译策略及Executor答疑</p> <p data-recommend-date="t" style="max-width: 100%;font-size: 11px;font-weight: 400;color: #999999;line-height: 16px;" data-mid="">2020-08-24</p> </section> <span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 0px;right: auto;bottom: auto;"><img data-ratio="0.42604166666666665" src="/upload/304c88c0ab061302154b005d6541be.jpg" data-type="jpeg" data-w="960" style="width: 51px;height: 51px;object-fit: cover;margin: 0px;"></span> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-normal" data-recomment-template-id="2" data-recommend-article-id="2247487955_1" data-recommend-article-time="1598141460" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/HmHDU48icAtYMwydBdFOXf7ia2liawPNmsuW0BN7oKibqaGZWFiaZI7j95H7BCoVFoI4PK5NS0RPRZKVoS5wLgiaNwGg/0?wx_fmt=jpeg" data-recommend-article-title="难住了N个面试者,http协议无状态中的 &quot;状态&quot; 到底指的是什么?!" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247487955&amp;idx=1&amp;sn=75e0d31afcad5b81b9b7cfc153f2e6ec&amp;chksm=97b78fcba0c006ddb122b104849a92f55e23f82edd9f9450e5019e96532c701557423d582626#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247487955&amp;idx=1&amp;sn=75e0d31afcad5b81b9b7cfc153f2e6ec&amp;chksm=97b78fcba0c006ddb122b104849a92f55e23f82edd9f9450e5019e96532c701557423d582626&amp;scene=21#wechat_redirect" data-linktype="1"> <section data-recommend-content="t" style="width: 100%;padding: 5px 10px;background: rgb(249, 249, 249);border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(239, 239, 239);margin-bottom: 8px;display: flex;justify-content: space-between;align-items: center;" data-mid=""> <section style="margin-right: 12px;flex: 1;" data-mid=""> <p data-recommend-title="t" style="font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;margin-bottom: 20px;max-width: 100%;overflow: hidden;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical;" data-mid="">难住了N个面试者,http协议无状态中的 "状态" 到底指的是什么?!</p> <p data-recommend-date="t" style="max-width: 100%;font-size: 11px;font-weight: 400;color: #999999;line-height: 16px;" data-mid="">2020-08-23</p> </section> <span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 0px;right: auto;bottom: auto;"><img data-ratio="0.42604166666666665" src="/upload/ed81cdba386a110e2813f25c4dae9a32.jpg" data-type="jpeg" data-w="960" style="width: 51px;height: 51px;object-fit: cover;margin: 0px;"></span> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-normal" data-recomment-template-id="2" data-recommend-article-id="2247487946_1" data-recommend-article-time="1598056200" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/HmHDU48icAtaricnq17r7YeTfB59FfFckBDEA8ygHLMgy6jEUpgW2L3wMW4gVj3cTCuNIZRmoplZ31NjjlObL3Rw/0?wx_fmt=jpeg" data-recommend-article-title="为什么 Java 线程没有Running状态?我懵了" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247487946&amp;idx=1&amp;sn=87617071de1a50a9e45902ca8e66ad4a&amp;chksm=97b78fd2a0c006c427f43df478cc4f086da94d07eb6a78571fa210639cb65c221ca03891272a#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzIxMzQzNzMwMw==&amp;mid=2247487946&amp;idx=1&amp;sn=87617071de1a50a9e45902ca8e66ad4a&amp;chksm=97b78fd2a0c006c427f43df478cc4f086da94d07eb6a78571fa210639cb65c221ca03891272a&amp;scene=21#wechat_redirect" data-linktype="1"> <section data-recommend-content="t" style="width: 100%;padding: 5px 10px;background: rgb(249, 249, 249);border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(239, 239, 239);margin-bottom: 8px;display: flex;justify-content: space-between;align-items: center;" data-mid=""> <section style="margin-right: 12px;flex: 1;" data-mid=""> <p data-recommend-title="t" style="font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;margin-bottom: 20px;max-width: 100%;overflow: hidden;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical;" data-mid="">为什么 Java 线程没有Running状态?我懵了</p> <p data-recommend-date="t" style="max-width: 100%;font-size: 11px;font-weight: 400;color: #999999;line-height: 16px;" data-mid="">2020-08-22</p> </section> <span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 0px;right: auto;bottom: auto;"><img data-ratio="0.42604166666666665" src="/upload/a4583c733ca8278c539b0f1333a25eaf.jpg" data-type="jpeg" data-w="960" style="width: 51px;height: 51px;object-fit: cover;margin: 0px;"></span> </section></a> </section> </section> </section> <p><br><br></p> <p data-lake-id="098b923ca3e65e99528d2c30d6cf4c26" style="text-align: center;font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="color: #8C8C8C;">一起进大厂,每日学干货</span></p> <p data-lake-id="95edb2040117493cdb083bce066a86c1" style="text-align: center;font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="color: #8C8C8C;">关注我,不迷路</span></p> <p style="text-align: center;"><img data-ratio="1" src="/upload/7b57b8ce8a74c4aebec729d708bae61d.png" data-type="png" data-w="258"></p> </section>

这款轻量级日志搬运神器,成百上千台服务器产生的日志不在话下

作者:微信小助手

<section style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"> <br> </section> <p data-mpa-powered-by="yiban.io" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(62, 71, 83);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 15px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-family: Optima-Regular, PingFangTC-light;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;"><span style="max-width: 100%;color: rgb(127, 127, 127);font-size: 14px;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;">点击上方“</span><span style="max-width: 100%;font-size: 14px;line-height: 1.75em;color: rgb(0, 176, 240);box-sizing: border-box !important;overflow-wrap: break-word !important;">服务端思维</span><span style="max-width: 100%;color: rgb(127, 127, 127);font-size: 14px;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;">”,选择“</span></span><span style="max-width: 100%;color: rgb(136, 136, 136);font-size: 14px;">设为星标</span><span style="max-width: 100%;color: rgb(127, 127, 127);letter-spacing: 0.544px;font-size: 14px;">”</span></span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;color: rgb(62, 62, 62);font-size: 15px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-family: Optima-Regular, PingFangTC-light;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(127, 127, 127);font-size: 14px;">回复”</span><span style="max-width: 100%;letter-spacing: 0.544px;font-size: 14px;color: rgb(0, 122, 170);">669</span><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(127, 127, 127);font-size: 14px;">“获取独家整理的精选资料集</span></span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;color: rgb(62, 62, 62);font-size: 15px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-family: Optima-Regular, PingFangTC-light;letter-spacing: 0.544px;color: rgb(127, 127, 127);font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;">回复”</span><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(0, 122, 170);">加群</span><span style="max-width: 100%;letter-spacing: 0.544px;">“加入全国服务端高端社群「后端圈」</span></span></p> <section> &nbsp; <img data-backh="340" data-backw="509" data-ratio="0.6666666666666666" src="/upload/88270c83d2dd2b20f16ad1ecc7e68421.jpg" data-type="jpeg" data-w="510" style="width: 100%;height: auto;"> </section> <section> <br> </section> <blockquote style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;padding-top: 12px;padding-right: 10px;padding-bottom: 12px;border-width: initial;border-style: none;border-color: initial;quotes: none;font-size: 16px;line-height: 1.5;color: rgb(80, 80, 80);background: rgb(248, 248, 248);border-radius: 4px;font-family: &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;text-align: start;white-space: normal;"> <p style="-webkit-tap-highlight-color: transparent;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;">之前我们搭建的ELK日志收集系统,主要是用来收集SpringBoot应用的日志。其原理是应用通过Logstash插件,使用TCP向Logstash传输日志,从而存储到Elasticsearch中去。但是有很多中间件的日志都是直接存储在文件中的,比如Nginx、Elasticsearch和MySQL,此时我们就需要一个搬运工来把日志搬到Elasticsearch中去,Filebeat正是这样一个日志搬运工,本文将详细介绍它的使用方法,希望对大家有所帮助。</p> </blockquote> <p>Filebeat是本地文件的日志数据采集器,可监控日志目录或特定日志文件(tail file),并将它们转发给Elasticsearch或Logstatsh进行索引、kafka等。带有内部模块(auditd,Apache,Nginx,System和MySQL),可通过一个指定命令来简化通用日志格式的收集,解析和可视化。</p> <p>官方网址:</p> <p>https://www.elastic.co/guide/en/beats/filebeat/current/index.html</p> <h1><span style="font-size: 20px;color: rgb(255, 120, 0);">1.工作原理</span></h1> <p>Filebeat涉及两个组件:查找器prospector和采集器harvester,来读取文件(tail file)并将事件数据发送到指定的输出。</p> <p>启动Filebeat时,它会启动一个或多个查找器,查看你为日志文件指定的本地路径。对于prospector所在的每个日志文件,prospector启动harvester。每个harvester都会为新内容读取单个日志文件,并将新日志数据发送到libbeat,后者将聚合事件并将聚合数据发送到你为Filebeat配置的输出。</p> <p><img data-ratio="0.7821989528795812" src="/upload/391cc1da7473ed9049fa4ab1eeae3b8c.other" data-type="other" data-w="955" style="cursor: zoom-in;"></p> <p>filebeat工作原理</p> <p>当发送数据到Logstash或Elasticsearch时,Filebeat使用一个反压力敏感(backpressure-sensitive)的协议来解释高负荷的数据量。当Logstash数据处理繁忙时,Filebeat放慢它的读取速度。一旦压力解除,Filebeat将恢复到原来的速度,继续传输数据。</p> <h3><span style="color: rgb(255, 120, 0);font-size: 20px;">1.1采集器Harvester</span></h3> <p>Harvester负责读取单个文件的内容。读取每个文件,并将内容发送到the output,每个文件启动一个harvester, harvester负责打开和关闭文件,这意味着在运行时文件描述符保持打开状态。</p> <p>如果文件在读取时被删除或重命名,Filebeat将继续读取文件。这有副作用,即在harvester关闭之前,磁盘上的空间被保留。默认情况下,Filebeat将文件保持打开状态,直到达到close_inactive状态</p> <p>关闭harvester会产生以下结果:</p> <p>1)如果在harvester仍在读取文件时文件被删除,则关闭文件句柄,释放底层资源。</p> <p>2)文件的采集只会在scan_frequency过后重新开始。</p> <p>3)如果在harvester关闭的情况下移动或移除文件,则不会继续处理文件。</p> <p>要控制收割机何时关闭,请使用close_ *配置选项</p> <h3><span style="font-size: 20px;color: rgb(255, 120, 0);">1.2查找器Prospector</span></h3> <p>Prospector负责管理harvester并找到所有要读取的文件来源。如果输入类型为日志,则查找器将查找路径匹配的所有文件,并为每个文件启动一个harvester。每个prospector都在自己的Go协程中运行。</p> <p>Filebeat目前支持两种prospector类型:log和stdin。每个prospector类型可以定义多次。日志prospector检查每个文件来查看harvester是否需要启动,是否已经运行,或者该文件是否可以被忽略(请参阅ignore_older)。</p> <p>只有在harvester关闭后文件的大小发生了变化,才会读取到新行。</p> <p>注:Filebeat prospector只能读取本地文件,没有功能可以连接到远程主机来读取存储的文件或日志。</p> <h1><span style="color: rgb(255, 120, 0);font-size: 20px;">2.配置文件</span></h1> <p>配置文件:$FILEBEAT_HOME/filebeat.yml。Filebeat可以一次读取某个文件夹下的所有后缀名为log的文件,也可以读取指定的某一个后缀名为log的文件。</p> <p><img data-ratio="0.9886178861788618" src="/upload/dd3be653a0fde867e115039a63f620ee.other" data-type="other" data-w="615" style="cursor: zoom-in;"></p> <p>yml配置</p> <p>配置文件详解</p> <p>(http://blog.51cto.com/michaelkang/1864225)</p> <p>(1)字段解释</p> <p><strong>paths:</strong>指定要监控的日志,目前按照Go语言的glob函数处理。没有对配置目录做递归处理,比如配置的如果是:</p> <p>/var/log/* /*.log</p> <p>则只会去/var/log目录的所有子目录中寻找以".log"结尾的文件,而不会寻找/var/log目录下以".log"结尾的文件。</p> <p><strong>encoding:</strong>指定被监控的文件的编码类型,使用plain和utf-8都是可以处理中文日志的。</p> <p><strong>input_type:</strong>指定文件的输入类型log(默认)或者stdin。</p> <p><strong>exclude_lines:</strong>在输入中排除符合正则表达式列表的那些行。</p> <p><strong>include_lines:</strong>包含输入中符合正则表达式列表的那些行(默认包含所有行),include_lines执行完毕之后会执行exclude_lines。</p> <p><strong>exclude_files:</strong>忽略掉符合正则表达式列表的文件(默认为每一个符合paths定义的文件都创建一个harvester)。</p> <p><strong>fields:</strong>向输出的每一条日志添加额外的信息,比如"level:debug",方便后续对日志进行分组统计。默认情况下,会在输出信息的fields子目录下以指定的新增fields建立子目录,</p> <p><strong>fields_under_root:</strong>如果该选项设置为true,则新增fields成为顶级目录,而不是将其放在fields目录下。自定义的field会覆盖filebeat默认的field。</p> <p><strong>ignore_older:</strong>可以指定Filebeat忽略指定时间段以外修改的日志内容,比如2h(两个小时)或者5m(5分钟)。</p> <p><strong>close_older:</strong>如果一个文件在某个时间段内没有发生过更新,则关闭监控的文件handle。默认1h。</p> <p><strong>force_close_files:</strong>Filebeat会在没有到达close_older之前一直保持文件的handle,如果在这个时间窗内删除文件会有问题,所以可以把force_close_files设置为true,只要filebeat检测到文件名字发生变化,就会关掉这个handle。</p> <p><strong>scan_frequency:</strong>Filebeat以多快的频率去prospector指定的目录下面检测文件更新(比如是否有新增文件),如果设置为0s,则Filebeat会尽可能快地感知更新(占用的CPU会变高)。默认是10s。</p> <p><strong>document_type:</strong>设定Elasticsearch输出时的document的type字段,也可以用来给日志进行分类。</p> <p><strong>harvester_buffer_size:</strong>每个harvester监控文件时,使用的buffer的大小。</p> <p><strong>max_bytes:</strong>日志文件中增加一行算一个日志事件,max_bytes限制在一次日志事件中最多上传的字节数,多出的字节会被丢弃。默认是10MB。</p> <p><strong>multiline:</strong>适用于日志中每一条日志占据多行的情况,比如各种语言的报错信息调用栈。这个配置的下面包含如下配置:</p> <p><strong>pattern:</strong>多行日志开始的那一行匹配的pattern</p> <p><strong>negate:</strong>是否需要对pattern条件转置使用,不翻转设为true,反转设置为false。</p> <p><strong>match:</strong>匹配pattern后,与前面(before)还是后面(after)的内容合并为一条日志</p> <p><strong>max_lines:</strong>合并的最多行数(包含匹配pattern的那一行),默认为500行。</p> <p><strong>timeout:</strong>到了timeout之后,即使没有匹配一个新的pattern(发生一个新的事件),也把已经匹配的日志事件发送出去</p> <p><strong>tail_files:</strong>如果设置为true,Filebeat从文件尾开始监控文件新增内容,把新增的每一行文件作为一个事件依次发送,而不是从文件开始处重新发送所有内容。</p> <p><strong>backoff:</strong>Filebeat检测到某个文件到了EOF之后,每次等待多久再去检测文件是否有更新,默认为1s。</p> <p><strong>max_backoff:</strong>Filebeat检测到某个文件到了EOF之后,等待检测文件更新的最大时间,默认是10秒。</p> <p><strong>backoff_factor:</strong>定义到达max_backoff的速度,默认因子是2,到达max_backoff后,变成每次等待max_backoff那么长的时间才backoff一次,直到文件有更新才会重置为backoff。比如:&nbsp;</p> <p>如果设置成1,意味着去使能了退避算法,每隔backoff那么长的时间退避一次。</p> <p><strong>spool_size:</strong>spooler的大小,spooler中的事件数量超过这个阈值的时候会清空发送出去(不论是否到达超时时间),默认1MB。</p> <p><strong>idle_timeout:</strong>spooler的超时时间,如果到了超时时间,spooler也会清空发送出去(不论是否到达容量的阈值),默认1s。</p> <p><strong>registry_file:</strong>记录filebeat处理日志文件的位置的文件</p> <p><strong>config_dir:</strong>如果要在本配置文件中引入其他位置的配置文件,可以写在这里(需要写完整路径),但是只处理prospector的部分。</p> <p><strong>publish_async:</strong>是否采用异步发送模式(实验功能)。</p> <p>具体的一个yml采集配置样例如下:该配置文件是filebeat采集数据的依据,并根据需求添加必要配置,filebeat收集日志后发往logstash,配置如下:</p> <p><img data-ratio="0.5263605442176871" src="/upload/34e679e29acce8b3aecb831ae1e368b4.other" data-type="other" data-w="1176" style="cursor: zoom-in;"></p> <p>yml配置样例</p> <h1><span style="font-size: 20px;color: rgb(255, 120, 0);">3.启动和停止</span></h1> <h3><span style="font-size: 20px;color: rgb(255, 120, 0);">3.1开启filebeat</span></h3> <p>cd FILEBEAT_HOME</p> <p>&nbsp;nohup ./bin/filebeat -f config/test.conf &gt;&gt;/FILEBEAT_HOME/logs/filebeat.log &amp;</p> <p>后台启动filebeat,配置对应的参数</p> <p>启动多个filebeat配置,新建一个目录(conf)存放多个filebeat的配置文件,</p> <p>#nohup ./bin/filebeat -f conf/* &gt;&gt;/FILEBEAT_HOME/logs/filebeat.log &amp;</p> <p>&nbsp;注意:一台服务器只能启动一个filebeat进程。</p> <h3><span style="color: rgb(255, 120, 0);font-size: 20px;">3.2停止filebeat</span></h3> <p>ps -ef |grep filebeat</p> <p>kill -9 $pid</p> <p>注意:非紧急情况下,杀掉进程只能用优雅方式。</p> <h3><span style="font-size: 20px;color: rgb(255, 120, 0);">3.3filebeat运行可能存在的问题(运行前最好是输出到控制台查看)<br></span></h3> <p>A、filebeat运行不成功</p> <p>问题:配置文件格式有问题,配置文件遵循yml文件格式,<strong>多或少一个空格</strong>都会导致启动问题,可以使用cmd命令窗口到filebeat安装路径下,使用filebeat.exe –c filebeat.yml 查看报错,也可以看filebeat路径下的log文件夹中的filebeat文件</p> <p>B、 filebeat第一次运行成功无数据</p> <p>问题:a、路径有问题</p> <p>b、运行条件设置有问题(例如只采集某个条件下的数据,文件中没有符合条件的数据,这种情况下先注释掉采集条件测试一下)</p> <p>C、filebeat运行成功第一次运行后有数据,第二次无数据</p> <p>问题:filebeat读取文件后会生成一个registry文件,注意windows机器中这个文件在手动启动的情况下会在filebeat安装目录下的data文件夹中,服务注册启动的情况下会在C盘下隐藏文件夹C:\ProgramData\filebeat中,删除掉这个就可以了</p> <p>D、filebeat运行成功有数据,但是新添加数据不读取问题</p> <p>问题:filebeat传输存在反压机制,在数据量特别大或者传输通道不通的情况下,filebeat会进行反压,暂停发送,等到数据量稳定或者数据传输通道正常的之后才会发送</p> <h1><span style="font-size: 20px;color: rgb(255, 120, 0);">4.注意问题</span></h1> <h3><span style="font-size: 20px;color: rgb(255, 120, 0);">4.1Filebeat如何保持文件的状态?</span></h3> <p>Filebeat 保存每个文件的状态并经常将状态刷新到磁盘上的注册文件中。该状态用于记住harvester正在读取的最后偏移量,并确保发送所有日志行。如果输出(例如Elasticsearch或Logstash)无法访问,Filebeat会跟踪最后发送的行,并在输出再次可用时继续读取文件。</p> <p>在Filebeat运行时,每个prospector内存中也会保存文件状态信息,当重新启动Filebeat时,将使用注册文件的数据来重建文件状态,Filebeat将每个harvester在从保存的最后偏移量继续读取。</p> <p>每个prospector为它找到的每个文件保留一个状态。由于文件可以被重命名或移动,因此文件名和路径不足以识别文件。对于每个文件,Filebeat存储唯一标识符以检测文件是否先前已被采集过。</p> <p>如果你使用的案例涉及每天创建大量新文件,你可能会发现注册文件增长过大。请参阅注册表文件太大?编辑有关你可以设置以解决此问题的配置选项的详细信息。</p> <h3><span style="font-size: 20px;color: rgb(255, 120, 0);">4.2Filebeat如何确保至少一次交付?</span></h3> <p>Filebeat保证事件至少会被传送到配置的输出一次,并且不会丢失数据。Filebeat能够实现此行为,因为它将每个事件的传递状态存储在注册文件中。</p> <p>在输出阻塞或未确认所有事件的情况下,Filebeat将继续尝试发送事件,直到接收端确认已收到。如果Filebeat在发送事件的过程中关闭,它不会等待输出确认所有收到事件。</p> <p>发送到输出但在Filebeat关闭前未确认的任何事件在重新启动Filebeat时会再次发送。这可以确保每个事件至少发送一次,但最终会将重复事件发送到输出。</p> <p>也可以通过设置shutdown_timeout选项来配置Filebeat以在关闭之前等待特定时间。</p> <p>注意:Filebeat的至少一次交付保证包括日志轮换和删除旧文件的限制。如果将日志文件写入磁盘并且写入速度超过Filebeat可以处理的速度,或者在输出不可用时删除了文件,则可能会丢失数据。</p> <p>在Linux上,Filebeat也可能因inode重用而跳过行。有关inode重用问题的更多详细信息,请参阅filebeat常见问题解答。</p> <h3><span style="font-size: 20px;color: rgb(255, 120, 0);">4.3Filebeat如何保证在日志文件被切割(或滚动rolling)时依然正确读取文件?</span></h3> <p>Logback日志切割用的是JDK里File#renameTo()方法。如果该方法失败,就再尝试使用复制数据的方式切割日志。查找该方法相关资料得知,只有当源文件和目标目录处于同一个文件系统、同volumn(即windows下的C, D盘)下该方法才会成功,切不会为重命名的后的文件分配新的inode值。也就是说,如果程序里一直保存着该文件的描述符,那么当程序再写日志时,就会向重命名后的文件中写。那么问题来了,filebeat是会一直打开并保存文件描述符的,那么它是怎么得知日志被切割这件事的呢?</p> <p>如果只用当前文件描述符一路监控到天黑的话,那么当logback把日志重命名后,filebeat仍然会监控重命名后的日志,新创建的日志文件就看不到了。实际上,filebeat是通过close_inactive和scan_frequency两个参数(机制)来应对这种情况的:</p> <p>(1)close_inactive</p> <p>该参数指定当被监控的文件多长时间没有变化后就关闭文件句柄(file handle)。官方建议将这个参数设置为一个比文件最大更新间隔大的值。比如文件最长5s更新一次,那就设置成1min。默认值为5min。</p> <p>(2)scan_frequency</p> <p>该参数指定Filebeat搜索新文件的频率(时间间隔)。当发现新的文件被创建时, Filebeat会为它再启动一个 harvester 进行监控,默认为10s。</p> <p>综合以上两个机制,当logback完成日志切割后(即重命名),此时老的harvester仍然在监控重命名后的日志文件,但是由于该文件不会再更新,因此会在close_inactive时间后关闭这个文件的 harvester。当scan_frequency时间过后,Filebeat会发现目录中出现了新文件,于是为该文件启动 harvester 进行监控。这样就保证了切割日志时也能不丢不重的传输数据。(不重是通过为每个日志文件保存offset实现的)</p> <p><span style="background-color: rgb(255, 255, 255);font-weight: 700;">&nbsp;</span><br></p> <section powered-by="xiumi.us" style="max-width: 100%;color: rgb(62, 71, 83);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 16px;letter-spacing: 0.544px;text-align: center;white-space: normal;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="margin-right: 16px;margin-left: 16px;max-width: 100%;min-height: 1em;letter-spacing: 1.5px;line-height: 1.5em;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(77, 168, 238);"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(77, 168, 238);max-width: 100%;font-size: 12px;letter-spacing: 1px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 17px;letter-spacing: 0.544px;font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(77, 168, 238);max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">— 本文结束 —</span></strong></span></strong></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p> <p style="margin-right: 16px;margin-left: 16px;max-width: 100%;min-height: 1em;letter-spacing: 1.5px;line-height: 1.5em;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p> <section data-mpa-template="t" mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section data-id="93709" mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section mpa-from-tpl="t" style="padding: 5px;max-width: 100%;border-style: solid;border-width: 2px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section mpa-from-tpl="t" style="padding: 15px;max-width: 100%;border-style: dashed;border-width: 1px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span data-role="width" data-width="80%" style="max-width: 100%;display: inline-block;width: 425.215px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img border="0" class="__bg_gif" data-ratio="0.08658008658008658" src="/upload/b0b0d65a8077fe5fc54ccd945913d458.gif" data-type="gif" data-w="462" data-width="80%" height="" style="box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;width: 340.156px !important;" title="" width="80%"></span> </section> <p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;text-align: justify;letter-spacing: 1.5px;line-height: normal;color: rgb(136, 136, 136);font-size: 13px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(77, 168, 238);">●&nbsp;</span><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA5NDg3MjAwMQ==&amp;mid=2457107501&amp;idx=1&amp;sn=fed5ff9f969eba814a9892c80b4e5e1f&amp;chksm=87c8d483b0bf5d951cdc8596b5470d6f3559d72452a51ce57cb4ccccc0d66ab3795c2ad17841&amp;scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" style="font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 13px;letter-spacing: 1.5px;white-space: normal;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);color: rgb(77, 168, 238);" data-linktype="2"><span style="color: rgb(77, 168, 238);">一键帮你生成所需代码--EasyCode试试这个工具吧</span></a></p> <p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;text-align: justify;letter-spacing: 1.5px;line-height: normal;color: rgb(136, 136, 136);font-size: 13px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(77, 168, 238);">●&nbsp;</span><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA5NDg3MjAwMQ==&amp;mid=2457103280&amp;idx=1&amp;sn=f3bd921a2f88a9a3440c0ff1e03d1bd9&amp;chksm=87c8c31eb0bf4a0820a572626141cfb1fc87ea1c7072a16f61992d67d21d9b61b556de2d6cd7&amp;scene=21#wechat_redirect" textvalue="颠覆微服务认知:深入思考微服务的七个主流观点" data-itemshowtype="11" tab="innerlink" data-linktype="2" hasload="1" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;color: rgb(77, 168, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(77, 168, 238);">颠覆微服务认知:</span></a><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA5NDg3MjAwMQ==&amp;mid=2457103280&amp;idx=1&amp;sn=f3bd921a2f88a9a3440c0ff1e03d1bd9&amp;chksm=87c8c31eb0bf4a0820a572626141cfb1fc87ea1c7072a16f61992d67d21d9b61b556de2d6cd7&amp;scene=21#wechat_redirect" textvalue="颠覆微服务认知:深入思考微服务的七个主流观点" data-itemshowtype="11" tab="innerlink" data-linktype="2" hasload="1" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;color: rgb(77, 168, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(77, 168, 238);">深入思考微服务的七个主流观点</span></a><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p> <p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;text-align: justify;letter-spacing: 1.5px;line-height: normal;color: rgb(136, 136, 136);font-size: 13px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(77, 168, 238);">●&nbsp;</span><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA5NDg3MjAwMQ==&amp;mid=2457107164&amp;idx=1&amp;sn=6e98f0086410453bf133a3fd038b8d28&amp;chksm=87c8d272b0bf5b64ae579313c60889519fdd2133faaeacb12e3e6da5a25eb6b6461f3d24e0a4&amp;scene=21#wechat_redirect" data-itemshowtype="11" tab="innerlink" style="font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 13px;letter-spacing: 1.5px;white-space: normal;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);color: rgb(77, 168, 238);" data-linktype="2"><span style="color: rgb(77, 168, 238);">给 IntelliJ IDEA 新用户的插件指南</span></a></p> <p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;text-align: justify;letter-spacing: 1.5px;line-height: normal;color: rgb(136, 136, 136);font-size: 13px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 13px;letter-spacing: 1.5px;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);color: rgb(77, 168, 238);">●&nbsp;<a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA5NDg3MjAwMQ==&amp;mid=2457107566&amp;idx=1&amp;sn=94db85f298a3fe06992ee6c9fbf720d5&amp;chksm=87c8d4c0b0bf5dd6798ebe44c73799aa6a3b8edeef27befa3ed9fd1be9455b42790364577c4f&amp;scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" style="font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 13px;letter-spacing: 1.5px;white-space: normal;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);" data-linktype="2">Kibana,Grafana——错误猎手,系统定海神针</a></span></p> <p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;text-align: justify;letter-spacing: 1.5px;line-height: normal;color: rgb(136, 136, 136);font-size: 13px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="text-align: center;color: rgb(77, 168, 238);">●&nbsp;</span><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA5NDg3MjAwMQ==&amp;mid=2457103538&amp;idx=1&amp;sn=9261bb2b742fd41168889738e0377a9c&amp;chksm=87c8c41cb0bf4d0a5401fac49320faebfc4ac5ad7711d39758ecb0fd9df43354d19aa5aa57e3&amp;scene=21#wechat_redirect" textvalue="一文讲透微服务下如何保证事务的一致性" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="text-align: center;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;color: rgb(77, 168, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(77, 168, 238);">一文讲透微服务下如何保证事务的一致性</span></a></p> <p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;text-align: justify;letter-spacing: 1.5px;line-height: normal;color: rgb(136, 136, 136);font-size: 13px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="text-align: center;color: rgb(77, 168, 238);">●&nbsp;</span><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA5NDg3MjAwMQ==&amp;mid=2457107361&amp;idx=1&amp;sn=821fd5e72866eb9a06bbfdfe06ba1e27&amp;chksm=87c8d30fb0bf5a193a7d4da898e9461a6b2a4d6bfaadeeaf5260b7c60906b9253018854070cd&amp;scene=21#wechat_redirect" data-itemshowtype="11" tab="innerlink" style="font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 13px;letter-spacing: 1.5px;white-space: normal;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);text-align: center;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;color: rgb(77, 168, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;" data-linktype="2"><span style="font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 13px;letter-spacing: 1.5px;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);text-align: center;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;color: rgb(77, 168, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">我只是下了个订单,鬼知道我在微服务里经历了什么?</span></a></p> </section> </section> </section> </section> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p> </section> </section> </section> </section> </section> </section> <section powered-by="xiumi.us" style="max-width: 100%;color: rgb(62, 71, 83);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 16px;letter-spacing: 0.544px;text-align: center;white-space: normal;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img data-ratio="0.3708738" src="/upload/328ca60e65e37255ffc9d00bfba58c3c.png" data-type="png" data-w="515" style="vertical-align: middle;box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;width: 515px !important;"></p> <p style="margin-right: 16px;margin-left: 16px;max-width: 100%;min-height: 1em;letter-spacing: 1.5px;line-height: 1.5em;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section powered-by="xiumi.us" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;display: inline-block;width: 574px;vertical-align: top;border-style: solid;border-width: 1px;border-radius: 0px;border-color: rgb(225, 238, 226);background-color: rgba(191, 224, 216, 0.58);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="padding: 10px;max-width: 100%;display: inline-block;width: 572px;vertical-align: top;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section powered-by="xiumi.us" style="max-width: 100%;font-size: 15px;color: rgb(46, 101, 91);line-height: 1.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 12px;box-sizing: border-box !important;overflow-wrap: break-word !important;">关注我,回复 「加群」 加入各种主题讨论群。</span></p> </section> </section> </section> </section> </section> <section powered-by="xiumi.us" style="margin-top: 10px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;background-color: rgb(147, 195, 186);height: 2px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> </section> </section> </section> </section> </section> </section> </section> </section> </section> <section powered-by="xiumi.us" style="max-width: 100%;color: rgb(62, 71, 83);font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 16px;letter-spacing: 0.544px;text-align: center;white-space: normal;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p> <p style="margin-right: 16px;margin-left: 16px;max-width: 100%;min-height: 1em;letter-spacing: 1.5px;line-height: 1.5em;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 12px;letter-spacing: 1px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">对「服务端思维」有期待,请在文末点个<span style="max-width: 100%;color: rgb(198, 19, 25);">在看</span></span></strong></p> <p style="margin-right: 16px;margin-left: 16px;max-width: 100%;min-height: 1em;letter-spacing: 1.5px;line-height: 1.5em;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 12px;letter-spacing: 1px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">喜欢这篇文章,欢迎<span style="max-width: 100%;color: rgb(198, 19, 25);">转发、分享</span>朋友圈</span></strong></p> </section> </section> </section> <p style="max-width: 100%;min-height: 1em;font-size: 14px;letter-spacing: 0.544px;text-align: center;white-space: normal;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);color: rgb(63, 63, 63);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p> <section data-role="outer" label="Powered by 135editor.com" style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;text-align: center;white-space: normal;caret-color: rgb(51, 51, 51);background-color: rgb(255, 255, 255);color: rgb(63, 63, 63);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section data-role="outer" label="Powered by 135editor.com" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section data-tools="135编辑器" data-id="94783" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;display: flex;justify-content: flex-end;align-items: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;width: 20px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="max-width: 100%;width: 20px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <img data-ratio="1.037037037037037" src="/upload/6087772642bee9a4b8104517c36c9aa7.png" data-type="png" data-w="27" data-width="100%" style="display: block;box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;width: 20px !important;"> </section> </section> <section style="max-width: 100%;display: inline-block;text-align: right;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section data-brushtype="text" style="margin-bottom: -15px;max-width: 100%;letter-spacing: 2px;transform: rotate(0deg);box-sizing: border-box !important;overflow-wrap: break-word !important;"> 在看点这里 </section> </section> <section style="max-width: 100%;width: 22px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 15px;max-width: 100%;width: 22px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <img class="__bg_gif" data-ratio="1" src="/upload/8b34c0e8ddcf33f56770ae9f44c95ce1.gif" data-type="gif" data-w="100" data-width="100%" style="display: block;box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;width: 22px !important;"> </section> </section> </section> </section> </section> </section>

一文教会你写90%的shell脚本!

作者:微信小助手

<section style="letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 14px;color: rgb(136, 136, 136);">作者丨匠心Java&nbsp;</span> </section> <section style="letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 14px;color: rgb(136, 136, 136);">来源丨匠心Java(<span style="font-size: 14px;font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;text-align: start;background-color: rgb(255, 255, 255);">code-the-world</span>)</span> </section> <section style="letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <br> </section> <section style="text-align: start;box-sizing: border-box;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">大家好,我是老C,很开心和大家见面</span> <img src="/upload/5f7442affd438551d482eff3276989cc.null" data-ratio="1" data-w="20" style="display:inline-block;width:20px;vertical-align:text-bottom;"> <span style="font-size: 16px;">~<br></span> </section> <blockquote style="text-align: start;white-space: normal;box-sizing: border-box;margin-bottom: 1.2em;padding: 15px 0.5em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);font-size: 14px;line-height: 22px;background: rgb(242, 247, 251);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"> <section style="box-sizing: border-box;margin-bottom: 15px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);letter-spacing: 1.5px;"> <span style="font-size: 16px;">在公司项目的开发过程中,总会有需要编写shell脚本去处理一个业务的时候,也有的小伙伴总会问我关于shell脚本的一些知识,在这里博主整理了一篇文章</span> </section> <section style="box-sizing: border-box;margin-top: 15px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);letter-spacing: 1.5px;"> <span style="font-size: 16px;">看完这边文章应该就可以独立完成大部分脚本的编写~</span> </section> </blockquote> <h4 style="text-align: start;white-space: normal;box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 18px;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;"><span style="font-size: 16px;">shell脚本?</span></h4> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">在说什么是shell脚本之前,先说说什么是shell。</strong></span> </section> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">shell是外壳的意思,就是操作系统的外壳。我们可以通过shell命令来操作和控制操作系统,比如Linux中的Shell命令就包括ls、cd、pwd等等。总结来说,Shell是一个命令解释器,它通过接受用户输入的Shell命令来启动、暂停、停止程序的运行或对计算机进行控制。</span> </section> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">shell 是一个应用程序,它连接了用户和 Linux 内核,让用户能够更加高效、安全、低成本地使用 Linux 内核,这就是 Shell 的本质。</span> </section> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">shell 本身并不是内核的一部分,它只是站在内核的基础上编写的一个应用程序。</span> </section> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">那么什么是shell脚本呢?</strong></span> </section> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">shell脚本就是由Shell命令组成的执行文件,将一些命令整合到一个文件中,进行处理业务逻辑,脚本不用编译即可运行。它通过解释器解释运行,所以速度相对来说比较慢。</span> </section> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">shell脚本中最重要的就是对shell命令的使用与组合,再使用shell脚本支持的一些语言特性,完成想要的功能。</span> </section> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;"><strong>写shell脚本需要了解什么知识呢?</strong></span> </section> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">语法-调试技巧-实战案例,本篇都有,开整~</span> <img src="/upload/f447b1cf6f50e8f0cea75951a3fa51b4.null" data-ratio="1" data-w="20" style="display:inline-block;width:20px;vertical-align:text-bottom;"> </section> <hr style="border-style: solid;border-width: 1px 0px 0px;border-color: rgba(0, 0, 0, 0.1);transform-origin: 0px 0px 0px;transform: scale(1, 0.5);padding-left: 0.5em;padding-right: 0.5em;"> <h4 style="text-align: start;white-space: normal;box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 18px;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;"><span style="font-size: 16px;">注释<br></span></h4> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">“# ”开头的就是注释,被编译器忽略</span> </section> <ul style="list-style-type: square;padding-left: 0.5em;padding-right: 0.5em;" class="list-paddingleft-2"> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">单行注释:#</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">多行注释::&lt;&lt;eof ...="" eof="" 或者="" :&lt;&lt;!="" !="" (:&lt;&lt;="" 标识多行注释开始,并指定一个标识符作为开始结束的标志)&lt;="" li="" style="box-sizing: border-box;"&gt;</span> </section></li> </ul> <hr style="text-align: start;white-space: normal;box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1.5rem;border-style: dashed none none;border-top-color: rgb(165, 165, 165);border-right-width: initial;border-right-color: initial;border-left-width: initial;border-left-color: initial;border-bottom-width: initial;border-bottom-color: initial;height: 1px;color: rgb(80, 97, 109);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 16px;padding-left: 0.5em;padding-right: 0.5em;"> <h4 style="text-align: start;white-space: normal;box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 18px;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;"><span style="font-size: 16px;">变量</span></h4> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">变量类型</strong>运行shell时,会同时存在三种变量:</span> </section> <ol class="list-paddingleft-2" style="padding-left: 0.5em;padding-right: 0.5em;"> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">局部变量:局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量。</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">环境变量:所有的程序,包括shell启动的程序,都能访问环境变量,有些程序需要环境变量来保证其正常运行。必要的时候shell脚本也可以定义环境变量。</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">shell变量:shell变量是由shell程序设置的特殊变量。shell变量中有一部分是环境变量,有一部分是局部变量,这些变量保证了shell的正常运行</span> </section></li> </ol> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">变量操作</strong></span> </section> <ul style="list-style-type: square;padding-left: 0.5em;padding-right: 0.5em;" class="list-paddingleft-2"> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">创建普通变量:name="test" (=两边不可有空格)</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">创建只可函数体中使用的局部变量:local name="test" (使用local修饰的变量在函数体外无法访问,并且local只能在函数体内使用)</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">使用变量:echo $name 或者 echo ${name} (推荐使用大括号版)</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">变量重新赋值:name="new_test" (将原值覆盖)</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">只读变量:name="only_read" -&gt; readonly name (使用readonly标识后的变量,不可被修改)</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">删除变量:unset name; (删除之后不可访问,删除不掉只读变量)</span> </section></li> </ul> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">字符串变量</strong>1)单引号</span> </section> <ul style="list-style-type: square;padding-left: 0.5em;padding-right: 0.5em;" class="list-paddingleft-2"> <li> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">单引号变量&nbsp;<code style="box-sizing: border-box;background: rgb(243, 241, 241);color: rgb(88, 88, 88);font-size: 16px;line-height: 18px;font-family: consolas, menlo, courier, monospace, &quot;Microsoft Yahei&quot;!important;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span style="box-sizing: border-box;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;display: inline-block;padding-right: 2px;padding-left: 2px;line-height: 22px;">var='test'</span></code>&nbsp;,只能原样输出,变量无效</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">单引号中不能出现一个单独的单引号,转义也不可以</span> </section></li> </ul> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">2)双引号</span> </section> <ul style="list-style-type: square;padding-left: 0.5em;padding-right: 0.5em;" class="list-paddingleft-2"> <li> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">双引号变量&nbsp;<code style="box-sizing: border-box;background: rgb(243, 241, 241);color: rgb(88, 88, 88);font-size: 16px;line-height: 18px;font-family: consolas, menlo, courier, monospace, &quot;Microsoft Yahei&quot;!important;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span style="box-sizing: border-box;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;display: inline-block;padding-right: 2px;padding-left: 2px;line-height: 22px;">var="my name is ${name}"</span></code>,变量有效</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">可出现转义符</span> </section></li> </ul> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">3)拼接字符串</span> </section> <ul style="list-style-type: square;padding-left: 0.5em;padding-right: 0.5em;" class="list-paddingleft-2"> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">中间无任何+,之类的字符</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">name="this is"" my name"; name="this is my name"; name="this" is "my name" 等效</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">name='this is'' my nam'; name='this is my name'; name='this' is 'my name' 等效</span> </section></li> </ul> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">4)获取字符串长度</span> </section> <ul style="list-style-type: square;padding-left: 0.5em;padding-right: 0.5em;" class="list-paddingleft-2"> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">在${}中使用“#”获取长度</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">name="test";</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">echo ${#name}; # 输出为4</span> </section></li> </ul> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">5)提取子字符串</span> </section> <ul style="list-style-type: square;padding-left: 0.5em;padding-right: 0.5em;" class="list-paddingleft-2"> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">1:4 从第2个开始 往后截取4个字符</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">::4 从第一个字符开始 往后截取4个字符</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">name="this is my name";</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">echo ${name:1:4} #输出 is i</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">echo ${name::4} #输出 this</span> </section></li> </ul> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">数组</strong></span> </section> <section style="text-align: start;box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 1.5px;padding-left: 0.5em;padding-right: 0.5em;"> <span style="font-size: 16px;">bash只支持一维数组,不支持多维数组</span> </section> <ul style="list-style-type: square;padding-left: 0.5em;padding-right: 0.5em;" class="list-paddingleft-2"> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">定义数组:array_name=(li wang xiang zhang) (小括号做边界、使用空格分离)</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">单独定义数组的元素:arraypara[0]="w"; arraypara[3]="s" (定义时下标不连续也可以)</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">赋值数组元素:array_name[0]="zhao";</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">获取数组元素:</span> </section></li> <ul style="list-style-type: circle;" class="list-paddingleft-2"> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="font-size: 16px;">array<span style="box-sizing: border-box;">name[0]="li"</span></span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="font-size: 16px;"><span style="box-sizing: border-box;">array</span>name[3]="zhang"</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="font-size: 16px;">echo ${array<span style="box-sizing: border-box;">name[0]} # 输出"li"</span></span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="font-size: 16px;"><span style="box-sizing: border-box;">echo ${array</span>name[1]} # 输出" "</span> </section></li> <li style="font-size: 16px;"> <section style="letter-spacing: 1.5px;"> <span style="font-size: 16px;">echo ${array<span style="box-sizing: border-box;">name[3]} # 输出"zhang"</span></span> </section></li> <li styl

QPS、TPS、RT、并发数、吞吐量理解和性能优化深入思考

作者:微信小助手

<h3 data-lake-id="0dfa002eee6bd769a79fe32c1433f6f4" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">吞吐量</h3> <p data-lake-id="83095c2a743fb1ce4c7b07b973bb9981" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">在了解qps、tps、rt、并发数之前,首先我们应该明确一个系统的吞吐量到底代表什么含义,一般来说,系统吞吐量指的是系统的抗压、负载能力,代表一个系统每秒钟能承受的最大用户访问量。</p> <p data-lake-id="affe5c518806d8ef3e602bf318236b0d" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">一个系统的吞吐量通常由qps(tps)、并发数来决定,每个系统对这两个值都有一个相对极限值,只要某一项达到最大值,系统的吞吐量就上不去了。</p> <p data-lake-id="639cf781c9774c505901d7e81658d62e" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <h3 data-lake-id="5c08382e70523be6bfad01b10c01536c" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">QPS</h3> <p data-lake-id="d287878ed4b03d6c0c07924a9528272f" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">Queries Per Second,每秒查询数,即是每秒能够响应的查询次数,注意这里的查询是指用户发出请求到服务器做出响应成功的次数,简单理解可以认为查询=请求request。</p> <p data-lake-id="7d4beed2bfc2049d8e83e83ae7679ecf" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="c010741e44f72dec3eebfa43a4e80980" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="background-color: #FADB14;">qps=每秒钟request数量</span></p> <p data-lake-id="b52405f6a3096b6d8ad2afad626297ad" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="background-color: #FADB14;"></span></p> <h3 data-lake-id="a46a27d8b050b91066fcb53bd599dd84" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">TPS</h3> <p data-lake-id="4daa0defb6dad6b6aa85376cc7532719" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">Transactions Per Second 的缩写,每秒处理的事务数。一个事务是指一个客户机向服务器发送请求然后服务器做出反应的过程。客户机在发送请求时开始计时,收到服务器响应后结束计时,以此来计算使用的时间和完成的事务个数。</p> <p data-lake-id="38af1174b1eaeb823907bac1be2d2f38" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">针对单接口而言,TPS可以认为是等价于QPS的,比如访问一个页面/index.html,是一个TPS,而访问/index.html页面可能请求了3次服务器比如css、js、index接口,产生了3个QPS。</p> <p data-lake-id="eb1e84bbce47ed642537b1931e8a0ac6" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="fb0c22cfc0cec84054faed2152f6cb9f" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="background-color: #FADB14;">tps=每秒钟事务数量</span></p> <p data-lake-id="398f1d39041d67991bcb0dc2aceb7a9c" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="background-color: #FADB14;"></span></p> <h3 data-lake-id="64297f955fa4cfc17aa08e700f3c98c1" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">RT</h3> <p data-lake-id="cbf6ea5ece71640550f91f09adfcfaf0" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">Response Time缩写,简单理解为系统从输入到输出的时间间隔,宽泛的来说,他代表从客户端发起请求到服务端接受到请求并响应所有数据的时间差。<span style="background-color: #FADB14;">一般取平均响应时间</span>。</p> <p data-lake-id="f2893cba0233960c09a98a1882b4733d" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <h3 data-lake-id="43883d16ed2f694300db8a8c0bfa8f86" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">并发数</h3> <p data-lake-id="94ed8793e2337d7edf23fb96707c83cc" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">简而言之,系统能同时处理的请求/事务数量。</p> <p data-lake-id="ca7d1698882f4fe81df8b5831dceb655" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <h3 data-lake-id="f6b4e87f7c26d15bc74c990b4ea835da" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">计算方式</h3> <p data-lake-id="abccd34e12ffb2d1d9ea0b6cce53292b" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span style="background-color: #FADB14;">QPS=并发数/RT 或者 并发数=QPS*RT</span></p> <p data-lake-id="cb6723d328b0d26587828e3554a4a9b3" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">举个栗子:</p> <p data-lake-id="631a9d4cb056bdbdee1579772bcb915b" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">假设公司每天早上9点到10点1个小时内都有员工要上厕所,公司有3600个员工,平均每个员工上厕所时间为10分钟,我们来计算一下。</p> <p data-lake-id="647928e4e7b8ec61b5a96fbe27ce7846" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">QPS &nbsp; &nbsp;= 3600/(60*60) &nbsp; 1</p> <p data-lake-id="b42f88e225fd5b6528293ee9d39a3941" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">RT &nbsp; &nbsp; &nbsp;= 10*60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;600秒</p> <p data-lake-id="aa68531b7ba30995ef635ad58c4f335f" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">并发数 = 1 * 600 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;600</p> <p data-lake-id="584a8585472dfd13652122507a65c63b" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">这样就意味着如果想达到最好的蹲坑体验,公司需要600个坑位来满足员工需求,否则的话上厕所就要排队等待了。</p> <p data-lake-id="99f76c6692aec421d9d65e3cdf9dff10" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <h3 data-lake-id="7eebe7592b79a9f1b3eeef725a0c5676" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">性能思考</h3> <p data-lake-id="98c87627d406032ad897a3c90c3d7bcf" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">按照QPS=并发数/RT公式,假设我们现在是单线程的场景,那么QPS公式应该是这样:QPS=1/RT,实际上RT应该=CPU time + CPU wait time,如果将线程数提高到2,那么QPS=2/(CPU time + CPU wait time),那么是否意味着我们只要单纯提高线程数就能提高QPS呢?</p> <p data-lake-id="249ad9e7e5e4002c20aa4136d5dd962a" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="5ae3982fe6ad727c96544f81a2e757f5" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <h3 data-lake-id="b645c9433867b0e282c48bbc6ceb9c36" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">最佳线程数计算</h3> <p data-lake-id="5ae3982fe6ad727c96544f81a2e757f5" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">假设CPU time是49ms,CPU wait time是200ms,那么QPS=1000ms/249ms=4.01,这里200ms的wait时间我们可以认为CPU一直处于等待状态啥也没干,理论上来说200ms还可以接受200/49≈4个请求,不考虑上下文切换和其他开销的话,可以认为总线程数=(200+49)/49=5,如果再考虑上CPU多核和利用率的问题,我们大致可以认为:<strong style="letter-spacing: 0.008em;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">最佳线程数=</strong><strong style="letter-spacing: 0.008em;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">RT/CPU Time * CPU核心数 * CPU利用率</strong></p> <p data-lake-id="9754183938b8fcb8cc85504c7177bd29" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="849803fbfbb75f813b00bfcec0714ae9" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="849803fbfbb75f813b00bfcec0714ae9" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">那么最大QPS公式推导为:</p> <p data-lake-id="a26df30619d3afdb6cfacd7248fcb610" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">最大QPS=最佳线程数*单线程QPS=(<strong>RT/CPU Time * CPU核心数 * CPU利用率)*(1/RT) = CPU核心数*CPU利用率/CPU time</strong></p> <p data-lake-id="bdc97f53baffe4bd0f32d1e2a79b5a57" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="6b5284bda5e1d6475626216e1e7cf8ed" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">那么这样是否意味着我们只要不停增加CPU核心数就能无限提高QPS呢</p> <p data-lake-id="0a452a19aff857789cae15cc18492a05" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="425ba81efa5f4c77f6ecf7fb0915076b" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <h3 data-lake-id="3e8f4a13c6dcaf02b7dd58b249781942" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">阿姆达尔定律Amdahl</h3> <p data-lake-id="425ba81efa5f4c77f6ecf7fb0915076b" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><span data-card-type="inline" data-lake-card="image" data-card-value="data:%7B%22src%22%3A%22https%3A%2F%2Fcdn.nlark.com%2Fyuque%2F0%2F2020%2Fpng%2F1399111%2F1598073681754-ba28810e-09c3-41cd-9d8d-72afe12f6a5e.png%22%2C%22originWidth%22%3A229%2C%22originHeight%22%3A52%2C%22name%22%3A%22image.png%22%2C%22size%22%3A13490%2C%22display%22%3A%22inline%22%2C%22align%22%3A%22left%22%2C%22linkTarget%22%3A%22_blank%22%2C%22status%22%3A%22done%22%2C%22ocrLocations%22%3A%5B%7B%22x%22%3A2.8484247%2C%22y%22%3A10.756137%2C%22width%22%3A54.947798299999995%2C%22height%22%3A14.494356999999999%2C%22text%22%3A%22Speedup%22%7D%2C%7B%22x%22%3A174.24815%2C%22y%22%3A19.048967%2C%22width%22%3A44.807289999999995%2C%22height%22%3A12.717870999999999%2C%22text%22%3A%22pctPar%22%7D%2C%7B%22x%22%3A77.37132%2C%22y%22%3A24.773264%2C%22width%22%3A82.93227999999999%2C%22height%22%3A17.952485999999997%2C%22text%22%3A%22(1-pctPar)%22%7D%5D%2C%22style%22%3A%22none%22%2C%22search%22%3A%22Speedup%20pctPar%20(1-pctPar)%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22width%22%3A158%2C%22height%22%3A36%7D"><img data-ratio="0.22707423580786026" src="/upload/7f8718ae7360e37f4e4e967d7431594e.png" data-type="png" data-w="229" style="visibility: visible;width: 291px;height: 66px;" title="image.png"></span><br></p> <p data-lake-id="eab27d71de0b2e39c3b49a26846729cf" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">G.M.Amdahl在1967年提出了Amdahl’s law,针对并行处理的scalability给出了一个模型,指出使用并行处理的提速由问题的可并行的部分所决定。我们可以简单理解为程序通过额外的计算资源,理论上能获得的加速值。</p> <p data-lake-id="715b81bb20c85f96b2fca43192e1662a" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">par为并行计算所占的比例,p为并行处理节点个数</p> <p data-lake-id="f26dc88793d58435d864a950c3fca5a6" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="386d93154e7d268ceb3ac6b8725d1776" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">假设你想从望京去顺义,坐一辆车需要3小时,虽然现在有3辆车,你也不能1小时就到。这里无法并行,所有Par=0%,p=3,加速比还是等于1,并没有提高速度。</p> <p data-lake-id="ccabd7663161dfcf0d3bdb204e2a013f" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="44bb2a1ab9c8537a120a69b0e94cc18d" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <h3 data-lake-id="aa6d26fcc8d41d5ae355a961159c4a39" style="padding-top: 7px;padding-bottom: 7px;font-weight: 700;font-size: 20px;line-height: 28px;">古斯塔夫森定律Gustafson</h3> <p data-lake-id="44bb2a1ab9c8537a120a69b0e94cc18d" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><strong><img data-ratio="0.17177914110429449" src="/upload/71f3d5187ae79df72b78fba2a2567fd7.png" data-type="png" data-w="163" style="visibility: visible;width: 269px;height: 46px;" title="image.png"></strong><br></p> <p data-lake-id="55f4f5a208a2175ce2add69555f79497" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">斯塔夫森定律又被称为扩展的加速比(scaled speedup),他说明处理器个数、串行比例和加速比之间的关系,只是和阿姆达尔定律侧重角度有所不同。</p> <p data-lake-id="81e9463b9a10492e7ab613a6d60906de" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="e9cedea297b7f3fa8a71fd3f433a07f7" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">按照阿姆达尔定律和QPS计算公式,在CPUtime 和 CPU利用率不变的情况下,增加CPU核心数就能增加最大QPS,在par不为0即并行的时候,增加并行数量p就能提升效率,但是实际上随着请求数量的增加,带来大量的上下文的切换、gc和锁变化。qps更高,产生对象越多,gc越频繁,cpu time和利用率都受到影响,尤其在串行的时候,锁自旋、自适应、偏向等等也成为影响par的因素。</p> <p data-lake-id="799c1e3e06bf78b2bb24758e8786a03a" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="ff43887fcc4af1bea7f81f596427af40" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">总结,为了提升达到最好的性能,我们需要不断的进行性能测试,调整小城池大小,找到最合适的参数来达到提高性能的目的。</p> <p data-lake-id="9481709e6ce7a1722388e173d12d0c39" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <p data-lake-id="7d9f107c74b6ed630522543f36f85d06" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;"><br></p> <blockquote class="js_blockquote_wrap" data-type="2" data-url="" data-author-name="" data-content-utf8-length="258" data-source-title=""> <section class="js_blockquote_digest"> <section> <p data-lake-id="a37b4a336f86327e526a76da9883c3da" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">参考:<br></p> <p data-lake-id="a37b4a336f86327e526a76da9883c3da" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">http://javahao123.com/?p=772</p> <p data-lake-id="a37b4a336f86327e526a76da9883c3da" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">https://cloud.tencent.com/developer/article/1106559</p> <p data-lake-id="a37b4a336f86327e526a76da9883c3da" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">https://www.cnblogs.com/caishunzhe/p/13056105.html</p> <p data-lake-id="a37b4a336f86327e526a76da9883c3da" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">https://www.jianshu.com/p/8532ac88ce72</p> <p data-lake-id="a37b4a336f86327e526a76da9883c3da" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">https://zhuanlan.zhihu.com/p/66929848</p> <p data-lake-id="a37b4a336f86327e526a76da9883c3da" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;overflow-wrap: break-word;">https://www.cnblogs.com/lupeng2010/p/12705795.html</p> </section> </section> </blockquote> <p><br></p> <pre ng-bind-html="message.MMActualContent" style="max-width: 100%;letter-spacing: 0.544px;text-align: left;color: rgb(62, 62, 62);font-size: 15px;background-color: rgb(255, 255, 255);word-spacing: 2px;white-space: pre-wrap;word-break: normal;box-sizing: border-box !important;word-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;color: rgb(255, 76, 0);box-sizing: border-box !important;word-wrap: break-word !important;">特别推荐一个分享架构+算法的优质内容,还没关注的小伙伴,可以长按关注一下:</span></p><p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.5555555555555556" data-s="300,640" data-type="jpeg" data-w="900" src="/upload/c86a3619353fb9ac25fa19df4ec53cb1.jpg" style="box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: 677px !important;"></p><p style="max-width: 100%;min-height: 1em;text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.5555555555555556" data-s="300,640" data-type="png" data-w="900" src="/upload/c8a5249d1ca6482fd512bd028bfaa944.png" style="box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: 677px !important;"></p></pre> <p style="max-width: 100%;min-height: 1em;color: rgba(0, 0, 0, 0.8);font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 16px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.5555555555555556" data-s="300,640" data-type="png" data-w="900" src="/upload/6ceab10a2b199fa5589c60da151b3e93.png" style="box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: 677px !important;"></p> <pre ng-bind-html="message.MMActualContent" style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;text-align: left;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);word-spacing: 2px;white-space: pre-wrap;word-break: normal;box-sizing: border-box !important;word-wrap: break-word !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;">长按订阅更多精彩▼</p><p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"><img class="rich_pages" data-ratio="1" data-s="300,640" data-type="jpeg" data-w="258" width="205px" src="/upload/cff6d4b6b063076da067005708088611.jpg" style="box-shadow: rgb(170, 170, 170) 0px 0px 14px 0px;box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: 205px !important;"></p><p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-align: right;line-height: 2em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;word-wrap: break-word !important;">如有收获,点个在看,诚挚感谢</span></p></pre>