文章列表

jdk8 里面常用的Stream Api

作者:微信小助手

<section style="width: 100%;margin-top: 10px;margin-right: auto;margin-left: auto;padding: 10px;display: flex;box-sizing: border-box;" data-width="100%"> <section style="width: 20px;border-top: 1px solid rgb(72, 83, 75);box-sizing: border-box;"> <br> <br> </section> <section style="width: 100%;border-top: 1px solid rgb(72, 83, 75);margin-left: 5px;box-sizing: border-box;" data-width="100%"></section> <section style="width: 40px;background: rgb(255, 255, 255) none repeat scroll 0% 0%;padding-right: 6px;padding-left: 6px;margin-top: -16px;box-sizing: border-box;"> <section style="width: 30px;"> <img class="" data-ratio="0.6551724137931034" src="/upload/edb1bb9a513def00fb8449722299462a.png" data-type="png" data-w="58" data-width="100%" style="width: 100% !important;height: auto !important;visibility: visible !important;" width="100%"> </section> </section> <section style="width: 100%;border-top: 1px solid rgb(72, 83, 75);margin-right: 5px;box-sizing: border-box;" data-width="100%"></section> <section style="width: 20px;border-top: 1px solid rgb(72, 83, 75);box-sizing: border-box;"> <br> </section> </section> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;white-space: normal;background-color: rgb(255, 255, 255);">为什么需要 Stream&nbsp;<br style="box-sizing: border-box;outline: 0px;word-break: break-all;">Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念。它也不同于 StAX 对 XML 解析的 Stream,也不是 Amazon Kinesis 对大数据实时处理的 Stream。Java 8 中的 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利、高效的聚合操作(aggregate operation),或者大批量数据操作 (bulk data operation)。Stream API 借助于同样新出现的 Lambda 表达式,极大的提高编程效率和程序可读性。同时它提供串行和并行两种模式进行汇聚操作,并发模式能够充分利用多核处理器的优势,使用 fork/join 并行方式来拆分任务和加速处理过程。通常编写并行代码很难而且容易出错, 但使用 Stream API 无需编写一行多线程的代码,就可以很方便地写出高性能的并发程序。所以说,Java 8 中首次出现的 java.util.stream 是一个函数式语言+多核时代综合影响的产物。</p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;font-size: 16px;text-align: justify;background-color: rgb(255, 255, 255);">1. Java 7 的排序、取值实现</span></p> <p style="text-align: center;"><img class="" data-backh="330" data-backw="558" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3Sia4bM2DCKCia1W5GYW39PvRDAoAKZGHo3JcIib63o3VOVr7JWcB0l9P9A/0?wx_fmt=png" data-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_jpg/48MFTQpxichntAkr4Wmc26VYKChciaEF3Szap4FamSwsVcwIdrM3S1tAbFc4N1SvPtTGKqricicbPcCzrHwet0Mg8g/0?wx_fmt=jpeg" data-ratio="0.5925925925925926" data-s="300,640" src="/upload/317a0803e1cb0df22bbe80846a14eaba.jpg" data-type="jpeg" data-w="567" style="width: 100%;height: auto;"></p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;font-size: 16px;text-align: justify;background-color: rgb(255, 255, 255);"><span style="font-family: Consolas, Inconsolata, Courier, monospace;font-size: 14px;white-space: pre;background-color: rgb(246, 248, 250);"><br></span></span></p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;white-space: normal;background-color: rgb(255, 255, 255);">而在 Java 8 使用 Stream,代码更加简洁易读;而且使用并发模式,程序执行速度更快。</p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;white-space: normal;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;">2. Java 8 的排序、取值实现</span></p> <p style="text-align: center;"><img class="" data-backh="123" data-backw="500" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3Sjw6PJNPrVy62RS9RxjKkvNEFxZjv5Ld8IVhvNKElRRgYm6krnObmicA/0?wx_fmt=png" data-ratio="0.24649298597194388" data-s="300,640" src="/upload/1c57dc9c8419b24a3167a94e778ccaf3.png" data-type="png" data-w="499" style="width: 100%;height: auto;"></p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;font-size: 16px;text-align: justify;background-color: rgb(255, 255, 255);"><span style="font-family: Consolas, Inconsolata, Courier, monospace;font-size: 14px;white-space: pre;background-color: rgb(246, 248, 250);"></span></span><br></p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;font-size: 16px;text-align: justify;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;font-size: 16px;text-align: justify;background-color: rgb(255, 255, 255);">3. 一个流操作的示例</span></span></p> <p style="text-align: center;"><img class="" data-backh="106" data-backw="478" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3Se4TTnd0bSct7LU8PydCvhEVRf4WDMVGGCspvrYWibZhwCEoGsk9nFicA/0?wx_fmt=png" data-ratio="0.22012578616352202" data-s="300,640" src="/upload/7ea8d6dc4c6d425af32fd3ae791a48d2.png" data-type="png" data-w="477" style="width: 100%;height: auto;"></p> <p><br></p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-variant-ligatures: common-ligatures;white-space: normal;background-color: rgb(255, 255, 255);">stream() 获取当前小物件的 source,filter 和 mapToInt 为 intermediate 操作,进行数据筛选和转换,最后一个 sum() 为 terminal 操作,对符合条件的全部小物件作重量求和。</p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-variant-ligatures: common-ligatures;white-space: normal;background-color: rgb(255, 255, 255);"><br></p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-variant-ligatures: common-ligatures;white-space: normal;background-color: rgb(255, 255, 255);">下面提供最常见的几种构造 Stream 的样例。</p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-variant-ligatures: common-ligatures;white-space: normal;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;">4. 构造流的几种常见方法</span></p> <p style="text-align: center;"><img class="" data-backh="214" data-backw="507" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3ShoKmiaq2UoH3ibBZmxbuQ4nIPfib4Fhxp35pqRbAxtbB8yz66DG0jia3Bw/0?wx_fmt=png" data-ratio="0.42292490118577075" data-s="300,640" src="/upload/9414c7c7e487de9455919a8a2aebf1da.png" data-type="png" data-w="506" style="width: 100%;height: auto;"></p> <p><span style="font-family: Consolas, Inconsolata, Courier, monospace;font-size: 14px;font-variant-ligatures: common-ligatures;white-space: pre;background-color: rgb(246, 248, 250);"><br></span></p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-variant-ligatures: common-ligatures;white-space: normal;background-color: rgb(255, 255, 255);">需要注意的是,对于基本数值型,目前有三种对应的包装类型 Stream:</p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-variant-ligatures: common-ligatures;white-space: normal;background-color: rgb(255, 255, 255);">IntStream、LongStream、DoubleStream。当然我们也可以用 Stream、Stream &gt;、Stream,但是 boxing 和 unboxing 会很耗时,所以特别为这三种基本数值型提供了对应的 Stream。</p> <p style="box-sizing: border-box;outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-variant-ligatures: common-ligatures;white-space: normal;background-color: rgb(255, 255, 255);">Java 8 中还没有提供其它数值型 Stream,因为这将导致扩增的内容较多。而常规的数值型聚合运算可以通过上面三种 Stream 进行。</p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);">5. 数值流的构造</span></p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><span style="font-family: Consolas, Inconsolata, Courier, monospace;font-size: 14px;font-variant-ligatures: common-ligatures;white-space: pre;background-color: rgb(246, 248, 250);"></span></span></p> <p style="text-align: center;"><img class="" data-backh="76" data-backw="556" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3SQ4knWj21ibzSYvr0uadUZ8s0ibYKxojCADXLC1ic37jOXs8RFEBGsxBZA/0?wx_fmt=png" data-ratio="0.13513513513513514" data-s="300,640" src="/upload/57be3214e02c450e925e93ff13f2c5cc.png" data-type="png" data-w="555" style="width: 100%;height: auto;"></p> <p><br></p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);">6. 转换大写</span><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><br></span><br></p> <p><span class="hljs-built_in" style="box-sizing: border-box;outline: 0px;color: rgb(79, 79, 79);font-size: 14px;word-break: break-all;font-family: Consolas, Inconsolata, Courier, monospace;font-variant-ligatures: common-ligatures;white-space: pre;"></span></p> <p style="text-align: left;"><img class="" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3SkiaHwwfEgw8SdYHW37vX5o2gxnRW9qBDXUQAYRdC7XAmIkUic9L1Teeg/0?wx_fmt=png" data-ratio="0.21508379888268156" data-s="300,640" src="/upload/13572e901e47985d59040b82fc61ff13.png" data-type="png" data-w="358" style="width: 358px;height: auto;"></p> <p><span style="font-family: Consolas, Inconsolata, Courier, monospace;font-size: 14px;font-variant-ligatures: common-ligatures;white-space: pre;background-color: rgb(246, 248, 250);"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);">这段代码能把所有的单词转换为大写。</span></span></p> <p><span style="font-family: Consolas, Inconsolata, Courier, monospace;font-size: 14px;font-variant-ligatures: common-ligatures;white-space: pre;background-color: rgb(246, 248, 250);"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><br></span></span></p> <p><span style="font-family: Consolas, Inconsolata, Courier, monospace;font-size: 14px;font-variant-ligatures: common-ligatures;white-space: pre;background-color: rgb(246, 248, 250);"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);">8. 平方数</span></span></span></p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><span class="hljs-built_in" style="box-sizing: border-box;outline: 0px;color: rgb(79, 79, 79);font-size: 14px;word-break: break-all;font-family: Consolas, Inconsolata, Courier, monospace;font-variant-ligatures: common-ligatures;white-space: pre;"></span></span></p> <p style="text-align: left;"><img class="" data-ratio="0.19769673704414586" data-s="300,640" src="/upload/1ff11dce2d8ad495e2cf4aaaae343164.png" data-type="png" data-w="521" style=""></p> <p><span style="color: rgb(79, 79, 79);font-variant-ligatures: common-ligatures;font-weight: 700;text-align: justify;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 14px;white-space: pre;background-color: rgb(246, 248, 250);"></span><br></p> <p><span style="font-family: Consolas, Inconsolata, Courier, monospace;font-size: 14px;font-variant-ligatures: common-ligatures;white-space: pre;background-color: rgb(246, 248, 250);"><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);">9 一对多</span></span></p> <p style="text-align: left;"><img class="" data-ratio="0.2526997840172786" data-s="300,640" src="/upload/f85e13d8ba5c4e64747a3cafa4235cdb.png" data-type="png" data-w="463" style=""></p> <p style="text-align: justify;"><span style="color:#4f4f4f;font-family:-apple-system, SF UI Text, Arial, PingFang SC, Hiragino Sans GB, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif;"><span style="font-variant-ligatures: common-ligatures;background-color: rgb(255, 255, 255);">Stream流里面还可以将多个集合转换至一个stream流中</span></span></p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><br></span></p> <p><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;font-weight: 700;text-align: justify;white-space: pre;background-color: rgb(255, 255, 255);">10 使用foreach打印</span></span></p> <p style="text-align: left;"><img class="" data-backh="231" data-backw="452" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3Sw52pvQMFdCWwCsDa30BGzPRBwPFCUCWX5mbmPBK7iaL6oiaNiajKaEWDQ/0?wx_fmt=png" data-ratio="0.5110619469026548" data-s="300,640" src="/upload/2019a22922c1bc4f98c4f2231731a165.png" data-type="png" data-w="452" style="width: 100%;height: auto;"></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);">对一个人员集合遍历,找出男性并打印姓名。可以看出来,forEach 是为 Lambda 而设计的,保持了最紧凑的风格。而且 Lambda 表达式本身是可以重用的,非常方便。当需要为多核系统优化时,可以 parallelStream().forEach(),只是此时原有元素的次序没法保证,并行的情况下将改变串行时操作的行为,此时 forEach 本身的实现不需要调整,而 Java8 以前的 for 循环 code 可能需要加入额外的多线程逻辑。</span></p> <p style="text-align: left;"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><br></span></p> <p style="text-align: left;"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;font-weight: 700;text-align: justify;white-space: pre;background-color: rgb(255, 255, 255);">11 r</span>educe 的使用用例</span></span></p> <p style="text-align: left;"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><br></span></span></p> <p style="text-align: left;"><img class="" data-ratio="0.4161616161616162" data-s="300,640" src="/upload/486e8a190d423561c1ce2a9994f36fbd.png" data-type="png" data-w="495" style=""></p> <p style="text-align: left;"><span style="color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;outline: 0px;font-weight: 700;word-break: break-all;color: rgb(79, 79, 79);font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;font-size: 16px;font-variant-ligatures: common-ligatures;text-align: justify;background-color: rgb(255, 255, 255);"></span></span>jdk8里面的reduce提供了相应的求和函数和最大值,最小值功能。</p> <p style="text-align: left;">利用reduce和map的搭配,我们可以做一些数值流的转换功能,例如说:</p> <p style="text-align: left;"><img class="" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3SYyTLKibtZPOFDdEFraMd36qnibnBtZQFeAygSYAL4zHricetKnHRunE8A/0?wx_fmt=png" data-ratio="0.4567307692307692" data-s="300,640" src="/upload/50110c27d1c93bf699eff860340d6442.png" data-type="png" data-w="416" style="width: 416px;height: auto;"></p> <p style="text-align: left;"><strong>12 filter去重筛选</strong></p> <p style="text-align: left;">java8里面的filter是最常用来去重的操作了,下面这个案例主要是演示如何使用filter来筛选数字。</p> <p style="text-align: center;"><img class="" data-backh="246" data-backw="558" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3SiaLcLCpicVzvxsLYQSIjYiaibGIOawL4CWH7FaWJ2TjVTzFV50RYicoAWkA/0?wx_fmt=png" data-ratio="0.43963254593175854" data-s="300,640" src="/upload/52868dca939937724a8a0b8abedf031b.png" data-type="png" data-w="762" style="width: 100%;height: auto;"></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><strong>13.Map和flatMap映射</strong></p> <p style="text-align: center;"><img class="" data-backh="337" data-backw="558" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3SgqXoyKjr4y8icnhFF3kAxKmyOLfkmVgGIf70KZB5GStHOYcO8rjeJLw/0?wx_fmt=png" data-ratio="0.6042003231017771" data-s="300,640" src="/upload/160812bddeaf534a3757090c6a4bf356.png" data-type="png" data-w="619" style="width: 100%;height: auto;"></p> <p style="text-align: left;">使用map和flatmap的映射可以减少冗余代码的产生,更加地使得我们的代码精简化。</p> <p style="text-align: left;">同时map也可以对于数组类型进行映射:</p> <p style="text-align: center;text-indent: 0em;"><img class="" data-backh="321" data-backw="471" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3ScDrK3lCkf3aVlS7EcDUiabMhB4oRa8tz0E44ibeWdfIWAFMe7l3fkMIw/0?wx_fmt=png" data-ratio="0.6800847457627118" data-s="300,640" src="/upload/50ff56a1180f643a770b778f4b80f865.png" data-type="png" data-w="472" style="width: 100%;height: auto;"></p> <p style="text-align: center;text-indent: 0em;"><br></p> <p style="text-align: left;"><strong>14.查找和匹配</strong></p> <p style="text-align: left;">jdk8里面提供了多种stream的函数,例如anyMatch,allMatch,noneMatch等</p> <p style="text-align: center;"><img class="" data-backh="479" data-backw="558" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3SWnAwwaCMyibMUaSWdIx2miat0LPwbefO9ic6oLMgjvHKDA3y4fQicfuBGA/0?wx_fmt=png" data-ratio="0.8586206896551725" data-s="300,640" src="/upload/c914e38c9931ea518794b2b7c0ceed96.png" data-type="png" data-w="580" style="width: 100%;height: auto;"></p> <p style="text-align: center;"><br></p> <p style="text-align: left;"><strong style="white-space: normal;">15.skip操作</strong></p> <p style="text-align: left;"><img class="" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/48MFTQpxichntAkr4Wmc26VYKChciaEF3SFkswpZNRODicE67qY8Z9pxeqj89ib9416mBmmRx5YZUHbicRMqM0R4EFg/0?wx_fmt=png" data-ratio="0.4147982062780269" data-s="300,640" src="/upload/30f1114001f602f452e84e6afbc981e1.png" data-type="png" data-w="446" style="width: 447px;height: auto;"></p> <p style="text-align: left;"><strong style="white-space: normal;"></strong>jdk8里面的skip操作可以直接跳过相应的元素,它的作用正好和limit功能互补。</p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><strong>16.分组操作</strong></p> <p style="text-align: left;">之前在公司的项目里面就有用到过分组操作,这种操作主要可以用于优化类似于Map&lt;String,List&lt;Object&gt;&gt;的数据结构,案例如下:</p> <p style="text-align: center;"><img class="" data-ratio="0.25375170532060026" data-s="300,640" src="/upload/ea1e528b3f63acf1b4d63e9fd9b52827.png" data-type="png" data-w="733" style=""></p> <p style="text-align: left;">使用groupby操作,直接可以将list中的每个元素,按照一定的规则来进行分类,这是一种特别高效率的手段<br></p> <p style="text-align: center;"><br></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><br></p> <section style="margin: 30px auto;max-width: 100%;text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"> <section style="margin-right: auto;margin-left: auto;max-width: 100%;box-sizing: border-box;border-width: 2px;border-style: solid;border-color: rgb(0, 0, 0);width: 120px;height: 120px;transform: rotate(30deg);word-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box;border-width: 2px;border-style: solid;border-color: rgb(0, 0, 0);width: 120px;height: 120px;transform: rotate(-15deg);word-wrap: break-word !important;"> <img class="KolImg" data-backh="109.02272" data-backw="109.02272" data-ratio="1" src="/upload/6dc5b486d325bd279b6bf85ff89fd231.jpg" data-type="jpeg" data-w="258" style="box-sizing: border-box;height: auto;z-index: -1;cursor: pointer;width: 100%;border-width: 2px;border-style: solid;border-color: rgb(0, 0, 0);float: left;transform: rotate(-15deg);word-wrap: break-word !important;"> </section> </section> </section> <section style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;white-space: pre-wrap;text-align: center;line-height: 1.5;font-size: 18px;box-sizing: border-box !important;word-wrap: break-word !important;">长按二维码</p> <p style="max-width: 100%;min-height: 1em;white-space: pre-wrap;text-align: center;line-height: 1.5;font-size: 18px;box-sizing: border-box !important;word-wrap: break-word !important;">就可以关注我们啦~</p> </section> <p style="text-align: left;"><br></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><br></p>

MySQL中Waiting for table metadata lock的解决方法

作者:じ☆ve宝贝

数据插入长时间阻塞,没有进度,通过 show processlist发现要操作的表Waiting for table metadata lock 状态的连接。首先就是先kill掉,但是问题依旧存在。 ### 1、查看没有提交的事务 - trx_state: 事务状态,一般为RUNNING - trx_started: 事务执行的起始时间,若时间较长,则要分析该事务是否合理 - trx_mysql_thread_id: MySQL的线程ID,用于kill - trx_query: 事务中的sql ``` select trx_state, trx_started, trx_mysql_thread_id, trx_query from information_schema.innodb_trx ``` ![](/upload/294143cd36f248e78b06a688abb72303.png) **一般只要kill掉这些线程,DDL操作就不会Waiting for table metadata lock。** ``` kill trx_mysql_thread_id; ```

25个常用的Linux iptables规则

作者:微信小助手

<section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"></section> <section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"></section> <section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"></section> <p style="text-align: center;"><img class="" data-copyright="0" data-ratio="0.4166666666666667" src="/upload/f6a61a047bb8f91d1d106b25e45f1c24.gif" data-type="gif" data-w="600" style="color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 17px;letter-spacing: 1px;text-align: justify;white-space: normal;border-radius: 4px;box-shadow: none;"></p> <section class="" data-tools="135编辑器" data-id="91525"> <section style="margin: 8px;padding: 10px;max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;line-height: 25.6px;border-radius: 10px;height: auto;box-shadow: rgb(221, 221, 221) 2px 2px 8px;display: -webkit-flex;"> <section style="max-width: 100%;flex: 0 0 2cm;height: 78px;width: 75px;box-sizing: border-box !important;word-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"><a href="https://mp.weixin.qq.com/s?__biz=MzU2MzcxNzgwMg==&amp;mid=2247483650&amp;idx=3&amp;sn=7d8d32410010e2f58a8b0e9b15fa23ec&amp;scene=21#wechat_redirect" target="_blank" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 0px;right: auto;bottom: auto;"><img class="" data-copyright="0" data-cropselx1="0" data-cropselx2="76" data-cropsely1="0" data-cropsely2="74" data-ratio="1" src="/upload/c978be494b76a0bf054d19dfd16ca72d.jpg" data-type="jpeg" data-w="456" style="letter-spacing: 0.544px;text-align: justify;color: rgb(62, 62, 62);border-radius: 37px;width: 76px;height: 76px;margin: 0px;box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;box-shadow: rgb(170, 170, 170) 0px 0px 14px 0px;"></span></a></p> </section> <section style="padding-right: 10px;padding-left: 10px;max-width: 100%;box-sizing: border-box;flex: 1 1 auto;height: 55px;word-wrap: break-word !important;"> <section style="max-width: 100%;line-height: 35px;white-space: nowrap;box-sizing: border-box !important;word-wrap: break-word !important;"> <a href="https://mp.weixin.qq.com/s?__biz=MzU2MzcxNzgwMg==&amp;mid=2247483650&amp;idx=3&amp;sn=7d8d32410010e2f58a8b0e9b15fa23ec&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2"><span style="max-width: 100%;font-family: 宋体, SimSun;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(31, 73, 125);box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="">Linux编程</strong></span></strong></span></a> <span style="max-width: 100%;font-family: 黑体, SimHei;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(31, 73, 125);box-sizing: border-box !important;word-wrap: break-word !important;"></span></strong><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(31, 73, 125);box-sizing: border-box !important;word-wrap: break-word !important;"></span></strong><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(31, 73, 125);box-sizing: border-box !important;word-wrap: break-word !important;"></span></strong></span> </section> <section style="max-width: 100%;font-size: 13px;line-height: 20px;color: rgb(127, 127, 127);box-sizing: border-box !important;word-wrap: break-word !important;"> <a href="https://mp.weixin.qq.com/s?__biz=MzU2MzcxNzgwMg==&amp;mid=2247483650&amp;idx=3&amp;sn=7d8d32410010e2f58a8b0e9b15fa23ec&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2"><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(165, 165, 165);font-family: 黑体, SimHei;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="">点击右侧关注,免费入门到精通!</span></span></a> </section> </section> <section style="max-width: 100%;flex: 0 0 1.5cm;font-size: 15px;color: rgb(86, 187, 55);letter-spacing: 0px;text-align: center;line-height: 6;box-sizing: border-box !important;word-wrap: break-word !important;"> <section style="margin-top: 25px;max-width: 100px;vertical-align: middle;overflow: hidden;box-sizing: border-box !important;word-wrap: break-word !important;"> <section style="max-width: 100%;width: 55px;height: 30px;box-sizing: border-box !important;word-wrap: break-word !important;background-image: url(https://mmbiz.qpic.cn/mmbiz_gif/S7SAuLQzeTVj0YaoibbZqxicYkQrLnR3WtTQlHpHouqUmibjUCY9F5wpG0DmyMetZy9pjDdiabWo4XXdtCib3VcnI7w/640?wx_fmt=gif);background-size: 100% 100%;background-repeat: no-repeat;"> <section style="max-width: 100%;opacity: 0;box-sizing: border-box !important;word-wrap: break-word !important;"> <a href="https://mp.weixin.qq.com/s?__biz=MzU2MzcxNzgwMg==&amp;mid=2247483650&amp;idx=3&amp;sn=7d8d32410010e2f58a8b0e9b15fa23ec&amp;scene=21#wechat_redirect" target="_blank" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 0px;right: auto;bottom: auto;"><img class="" data-copyright="0" data-cropselx1="0" data-cropselx2="56" data-cropsely1="0" data-cropsely2="56" data-ratio="1" src="/upload/f92c6b5f891800a8e8a41e6dad1950e4.jpg" data-type="jpeg" data-w="258" style="margin: 0px;top: auto;left: auto;right: auto;bottom: auto;width: 56px;height: 56px;box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;" title="1081255447.jpg"></span></a> </section> </section> </section> </section> </section> </section> <p><br></p> <p style="line-height: 36.2667px;white-space: normal;padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 14px;color: rgb(136, 136, 136);">作者丨数据库开发</span></p> <p style="line-height: 36.2667px;white-space: normal;padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 14px;color: rgb(136, 136, 136);">http://os.51cto.com/art/201709/551245.htm</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><br></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);">一些常用的 Linux iptables 规则,请根据自己的具体需要再修改。</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img border="0" class="" data-ratio="0.5867895545314901" src="/upload/e8d9527389bc666377fc64daf980c19b.jpg" data-type="jpeg" data-w="651" style="border: none;box-shadow: none;border-radius: 24px;box-sizing: border-box !important;word-wrap: break-word !important;width: 651px !important;visibility: visible !important;" title=""></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><br></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 1. 删除所有现有规则</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.10434782608695652" data-s="300,640" src="/upload/655bc7a9d5987e7177dcabd3cc15e786.png" data-type="png" data-w="345" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 345px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><br></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 2. 设置默认的 chain 策略</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.3353846153846154" data-s="300,640" src="/upload/7ac76b88430ae7e0819a205618b32b33.png" data-type="png" data-w="325" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 325px !important;visibility: visible !important;"><span style="font-size: 16px;color: rgb(74, 74, 74);">&nbsp;</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 3. 阻止某个特定的 IP 地址</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.1745635910224439" data-s="300,640" src="/upload/e02e9019ab5115479d18e03455ae30e4.png" data-type="png" data-w="401" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 401px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><br></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 4. 允许全部进来的(incoming)SSH</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.10792580101180438" data-s="300,640" src="/upload/7f9b1763ea5babdec3b798f5193b951b.png" data-type="png" data-w="593" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 593px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><br></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 5. 只允许某个特定网络进来的 SSH</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.09396914446002805" data-s="300,640" src="/upload/cd018dd3cfd9e933bf9001536fd42a32.png" data-type="png" data-w="713" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 6. 允许进来的(incoming)HTTP</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.11513157894736842" data-s="300,640" src="/upload/3ab054042dd2a8a3ca999f249530f7dc.png" data-type="png" data-w="608" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 608px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 7. 多端口(允许进来的 SSH、HTTP 和 HTTPS)</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.09537166900420757" data-s="300,640" src="/upload/1b82d26e1e44211015a368ffd9c4f6bf.png" data-type="png" data-w="713" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 8. 允许出去的(outgoing)SSH</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.11093247588424437" data-s="300,640" src="/upload/b632eda6b39dc7f08cfc1b4879f4286b.png" data-type="png" data-w="622" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 622px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 9. 允许外出的(outgoing)SSH,但仅访问某个特定的网络</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.09192200557103064" data-s="300,640" src="/upload/17502dcd364ff1b29bb32e80874f58d3.png" data-type="png" data-w="718" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 10. 允许外出的(outgoing) HTTPS</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.11594202898550725" data-s="300,640" src="/upload/363ba4fd1e131d484316cc8b052a3022.png" data-type="png" data-w="621" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 621px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 11. 对进来的 HTTPS 流量做负载均衡</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.24489795918367346" data-s="300,640" src="/upload/9526a5cf37c2ac104190e4d328bb8f68.png" data-type="png" data-w="686" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 12. 从内部向外部 Ping</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.1183206106870229" data-s="300,640" src="/upload/6b53927ded838d32456885cdf911964b.png" data-type="png" data-w="524" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 524px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 13. 从外部向内部 Ping</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.13821138211382114" data-s="300,640" src="/upload/854e8a74fc11346b16ad0f506c91195a.png" data-type="png" data-w="492" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 492px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 14. 允许环回(loopback)访问</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.2098360655737705" data-s="300,640" src="/upload/b4d98e61d24ccc996d1578d1bac5c734.png" data-type="png" data-w="305" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 305px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 15. 允许 packets 从内网访问外网</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.22453222453222454" data-s="300,640" src="/upload/41423f62a46942d1e67c8f17e8fa7e24.png" data-type="png" data-w="481" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 481px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 16. 允许外出的 DNS</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.15536105032822758" data-s="300,640" src="/upload/81c3eab103b5d8dc7a5c062d8b62b284.png" data-type="png" data-w="457" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 457px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 17. 允许 NIS 连接</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.5261044176706827" data-s="300,640" src="/upload/ead293126cdd9954d8ad9eb936511009.png" data-type="png" data-w="498" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 498px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 18. 允许某个特定网络 rsync 进入本机</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.1348973607038123" data-s="300,640" src="/upload/af758a64591f77eb9cb95524cc44de2d.png" data-type="png" data-w="682" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 19. 仅允许来自某个特定网络的 MySQL 的链接</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.11884057971014493" data-s="300,640" src="/upload/de06b9f389f30b5587cb6b2035b1d695.png" data-type="png" data-w="690" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 20. 允许 Sendmail 或 Postfix</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.11410459587955626" data-s="300,640" src="/upload/b80d427cab3c3bcaa6a9df6920908f9b.png" data-type="png" data-w="631" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 631px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 21. 允许 IMAP 和 IMAPS</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.25283630470016205" data-s="300,640" src="/upload/301f1358d7122342964e77f46813ff61.png" data-type="png" data-w="617" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 617px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 22. 允许 POP3 和 POP3S</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.23410852713178296" data-s="300,640" src="/upload/bf8fe5505f592e47274a0fc35f149702.png" data-type="png" data-w="645" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 645px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 23. 防止 DoS 攻击</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.0589171974522293" data-s="300,640" src="/upload/9809409e22220d65af0e0d04f869cb3f.png" data-type="png" data-w="628" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 628px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 24. 设置 422 端口转发到 22 端口</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.1746987951807229" data-s="300,640" src="/upload/694803eff9261cc1b6b47b4a1dbd34da.png" data-type="png" data-w="664" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 664px !important;visibility: visible !important;"></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"># 25. 为丢弃的包做日志(Log)</span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><span style="font-size: 16px;color: rgb(74, 74, 74);"><br></span></p> <p style="padding-left: 0.5em;padding-right: 0.5em;letter-spacing: 1px;"><img class="" data-ratio="0.21813031161473087" data-s="300,640" src="/upload/2c719df80a6ec1d07562e68b8e699418.png" data-type="png" data-w="706" style="box-shadow: none;box-sizing: border-box !important;word-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="letter-spacing: 1px;padding-left: 0.5em;padding-right: 0.5em;"><br></p> <section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"></section> <section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"></section> <p style="text-align: center;letter-spacing: 1.5px;"><span style="background-color: rgb(0, 213, 255);color: rgb(255, 255, 255);"><strong><span style="background-color: rgb(0, 213, 255);font-size: 20px;">&nbsp;推荐↓↓↓&nbsp;</span></strong></span></p> <section style="box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin: 80px 0% 10px;text-align: center;box-sizing: border-box;"> <section style="display: inline-block;vertical-align: top;width: 100%;padding-right: 10px;padding-bottom: 10px;padding-left: 10px;border-radius: 12px;border-width: 0px;border-style: none;border-color: rgb(62, 62, 62);box-sizing: border-box;background-color: rgba(205, 234, 242, 0.96);"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: -70px;margin-right: 0%;margin-left: 0%;box-sizing: border-box;"> <section style="max-width: 100%;vertical-align: middle;display: inline-block;width: 60%;border-width: 5px;border-style: solid;border-color: rgba(100, 217, 251, 0.96);border-radius: 0px;box-sizing: border-box;"> <img class="" data-copyright="0" data-cropselx1="0" data-cropselx2="313" data-cropsely1="0" data-cropsely2="313" data-ratio="1" src="/upload/a04f15a62950ae7679e06356298b7b41.png" data-type="png" data-w="258" style="vertical-align: middle;width: 313px;box-sizing: border-box;height: 313px;"> </section> </section> </section> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="box-sizing: border-box;"> <section style="display: inline-block;vertical-align: top;width: 16%;box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: 10px;margin-right: 0%;margin-left: 0%;font-size: 10px;box-sizing: border-box;"> <section style="padding: 4px;display: inline-block;border-radius: 100%;box-sizing: border-box;background-color: rgba(0, 0, 0, 0);"> <section style="border-radius: 100%;border-width: 2px;border-style: solid;border-color: rgb(0, 0, 0);width: 2em;height: 2em;font-size: 13px;line-height: 1.8em;box-sizing: border-box;"> <p style="box-sizing: border-box;"><strong style="box-sizing: border-box;">长</strong></p> </section> </section> </section> </section> </section> <section style="display: inline-block;vertical-align: top;width: 16%;box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: 10px;margin-right: 0%;margin-left: 0%;font-size: 10px;box-sizing: border-box;"> <section style="padding: 4px;display: inline-block;border-radius: 100%;box-sizing: border-box;background-color: rgba(0, 0, 0, 0);"> <section style="border-radius: 100%;border-width: 2px;border-style: solid;border-color: rgb(0, 0, 0);width: 2em;height: 2em;font-size: 13px;line-height: 1.8em;box-sizing: border-box;"> <p style="box-sizing: border-box;"><strong style="box-sizing: border-box;">按</strong></p> </section> </section> </section> </section> </section> <section style="display: inline-block;vertical-align: top;width: 16%;box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: 10px;margin-right: 0%;margin-left: 0%;font-size: 10px;box-sizing: border-box;"> <section style="padding: 4px;display: inline-block;border-radius: 100%;box-sizing: border-box;background-color: rgba(0, 0, 0, 0);"> <section style="border-radius: 100%;border-width: 2px;border-style: solid;border-color: rgb(0, 0, 0);width: 2em;height: 2em;font-size: 13px;line-height: 1.8em;box-sizing: border-box;"> <p style="box-sizing: border-box;"><strong style="box-sizing: border-box;">关</strong></p> </section> </section> </section> </section> </section> <section style="display: inline-block;vertical-align: top;width: 16%;box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: 10px;margin-right: 0%;margin-left: 0%;font-size: 10px;box-sizing: border-box;"> <section style="padding: 4px;display: inline-block;border-radius: 100%;box-sizing: border-box;background-color: rgba(0, 0, 0, 0);"> <section style="border-radius: 100%;border-width: 2px;border-style: solid;border-color: rgb(0, 0, 0);width: 2em;height: 2em;font-size: 13px;line-height: 1.8em;box-sizing: border-box;"> <p style="box-sizing: border-box;"><strong style="box-sizing: border-box;">注</strong></p> </section> </section> </section> </section> </section> </section> </section> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: 10px;margin-right: 0%;margin-left: 0%;box-sizing: border-box;"> <section style="font-size: 14px;color: rgb(68, 68, 68);padding-right: 15px;padding-left: 15px;box-sizing: border-box;"> <p style="letter-spacing: 0px;"><strong><span style="font-size: 18px;">👉</span></strong><span style="font-size: 17px;"><strong>【</strong></span><a href="https://mp.weixin.qq.com/s?__biz=MzUzMDc0NzU4Nw==&amp;mid=2247483768&amp;idx=1&amp;sn=4ef4f1510616baa395c507e32bb439d7&amp;scene=21#wechat_redirect" target="_blank" style="text-decoration: underline;color: rgb(255, 79, 121);font-size: 17px;" data-linktype="2"><span style="color: rgb(255, 79, 121);font-size: 17px;"><strong>16个技术公众号</strong></span></a><span style="font-size: 17px;"><strong>】都在这里!</strong></span></p> </section> </section> </section> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin: 8px 0% 15px;box-sizing: border-box;"> <section style="height: 1px;box-sizing: border-box;background-color: rgb(0, 0, 0);"></section> </section> </section> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="box-sizing: border-box;"> <section style="text-align: left;box-sizing: border-box;"> <p style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(136, 136, 136);font-size: 15px;">涵盖:程序员大咖、源码共读、程序员共读、数据结构与算法、黑客技术和网络安全、大数据科技、编程前端、Java、Python、Web编程开发、Android、iOS开发、Linux、数据库研发、幽默程序员等。</span></p> </section> </section> </section> </section> </section> </section> </section> <section data-role="outer" label="Powered by 135editor.com" style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" data-tools="135编辑器" data-id="94250" style="max-width: 100%;box-sizing: border-box;border-width: 0px;border-style: none;border-color: initial;word-wrap: break-word !important;"> <section class="" data-tools="135编辑器" data-id="91842" style="max-width: 100%;box-sizing: border-box;border-width: 0px;border-style: none;border-color: initial;word-wrap: break-word !important;"> <section style="max-width: 100%;text-align: right;width: auto;box-sizing: border-box !important;word-wrap: break-word !important;"> <section style="max-width: 100%;display: inline-block;clear: both;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" data-brushtype="text" style="padding: 18px 15px 20px 10px;max-width: 100%;box-sizing: border-box;color: rgb(86, 146, 214);background-image: url(&quot;https://mmbiz.qpic.cn/mmbiz_png/ol72Wnba7fLkfGhCjKwHfZOmHMkVTIomF2Oicxr71sYib6EI8Hyhwtntt683HCer9AfzlYzWQ12A8LiaTuUutwvtg/640?wx_fmt=png&quot;);background-repeat: no-repeat;text-align: center;background-size: 100% 100%;letter-spacing: 1.5px;word-wrap: break-word !important;"> <section style="max-width: 100%;display: flex;justify-content: center;align-items: center;box-sizing: border-box !important;word-wrap: break-word !important;"> <section style="margin-left: 2px;max-width: 100%;width: 20px;box-sizing: border-box !important;word-wrap: break-word !important;"> <img class="" data-ratio="0.8936170212765957" src="/upload/737696b8734b6f688ae95660c4411917.png" data-type="png" data-w="47" style="margin-bottom: -6px;box-sizing: border-box !important;word-wrap: break-word !important;width: 20px !important;visibility: visible !important;"> </section> <section class="" data-brushtype="text" style="max-width: 100%;font-size: 14px;color: rgb(51, 51, 51);box-sizing: border-box !important;word-wrap: break-word !important;"> 万水千山总是情,点个 “ <strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 112, 192);box-sizing: border-box !important;word-wrap: break-word !important;">好看</span></strong>” 行不行 </section> </section> </section> </section> </section> </section> </section> </section>

eclipse 指定启动使用的jdk

作者:不要哭啦

到eclipse目录,打开eclipse.ini文件,在指定位置加入你的jdk相关文件 加入 ``` -vm D:\Develop\Tool\Java\jdk1.8.0_101\jre\bin\server/jvm.dll ``` 完整配置文件 ``` -startup plugins/org.eclipse.equinox.launcher_1.3.200.v20160318-1642.jar --launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.400.v20160518-1444 -product org.eclipse.epp.package.jee.product --launcher.defaultAction openFile -showsplash org.eclipse.platform --launcher.defaultAction openFile --launcher.appendVmargs -vm D:\Develop\Tool\Java\jdk1.8.0_101\jre\bin\server/jvm.dll -vmargs -Dosgi.requiredJavaVersion=1.8 -XX:+UseG1GC -XX:+UseStringDeduplication -Dosgi.requiredJavaVersion=1.8 -Xms256m -Xmx1024m ```

mycat 教程讲解(二)

作者:演戏

## 应用场景 Mycat 发展到现在,适用的场景已经很丰富,而且不断有新用户给出新的创新性的方案,以下是几个典 型的应用场景: 1.单纯的读写分离,此时配置最为简单,支持读写分离,主从切换 2.分表分库,对于超过 1000 万的表进行分片,最大支持 1000 亿的单表分片 3.多租户应用,每个应用一个库,但应用程序只连接 Mycat,从而不改造程序本身,实现多租户化 4.报表系统,借助于 Mycat 的分表能力,处理大规模报表的统计 5.替代 Hbase,分析大数据 6.作为海量数据实时查询的一种简单有效方案,比如 100 亿条频繁查询的记录需要在 3 秒内查询出来 结果,除了基于主键的查询,还可能存在范围查询或其他属性查询,此时 Mycat 可能是最简单有效的选择。

线上服务 CPU 又 100% 啦?一键定位 so easy!

作者:微信小助手

<p style="text-align: center;"><img class="rich_pages" data-ratio="0.6671875" data-s="300,640" src="/upload/996bac76eed809de76e0c779da413910.jpg" data-type="jpeg" data-w="1280" style=""></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;background-color: rgb(255, 255, 255);font-size: 15px;widows: 1;text-align: right;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(136, 136, 136);font-size: 12px;letter-spacing: 0.544px;text-align: right;">来源:my.oschina.net/leejun2005/blog/1524687</span><br></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;background-color: rgb(255, 255, 255);font-size: 15px;widows: 1;text-align: right;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(136, 136, 136);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: 10px;letter-spacing: 0.544px;text-align: right;background-color: rgb(255, 255, 255);"></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;background-color: rgb(255, 255, 255);font-size: 15px;widows: 1;text-align: right;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <ul class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">背景</p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">1、java 正则表达式回溯造成 CPU 100%</p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">2、线程死锁,程序 hang 住</p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">3、免费实用的脚本工具大礼包</p></li> <ul class=" list-paddingleft-2" style="list-style-type: square;"> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">(1)show-duplicate-java-classes</p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">(2)find-in-jars</p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">(3)housemd pid [java_home]</p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">(4)jvm pid</p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">(5)greys[@IP:PORT]</p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">(6)sjksjk --commands sjk --help</p></li> </ul> </ul> <hr style="margin-top: 16px;margin-bottom: 16px;max-width: 100%;box-sizing: content-box;letter-spacing: 0.544px;white-space: normal;border-width: 0px;border-style: none;border-color: initial;height: 2px;background-color: rgb(231, 231, 231);color: rgb(52, 73, 94);font-family: &quot;Source Sans Pro&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;overflow-wrap: break-word !important;"> <h1 style="margin-top: 35px;margin-bottom: 15px;padding-bottom: 0.5em;font-weight: bold;font-size: 1.2rem;max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.225;cursor: text;border-bottom: 1px solid rgb(221, 221, 221);color: rgb(52, 73, 94);font-family: &quot;Source Sans Pro&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;text-align: start;box-sizing: border-box !important;overflow-wrap: break-word !important;">背景</h1> <p style="margin-top: 0.8em;margin-bottom: 0.8em;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(52, 73, 94);font-family: &quot;Source Sans Pro&quot;, &quot;Helvetica Neue&quot;, Arial, sans-serif;font-size: 16px;text-align: start;box-sizing: border-box !important;overflow-wrap: break-word !important;">经常做后端服务开发的同学,或多或少都遇到过 CPU 负载特别高的问题。尤其是在周末或大半夜,突然群里有人反�

为什么redis 是单线程的?

作者:微信小助手

<p>以前一直有个误区,以为:高性能服务器 一定是 多线程来实现的</p> <p><br></p> <p>原因很简单因为误区二导致的:多线程 一定比 单线程 效率高。其实不然。</p> <p><br></p> <p>在说这个事前希望大家都能对 CPU 、 内存 、 硬盘的速度都有了解了,这样可能理解得更深刻一点,不了解的朋友点:CPU到底比内存跟硬盘快多少</p> <p><br></p> <p>redis 核心就是 如果我的数据全都在内存里,我单线程的去操作 就是效率最高的,为什么呢,因为多线程的本质就是 CPU 模拟出来多个线程的情况,这种模拟出来的情况就有一个代价,就是上下文的切换,对于一个内存的系统来说,它没有上下文的切换就是效率最高的。redis 用 单个CPU 绑定一块内存的数据,然后针对这块内存的数据进行多次读写的时候,都是在一个CPU上完成的,所以它是单线程处理这个事。在内存的情况下,这个方案就是最佳方案 &nbsp;—— 阿里 沈询</p> <p><br></p> <p>因为一次CPU上下文的切换大概在 1500ns 左右。</p> <p><br></p> <p>从内存中读取 1MB 的连续数据,耗时大约为 250us,假设1MB的数据由多个线程读取了1000次,那么就有1000次时间上下文的切换,</p> <p><br></p> <p>那么就有1500ns * 1000 = 1500us ,我单线程的读完1MB数据才250us ,你光时间上下文的切换就用了1500us了,我还不算你每次读一点数据 的时间,</p> <p><br></p> <p>那什么时候用多线程的方案呢?</p> <p><br></p> <p>答案是:下层的存储等慢速的情况。比如磁盘</p> <p><br></p> <p>内存是一个 IOPS 非常高的系统,因为我想申请一块内存就申请一块内存,销毁一块内存我就销毁一块内存,内存的申请和销毁是很容易的。而且内存是可以动态的申请大小的。</p> <p><br></p> <p>磁盘的特性是:IPOS很低很低,但吞吐量很高。这就意味着,大量的读写操作都必须攒到一起,再提交到磁盘的时候,性能最高。为什么呢?</p> <p><br></p> <p>如果我有一个事务组的操作(就是几个已经分开了的事务请求,比如写读写读写,这么五个操作在一起),在内存中,因为IOPS非常高,我可以一个一个的完成,但是如果在磁盘中也有这种请求方式的话,</p> <p><br></p> <p>我第一个写操作是这样完成的:我先在硬盘中寻址,大概花费10ms,然后我读一个数据可能花费1ms然后我再运算(忽略不计),再写回硬盘又是10ms ,总共21ms</p> <p><br></p> <p>第二个操作去读花了10ms, 第三个又是写花费了21ms ,然后我再读10ms, 写21ms ,五个请求总共花费83ms,这还是最理想的情况下,这如果在内存中,大概1ms不到。</p> <p><br></p> <p>所以对于磁盘来说,它吞吐量这么大,那最好的方案肯定是我将N个请求一起放在一个buff里,然后一起去提交。</p> <p><br></p> <p>方法就是用异步:将请求和处理的线程不绑定,请求的线程将请求放在一个buff里,然后等buff快满了,处理的线程再去处理这个buff。然后由这个buff 统一的去写入磁盘,或者读磁盘,这样效率就是最高。java里的 IO不就是这么干的么~</p> <p><br></p> <p>对于慢速设备,这种处理方式就是最佳的,慢速设备有磁盘,网络 ,SSD 等等,</p> <p><br></p> <p>多线程 ,异步的方式处理这些问题非常常见,大名鼎鼎的netty 就是这么干的。</p> <p><br></p> <p>终于把 redis 为什么是单线程说清楚了,把什么时候用单线程跟多线程也说清楚了,其实也是些很简单的东西,只是基础不好的时候,就真的尴尬。。。。</p> <p><br></p> <p>补一发大师语录:来说说,为何单核cpu绑定一块内存效率最高</p> <p><br></p> <p>“我们不能任由操作系统负载均衡,因为我们自己更了解自己的程序,所以我们可以手动地为其分配CPU核,而不会过多地占用CPU”,默认情况下单线程在进行系统调用的时候会随机使用CPU内核,为了优化Redis,我们可以使用工具为单线程绑定固定的CPU内核,减少不必要的性能损耗!</p> <p><br></p> <p>redis作为单进程模型的程序,为了充分利用多核CPU,常常在一台server上会启动多个实例。而为了减少切换的开销,有必要为每个实例指定其所运行的CPU。</p> <p>Linux 上 &nbsp;taskset 可以将某个进程绑定到一个特定的CPU。你比操作系统更了解自己的程序,为了避免调度器愚蠢的调度你的程序,或是为了在多线程程序中避免缓存失效造成的开销。</p> <p><br></p> <p>顺便再提一句:redis 的瓶颈在网络上 。。。。</p> <section class="_135editor" data-tools="135编辑器" data-id="85996" style="border-width: 0px;border-style: none;border-color: initial;box-sizing: border-box;"> <section style="text-align:center;"> <section style="background-color: rgb(244, 244, 244);box-sizing: border-box;padding: 40px;"> <section style="margin-bottom: 10px;"> <span style="color: rgb(3, 3, 3);font-family: arial, helvetica, sans-serif;"><span style="font-size: 18px;"><strong>Java高级架构</strong></span></span> <span style="font-size: 40px;color: #3f3f3f;">∣</span> <span style="color:#030303;" class="135brush" data-brushtype="text">干货|交流</span> </section> <section> <section style="display:inline-block;vertical-align: top;margin-top: 14px;width: 50%;text-align: right;" data-width="50%"> <img border="0" class="" data-ratio="0.993993993993994" src="/upload/b65b49bf5b4b93db5800dd70c5faae54.png" data-type="png" data-w="666" height="auto" opacity="" style="width: 120px;" title="" width="120"> </section> <section style="display:inline-block;box-sizing:border-box;padding-left: 10px;width: 50%;text-align: left;" data-width="50%"> <img class="" data-ratio="1.39" src="/upload/9d2924f9c5be1305d2d10457a2988b10.png" data-type="png" data-w="100" style="width: 100px;"> </section> </section> <section style="margin-top: 6px;"> <span style="color: #0c0c0c;" class="135brush" data-brushtype="text">长按,识别二维码,加关注</span> </section> </section> </section> </section> <section class="_135editor" data-tools="135编辑器" data-id="108" style="border-width: 0px;border-style: none;border-color: initial;box-sizing: border-box;"> <section style="border-width: initial;border-style: none;border-color: initial;box-sizing: border-box;"> <section style="text-align: center;text-decoration: inherit;color:#fff;box-sizing: border-box;"> <section style="padding:0.4em 0.2em;box-sizing: border-box;border-color:#30cab0;border-radius:50px;background-color:#30cab0;"> <section style="width:70px;margin-top:-5px;float: left;"> <img class="" data-ratio="0.5" src="/upload/754e346d88977c4ada0185706960b005.png" data-type="png" data-w="160" data-width="100%" style="width:100%;margin-left: -30px;"> </section> <section class="135brush" data-brushtype="text" style="font-size: 14px;text-align:left;letter-spacing: 1px;line-height: 1.75em;"> 转载是一种动力 分享是一种美德 </section> </section> </section> </section> </section> <p><br></p>

Solr查询忽略field值的大小写

作者:じ☆ve宝贝

## 1、types标签下加入如下fieldType ``` <fieldType name="str_lower" class="solr.TextField" sortMissingLast="true" omitNorms="true"> <analyzer> <tokenizer class="solr.KeywordTokenizerFactory"/> <filter class="solr.LowerCaseFilterFactory" /> </analyzer> </fieldType> ``` ## 2、fields标签下自定义field的type改为str_lower即可解决 ``` <field name="en_name" type="str_lower" indexed="true" stored="true"/> ``` ** java调用传参不区分大小写,皆可查询到结果 **

【分布式事务】基于RocketMQ搭建生产级消息集群?

作者:微信小助手

<p style="text-align: center;"><img class="" data-backh="293" data-backw="556" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_jpg/l89kosVutonDlJQHqx8ib2qgeicbx1Nwq3nR3YibRia1eLYibfYoIY5uJqDHUSKHSL7XSW2gPLZQ8lpme9TI8aExFtQ/0?wx_fmt=jpeg" data-croporisrc="/upload/5915af3c8f57b51acd908e1658cf3a4c.jpg" data-cropx1="0" data-cropx2="1080" data-cropy1="0" data-cropy2="569.1366906474819" data-ratio="0.5268518518518519" data-s="300,640" src="https://mmbiz.qpic.cn/mmbiz_jpg/l89kosVutonDlJQHqx8ib2qgeicbx1Nwq3nR3YibRia1eLYibfYoIY5uJqDHUSKHSL7XSW2gPLZQ8lpme9TI8aExFtQ/640?wx_fmt=jpeg" data-type="jpeg" data-w="1080" style="width: 100%;height: auto;"></p> <p><span style="font-size: 17px;"><strong><br></strong></span></p> <p><span style="font-size: 17px;color: rgb(0, 122, 170);"><strong>导读</strong></span></p> <p style="line-height: 1.75em;"><span style="font-size: 17px;"><strong><br></strong></span></p> <p style="line-height: 1.75em;"><span style="font-size: 16px;">目前很多互联网公司的系统都在朝着微服务化、分布式化系统的方向在演进,这带来了很多好处,也带来了一些棘手的问题,其中最棘手的莫过于数据一致性问题了。早期我们的软件功能都在一个进程中,数据的一致性可以通过数据库本地事务来加以控制。而在分布式架构下,原本比较完整的本地功能可能被拆分成了多个独立的服务进程。与之前相比,同样一笔业务订单此时可能会经历很多服务模块的处理,<strong>调用链路会变得很长</strong>,例如某电商平台,一笔购物订单可能会经过:<strong>商品中心、订单、支付、物流</strong>等多个服务的调用,而这可能还只是比较粗粒度的划分,某些比较大型的服务,如支付系统,可能本身又会按照分布式的架构拆分成多个微服务,所以整个业务的调用链路会变得更加冗长。<br><br>而这不可避免的就会产生数据不一致的问题,为了<strong>实现业务上的最终一致性</strong>,功能比较独立的系统,如订单系统与支付系统就会<strong>通过额外的业务逻辑设计来确保彼此之间的最终一致性</strong>,如订单系统会通过订单的支付状态来保持与支付系统的数据一致,而支付系统则会提供支付状态查询接口,或者实现最大可能的主动回调功能,来确保二者数据状态的最终一致。此外可能还会通过日终的订单对账来发现不一致的数据,并进行数据校正。<br><br>但是这些都只是业务逻辑上的手段,对于某些内部服务之间的调用,如果可以通过分布式事务解决方案来加以保证的话,其实是可以大大减少一些不必要的复杂业务逻辑的。实际上,目前市面上能够提供分布式事务解决方案、又比较成熟的开源技术框架比较少,而<strong>RocketMQ在4.3.0之后的版本提供了事务消息的功能</strong>,因为RocketMQ本身拥有比较多的生产实践的关系,所以这一功能备受关注,作者所在的公司也有一些实践。<br><br>以此为契机,为了给大家关于分布式事务一个比较清晰的认识,这里我打算以RocketMQ的事务消息功能为示例,来相对全面的总结下分布式事务的内容。本篇文章的主要内容,是先介绍如何搭建一套生产级的RocketMQ消息集群,以此准备下试验环境。在下一篇</span><strong><span style="font-size: 16px;text-decoration: underline;">《【分布式事务】基于RocketMQ的分布式事务实现》</span></strong><span style="font-size: 16px;">会整体介绍下分布式事务的概念和原理,并做一些代码级的试验。</span><br></p> <p><br></p> <p><span style="color: rgb(0, 122, 170);"><strong>什么是RocketMQ</strong></span></p> <p><strong><br></strong></p> <p><span style="font-size: 16px;">RocketMQ是阿里开源的并贡献给Apache基金会的一款分布式消息平台,它具有低延迟、高性能和可靠性、万亿级容量和灵活的可伸缩性的特点,单机也可以支持<strong>亿级的消息堆积能力</strong>、单机写入<strong>TPS单实例约7万条/秒</strong>,单机部署3个Broker,可以跑到最高12万条/秒。</span></p> <p><br></p> <p><span style="font-size: 16px;">基于以上强大的性能,以及阿里的技术影响力,RocketMQ目前在国内互联网公司中被使用得比较广泛。那么,我们先大概来了解一下RocketMQ的整体结构吧!</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;">整个RocketMQ消息系统主要由如下<strong>4个部分组成</strong>:</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;">从中间件服务角度来看整个RocketMQ消息系统(服务端)主要分为:<strong>NameSrv和Broker</strong>两个部分。</span></p> <p><br></p> <p><strong><span style="font-size: 16px;">NameSrv</span></strong><span style="font-size: 16px;">:在RocketMQ分布式消息系统中,NameSrv主要提供两个功能:</span></p> <p><span style="font-size: 16px;"><br></span></p> <section class="" style="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;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;letter-spacing: 0px;color: rgb(62, 62, 62);line-height: 1.6;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <blockquote style="max-width: 100%;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;box-sizing: border-box !important;word-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;">提供服务发现和注册,这里主要是管理Broker,NameSrv接受来自Broker的注册,并通过心跳机制来检测Broker服务的健康性;<br></span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);font-size: 16px;box-sizing: border-box !important;word-wrap: break-word !important;"><br></span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;">提供路由功能,集群(这里是指以集群方式部署的NameSrv)中的每个NameSrv都保存了Broker集群(这里是指以集群方式部署的Broker)中整个的路由信息和队列信息。这里需要注意,在NameSrv集群中,每个NameSrv都是相互独立的,所以每个Broker需要连接所有的NameSrv,每创建一个新的topic都要同步到所有的NameSrv上。</span></p> </blockquote> </section> </section> </section> </section> </section> </section> <p><br></p> <p><strong><span style="font-size: 16px;">Broker:</span></strong><span style="font-size: 16px;">主要是负责消息的存储、传递、查询以及高可用(HA)保证等。其由如下几个子模块(源码总体上也是这么拆分的)构成:</span></p> <p><span style="font-size: 16px;"><br></span></p> <section class="" style="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;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;letter-spacing: 0px;color: rgb(62, 62, 62);line-height: 1.6;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <section class="" style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;box-sizing: border-box !important;word-wrap: break-word !important;"> <blockquote style="max-width: 100%;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;box-sizing: border-box !important;word-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;">remoting,是Broker的服务入口,负责客户端的接入(Producer和Consumer)和请求处理。</span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);font-size: 16px;box-sizing: border-box !important;word-wrap: break-word !important;"><br></span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;">client,管理客户端和维护消费者对于Topic的订阅。<br></span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;"><br></span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;">store,提供针对存储和消息查询的简单的API(数据存储在物理磁盘)。</span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;"><br></span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;">HA, 提供数据在主从节点间同步的功能特性。</span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;"><br></span></p> <p style="max-width: 100%;min-height: 1em;line-height: 1.5em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 16px;">Index,通过特定的key构建消息索引,并提供快速的索引查询服务。</span></p> </blockquote> </section> </section> </section> </section> </section> </section> <p><br><span style="font-size: 16px;"></span></p> <p><span style="font-size: 16px;">而从客户端的角度看主要有:<strong>Producer、Consumer</strong>两个部分。</span></p> <p><br></p> <p><span style="font-size: 16px;"><strong>Producer</strong>:消息的生<span style="font-size: 16px;font-family: SimSun;">产</span>者,由用<span style="font-size: 16px;font-family: SimSun;">户进</span>行分布式部署,消息由Producer通<span style="font-size: 16px;font-family: SimSun;">过</span>多种<span style="font-size: 16px;font-family: SimSun;">负载</span>均衡模式<span style="font-size: 16px;font-family: SimSun;">发</span>送到Broker集群,<span style="font-size: 16px;font-family: SimSun;">发</span>送低延<span style="font-size: 16px;font-family: SimSun;">时</span>,支持快速失<span style="font-size: 16px;font-family: SimSun;">败</span>。</span></p> <p><br></p> <p><span style="font-size: 16px;"><strong>Consumer</strong>:消息的消<span style="font-size: 16px;font-family: SimSun;">费</span>者,也由用<span style="font-size: 16px;font-family: SimSun;">户</span>部署,支持PUSH和PULL两种消<span style="font-size: 16px;font-family: SimSun;">费</span>模式,支持集群消<span style="font-size: 16px;font-family: SimSun;">费</span>和广播消息,提供<span style="font-size: 16px;font-family: SimSun;">实时</span>的消息<span style="font-size: 16px;font-family: SimSun;">订阅</span>机制,<span style="font-size: 16px;font-family: SimSun;">满</span>足大多数消<span style="font-size: 16px;font-family: SimSun;">费场</span>景。</span></p> <p><br></p> <p><span style="font-size: 16px;">来总结下,整个RocketMQ消息集群就是由NameSrv/Broker、Producer/Consumer组成的。为了让大家更清晰的理解它们之间的关系,我们以一条完整的信息流转为例,来看看RocketMQ消息系统是如何运转的,如下图所示:</span></p> <p><span style="font-size: 16px;"><br></span></p> <p style="text-align: center;"><img class="" data-croporisrc="/upload/1089cefb06fcf7b7759df7bbcdef30ab.png" data-cropx1="30.51438848920863" data-cropx2="998" data-cropy1="17.949640287769782" data-cropy2="874.1474820143885" data-ratio="0.8853305785123967" data-s="300,640" src="https://mmbiz.qpic.cn/mmbiz_jpg/l89kosVutolicSibQZk2KBYJFDq9uCfiaPZsTuNMaYIss9by1eePkLLZucW3ibtibKrnOYgiaSRokmSu4Dh1UgSfNTsQ/640?wx_fmt=jpeg" data-type="jpeg" data-w="968" style="width: 100%;height: auto;" data-backw="539" data-backh="477" data-before-oversubscription-url="https://mmbiz.qpic.cn/mmbiz_jpg/l89kosVutolicSibQZk2KBYJFDq9uCfiaPZsTuNMaYIss9by1eePkLLZucW3ibtibKrnOYgiaSRokmSu4Dh1UgSfNTsQ/640?wx_fmt=jpeg"></p> <p style="text-align: left;"><span style="font-size: 16px;"><br></span></p> <p style="text-align: left;"><span style="font-size: 16px;">看到这里相信大家应该对RocketMQ有一个大致的了解了,那么下面我们就具体看看,如何搭建一套生产级的RocketMQ消息集群系统吧!</span></p> <p style="text-align: left;"><span style="font-size: 16px;"><br></span></p> <p style="text-align: left;"><span style="color: rgb(0, 122, 170);"><strong><span style="font-size: 17px;">RocketMQ集群模式</span></strong></span><br></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;">RocketMQ集群部署有多种模式,对于NameSrv来说可以同时部署多个节点,并且这些节点间也不需要有任何的信息同步,这里因为每个NameSrv节点都会存储全量路由信息,在NameSrv集群模式下,每个Broker都需要同时向集群中的每个NameSrv节点发送注册信息,所以这里对于NameSrv的集群部署来说并不需要做什么额外的设置。</span></p> <p><br></p> <p><span style="font-size: 16px;">而对于Broker集群来说就有多种模式了,这里我先给大家介绍下这几种模式,然后我们再来看看生产级的部署方式是什么:</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><strong><span style="font-size: 16px;">1)、单个Master模式</span></strong></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;">一个Broker作为主服务,不设置任何Slave,很显然这种方式风险比较大,存在单节点故障会导致整个基于消息的服务挂掉,所以生产环境不可能采用这种模式。</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><strong><span style="font-size: 16px;">2)、多Master模式</span></strong></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;">这种模式的Broker集群,全是Master,没有Slave节点。这种方式的优缺点如下:</span></p> <p><br></p> <p><span style="font-size: 16px;"><strong>优点</strong>:配置会比较简单一些,如果单个Master挂掉或重启维护的话对应用是没有什么影响的。如果磁盘配置为RAID10(服务器的磁盘阵列模式,遗忘的同学可以自己查下资料)的话,即使在机器宕机不可恢复的情况下,由于RAID10磁盘本身的可靠性,消息也不会丢失(异步刷盘丢失少量消息,同步刷盘一条不丢),这种Broker的集群模式性能相对来说是最高的。</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;"><strong>缺点</strong>:就是在单台机器宕机期间,这台机器上未被消费的消息在机器恢复之前是不可以进行消息订阅的,这对消息的实时性会有一些影响。</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><strong><span style="font-size: 16px;">3)、多Master多Slave模式(异步复制)</span></strong><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;">在这种模式下Broker集群存在多个Master节点,并且每个Master节点都会对应一个Slave节点,有多对Master-Slave,HA(高可用)之间采用异步复制的方式进行信息同步,在这种方式下主从之间会有短暂的毫秒级的消息延迟。</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;"><strong>优点</strong>:在这种模式下即便磁盘损坏了,消息丢失的情况也非常少,因为主从之间有信息备份;并且,在这种模式下消息的实时性也不会受影响,因为Master宕机后Slave可以自动切换为Master模式,这样Consumer仍然可以通过Slave进行消息消费,而这个过程对应用来说则是完全透明的,并不需要人工干预;另外,这种模式的性能与多Master模式几乎差不多。</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;"><strong>缺点</strong>:如果Master宕机,并且在磁盘损坏的情况下,会丢失少量的消息。</span></p> <p><br></p> <p><strong><span style="font-size: 16px;">4)、多Master多Slave模式(同步复制)</span></strong><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;">这种模式与3)差不多,只是HA采用的是同步双写的方式,即主备都写成功后,才会向应用返回成功。</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;"><strong>优点</strong>:在这种模式下数据与服务都不存在单点的情况,在Master宕机的情况下,消息也没有延迟,服务的可用性以及数据的可用性都非常高。</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;"><strong>缺点:</strong>性能相比于异步复制来说略低一些(大约10%);另外一个缺点就是相比于异步复制,目前Slave备机还暂时不能实现自动切换为Master,可能后续的版本会支持Master-Slave的自动切换功能。</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;color: rgb(0, 122, 170);"><strong><span style="font-size: 17px;">生产级RocketMQ集群</span></strong></span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;">综合考虑以上集群模式的优缺点,在实际生产环境中目前基于RocketMQ消息集群的部署方式基本都是采用<strong>多Master多Slave(异步复制)</strong>这种模式,作者目前所在公司的生产环境的Rocket消息系统也是采用这种模式进行部署的。</span></p> <p><span style="font-size: 16px;"><br></span></p> <p><span style="font-size: 16px;">以下为目前作者所在公司的实际部署结构:</span></p> <p><span style="font-size: 16px;"><br></span></p> <p style="text-align: center;"><span style="font-size: 16px;"></span></p> <p style="text-align: center;"><img class="" data-backh="265" data-backw="556" data-before-oversubscription-url="https://mmbiz.qpic.cn/mmbiz_png/l89kosVutonU4QwBmwAvs5jDqhNicDibCRFBsibJjdDiaCjujIecCERg4zO6b7Kmz5pIM6BgGDbCxibV3D1SW2A8mibA/0?wx_fmt=png" data-copyright="0" data-ratio="0.476221928665786" data-s="300,640" src="/upload/b2bce4ec87470de3bdbd69f25c3316c9.png" data-type="png" data-w="1514" style="width: 100%;height: auto;"></p> <p style="text-align: center;"><br></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">在以上实践中,部署了<strong>3个NameSrv节点</strong>,<strong>Broker采用2主2从</strong>的异步复制模式进行集群部署。</span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;"><br></span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">为了更好地理解RocketMQ的集群运行原理,接下来我们以4台虚拟机来模拟上述集群的搭建过程,假设这4台机器的IP分别为:</span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;"><br></span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;word-spacing: 0px;letter-spacing: 0px;font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%);background-size: 20px 20px;background-position: center center;"> <blockquote style="line-height: inherit;display: block;padding: 15px 15px 15px 1rem;font-size: 0.9em;margin: 1em 0px;color: rgb(129, 145, 152);border-left: 6px solid rgb(220, 230, 240);background: rgb(242, 247, 251) none repeat scroll 0% 0%;overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;padding: 0px;margin: 0px;">10.211.55.4<br>10.211.55.5<br>10.211.55.6</p> <p style="font-size: inherit;color: inherit;line-height: inherit;padding: 0px;margin: 0px;">10.211.55.7<br></p> </blockquote> </section> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;"><br></span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">首先确保几台虚拟机上安装了JDK1.8+:</span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;"><br></span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;word-spacing: 0px;letter-spacing: 0px;font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%);background-size: 20px 20px;background-position: center center;"> <pre style="font-size: inherit;color: inherit;line-height: inherit;margin: 0px;padding: 0px;"><code class="shell language-shell hljs" style="overflow-wrap: break-word;margin: 0px 2px;line-height: 18px;font-size: 14px;font-weight: normal;word-spacing: 0px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46) none repeat scroll 0% 0%;overflow-x: auto;padding: 0.5em;display: block !important;white-space: pre !important;word-wrap: normal !important;word-break: normal !important;overflow: auto !important;">wget&nbsp;--no-check-certificate&nbsp;--no-cookies&nbsp;--header&nbsp;"Cookie:&nbsp;oraclelicense=accept-securebackup-cookie"&nbsp;https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-linux-x64.tar.gz<br><br>JAVA_HOME=/opt/java/jdk1.8.0_191<br>CLASSPATH=$JAVA_HOME/lib/<br>PATH=$PATH:$JAVA_HOME/bin<br>export&nbsp;PATH&nbsp;JAVA_HOME&nbsp;CLASSPATH<br></code></pre> </section> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br><span style="font-size: 16px;"></span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">其次我们打算通过RocketMQ的源码进行编译,因为源码是基于Maven开发的Java工程,所以我们需要安装下Maven环境:</span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;"><br></span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;word-spacing: 0px;letter-spacing: 0px;font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%);background-size: 20px 20px;background-position: center center;"> <pre style="font-size: inherit;color: inherit;line-height: inherit;margin: 0px;padding: 0px;"><code class="shell language-shell hljs" style="overflow-wrap: break-word;margin: 0px 2px;line-height: 18px;font-size: 14px;font-weight: normal;word-spacing: 0px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46) none repeat scroll 0% 0%;overflow-x: auto;padding: 0.5em;display: block !important;white-space: pre !important;word-wrap: normal !important;word-break: normal !important;overflow: auto !important;">wget&nbsp;https://archive.apache.org/dist/maven/binaries/apache-maven-3.2.1-bin.tar.gz<br><br>export&nbsp;MAVEN_HOME=/opt/apache-maven-3.2.1<br>export&nbsp;PATH=${PATH}:${MAVEN_HOME}/bin<br><span class="hljs-meta" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(91, 218, 237);word-wrap: inherit !important;word-break: inherit !important;"><br>#</span><span class="bash" style="font-size: inherit;color: inherit;line-height: inherit;margin: 0px;padding: 0px;word-wrap: inherit !important;word-break: inherit !important;">配置阿里云镜像</span><br>&lt;mirror&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;id&gt;nexus-aliyun&lt;/id&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;mirrorOf&gt;central&lt;/mirrorOf&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;name&gt;Nexus&nbsp;aliyun&lt;/name&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;url&gt;http://maven.aliyun.com/nexus/content/groups/public&lt;/url&gt;<br>&lt;/mirror&gt;<br></code></pre> </section> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;"><br></span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">如果多台机器,没有必要依次下载,可以通过远程复制命令完成机器间的拷贝:</span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;"><br></span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;word-spacing: 0px;letter-spacing: 0px;font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%);background-size: 20px 20px;background-position: center center;"> <pre style="font-size: inherit;color: inherit;line-height: inherit;margin: 0px;padding: 0px;"><code class="shell language-shell hljs" style="overflow-wrap: break-word;margin: 0px 2px;line-height: 18px;font-size: 14px;font-weight: normal;word-spacing: 0px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46) none repeat scroll 0% 0%;overflow-x: auto;padding: 0.5em;display: block !important;white-space: pre !important;word-wrap: normal !important;word-break: normal !important;overflow: auto !important;">scp&nbsp;-r&nbsp;/opt/apache-maven-3.2.1/&nbsp;root@10.211.55.6:/opt/<br></code></pre> </section> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">完成后,我们就可以在主机的指定目录下载RocketMQ的源码发布包(这里为4.3.2版本)进行编译了:</span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;"><br></span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;word-spacing: 0px;letter-spacing: 0px;font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%);background-size: 20px 20px;background-position: center center;"> <pre style="font-size: inherit;color: inherit;line-height: inherit;margin: 0px;padding: 0px;"><code class="shell language-shell hljs" style="overflow-wrap: break-word;margin: 0px 2px;line-height: 18px;font-size: 14px;font-weight: normal;word-spacing: 0px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46) none repeat scroll 0% 0%;overflow-x: auto;padding: 0.5em;display: block !important;white-space: pre !important;word-wrap: normal !important;word-break: normal !important;overflow: auto !important;"><span class="hljs-meta" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(91, 218, 237);word-wrap: inherit !important;word-break: inherit !important;">#</span><span class="bash" style="font-size: inherit;color: inherit;line-height: inherit;margin: 0px;padding: 0px;word-wrap: inherit !important;word-break: inherit !important;">download-4.3.2源码准备编译</span><br><br>wget&nbsp;http://mirror.bit.edu.cn/apache/rocketmq/4.3.2/rocketmq-all-4.3.2-source-release.zip&nbsp;<br><br>mvn&nbsp;-Prelease-all&nbsp;-DskipTests&nbsp;clean&nbsp;install&nbsp;-U<br><br>cd&nbsp;/opt/rocketmq-all-4.3.2/distribution/target/apache-rocketmq<br></code></pre> </section> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">以上动作需要在各个机器节点也同步操作!编译完成后,我们来规划下如何利用这4台虚拟机来实现“<strong>3个NameSrv节点、2组Master-Slave Broker集群</strong>”的效果。</span><br></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="text-align: center;"><img class="" data-backh="130" data-backw="556" data-before-oversubscription-url="http://mmbiz.qpic.cn/mmbiz_png/l89kosVutommu1rlC8NOp20mQG7xq7jRqOPOUxZPDt9Y65Xv4a9f81iaPJYSBlWeTlpoMn7IgKVyHIQ8NV58g1Q/0?wx_fmt=png" data-ratio="0.23444283646888567" data-s="300,640" src="/upload/a5c60b981379b450b3d9131c2401720c.png" data-type="png" data-w="1382" style="width: 100%;height: auto;"></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;"><br></span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">因为本地机器资源原因,我们通过虚拟机混部的方式来实现上述集群的效果,4台机器的3台会分别作为NameSrv节点,而对于Broker集群则两两组合,如上表所示!</span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">按照上述规划,接下来我们就来看下具体的配置方式:</span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong><span style="font-size: 16px;">1)Master-Broker-a的配置</span></strong></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p><span style="font-size: 16px;">#创建数据存储目录</span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;word-spacing: 0px;letter-spacing: 0px;font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%);background-size: 20px 20px;background-position: center center;"> <pre style="font-size: inherit;color: inherit;line-height: inherit;margin: 0px;padding: 0px;"><code class="hljs perl" style="overflow-wrap: break-word;margin: 0px 2px;line-height: 18px;font-size: 14px;font-weight: normal;word-spacing: 0px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46) none repeat scroll 0% 0%;overflow-x: auto;padding: 0.5em;display: block !important;white-space: pre !important;word-wrap: normal !important;word-break: normal !important;overflow: auto !important;">[root@bogon&nbsp;<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">local</span>]<span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#&nbsp;mkdir&nbsp;-p&nbsp;/usr/local/rocketmq/data/store</span><br>[root@bogon&nbsp;<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">local</span>]<span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#&nbsp;mkdir&nbsp;-p&nbsp;/usr/local/rocketmq/data/store/commitlog</span><br>[root@bogon&nbsp;<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">local</span>]<span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#&nbsp;mkdir&nbsp;-p&nbsp;/usr/local/rocketmq/data/store/consumequeue</span><br>[root@bogon&nbsp;<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">local</span>]<span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#&nbsp;mkdir&nbsp;-p&nbsp;/usr/local/rocketmq/data/store/index</span><br>[root@bogon&nbsp;<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">local</span>]<span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#&nbsp;mkdir&nbsp;-p&nbsp;/usr/local/rocketmq/data/store/checkpoint</span><br>[root@bogon&nbsp;<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">local</span>]<span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#&nbsp;mkdir&nbsp;-p&nbsp;/usr/local/rocketmq/data/store/abort</span><br></code></pre> </section> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">#切换服务器目录为对应的配置文件目录</span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;word-spacing: 0px;letter-spacing: 0px;font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%);background-size: 20px 20px;background-position: center center;"> <pre style="font-size: inherit;color: inherit;line-height: inherit;margin: 0px;padding: 0px;"><code class="hljs bash" style="overflow-wrap: break-word;margin: 0px 2px;line-height: 18px;font-size: 14px;font-weight: normal;word-spacing: 0px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46) none repeat scroll 0% 0%;overflow-x: auto;padding: 0.5em;display: block !important;white-space: pre !important;word-wrap: normal !important;word-break: normal !important;overflow: auto !important;"><span class="hljs-built_in" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">cd</span>&nbsp;/opt/rocketmq-all-4.3.2/distribution/target/apache-rocketmq/conf<br><br><span class="hljs-built_in" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">cd</span>&nbsp;2m-2s-async&nbsp;//这里因为我们采用的是异步复制模式,所以需要编辑2m-2s-async中的配置文件<br></code></pre> </section> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: center;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;letter-spacing: 0.544px;color: rgb(137, 135, 145);font-size: 16px;box-sizing: border-box !important;word-wrap: break-word !important;overflow-wrap: break-word !important;"><br></span></p> <p class="" style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 16px;">#编辑Broker集群配置文件</span></p> <p><span style="font-size: 16px;">[root@bogon 2m-2s-async]# vim broker-a.properties </span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;word-spacing: 0px;letter-spacing: 0px;font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%);background-size: 20px 20px;background-position: center center;"> <pre style="font-size: inherit;color: inherit;line-height: inherit;margin: 0px;padding: 0px;"><code class="hljs ini" style="overflow-wrap: break-word;margin: 0px 2px;line-height: 18px;font-size: 14px;font-weight: normal;word-spacing: 0px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46) none repeat scroll 0% 0%;overflow-x: auto;padding: 0.5em;display: block !important;white-space: pre !important;word-wrap: normal !important;word-break: normal !important;overflow: auto !important;"><span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#broker所属哪个集群,默认【DefaultCluster】</span><br><span class="hljs-attr" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(165, 218, 45);word-wrap: inherit !important;word-break: inherit !important;">brokerClusterName</span>=DefaultCluster<br><br><span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#broker&nbsp;实列名称,主从关系的需要保持名称一致</span><br><span class="hljs-attr" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(165, 218, 45);word-wrap: inherit !important;word-break: inherit !important;">brokerName</span>=broker-a<br><br><span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#brokerId,必须是大等于0的整数,0表示Master,&gt;0表示Slave</span><br><span class="hljs-attr" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(165, 218, 45);word-wrap: inherit !important;word-break: inherit !important;">brokerId</span>=<span class="hljs-number" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(174, 135, 250);word-wrap: inherit !important;word-break: inherit !important;">0</span><br><br><span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#删除文件的时间点,默认为凌晨4点</span><br><span class="hljs-attr" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(165, 218, 45);word-wrap: inherit !important;word-break: inherit !important;">deleteWhen</span>=<span class="hljs-number" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(174, 135, 250);word-wrap: inherit !important;word-break: inherit !important;">04</span><br><br><span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#文件保留时间,默认为48小时</span><br><span class="hljs-attr" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(165, 218, 45);word-wrap: inherit !important;word-break: inherit !important;">fileReservedTime</span>=<span class="hljs-number" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(174, 135, 250);word-wrap: inherit !important;word-break: inherit !important;">48</span><br><br><span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#-ASYNC_MASTER&nbsp;异步复制Master</span><br><span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#-SYNC_MASTER&nbsp;&nbsp;同步双写Master</span><br><span class="hljs-comment" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: rgb(128, 128, 128);word-wrap: inherit !important;word-break: inherit !important;">#-SLAVE</span><br><span class="hljs-attr" style="font-size: inherit;line-height: inherit;margin: 0px;padding: 0px;color: r

Tomcat同时配置https和http互不影响

作者:じ☆ve宝贝

###### 这个是tomcat conf中的server.xml关键就是配置了多个Service,如果还有疑问可以联系我 ``` <?xml version='1.0' encoding='utf-8'?> <Server port="8006" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JasperListener" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" useBodyEncodingForURI="true" URIEncoding="UTF-8" compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"/> <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="115.29.103.119"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="studyjava.cn" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="www.studyjava.cn" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="www.sunaomei.xin" appBase="/data/tomcat/webapps/sun" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/data/tomcat/webapps/sun" debug="0" reloadable="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="sunaomei.xin" appBase="/data/tomcat/webapps/sun" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/data/tomcat/webapps/sun" debug="0" reloadable="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="fentiao.studyjava.cn" appBase="/data/tomcat/webapps/fentiao" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/data/tomcat/webapps/fentiao" debug="0" reloadable="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="daohang.studyjava.cn" appBase="/data/tomcat/webapps/daohang" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/data/tomcat/webapps/daohang" debug="0" reloadable="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="115.29.103.119" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> <Service name="Catalina2"> <Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="/data/tomcat/keys/www.studyjava.cn.jks" keystorePass="Long82339213"/> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server> ```