作者:微信小助手
<p style="margin-bottom: 10px;color: rgb(62, 62, 62);font-size: 15px;white-space: normal;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);line-height: normal;text-align: center;" data-mpa-powered-by="yiban.io"><span style="font-size: 14px;letter-spacing: 0.544px;word-spacing: 2px;color: rgb(136, 136, 136);">点击蓝色“</span><span style="font-size: 14px;letter-spacing: 0.544px;word-spacing: 2px;color: rgb(0, 128, 255);">架构文摘</span><span style="font-size: 14px;letter-spacing: 0.544px;word-spacing: 2px;color: rgb(136, 136, 136);">”关注我哟</span></p> <p style="margin-bottom: 10px;color: rgb(62, 62, 62);font-size: 15px;white-space: normal;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-size-adjust: auto;word-spacing: 2px;text-align: center;"><span style="font-size: 14px;color: rgb(136, 136, 136);">加个“</span><span style="color: rgb(0, 128, 255);font-size: 14px;">星标</span><span style="font-size: 14px;color: rgb(136, 136, 136);">”,每天上午 09:25,干货推送!</span></p> <p style="color: rgb(62, 62, 62);white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;font-size: 14px;background-color: rgb(255, 255, 255);text-align: center;"><img data-backh="36" data-backw="578" data-ratio="0.0625" data-s="300,640" data-type="jpeg" data-w="640" width="100%" src="/upload/8c292e55ba5a23cb6ebc11f2a2c4fece.jpg" style="font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;widows: 1;word-spacing: 2px;color: rgb(136, 136, 136);box-sizing: border-box !important;visibility: visible !important;width: 677px !important;"></p> <section style="font-size: 15px;color: rgb(62, 62, 62);line-height: 1.6;letter-spacing: 0px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"> <p style="font-size: inherit;color: inherit;line-height: inherit;"><br></p> <p style="font-size: inherit;color: inherit;line-height: inherit;"><span style="font-size: 12px;color: rgb(136, 136, 136);">来源:https://juejin.im/post/6860252224930070536</span></p> <section style="font-size: 15px;color: rgb(62, 62, 62);line-height: 1.6;letter-spacing: 0px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom:
作者:微信小助手
<p style="text-align: center;"><span style="font-size: 14px;letter-spacing: 0.5440000295639038px;text-align: center;max-width: 100%;color: rgb(255, 41, 65);line-height: 22.4px;">(给</span><span style="font-size: 14px;letter-spacing: 0.5440000295639038px;text-align: center;max-width: 100%;line-height: 22.4px;color: rgb(0, 128, 255);">ImportNew</span><span style="font-size: 14px;letter-spacing: 0.5440000295639038px;text-align: center;max-width: 100%;color: rgb(255, 41, 65);line-height: 22.4px;">加星标,提高Java技能)</span></p> <blockquote> <p style="letter-spacing: 0.5440000295639038px;white-space: normal;background-color: rgb(255, 255, 255);max-width: 100%;min-height: 1em;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">转自:骆俊武</span></p> </blockquote> <p><br></p> <section data-mpa-powered-by="yiban.io" style="max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img data-ratio="0.5098039507865906" data-type="jpeg" data-w="408" data-backw="578" data-backh="299" src="/upload/323832b02025e6e5ba7407af5a0135f1.jpg" style="width: 677px;box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;"><span style="max-width: 100%;color: rgb(154, 154, 154);font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></p> <p style="margin-top: 20px;padding-right: 4px;padding-left: 4px;max-width: 100%;min-height: 1em;letter-spacing: 1px;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 16px;white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">之前,我写过几篇有关「线上问题排查」的文章,文中附带了一些监控图,有些读者对此很感兴趣,问我监控系统选型上有没有好的建议?</span></p> <p style="margin-top: 20px;padding-right: 4px;padding-left: 4px;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 16px;white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">目前我所经历的几家公司,监控系统都是自研的。其实业界有很多优秀的开源产品可供选择,能满足绝大部分的监控需求,如果能从中选择一款满足企业当下的诉求,显然最省时省力。</span></p> <p style="margin-top: 20px;margin-bottom: 10px;padding-right: 4px;padding-left: 4px;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 16px;white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">这篇文章,我将对监控体系的基础知识、原理和架构做一次系统性整理,同时还会对几款最常用的开源监控产品做下介绍,以便大家选型时参考。内容包括3部分</span><span style="max-width: 100%;font-size: 15px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">:</span><span style="max-width: 100%;font-size: 15px;letter-spacing: 0.51px;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></p> <ul class="list-paddingleft-2" style="padding-left: 1.5em;max-width: 100%;letter-spacing: 0.544px;width: 577.417px;list-style-type: square;overflow-wrap: break-word !important;"> <li style="max-width: 100%;text-align: left;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 5px;max-width: 100%;text-align: justify;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> 必知必会的监控基础知识 </section></li> <li style="max-width: 100%;text-align: left;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 5px;max-width: 100%;text-align: justify;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> 主流监控系统介绍 </section></li> <li style="max-width: 100%;text-align: left;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 5px;max-width: 100%;text-align: justify;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">监控系统的选型建议</span> </section></li> </ul> </section> <section style="margin-top: 5px;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> </section> <section style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(63, 63, 63);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: center;background-position: center 20px;background-size: 112px;background-repeat: no-repeat;background-attachment: initial;background-origin: initial;background-clip: initial;font-size: 19px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="margin-bottom: 10px;max-width: 100%;line-height: 48px;color: rgb(234, 84, 41);border-bottom: 2px solid rgb(234, 84, 41);display: inline-block;font-size: 17px;box-sizing: border-box !important;overflow-wrap: break-word !important;">01 必知必会的监控基础知识</span> </section> <p style="padding-top: 20px;padding-right: 4px;padding-left: 4px;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;background-color: rgb(255, 255, 255);white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">监控系统</span>俗称<span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">「</span>第三只眼<span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">」</span>,几乎是我们每天都会打交道的系统,下面 4 项基础知识我认为是必须要了解的。</span><span style="max-width: 100%;letter-spacing: 0.544px;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></p> <section style="margin-bottom: 10px;padding-top: 20px;padding-right: 4px;padding-left: 4px;max-width: 100%;letter-spacing: 0.544px;min-height: 1em;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;background-color: rgb(255, 255, 255);font-size: 16px;white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;font-weight: 700;letter-spacing: 0.51px;color: rgb(123, 12, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">1. 监控系统的7大作用</span> </section> <section style="margin-bottom: 5px;padding-right: 4px;padding-left: 4px;max-width: 100%;letter-spacing: 0.544px;min-height: 1em;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;background-color: rgb(255, 255, 255);font-size: 16px;white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">正所谓「无监控,不运维」,监控系统的地位不言而喻。不管你是监控系统的开发者还是使用者,首先肯定要清楚:监控系统的目标是什么?它能发挥什么作用?</span></span> </section> <p style="margin-top: 10px;margin-bottom: 15px;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-backh="358" data-backw="578" data-ratio="0.6197975253093363" data-s="300,640" data-type="png" data-w="889" src="/upload/ca0a577ec3648d14e3b0c4349b86c6ad.png" style="box-shadow: none;width: 677px;box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;"></p> <ul class="list-paddingleft-2" style="padding-left: 1.2em;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);width: 577.417px;list-style-type: square;overflow-wrap: break-word !important;"> <li style="max-width: 100%;text-align: left;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 5px;max-width: 100%;text-align: justify;white-space: pre-line;line-height: 28px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">实时采集监控数据</strong></span>:包括 <span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">硬件、操作系统、中间件、应用程序等各个维度的数据。</span> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> </section></li> <li style="max-width: 100%;text-align: left;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 15px;max-width: 100%;text-align: justify;white-space: pre-line;line-height: 28px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">实时反馈监控状态</strong></span>:通过对采集的数据进行多维度统计和可视化展示,能实时体现监控对象的状态是正常还是异常。 </section></li> <li style="max-width: 100%;text-align: left;font-size: 15px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 15px;max-width: 100%;text-align: justify;white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">预知故障和告警:</span></strong> <span style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">能够提前预知故障风险,并及时发出告警信息。</span> </section></li> <li style="max-width: 100%;text-align: left;font-size: 15px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 15px;max-width: 100%;text-align: justify;white-space: pre-line;line-height: 28px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">辅助定位故障:</span></strong><span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">提供故障发生时的各项指标数据,辅助故障分析和定位。</span></span> <span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span> </section></li> <li style="max-width: 100%;text-align: left;font-size: 15px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="margin-top: 15px;max-width: 100%;min-height: 1em;text-align: justify;line-height: 28px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;color: rgb(74, 74, 74);letter-spacing: 0.544px;white-space: pre-line;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">辅助性能调优:</span></strong><span style="max-width: 100%;color: rgb(74, 74, 74);letter-spacing: 0.544px;white-space: pre-line;box-sizing: border-box !important;overflow-wrap: break-word !important;">为性能调优提供数据支持,比如慢SQL,接口响应时间等。</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p></li> <li style="max-width: 100%;text-align: left;font-size: 15px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 15px;max-width: 100%;text-align: justify;line-height: 28px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <strong style="max-width: 100%;color: rgb(74, 74, 74);letter-spacing: 0.544px;white-space: pre-line;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">辅助容量规划:</span></strong> <span style="max-width: 100%;color: rgb(74, 74, 74);letter-spacing: 0.544px;white-space: pre-line;box-sizing: border-box !important;overflow-wrap: break-word !important;">为服务器、中间件以及应用集群的容量规划提供数据支撑。</span> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> </section></li> <li style="max-width: 100%;text-align: left;font-size: 15px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin-top: 15px;max-width: 100%;text-align: justify;white-space: pre-line;line-height: 28px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">辅助自动化运维:</span></strong> <span style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">为自动扩容或者根据配置的SLA进行服务降级等智能运维提供数据支撑。</span> <span style="max-width: 100%;letter-spacing: 0.544px;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span> </section></li> </ul> <section style="margin-top: 15px;margin-bottom: 10px;padding-top: 20px;padding-right: 4px;padding-left: 4px;max-width: 100%;letter-spacing: 0.544px;min-height: 1em;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;background-color: rgb(255, 255, 255);font-size: 16px;white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;font-weight: 700;letter-spacing: 0.51px;color: rgb(123, 12, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">2. 使用监控系统的正确姿势</span> </section> <blockquote data-tool="mdnice编辑器" style="margin-top: 20px;margin-bottom: 20px;padding-top: 10px;padding-right: 10px;padding-bottom: 10px;border-width: initial;border-style: none;border-color: initial;color: rgb(136, 136, 136);font-size: 0.9em;max-width: 100%;letter-spacing: 0.544px;white-space: normal;line-height: 1.8;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;overflow: auto;background: rgba(0, 0, 0, 0.047);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(85, 85, 85);font-size: 3em;font-family: Arial, serif;line-height: 1em;font-weight: 700;box-sizing: border-box !important;overflow-wrap: break-word !important;">“</span> <p style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-size: 16px;color: black;line-height: 26px;display: inline;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(74, 74, 74);font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">出任何线上事故,先不说其他地方有问题,监控部分一定是有问题的。</span></p> </blockquote> <p style="margin-bottom: 20px;padding-right: 4px;padding-left: 4px;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;background-color: rgb(255, 255, 255);white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">听着很甩锅的一句话,仔细思考好像有一定道理。我们在事故复盘时,通常会思考这3个和监控有关的问题:有没有做监控?监控是否及时?监控信息是否有助于快速定位问题?</span></p> <p style="margin-bottom: 5px;padding-right: 4px;padding-left: 4px;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;background-color: rgb(255, 255, 255);white-space: pre-line;line-height: 27px;color: rgb(74, 74, 74);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">可见光有一套好的监控系统还不够,还必须知道</span><span style="max-width: 100%;letter-spacing: 0.544px;font-size: 15px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">「</span></strong><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何</span></strong><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">用好它<span style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !
作者:じ☆ve不哭
最近在pycharm里面创建了一个Python项目,pycharm会自动搭建一个新的Python环境,在当前的目录下 使用正常的pip升级命令:python -m pip install --upgrade pip 出现下面错误  AttributeError: 'NoneType' object has no attribute 'bytes' ### 解决方案: ``` easy_install -U pip # 推荐指定国内源 easy_install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 
作者:微信小助手
<section style="font-size: 16px;color: rgb(62, 62, 62);line-height: 2em;word-spacing: 1px;letter-spacing: 1px;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;" data-mpa-powered-by="yiban.io"> <section data-mpa-template="t" mpa-paragraph-type="ignored" style="max-width: 100%;white-space: normal;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-wrap: break-word !important;box-sizing: border-box !important;"> <section data-mpa-template="t" mpa-paragraph-type="ignored" style=""> <section data-mpa-template="t" mpa-paragraph-type="ignored" style="max-width: 100%;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <section data-mpa-template="t" mpa-paragraph-type="ignored" style="max-width: 100%;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <section data-mpa-template="t" mpa-paragraph-type="ignored" style="max-width: 100%;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <p style="white-space: normal;"><span style="font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;">往期热门文章:<br></span></p> <section data-role="outer" label="Powered by 135editor.com" style="margin-right: 0em;margin-left: 0em;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);font-size: 16px;text-align: left;widows: 1;color: rgb(0, 0, 0);"> <section data-role="outer" label="Powered by 135editor.com"> <section data-role="paragraph"> <section data-role="outer" label="Powered by 135editor.com" style="margin-right: 0em;margin-left: 0em;letter-spacing: 0.544px;"> <section data-role="outer" label="Powered by 135editor.com"> <section data-role="paragraph"> <section data-mpa-template="t" mpa-paragraph-type="ignored" style="letter-spacing: 0.544px;color: rgb(62, 62, 62);font-size: 15px;"> <h4 style="letter-spacing: 0.544px;word-spacing: 1px;"> <section style="margin-top: 10px;margin-bottom: 10px;font-size: 15px;font-variant-numeric: normal;font-variant-east-asian: normal;letter-spacing: 0.544px;caret-color: rgb(255, 0, 0);line-height: 1.75em;text-indent: 0em;"> <span style="font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">1、</span> <a href="http://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&mid=2247489898&idx=1&sn=5c5e41a0f695649046de3db1b1810df5&chksm=e9c5e0dbdeb269cd52f798c675f58295ad0f632283c0fb07db4140949c72796deed7dfe7d434&scene=21#wechat_redirect" target="_blank" data-itemshowtype="0" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><span style="font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">《</span></a> <a href="http://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&mid=2247489402&idx=2&sn=af5c3bb38717e828d92ed48874f03fe8&chksm=e9c5eecbdeb267dd3d05c159bdb9c611f24c4ca7fb7dafa12daf459becb0ef4fab7bcc2d1a67&scene=21#wechat_redirect" target="_blank" data-linktype="2" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><span style="font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><span style="font-size: 14px;vertical-align: inherit;">往期</span><span style="cursor: pointer;letter-spacing: 0.544px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);font-size: 14px;vertical-align: inherit;">精选优秀博文都在这里了!</span><span style="cursor: pointer;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);letter-spacing: 0.544px;vertical-align: inherit;">》</span></span></a> </section> <section style="margin-top: 10px;margin-bottom: 10px;font-size: 15px;font-variant-numeric: normal;font-variant-east-asian: normal;letter-spacing: 0.544px;caret-color: rgb(255, 0, 0);line-height: 1.75em;text-indent: 0em;"> <span style="font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">2、<a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&mid=2247492950&idx=1&sn=0a19bf5bb4fef9325d7d92893b453c04&chksm=e9c61ce7deb195f158162ce018b0405b083d168d4ae5f35bde640ee264b3f5fe24886babee4c&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2">记一次订单号重复的事故,快看看你的 UUID 在并发下还正确吗?</a></span> </section> <section style="margin-top: 10px;margin-bottom: 10px;font-size: 15px;font-variant-numeric: normal;font-variant-east-asian: normal;letter-spacing: 0.544px;caret-color: rgb(255, 0, 0);line-height: 1.75em;text-indent: 0em;"> <span style="font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&mid=2247492898&idx=1&sn=967d4398cf563932cb479334386db30c&chksm=e9c61c93deb195858d2768ee2af52fc232b8bcf9a2ed0476af2d4ec4fea4c52a01b123332756&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">3、终于放弃了Maven,我选择用Gradle!</a></span> </section> <section style="margin-top: 10px;margin-bottom: 10px;font-size: 15px;font-variant-numeric: normal;font-variant-east-asian: normal;letter-spacing: 0.544px;caret-color: rgb(255, 0, 0);line-height: 1.75em;text-indent: 0em;"> <span style="font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&mid=2247492820&idx=1&sn=49fd246c24aba3666251dc1ae42f70fc&chksm=e9c61d65deb19473ab6b4edeaf1967b89f6fb1e298bee5054bb564d79139a59fa461ada3e1e3&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">4、</a><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&mid=2247492888&idx=1&sn=f8e1262cbbec3908a8ccbd80956b8928&chksm=e9c61ca9deb195bf43980a06d66b07965b411ecf42f4b54cebd93f0d467a74f2fbf397409404&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">Kotlin 越来越牛逼了!学Java的我想转了!</a></span> </section> <section style="margin-top: 10px;margin-bottom: 10px;font-size: 15px;font-variant-numeric: normal;font-variant-east-asian: normal;letter-spacing: 0.544px;caret-color: rgb(255, 0, 0);line-height: 1.75em;text-indent: 0em;"> <span style="font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&mid=2247492820&idx=1&sn=49fd246c24aba3666251dc1ae42f70fc&chksm=e9c61d65deb19473ab6b4edeaf1967b89f6fb1e298bee5054bb564d79139a59fa461ada3e1e3&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">5、</a><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&mid=2247492882&idx=1&sn=5ebfb94eb98bb5e43149f96cdd6e94d6&chksm=e9c61ca3deb195b5cc22fce43a3bd953756745a36091a0ee4fc7ae9c590ff76e942ed9166121&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">骚操作:不重启 JVM,如何替换掉已经加载的类?</a></span> </section></h4> </section> </section> </section> </section> </section> </section> </section> </section> </section> <section data-mpa-template="t" mpa-paragraph-type="ignored" style="max-width: 100%;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <p><span style="font-size: 14px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;color: rgb(255, 76, 0);">作者:blazemeter | </span><span style="color: rgb(255, 76, 0);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 0.544px;">来</span><span style="color: rgb(255, 76, 0);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 0.544px;">源:</span><span style="color: rgb(255, 76, 0);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 0.544px;">t.cn/ES7KBkW</span></p> </section> </section> </section> </section> <ul style="font-size: inherit;color: inherit;line-height: inherit;padding-left: 32px;" class="list-paddingleft-2"> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;line-height: 2em;text-align: left;margin-top: 5px;margin-bottom: 5px;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">步骤1 : 编写你的脚本</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;line-height: 2em;text-align: left;margin-top: 5px;margin-bottom: 5px;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">步骤2 : 使用JMeter进行本地测试</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;line-height: 2em;text-align: left;margin-top: 5px;margin-bottom: 5px;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">步骤3 : BlazeMeter沙箱测试</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;line-height: 2em;text-align: left;margin-top: 5px;margin-bottom: 5px;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">步骤4 : 使用1个控制台和1个引擎来设置每个引擎用户的数量</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;line-height: 2em;text-align: left;margin-top: 5px;margin-bottom: 5px;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">步骤5:安装并测试集群</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;line-height: 2em;text-align: left;margin-top: 5px;margin-bottom: 5px;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">步骤 6 : 使用 Master / Slave 特性来达成你的最大CC目标</span></p></li> </ul> <p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">本文将从负载测试的角度,描述了做一次流畅的5万用户并发测试需要做的事情.</span></p> <p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><strong style="font-size: inherit;color: inherit;line-height: inherit;">你可以在本文的结尾部分看到讨论的记录.</strong></span></p> <p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">快速的步骤概要</span></p> <ol style="font-size: inherit;color: inherit;line-height: inherit;padding-left: 32px;" class="list-paddingleft-2"> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">编写你的脚本</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">使用JMeter进行本地测试</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">BlazeMeter沙箱测试</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">使用一个控制台和一个引擎设置Users-per-Engine的数量</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">设置并测试你的集合 (1个控制台和10-14 引擎)</span></p></li> <li style="font-size: 15px;color: inherit;line-height: inherit;margin-bottom: 0.5em;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"><p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">使用 Master / Slave 特性来达成你的最大CC目标</span></p></li> </ol> <figure style="font-size: inherit;color: inherit;line-height: inherit;"> <img data-ratio="0.5562248995983936" data-type="png" data-w="996" src="/upload/674b2ee900e1e3f2c2a14770f582e74d.png" style="font-size: inherit;color: inherit;line-height: inherit;display: block;margin-right: auto;margin-left: auto;" title=""> </figure> <p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">img</span></p> <h1 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;color: rgb(21, 153, 87);font-size: 20px;text-align: left;"><span style="color: inherit;line-height: inherit;font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">步骤1 : 编写你的脚本</span></h1> <p style="color: inherit;margin-top: 15px;margin-bottom: 15px;line-height: 2em;text-align: left;"><span style="font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">开始之前,请确定从JMeter的Apache�
作者:微信小助手
<p data-mpa-powered-by="yiban.io"><img class="rich_pages" data-ratio="0.14946070878274267" data-s="300,640" data-type="png" data-w="1298" src="/upload/20f3115824a4736dedab25818f5b9f38.png" style="white-space: normal;caret-color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-size-adjust: auto;text-align: center;box-sizing: border-box !important;visibility: visible !important;width: 677px !important;"></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">我们都了解 <a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247484912&idx=1&sn=523f7e9739a3be55bfa4dc91362a8a8b&chksm=fd60154fca179c59df0f950db67a9bf5243e6158674251a3f5d03d9cf48ad0c2c852496b4926&scene=21#wechat_redirect" textvalue="Redis主从库集群模式" data-itemshowtype="0" tab="innerlink" data-linktype="2"><strong>Redis主从库集群模式</strong></a>。在这个模式下,如果从库发生故障了,客户端可以继续向主库或其他从库发送请求,进行相关的操作,但是如果主库发生故障了,那就直接会影响到从库的同步,因为从库没有相应的主库可以进行数据复制操作了。</span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;"><br></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">而且,如果客户端发送的都是读操作请求,那还可以由从库继续提供服务,这在纯读的业务场景下还能被接受。但是,一旦有写操作请求了,按照主从库模式下的读写分离要求,需要由主库来完成写操作。此时,也没有实例可以来服务客户端的写操作请求了,如下图所示:</span></p> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><img data-ratio="0.6674577276772471" src="/upload/afa1b49030058fc02162c2260a848c93.jpg" data-type="jpeg" data-w="3371"></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">无论是写服务中断,还是从库无法进行数据同步,都是不能接受的。所以,如果主库挂了,我们就需要运行一个新主库,比如说把一个从库切换为主库,把它当成主库。这就涉及到三个问题:</span></p> <ol class="list-paddingleft-2" style="list-style-type: decimal;"> <li style="font-size: 15px;letter-spacing: 0.5px;"><p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">主库真的挂了吗?</span></p></li> <li style="font-size: 15px;letter-spacing: 0.5px;"><p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">该选择哪个从库作为主库?</span></p></li> <li style="font-size: 15px;letter-spacing: 0.5px;"><p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">怎么把新主库的相关信息通知给从库和客户端呢?</span></p></li> </ol> <p style="text-align: left;"><br></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">这就要提到哨兵机制了。在 Redis 主从集群中,哨兵机制是实现主从库自动切换的关键机制,它有效地解决了主从复制模式下故障转移的这三个问题。</span></p> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><br></span></p> <p><span style="letter-spacing: 0.5px;"><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);">哨兵机制的基本流程</span></strong></span><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"></span></strong></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">哨兵其实就是一个运行在特殊模式下的 Redis 进程,主从库实例运行的同时,它也在运行。哨兵主要负责的就是三个任务:监控、选主(选择主库)和通知。</span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;"><br></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">我们先看监控。监控是指哨兵进程在运行时,周期性地给所有的主从库发送 PING 命令,检测它们是否仍然在线运行。如果从库没有在规定时间内响应哨兵的 PING 命令,哨兵就会把它标记为“下线状态”;同样,如果主库也没有在规定时间内响应哨兵的 PING 命令,哨兵就会判定主库下线,然后开始<strong>自动切换主库</strong>的流程。</span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;"><br></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">这个流程首先是执行哨兵的第二个任务,选主。主库挂了以后,哨兵就需要从很多个从库里,按照一定的规则选择一个从库实例,把它作为新的主库。这一步完成后,现在的集群里就有了新主库。</span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;"><br></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">然后,哨兵会执行最后一个任务:通知。在执行通知任务时,哨兵会把新主库的连接信息发给其他从库,让它们执行 replicaof 命令,和新主库建立连接,并进行数据复制。同时,哨兵会把新主库的连接信息通知给客户端,让它们把请求操作发到新主库上。</span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;"><br></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">我画了一张图片,展示了这三个任务以及它们各自的目标。</span></p> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><img data-ratio="0.3522491349480969" src="/upload/1868dac5efe826261c63de493d438f3a.jpg" data-type="jpeg" data-w="2890"></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">在这三个任务中,通知任务相对来说比较简单,哨兵只需要把新主库信息发给从库和客户端,让它们和新主库建立连接就行,并不涉及决策的逻辑。但是,在监控和选主这两个任务中,哨兵需要做出两个决策:</span></p> <ul class="list-paddingleft-2" style="list-style-type: square;"> <li style="font-size: 15px;letter-spacing: 0.5px;"><p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">在监控任务中,哨兵需要判断主库是否处于下线状态;</span></p></li> <li style="font-size: 15px;letter-spacing: 0.5px;"><p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">在选主任务中,哨兵也要决定选择哪个从库实例作为主库。</span></p></li> </ul> <p style="text-align: left;"><br></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">接下来,我们就先说说如何判断主库的下线状态。</span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;"><br></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">你首先要知道的是,哨兵对主库的下线判断有“主观下线”和“客观下线”两种。那么,为什么会存在两种判断呢?它们的区别和联系是什么呢?</span></p> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><br></span></p> <p><span style="letter-spacing: 0.5px;"><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);">主观下线和客观下线</span></strong></span><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"></span></strong></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;"><strong>哨兵进程会使用 PING 命令检测它自己和主、从库的网络连接情况,用来判断实例的状态</strong>。如果哨兵发现主库或从库对 PING 命令的响应超时了,那么,哨兵就会先把它标记为“主观下线”。</span><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><br></span></strong></p> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><br></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">如果检测的是从库,那么,哨兵简单地把它标记为“主观下线”就行了,因为从库的下线影响一般不太大,集群的对外服务不会间断。</span></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">但是,如果检测的是主库,那么,哨兵还不能简单地把它标记为“主观下线”,开启主从切换。因为很有可能存在这么一个情况:那就是哨兵误判了,其实主库并没有故障。可是,一旦启动了主从切换,后续的选主和通知操作都会带来额外的计算和通信开销。</span></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">为了避免这些不必要的开销,我们要特别注意误判的情况。</span></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">首先,我们要知道啥叫误判。很简单,就是主库实际并没有下线,但是哨兵误以为它下线了。误判一般会发生在集群网络压力较大、网络拥塞,或者是主库本身压力较大的情况下。</span></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">一旦哨兵判断主库下线了,就会开始选择新主库,并让从库和新主库进行数据同步,这个过程本身就会有开销,例如,哨兵要花时间选出新主库,从库也需要花时间和新主库同步。而在误判的情况下,主库本身根本就不需要进行切换的,所以这个过程的开销是没有价值的。正因为这样,我们需要判断是否有误判,以及减少误判。</span></p> <p style="text-align: left;"><br></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">那怎么减少误判呢?在日常生活中,当我们要对一些重要的事情做判断的时候,经常会和家人或朋友一起商量一下,然后再做决定。</span></p> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><br></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">哨兵机制也是类似的,它通常会采用多实例组成的集群模式进行部署,这也被称为哨兵集群。引入多个哨兵实例一起来判断,就可以避免单个哨兵因为自身网络状况不好,而误判主库下线的情况。同时,多个哨兵的网络同时不稳定的概率较小,由它们一起做决策,误判率也能降低。</span></p> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><br></span></p> <p><span style="letter-spacing: 0.5px;"><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);">如何选定新主库?</span></strong></span><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"></span></strong></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">一般来说,我把哨兵选择新主库的过程称为“筛选 + 打分”。简单来说,我们在多个从库中,先按照一定的筛选条件,把不符合条件的从库去掉。然后,我们再按照一定的规则,给剩下的从库逐个打分,将得分最高的从库选为新主库,如下图所示:</span><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><br></span></strong></p> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><img data-ratio="0.4748025061291201" src="/upload/b565abd3af8647d8713f2de25f14e4e0.jpg" data-type="jpeg" data-w="3671"></span></p> <p><span style="background-color: rgb(255, 255, 255);font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;"><br></span></p> <p><span style="letter-spacing: 0.5px;"><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);">第一轮:优先级最高的从库得分高。</span></strong></span><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"></span></strong></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">用户可以通过 slave-priority 配置项,给不同的从库设置不同优先级。比如,你有两个从库,它们的内存大小不一样,你可以手动给内存大的实例设置一个高优先级。在选主时,哨兵会给优先级高的从库打高分,如果有一个从库优先级最高,那么它就是新主库了。如果从库的优先级都一样,那么哨兵开始第二轮打分。</span></p> <p><br></p> <p><span style="letter-spacing: 0.5px;"><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);">第二轮:和旧主库同步程度最接近的从库得分高。</span></strong></span><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"></span></strong></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">这个规则的依据是,如果选择和旧主库同步最接近的那个从库作为主库,那么,这个新主库上就有最新的数据。</span></p> <p><br></p> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);letter-spacing: 0.5px;"><strong>第三轮:ID 号小的从库得分高。</strong></span><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"></span></p> <p style="text-align: left;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">每个实例都会有一个 ID,这个 ID 就类似于这里的从库的编号。目前,Redis 在选主库时,有一个默认的规定:在优先级和复制进度都相同的情况下,<strong>ID 号最小的从库得分最高,会被选为新主库</strong>。</span><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><strong><br></strong></span></p> <p><br></p> <p style="text-align: start;"><span style="letter-spacing: 0.5px;"><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;background-color: rgb(255, 255, 255);">总结</span></strong></span><strong><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;background-color: rgb(255, 255, 255);"></span></strong></p> <p style="text-align: start;"><span style="font-family:PingFang SC, Lantinghei SC, Microsoft Yahei, Hiragino Sans GB, Microsoft Sans Serif, WenQuanYi Micro Hei, Helvetica, sans-serif;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">哨兵机制,它是实现 Redis 不间断服务的重要保证。具体来说,主从集群的数据同步,是数据可靠的基础保证;而在主库发生故障时,自动的主从切换是服务不间断的关键支撑。</span></span><span style="font-size: 15px;letter-spacing: 0.5px;"><strong><span style="font-family:PingFang SC, Lantinghei SC, Microsoft Yahei, Hiragino Sans GB, Microsoft Sans Serif, WenQuanYi Micro Hei, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);"><br></span></span></strong></span></p> <p style="text-align: start;"><span style="font-family:PingFang SC, Lantinghei SC, Microsoft Yahei, Hiragino Sans GB, Microsoft Sans Serif, WenQuanYi Micro Hei, Helvetica, sans-serif;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">Redis 的哨兵机制自动完成了以下三大功能,从而实现了主从库的自动切换,可以降低 Redis 集群的运维开销:</span></span></p> <ul class="list-paddingleft-2" style="list-style-type: square;"> <li style="font-size: 15px;letter-spacing: 0.5px;"><p style="text-align: start;"><span style="font-family:PingFang SC, Lantinghei SC, Microsoft Yahei, Hiragino Sans GB, Microsoft Sans Serif, WenQuanYi Micro Hei, Helvetica, sans-serif;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">监控主库运行状态,并判断主库是否客观下线;</span></span></p></li> <li style="font-size: 15px;letter-spacing: 0.5px;"><p style="text-align: start;"><span style="font-family:PingFang SC, Lantinghei SC, Microsoft Yahei, Hiragino Sans GB, Microsoft Sans Serif, WenQuanYi Micro Hei, Helvetica, sans-serif;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">在主库客观下线后,选取新主库;</span></span></p></li> <li style="font-size: 15px;letter-spacing: 0.5px;"><p style="text-align: start;"><span style="font-family:PingFang SC, Lantinghei SC, Microsoft Yahei, Hiragino Sans GB, Microsoft Sans Serif, WenQuanYi Micro Hei, Helvetica, sans-serif;"><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;text-align: start;background-color: rgb(255, 255, 255);font-size: 15px;letter-spacing: 0.5px;">选出新主库后,通知从库和客户端。</span></span></p></li> </ul> <p><span style="font-family: "PingFang SC", "Lantinghei SC", "Microsoft Yahei", "Hiragino Sans GB", "Microsoft Sans Serif", "WenQuanYi Micro Hei", Helvetica, sans-serif;font-size: 16px;text-align: start;background-color: rgb(255, 255, 255);"><br></span></p> <section data-recommend-type="list-title" data-recommend-tid="8" data-mpa-template="t" style="width: 100%;display: flex;justify-content: center;align-items: center;" data-mid="" data-from="yb-recommend"> <section style="width: 100%;background: rgb(255, 255, 255);border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(232, 232, 235);padding: 20px 14px;" data-mid=""> <section style="width: 100%;display: flex;justify-content: center;align-items: center;align-items: flex-end;" data-mid=""> <section style="display: flex;justify-content: center;align-items: center;max-width: 100%;background: #fff;margin-bottom: -10px;z-index: 10;" data-mid=""> <section style="width: 10px;height: 10px;border-radius: 50%;border-width: 1px;border-style: solid;border-color: rgb(51, 51, 51);" data-mid=""> <br> </section> <section style="margin: 0 8px;height: 20px;font-size: 14px;font-weight: 500;color: #333333;line-height: 20px;" data-mid=""> <p data-mid="">往期推荐</p> </section> <section style="width: 10px;height: 10px;border-radius: 50%;border-width: 1px;border-style: solid;border-color: rgb(51, 51, 51);" data-mid=""> <br> </section> </section> </section> <section style="width: 100%;height: 1px;background: rgb(51, 51, 51);margin-bottom: 16px;" data-mid=""> <br> </section> <section style="width: 100%;" data-mid=""> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="8" data-recommend-article-id="2247486397_2" data-recommend-article-time="1597624200" data-recommend-article-cover="http://mmbiz.qpic.cn/sz_mmbiz_jpg/d7YzaYDnrxEzH6XgwvqOYebULn4ZtjrW566JCAticgqmnJLDnlW7IddGx3ew8kyOZKEGpDlmmHFSzynIfpia6c9w/0?wx_fmt=jpeg" data-recommend-article-title="宕机后,Redis如何实现快速恢复?" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247486397&idx=2&sn=96d06435745251e426b09fa984c0df6a&chksm=fd601b02ca1792146ee496757c14ceb2c0a5644f50d3d80c0db390257efa940d1c4718d1bcb0#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247486397&idx=2&sn=96d06435745251e426b09fa984c0df6a&chksm=fd601b02ca1792146ee496757c14ceb2c0a5644f50d3d80c0db390257efa940d1c4718d1bcb0&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" style="width: 100%;background: #f5f5f5;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;padding: 6px 16px 6px;font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">宕机后,Redis如何实现快速恢复?</p> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="8" data-recommend-article-id="2247484912_1" data-recommend-article-time="1592266200" data-recommend-article-cover="http://mmbiz.qpic.cn/sz_mmbiz_jpg/d7YzaYDnrxFdQuJPZ7jlib3do2VnGj8uAIlgibpJicVlnWJJjheQgpS4fRC25tY1QiciaPicGNYzYeZNfHrLdG2fhjBA/0?wx_fmt=jpeg" data-recommend-article-title="Redis主从复制以及主从复制原理" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247484912&idx=1&sn=523f7e9739a3be55bfa4dc91362a8a8b&chksm=fd60154fca179c59df0f950db67a9bf5243e6158674251a3f5d03d9cf48ad0c2c852496b4926#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247484912&idx=1&sn=523f7e9739a3be55bfa4dc91362a8a8b&chksm=fd60154fca179c59df0f950db67a9bf5243e6158674251a3f5d03d9cf48ad0c2c852496b4926&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" style="width: 100%;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;padding: 6px 16px 6px;font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">Redis主从复制以及主从复制原理</p> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="8" data-recommend-article-id="2247484976_1" data-recommend-article-time="1596533400" data-recommend-article-cover="http://mmbiz.qpic.cn/sz_mmbiz_jpg/d7YzaYDnrxEtSZibnQWhnlN5pPL3pc8AzC6K3dANNIJYRaF5cSb0M8ApZOpRHvpHOmutgIbNMDkib0CUd05UIkxQ/0?wx_fmt=jpeg" data-recommend-article-title="节点负载差距这么大,为什么收到的流量还一样?" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247484976&idx=1&sn=3c45a9c0d7319f8210a00c6cbe2cb16a&chksm=fd60168fca179f99763c48d9a56695a1bbfa8e48ba73cbe50acd7e22ad0564bd9244140caaf1#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247484976&idx=1&sn=3c45a9c0d7319f8210a00c6cbe2cb16a&chksm=fd60168fca179f99763c48d9a56695a1bbfa8e48ba73cbe50acd7e22ad0564bd9244140caaf1&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" style="width: 100%;background: #f5f5f5;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;padding: 6px 16px 6px;font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">节点负载差距这么大,为什么收到的流量还一样?</p> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="8" data-recommend-article-id="2247484928_1" data-recommend-article-time="1592352600" data-recommend-article-cover="http://mmbiz.qpic.cn/sz_mmbiz_jpg/d7YzaYDnrxHVZG5Oq840NfYwXubicGVibuxhqQLFAQcu7OphM213syHk6M6zAe4ZPdeJnvtR0OroWvx3JzOecz5g/0?wx_fmt=jpeg" data-recommend-article-title="用Redis构建缓存集群的最佳实践有哪些?" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247484928&idx=1&sn=0fc5683b5f0ccc0ff708b9f851bbb8cf&chksm=fd6016bfca179fa93af9305e19d16511ffd09c3b5cb4f14a25d278917287adbcbb3b03b9d401#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247484928&idx=1&sn=0fc5683b5f0ccc0ff708b9f851bbb8cf&chksm=fd6016bfca179fa93af9305e19d16511ffd09c3b5cb4f14a25d278917287adbcbb3b03b9d401&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" style="width: 100%;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;padding: 6px 16px 6px;font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">用Redis构建缓存集群的最佳实践有哪些?</p> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="8" data-recommend-article-id="2247485365_2" data-recommend-article-time="1597019400" data-recommend-article-cover="http://mmbiz.qpic.cn/sz_mmbiz_jpg/d7YzaYDnrxEHAiaDJOGTmW9Nfv7rvbsmgqSiaSbzMccooNrUz4aKHdeHNy8cGBCVrKktmywbLmSIpF79vA1J5aUQ/0?wx_fmt=jpeg" data-recommend-article-title="福利|Redis 全套面试题免费领取" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247485365&idx=2&sn=6543b822823f52ce8032f0a73a8249d9&chksm=fd60170aca179e1cd05947034f6d259d6e33803d79163f4054c761815aa57a6af09510b1a95e#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247485365&idx=2&sn=6543b822823f52ce8032f0a73a8249d9&chksm=fd60170aca179e1cd05947034f6d259d6e33803d79163f4054c761815aa57a6af09510b1a95e&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" style="width: 100%;background: #f5f5f5;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;padding: 6px 16px 6px;font-size: 13px;font-weight: 400;color: #333333;line-height: 18px;border-bottom:none !important;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">福利|Redis 全套面试题免费领取</p> </section></a> </section> </section> </section> </section> <p><br></p> <p style="white-space: normal;caret-color: rgb(51, 51, 51);letter-spacing: 0.544px;text-size-adjust: auto;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);"><img class="rich_pages" data-ratio="0.2768" data-s="300,640" data-type="png" data-w="1250" data-backw="578" data-backh="160" src="/upload/5465272df80fe618e627da0ff02a3410.png" style="letter-spacing: 0.544px;text-align: center;box-sizing: border-box !important;visibility: visible !important;width: 677px !important;"></p> <p style="white-space: normal;caret-color: rgb(51, 51, 51);letter-spacing: 0.544px;text-size-adjust: auto;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);text-align: right;"><span style="letter-spacing: 0.544px;caret-color: rgb(136, 136, 136);color: rgb(136, 136, 136);font-family: 微软雅黑;font-size: 15px;text-align: left;">🧐</span><span style="letter-spacing: 0.544px;font-family: 微软雅黑;font-size: 15px;text-align: left;color: rgb(0, 0, 0);"><strong>分享、点赞、在看</strong>,给个<strong>三连击</strong>呗!</span><span style="letter-spacing: 0.544px;caret-color: rgb(136, 136, 136);color: rgb(136, 136, 136);font-family: 微软雅黑;font-size: 15px;text-align: left;"><strong style="letter-spacing: 0.544px;">👇 </strong></span></p>
作者:微信小助手
<section style="display: none;" data-tools="新媒体管家" data-label="powered by xmt.cn" data-mpa-powered-by="yiban.io"> <br> </section> <p style="text-align: center;"><span style="font-size: 13px;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;">点击上方蓝色“</span><span style="font-size: 13px;font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;color: rgb(0, 128, 255);">程序猿DD</span><span style="font-size: 13px;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;">”,选择“设为星标”</span><br></p> <p style="max-width: 100%;min-height: 1em;white-space: normal;text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;font-size: 13px;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;">回复“</span><span style="max-width: 100%;font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;color: rgb(0, 128, 255);overflow-wrap: break-word !important;box-sizing: border-box !important;">资源</span><span style="max-width: 100%;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;">”获取独家整理的学习资料!</span></span></p> <p style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;min-height: 1em;white-space: normal;text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;font-size: 14px;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;"><img data-backh="34" data-backw="540" data-ratio="0.0625" data-s="300,640" data-type="jpeg" data-w="640" width="100%" src="/upload/8c292e55ba5a23cb6ebc11f2a2c4fece.jpg" style="font-family: -apple-system-font, system-ui
作者:微信小助手
<section style="font-size: 15px;color: rgb(62, 62, 62);line-height: 1.6;letter-spacing: 0px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;" data-mpa-powered-by="yiban.io"> <p style="margin-bottom: 10px;color: rgb(62, 62, 62);font-size: 15px;white-space: normal;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);line-height: normal;text-align: center;"><span style="font-size: 14px;letter-spacing: 0.544px;word-spacing: 2px;color: rgb(136, 136, 136);">点击蓝色“</span><span style="font-size: 14px;letter-spacing: 0.544px;word-spacing: 2px;color: rgb(0, 128, 255);">架构文摘</span><span style="font-size: 14px;letter-spacing: 0.544px;word-spacing: 2px;color: rgb(136, 136, 136);">”关注我哟</span></p> <p style="margin-bottom: 10px;color: rgb(62, 62, 62);font-size: 15px;white-space: normal;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-size-adjust: auto;word-spacing: 2px;text-align: center;"><span style="font-size: 14px;color: rgb(136, 136, 136);">加个“</span><span style="color: rgb(0, 128, 255);font-size: 14px;">星标</span><span style="font-size: 14px;color: rgb(136, 136, 136);">”,每天上午 09:25,干货推送!</span></p> <p style="color: rgb(62, 62, 62);white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;font-size: 14px;background-color: rgb(255, 255, 255);text-align: center;"><img data-backh="36" data-backw="578" data-ratio="0.0625" data-s="300,640" data-type="jpeg" data-w="640" width="100%" src="/upload/8c292e55ba5a23cb6ebc11f2a2c4fece.jpg" style="font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;widows: 1;word-spacing: 2px;color: rgb(136, 136, 136);box-sizing: border-box !important;visibility: visible !important;width: 677px !important;"></p> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;"><span style="color: rgb(136, 136, 136);font-size: 12px;"><em>原文::http://elasticsearch.cn/article/6202</em></span></p> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">因为总是看到很多同学在说elasticsearch性能不够好,集群不够稳定,询问关于elasticsearch的调优,但是每次都是一个个点的单独讲,很多时候都是case by case的解答,今天简单梳理下日常的elasticsearch使用调优,以下仅为自己日常经验之谈,如有疏漏,还请大家帮忙指正。</p> <h3 style="color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.3em;border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit;line-height: inherit;display: inline-block;font-weight: normal;background: rgb(0, 172, 193);color: rgb(255, 255, 255);padding-top: 3px;padding-right: 10px;padding-left: 10px;border-top-right-radius: 3px;border-top-left-radius: 4px;margin-right: 2px;">一、配置文件调优</span></h3> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">elasticsearch.yml</span></h4> <h5 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">内存锁定</span></h5> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;"><code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">bootstrap.memory_lock:true</code> 允许 JVM 锁住内存,禁止操作系统交换出去。</p> <h5 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">zen.discovery</span></h5> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">Elasticsearch 默认被配置为使用单播发现,以防止节点无意中加入集群。组播发现应该永远不被使用在生产环境了,否则你得到的结果就是一个节点意外的加入到了你的生产环境,仅仅是因为他们收到了一个错误的组播信号。ES是一个P2P类型的分布式系统,使用gossip协议,集群的任意请求都可以发送到集群的任一节点,然后es内部会找到需要转发的节点,并且与之进行通信。在es1.x的版本,es默认是开启组播,启动es之后,可以快速将局域网内集群名称,默认端口的相同实例加入到一个大的集群,后续再es2.x之后,都调整成了单播,避免安全问题和网络风暴;</p> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">单播<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">discovery.zen.ping.unicast.hosts</code>,建议写入集群内所有的节点及端口,如果新实例加入集群,新实例只需要写入当前集群的实例,即可自动加入到当前集群,之后再处理原实例的配置即可,新实例加入集群,不需要重启原有实例;节点zen相关配置:<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">discovery.zen.ping_timeout</code>:判断master选举过程中,发现其他node存活的超时设置,主要影响选举的耗时,参数仅在加入或者选举 master 主节点的时候才起作用<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">discovery.zen.join_timeout</code>:节点确定加入到集群中,向主节点发送加入请求的超时时间,默认为3<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">sdiscovery.zen.minimum_master_nodes</code>:参与master选举的最小节点数,当集群能够被选为master的节点数量小于最小数量时,集群将无法正常选举。</p> <h5 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">故障检测( fault detection )</span></h5> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">两种情况下回进行故障检测,第一种是由master向集群的所有其他节点发起ping,验证节点是否处于活动状态;第二种是:集群每个节点向master发起ping,判断master是否存活,是否需要发起选举。故障检测需要配置以下设置使用 形如:<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">discovery.zen.fd.ping_interval</code> 节点被ping的频率,默认为1s。<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">discovery.zen.fd.ping_timeout</code> 等待ping响应的时间,默认为 30s,运行的集群中,master 检测所有节点,以及节点检测 master 是否正常。<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">discovery.zen.fd.ping_retries</code> ping失败/超时多少导致节点被视为失败,默认为3。</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/en/elasticsearch/reference/6.x/modules-discovery-zen.html</p> </blockquote> <h5 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">队列数量</span></h5> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">不建议盲目加大es的队列数量,如果是偶发的因为数据突增,导致队列阻塞,加大队列size可以使用内存来缓存数据,如果是持续性的数据阻塞在队列,加大队列size除了加大内存占用,并不能有效提高数据写入速率,反而可能加大es宕机时候,在内存中可能丢失的上数据量。哪些情况下,加大队列size呢?GET /_cat/thread_pool,观察api中返回的queue和rejected,如果确实存在队列拒绝或者是持续的queue,可以酌情调整队列size。</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/en/elasticsearch/reference/6.x/modules-threadpool.html</p> </blockquote> <h5 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">内存使用</span></h5> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">设置indices的内存熔断相关参数,根据实际情况进行调整,防止写入或查询压力过高导致OOM, <code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">indices.breaker.total.limit</code>: 50%,集群级别的断路器,默认为jvm堆的70%;<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">indices.breaker.request.limit</code>: 10%,单个request的断路器限制,默认为jvm堆的60%;<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">indices.breaker.fielddata.limit</code>: 10%,fielddata breaker限制,默认为jvm堆的60%。</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/en/elasticsearch/reference/6.x/circuit-breaker.html</p> </blockquote> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">根据实际情况调整查询占用cache,避免查询cache占用过多的jvm内存,参数为静态的,需要在每个数据节点配置。indices.queries.cache.size: 5%,控制过滤器缓存的内存大小,默认为10%。接受百分比值,5%或者精确值,例如512mb。</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/en/elasticsearch/reference/6.x/query-cache.html</p> </blockquote> <h5 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">创建shard</span></h5> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">如果集群规模较大,可以阻止新建shard时扫描集群内全部shard的元数据,提升shard分配速度。<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">cluster.routing.allocation.disk.include_relocations: false</code>,默认为true。</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/en/elasticsearch/reference/6.x/disk-allocator.html</p> </blockquote> <h3 style="color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.3em;border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit;line-height: inherit;display: inline-block;font-weight: normal;background: rgb(0, 172, 193);color: rgb(255, 255, 255);padding-top: 3px;padding-right: 10px;padding-left: 10px;border-top-right-radius: 3px;border-top-left-radius: 4px;margin-right: 2px;">二、系统层面调优</span></h3> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">jdk版本</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">当前根据官方建议,选择匹配的jdk版本;</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">jdk内存配置</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">首先,-Xms和-Xmx设置为相同的值,避免在运行过程中再进行内存分配,同时,如果系统内存小于64G,建议设置略小于机器内存的一半,剩余留给系统使用。同时,jvm heap建议不要超过32G(不同jdk版本具体的值会略有不同),否则jvm会因为内存指针压缩导致内存浪费,详见:</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/cn/elasticsearch/guide/current/heap-sizing.html</p> </blockquote> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">交换分区</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">关闭交换分区,防止内存发生交换导致性能下降(部分情况下,宁死勿慢) <code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">swapoff -a</code></p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">文件句柄</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">Lucene 使用了 大量的 文件。同时,Elasticsearch 在节点和 HTTP 客户端之间进行通信也使用了大量的套接字,所有这一切都需要足够的文件描述符,默认情况下,linux默认运行单个进程打开1024个文件句柄,这显然是不够的,故需要加大文件句柄数 ulimit -n 65536</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/en/elasticsearch/reference/6.5/setting-system-settings.html</p> </blockquote> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">mmap</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">Elasticsearch 对各种文件混合使用了 NioFs( 注:非阻塞文件系统)和 MMapFs ( 注:内存映射文件系统)。请确保你配置的最大映射数量,以便有足够的虚拟内存可用于 mmapped 文件。这可以暂时设置:sysctl -w <code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">vm.max_map_count=262144</code> 或者你可以在 <code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">/etc/sysctl.conf</code> 通过修改 <code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">vm.max_map_count</code> 永久设置它。</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_file_descriptors_and_mmap.html</p> </blockquote> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">磁盘</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">如果你正在使用 SSDs,确保你的系统 I/O 调度程序是配置正确的。当你向硬盘写数据,I/O 调度程序决定何时把数据实际发送到硬盘。大多数默认 nix 发行版下的调度程序都叫做 cfq(完全公平队列)。但它是为旋转介质优化的:机械硬盘的固有特性意味着它写入数据到基于物理布局的硬盘会更高效。这对 SSD 来说是低效的,尽管这里没有涉及到机械硬盘。但是,deadline 或者 noop 应该被使用。deadline 调度程序基于写入等待时间进行优化, noop 只是一个简单的 FIFO 队列。<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">echo noop > /sys/block/sd/queue/scheduler</code></p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">磁盘挂载</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;"><code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">mount -o noatime,data=writeback,barrier=0,nobh /dev/sd* /esdata*</code> 其中,noatime,禁止记录访问时间戳;data=writeback,不记录journal;barrier=0,因为关闭了journal,所以同步关闭barrier;nobh,关闭buffer_head,防止内核影响数据IO</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">磁盘其他注意事项</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">使用 RAID 0。条带化 RAID 会提高磁盘I/O,代价显然就是当一块硬盘故障时整个就故障了,不要使用镜像或者奇偶校验 RAID 因为副本已经提供了这个功能。另外,使用多块硬盘,并允许 Elasticsearch 通过多个 path.data 目录配置把数据条带化分配到它们上面。不要使用远程挂载的存储,比如 NFS 或者 SMB/CIFS。这个引入的延迟对性能来说完全是背道而驰的。</p> <h3 style="color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.3em;border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit;line-height: inherit;display: inline-block;font-weight: normal;background: rgb(0, 172, 193);color: rgb(255, 255, 255);padding-top: 3px;padding-right: 10px;padding-left: 10px;border-top-right-radius: 3px;border-top-left-radius: 4px;margin-right: 2px;">三、elasticsearch使用方式调优</span></h3> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">当elasticsearch本身的配置没有明显的问题之后,发现es使用还是非常慢,这个时候,就需要我们去定位es本身的问题了,首先祭出定位问题的第一个命令:</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">hot_threads</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;"><code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">GET /_nodes/hot_threads&interval=30s</code></p> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">抓取30s的节点上占用资源的热线程,并通过排查占用资源最多的TOP线程来判断对应的资源消耗是否正常,一般情况下,bulk,search类的线程占用资源都可能是业务造成的,但是如果是merge线程占用了大量的资源,就应该考虑是不是创建index或者刷磁盘间隔太小,批量写入size太小造成的。</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/en/elasticsearch/reference/6.x/cluster-nodes-hot-threads.html</p> </blockquote> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">pending_tasks</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;"><code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">GET /_cluster/pending_tasks</code></p> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">有一些任务只能由主节点去处理,比如创建一个新的 索引或者在集群中移动分片,由于一个集群中只能有一个主节点,所以只有这一master节点可以处理集群级别的元数据变动。在99.9999%的时间里,这不会有什么问题,元数据变动的队列基本上保持为零。在一些罕见的集群里,元数据变动的次数比主节点能处理的还快,这会导致等待中的操作会累积成队列。这个时候可以通过pending_tasks api分析当前什么操作阻塞了es的队列,比如,集群异常时,会有大量的shard在recovery,如果集群在大量创建新字段,会出现大量的put_mappings的操作,所以正常情况下,需要禁用动态mapping。</p> <blockquote style="line-height: inherit;padding: 15px 15px 15px 1rem;font-size: 0.9em;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);background: rgb(242, 247, 251);overflow: auto;overflow-wrap: normal;word-break: normal;"> <p style="font-size: inherit;color: inherit;line-height: inherit;">https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-pending.html</p> </blockquote> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">字段存储</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">当前es主要有doc_values,fielddata,storefield三种类型,大部分情况下,并不需要三种类型都存储,可根据实际场景进行调整:当前用得最多的就是doc_values,列存储,对于不需要进行分词的字段,都可以开启doc_values来进行存储(且只保留keyword字段),节约内存,当然,开启doc_values会对查询性能有一定的影响,但是,这个性能损耗是比较小的,而且是值得的;</p> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">fielddata构建和管理 100% 在内存中,常驻于 JVM 内存堆,所以可用于快速查询,但是这也意味着它本质上是不可扩展的,有很多边缘情况下要提防,如果对于字段没有分析需求,可以关闭fielddata;</p> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">storefield主要用于_source字段,默认情况下,数据在写入es的时候,es会将doc数据存储为_source字段,查询时可以通过_source字段快速获取doc的原始结构,如果没有update,reindex等需求,可以将_source字段disable;</p> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">_all,ES在6.x以前的版本,默认将写入的字段拼接成一个大的字符串,并对该字段进行分词,用于支持整个doc的全文检索,在知道doc字段名称的情况下,建议关闭掉该字段,节约存储空间,也避免不带字段key的全文检索;</p> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">norms:搜索时进行评分,日志场景一般不需要评分,建议关闭;</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">tranlog</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">Elasticsearch 2.0之后为了保证不丢数据,每次 index、bulk、delete、update 完成的时候,一定触发刷新 translog 到磁盘上,才给请求返回 200 OK。这个改变在提高数据安全性的同时当然也降低了一点性能。如果你不在意这点可能性,还是希望性能优先,可以在 index template 里设置如下参数:</p> <pre style="font-size: inherit;color: inherit;line-height: inherit;"><code style="margin-right: 2px;margin-left: 2px;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);padding: 0.5em;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;">{<br> <span style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">"index.translog.durability"</span>: <span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"async"</span><br>}<br></code></pre> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">index.translog.sync_interval:对于一些大容量的偶尔丢失几秒数据问题也并不严重的集群,使用异步的 fsync 还是比较有益的。比如,写入的数据被缓存到内存中,再每5秒执行一次 fsync ,默认为5s。小于的值100ms是不允许的。index.translog.flush_threshold_size:translog存储尚未安全保存在Lucene中的所有操作。虽然这些操作可用于读取,但如果要关闭并且必须恢复,则需要重新编制索引。此设置控制这些操作的最大总大小,以防止恢复时间过长。达到设置的最大size后,将发生刷新,生成新的Lucene提交点,默认为512mb。</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">refresh_interval</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">执行刷新操作的频率,这会使索引的最近更改对搜索可见,默认为1s,可以设置-1为禁用刷新,对于写入速率要求较高的场景,可以适当的加大对应的时长,减小磁盘io和segment的生成;</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">禁止动态mapping</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">动态mapping的坏处:</p> <ol style="font-size: inherit;color: inherit;line-height: inherit;padding-left: 32px;" class="list-paddingleft-2"> <li style="font-size: inherit;color: inherit;line-height: inherit;margin-bottom: 0.5em;"><p><span style="font-size: inherit;color: inherit;line-height: inherit;">造成集群元数据一直变更,导致集群不稳定;</span></p></li> <li style="font-size: inherit;color: inherit;line-height: inherit;margin-bottom: 0.5em;"><p><span style="font-size: inherit;color: inherit;line-height: inherit;">可能造成数据类型与实际类型不一致;</span></p></li> <li style="font-size: inherit;color: inherit;line-height: inherit;margin-bottom: 0.5em;"><p><span style="font-size: inherit;color: inherit;line-height: inherit;">对于一些异常字段或者是扫描类的字段,也会频繁的修改mapping,导致业务不可控。</span></p></li> </ol> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">动态mapping配置的可选值及含义如下:true:支持动态扩展,新增数据有新的字段属性时,自动添加对于的mapping,数据写入成功 false:不支持动态扩展,新增数据有新的字段属性时,直接忽略,数据写入成功 strict:不支持动态扩展,新增数据有新的字段时,报错,数据写入失败</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">批量写入</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">批量请求显然会大大提升写入速率,且这个速率是可以量化的,官方建议每次批量的数据物理字节数5-15MB是一个比较不错的起点,注意这里说的是物理字节数大小。文档计数对批量大小来说不是一个好指标。比如说,如果你每次批量索引 1000 个文档,记住下面的事实:1000 个 1 KB 大小的文档加起来是 1 MB 大。1000 个 100 KB 大小的文档加起来是 100 MB 大。这可是完完全全不一样的批量大小了。批量请求需要在协调节点上加载进内存,所以批量请求的物理大小比文档计数重要得多。从 5–15 MB 开始测试批量请求大小,缓慢增加这个数字,直到你看不到性能提升为止。然后开始增加你的批量写入的并发度(多线程等等办法)。用iostat 、 top 和 ps 等工具监控你的节点,观察资源什么时候达到瓶颈。如果你开始收到 EsRejectedExecutionException ,你的集群没办法再继续了:至少有一种资源到瓶颈了。或者减少并发数,或者提供更多的受限资源(比如从机械磁盘换成 SSD),或者添加更多节点。</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">索引和shard</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">es的索引,shard都会有对应的元数据,且因为es的元数据都是保存在master节点,且元数据的更新是要hang住集群向所有节点同步的,当es的新建字段或者新建索引的时候,都会要获取集群元数据,并对元数据进行变更及同步,此时会影响集群的响应,所以需要关注集群的index和shard数量,建议如下:1.使用shrink和rollover api,相对生成合适的数据shard数;2.根据数据量级及对应的性能需求,选择创建index的名称,形如:按月生成索引:test-YYYYMM,按天生成索引:test-YYYYMMDD;3.控制单个shard的size,正常情况下,日志场景,建议单个shard不大于50GB,线上业务场景,建议单个shard不超过20GB;</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">segment merge</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">段合并的计算量庞大, 而且还要吃掉大量磁盘 I/O。合并在后台定期操作,因为他们可能要很长时间才能完成,尤其是比较大的段。这个通常来说都没问题,因为大规模段合并的概率是很小的。如果发现merge占用了大量的资源,可以设置:<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">index.merge.scheduler.max_thread_count: 1</code> 特别是机械磁盘在并发 I/O 支持方面比较差,所以我们需要降低每个索引并发访问磁盘的线程数。这个设置允许 max_thread_count + 2 个线程同时进行磁盘操作,也就是设置为 1 允许三个线程。对于 SSD,你可以忽略这个设置,默认是 Math.min(3, Runtime.getRuntime().availableProcessors() / 2) ,对 SSD 来说运行的很好。业务低峰期通过force_merge强制合并segment,降低segment的数量,减小内存消耗;关闭冷索引,业务需要的时候再进行开启,如果一直不使用的索引,可以定期删除,或者备份到hadoop集群;</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">自动生成_id</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">当写入端使用特定的id将数据写入es时,es会去检查对应的index下是否存在相同的id,这个操作会随着文档数量的增加而消耗越来越大,所以如果业务上没有强需求,建议使用es自动生成的id,加快写入速率。</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">routing</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">对于数据量较大的业务查询场景,es侧一般会创建多个shard,并将shard分配到集群中的多个实例来分摊压力,正常情况下,一个查询会遍历查询所有的shard,然后将查询到的结果进行merge之后,再返回给查询端。此时,写入的时候设置routing,可以避免每次查询都遍历全量shard,而是查询的时候也指定对应的routingkey,这种情况下,es会只去查询对应的shard,可以大幅度降低合并数据和调度全量shard的开销。</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">使用alias</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">生产提供服务的索引,切记使用别名提供服务,而不是直接暴露索引名称,避免后续因为业务变更或者索引数据需要reindex等情况造成业务中断。</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">避免宽表</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">在索引中定义太多字段是一种可能导致映射爆炸的情况,这可能导致内存不足错误和难以恢复的情况,这个问题可能比预期更常见,<code style="font-size: inherit;line-height: inherit;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">index.mapping.total_fields.limit</code> ,默认值是1000</p> <h4 style="line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;font-weight: bold;font-size: 1.2em;color: rgb(0, 172, 193);"><span style="font-size: inherit;color: inherit;line-height: inherit;">避免稀疏索引</span></h4> <p style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 1.5em;margin-bottom: 1.5em;">因为索引稀疏之后,对应的相邻文档id的delta值会很大,lucene基于文档id做delta编码压缩导致压缩率降低,从而导致索引文件增大,同时,es的keyword,数组类型采用doc_values结构,每个文档都会占用一定的空间,即使字段是空值,所以稀疏索引会造成磁盘size增大,导致查询和写入效率降低。</p> </section> <p><br></p> <section data-mpa-template="t" mpa-from-tpl="t" style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;font-size: 16px;background-color: rgb(255, 255, 255);text-align: left;color: rgb(120, 114, 114);widows: 1;word-spacing: 2px;"> <section data-id="89429" mpa-from-tpl="t" style="border-width: 0px;border-style: none;border-color: initial;"> <section mpa-from-tpl="t" style="text-align: center;height: 50px;"> <section mpa-from-tpl="t" style="margin-right: auto;margin-left: auto;width: 48px;height: 48px;color: rgb(255, 255, 255);line-height: 48px;display: inline-block;background-image: url("https://mmbiz.qpic.cn/mmbiz_gif/kiaqiahsnxHd4Zt4378tqib1DnnfKYvZAI7sUNZCYmGN2HCMDFDYV5hLu6HrHIK5BynAAwgHGiafFFU7ibYez6mXL4w/640?wx_fmt=gif");background-repeat: no-repeat;background-size: 100%;background-position: 0px center;"> <p style="font-weight: bold;"><span style="font-size: 14px;">end</span></p> </section> <section mpa-from-tpl="t" style="margin-top: -26px;height: 2px;background-color: rgb(34, 33, 33);"> <br> </section> </section> </section> </section> <p style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;font-size: 16px;background-color: rgb(255, 255, 255);text-align: left;color: rgb(120, 114, 114);widows: 1;word-spacing: 2px;"><br></p> <section mpa-from-tpl="t" style="padding-left: 5px;letter-spacing: 0.544px;white-space: normal;color: rgb(62, 62, 62);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: medium;background-color: rgb(255, 255, 255);text-align: left;widows: 1;word-spacing: 2px;border-left: 5px solid rgb(51, 51, 51);line-height: 1.5em;"> <section powered-by="xiumi.us" mpa-from-tpl="t"> <section mpa-from-tpl="t"> <section mpa-from-tpl="t" style="font-size: 18px;"> <p><span style="font-size: 16px;">推荐阅读:</span></p> </section> </section> </section> </section> <ul class="list-paddingleft-2" mpa-from-tpl="t" style="letter-spacing: 0.544px;white-space: normal;color: rgb(62, 62, 62);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: medium;background-color: rgb(255, 255, 255);text-align: left;widows: 1;word-spacing: 2px;"> <li style="font-size: 14px;"><p style="margin-top: 5px;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA3ODIxNjYxNQ==&mid=2247491445&idx=1&sn=1ee24183530f36382423111d05de706a&chksm=9f4773bda830faabd33c804a833275ffa33824bf7c99338392d2a1aaa73ea0c0d3626083ded6&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">如何保证缓存与数据库的双写一致性?</a><br></p></li> <li style="font-size: 14px;"><p style="margin-top: 5px;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA3ODIxNjYxNQ==&mid=2247491207&idx=1&sn=243f42629345a5a6121a7fbc594d9625&chksm=9f47724fa830fb5929049327c3c5fc72f72bdf202706582a0791433bf2223209e6e798b69791&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">亿级数据从 MySQL 到 Hbase 的三种同步方案与实践!</a><br></p></li> <li style="font-size: 14px;"><p style="margin-top: 5px;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA3ODIxNjYxNQ==&mid=2247491193&idx=1&sn=780ee5479d8eec22dbd3f573589ae80e&chksm=9f4772b1a830fba79ca6e136e45ed5e8bba822934747ef09d8c3ffe2fc8fb80116ee717b5111&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">终于有人把 Docker 讲清楚了,别再说不会 Docker 了!</a><br></p></li> <li style="font-size: 14px;"><p style="margin-top: 5px;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA3ODIxNjYxNQ==&mid=2247489178&idx=1&sn=7f7f730d290e8d09f1d83af674655f41&chksm=9f477a52a830f344a6e9cd916706c5bb9e88064baba787345feb076d70fd7a9fbfb16873fa9a&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">前后端分离模式下的权限设计方案</a></p></li> <li style="font-size: 14px;"><p style="margin-top: 5px;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzA3ODIxNjYxNQ==&mid=2247489451&idx=1&sn=ce18154070fc3d868750bfca5dd6e5ba&chksm=9f477b63a830f275f4c3d1ddee3b352e67348069f142ecb3e1cf479c13054869f3a7e405cc5b&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2" hasload="1" style="color: var(--weui-LINK);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;">究竟什么是图数据库,它有哪些应用场景?</a></p></li> </ul> <p style="margin-top: 5px;letter-spacing: 0.544px;white-space: normal;color: rgb(62, 62, 62);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;background-color: rgb(255, 255, 255);text-align: left;widows: 1;word-spacing: 2px;"><br></p> <p style="margin-top: 5px;letter-spacing: 0.544px;white-space: normal;color: rgb(62, 62, 62);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;background-color: rgb(255, 255, 255);widows: 1;word-spacing: 2px;text-align: right;"><img class="rich_pages" data-ratio="0.5555555555555556" data-s="300,640" data-type="jpeg" data-w="900" src="/upload/9d786e52e671b1ce4beb338d1e0d1815.jpg" style="letter-spacing: 0.544px;text-align: center;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;box-sizing: border-box !important;visibility: visible !important;width: 677px !important;"><span style="letter-spacing: 0.544px;font-size: 14px;">如有收获,点个在看,诚挚感谢</span><img data-ratio="1" data-type="png" data-w="19" width="19px" src="/upload/5f7454d24a334e968c32b8ce9ae53c5.png" style="font-size: 16px;letter-spacing: 0.544px;display: inline-block;vertical-align: text-bottom;box-sizing: border-box !important;visibility: visible !important;width: 19px !important;"></p>
作者:微信小助手
<section style="display: none;" data-tools="新媒体管家" data-label="powered by xmt.cn" data-mpa-powered-by="yiban.io"> <br> </section> <p style="text-align: center;"><span style="font-size: 13px;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;">点击上方蓝色“</span><span style="font-size: 13px;font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;color: rgb(0, 128, 255);">程序猿DD</span><span style="font-size: 13px;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;">”,选择“设为星标”</span><strong style="max-width: 100%;letter-spacing: 0.544px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 16px;white-space: pre-line;background-color: rgb(255, 255, 255);text-align: right;box-sizing: border-box !important;overflow-wrap: break-word !important;"></strong></p> <p style="max-width: 100%;min-height: 1em;white-space: normal;text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;font-size: 13px;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;">回复“</span><span style="max-width: 100%;font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;color: rgb(0, 128, 255);overflow-wrap: break-word !important;box-sizing: border-box !important;">资源</span><span style="max-width: 100%;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;">”获取独家整理的学习资料!</span></span></p> <p style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;min-height: 1em;white-space: normal;text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;color: rgb(136, 136, 136);font-family: Georgia, "Times New Roman", Times, "Songti SC", serif;font-size: 14px;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;"><img data-backh="34" data-backw="540" data-ratio="0.0625" data-s="300,640" data-type="jpeg" data-w="640" width="100%" src="/upload/8c292e55ba5a23cb6ebc11f2a2c4fece.jpg" style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;overflow-wrap: break-word !important;box-sizing: border-box !important;visibility: visible !important;width: 654px !important;"></span></p> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">gitee开源地址</h1> <blockquote data-tool="mdnice编辑器" style="margin-top: 20px;margin-bottom: 20px;padding-top: 10px;padding-right: 10px;padding-bottom: 10px;border-width: initial;border-style: none;border-color: initial;color: rgb(136, 136, 136);font-size: 0.9em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;overflow: auto;background: rgba(0, 0, 0, 0.05);line-height: 1.8;"> <span style="color: rgb(85, 85, 85);font-size: 4em;font-family: Arial, serif;line-height: 1em;font-weight: 700;">“</span> <p style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px;display: inline;">https://gitee.com/admin_yu/yx-image-recognition</p> <span style="white-space: pre-line;color: rgb(178, 178, 178);font-family: Optima-Regular, PingFangTC-light;font-size: 13px;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);"></span> </blockquote> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">介绍</h1> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> spring boot + maven 实现的车牌识别及训练系统 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 基于java语言的深度学习项目,在整个开源社区来说都相对较少;而基于java语言实现车牌识别EasyPR-Java项目,最后的更新已经是五年以前。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 本人参考了EasyPR原版C++项目、以及fan-wenjie的EasyPR-Java项目;同时查阅了部分opencv官方4.0.1版本C++的源码,结合个人对java语言理解,整理出当前项目 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 这是一个入门级教程项目,本人目前也正在学习图片识别相关技术;大牛请绕路 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 当前项目在原有EasyPR项目基础上,增加了绿牌识别功能,只不过当前的训练库文件包含绿牌的样本太少,还需要重新增加绿牌样本的训练,后续会逐步上传 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 当前已经添加基于svm算法的车牌检测训练、以及基于ann算法的车牌号码识别训练功能 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 后续会逐步加入证件识别、人脸识别等功能 </section></li> </ul> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">包含功能</h1> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 黄 蓝 绿 黄蓝绿车牌检测及车牌号码识别 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 单张图片、多张图片并发、单图片多车牌检测及识别 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 图片车牌检测训练 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 图片文字识别训练 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 包含两种依赖包的实现方式:基于org.bytedeco.javacpp包的实现方式;基于org.opencv官方包的实现方式 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> org.opencv官方包,提供了java语言api;java项目可以通过build path方式或者环境变量的方式引用; </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> org.bytedeco.javacpp包,JavaCPP是一个开源库,它提供了在 Java 中高效访问本地 C++的方法;在pom中引入坐标依赖即可 </section></li> </ul> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">软件版本</h1> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> jdk 1.8.61+ </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> maven 3.0+ </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> opencv 4.0.1 ;javacpp1.4.4;opencv-platform 4.0.1-1.4.4 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> spring boot 2.1.5.RELEASE </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> yx-image-recognition 1.0.0版本 </section></li> </ul> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">软件架构</h1> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> B/S 架构,前端html + requireJS,后端java </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 数据库使用 sqlite3.0 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 接口文档使用swagger 2.0 </section></li> </ul> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">操作界面</h1> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.6620370370370371" data-type="png" data-w="1080" src="/upload/55748e3c2adaee62b9c8126bb6875f48.png" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 677px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">车牌图片来源于网络,仅用于交流学习,不得用于商业用途;如有侵权,请联系本人删除</p> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">车牌检测过程</h1> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">高斯模糊:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.75" data-type="jpeg" data-w="400" src="/upload/7273fd9a7ffa92597af4977769de179d.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 400px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">图像灰度化:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.75" data-type="jpeg" data-w="400" src="/upload/dd4e122a4134627e0e051a78e96f001f.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 400px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">Sobel 算子:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.75" data-type="jpeg" data-w="400" src="/upload/a2ca39bce0883965803a94fc3bb5ab25.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 400px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">图像二值化:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.75" data-type="jpeg" data-w="400" src="/upload/54277f61a74ef43f47f7d43f5be85fc7.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 400px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">图像闭操作:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.75" data-type="jpeg" data-w="400" src="/upload/31b2c05ee42b07d161b8b46309cb8c57.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 400px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">二值图像降噪:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.75" data-type="jpeg" data-w="400" src="/upload/62d09e54f44b73ac61f817b0b6d7b454.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 400px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">提取外部轮廓:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.75" data-type="jpeg" data-w="400" src="/upload/b2cca039153503b6db5df6c27f1b690b.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 400px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">外部轮廓筛选:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.75" data-type="jpeg" data-w="400" src="/upload/b2be0e7ee72ece5bd1c1e3a3dfb92329.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 400px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">切图:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.22672064777327935" data-type="png" data-w="247" src="/upload/a6b65b28a1377f120742ba42541ba1fc.png" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 247px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">重置切图尺寸:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.12873563218390804" data-type="png" data-w="435" src="/upload/373fe0450df7110178b36d5739ab070a.png" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 435px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">车牌检测结果:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.2647058823529412" data-type="png" data-w="136" src="/upload/8a694632f572417b132b26c948d89917.png" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 136px !important;visibility: visible !important;"> </figure> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">图片车牌文字识别过程</h1> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">debug_char_threshold:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.2647058823529412" data-type="jpeg" data-w="136" src="/upload/23da66f8403eeb5183a6d4232ff02b35.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 136px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">debug_char_clearLiuDing:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.2647058823529412" data-type="jpeg" data-w="136" src="/upload/62e3b4ab87b2d95583d37bd92a603b3b.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 136px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">debug_specMat:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="1.6923076923076923" data-type="jpeg" data-w="13" src="/upload/82ea5e6ecdb16fca228de674245105dd.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 13px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">debug_chineseMat:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="1.5714285714285714" data-type="jpeg" data-w="14" src="/upload/c8c52a0ae65ec2de6128aee36ca25e89.jpg" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 14px !important;visibility: visible !important;"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(89, 89, 89);">debug_char_auxRoi:</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img data-ratio="0.17647058823529413" data-type="png" data-w="204" src="/upload/810ba7d5b93d978e9e9f210c6d4fd005.png" style="margin-right: auto;margin-left: auto;display: block;box-sizing: border-box !important;width: 204px !important;visibility: visible !important;"> </figure> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">安装教程</h1> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 开发环境搭建:./doc/01_开发环境搭建.md </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 将项目拉取到本地,PlateDetect文件夹拷贝到d盘下,默认车牌识别操作均在d:/PlateDetect/目录下处理 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 需要修改操作路径,修改com/yuxue/constant/Constant.java文件常量参数即可,可以使用绝对盘符路径,也可以使用项目相对路径 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> spring boot方式运行项目,浏览器上输入 http://localhost:16666/index 即可打开操作界面 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 浏览器上输入 http://localhost:16666/swagger-ui.html 即可打开接口文档页面 </section></li> </ul> <h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 24px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(89, 89, 89);">参考文档</h1> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> liuruoze/EasyPR:https://gitee.com/easypr/EasyPR </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> fan-wenjie/EasyPR-Java:https://github.com/fan-wenjie/EasyPR-Java </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> opencv官方:https://opencv.org/ </section></li> </ul> <p style="max-width: 100%;min-height: 1em;overflow-wrap: break-word !important;box-sizing: border-box !important;"><br></p> <section data-recommend-type="list-title" data-recommend-tid="6" data-mpa-template="t" style="width: 100%;display: flex;justify-content: center;align-items: center;" data-mid="" data-from="yb-recommend"> <section style="width: 100%;padding: 14px;background: rgb(255, 255, 255);border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(232, 232, 235);" data-mid=""> <section style="width: 100%;display: flex;justify-content: center;align-items: center;align-items: flex-end;" data-mid=""> <section data-mid="" style="height: 28px;padding: 4px 22px;font-size: 14px;font-weight: 500;color: rgb(19, 52, 86);line-height: 20px;background-image: url("https://mmbiz.qpic.cn/mmbiz_png/sUbvrqLicbpzB81mjeBxPuxnYdalGxNnJo30L2Hq3WwGficcq8w5YJkLeXnsNHocN53k55TfN5mBpCdicGRyfDg1g/640?wx_fmt=png");background-repeat: no-repeat;background-size: 100% 100%;margin-bottom: -14px;z-index: 10;"> <p data-mid="">往期推荐</p> </section> </section> <section style="width: 100%;border-width: 1px;border-style: solid;border-color: rgb(198, 226, 255);padding: 17px 16px 9px;" data-mid=""> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="6" data-recommend-article-id="2247500723_2" data-recommend-article-time="1599093060" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/R3InYSAIZkFBv6eQtC4EpPoeHrdZM69DovV5y7M5q89bcF4UW1MV8E4b3TaT4r9vhrIdCWmwZQfXMqvUT8r1QQ/0?wx_fmt=jpeg" data-recommend-article-title="求职屡次被虐?务必拿走这300道面试宝藏题!" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500723&idx=2&sn=53942ae8070ad11629edab7fadf5f92d&chksm=9bd36e2baca4e73dab9afaf6c70aa6792f4beadd6850000382d7f8b71c1c9060ec5722f427b9#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500723&idx=2&sn=53942ae8070ad11629edab7fadf5f92d&chksm=9bd36e2baca4e73dab9afaf6c70aa6792f4beadd6850000382d7f8b71c1c9060ec5722f427b9&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" data-recommend-content="t" style="width: 100%;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;border-bottom: 1px dashed #c6e2ff;padding: 6px;font-size: 13px;font-weight: 400;color: #2c5f95;line-height: 18px;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">求职屡次被虐?务必拿走这300道面试宝藏题!</p> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="6" data-recommend-article-id="2247500723_3" data-recommend-article-time="1599093060" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/R3InYSAIZkHcpbhfkICKtP9YE1935tHtpPyZscFibhictGXkHQsmYiagI1sUpuQUOPKvv4V4ib9JhBJtOmsUHoOhFg/0?wx_fmt=jpeg" data-recommend-article-title="线下实操:搭建微服务天气预报应用!" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500723&idx=3&sn=277338451e3a9686953e1a6c626ee6c9&chksm=9bd36e2baca4e73df2aefa0d961e55dc48e6fecc4b15e00dd55fba82dc3a0682ef71f5d7e469#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500723&idx=3&sn=277338451e3a9686953e1a6c626ee6c9&chksm=9bd36e2baca4e73df2aefa0d961e55dc48e6fecc4b15e00dd55fba82dc3a0682ef71f5d7e469&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" data-recommend-content="t" style="width: 100%;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;border-bottom: 1px dashed #c6e2ff;padding: 6px;font-size: 13px;font-weight: 400;color: #2c5f95;line-height: 18px;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">线下实操:搭建微服务天气预报应用!</p> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="6" data-recommend-article-id="2247500723_4" data-recommend-article-time="1599093060" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/R3InYSAIZkHcpbhfkICKtP9YE1935tHtLKVOEYgNcE82ftGH1aUTD32r4MWeLoiblqwX8BdgojqmhIqp4tL6McQ/0?wx_fmt=jpeg" data-recommend-article-title="分布式事务、分布式锁、分布式session" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500723&idx=4&sn=87789a42ac4b207723307d8be87fe665&chksm=9bd36e2baca4e73d5a77af5d4909dc3dbc9ac0c3bcdc6ffa4cd21865cc186f84fafb04b706b5#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500723&idx=4&sn=87789a42ac4b207723307d8be87fe665&chksm=9bd36e2baca4e73d5a77af5d4909dc3dbc9ac0c3bcdc6ffa4cd21865cc186f84fafb04b706b5&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" data-recommend-content="t" style="width: 100%;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;border-bottom: 1px dashed #c6e2ff;padding: 6px;font-size: 13px;font-weight: 400;color: #2c5f95;line-height: 18px;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">分布式事务、分布式锁、分布式session</p> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="6" data-recommend-article-id="2247500655_3" data-recommend-article-time="1599006660" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/R3InYSAIZkFBv6eQtC4EpPoeHrdZM69DP6iaAGqTNJApzxxqpGl7iaXDChpjfezgBZakkgWmJyq3nAnCcuVkW27A/0?wx_fmt=jpeg" data-recommend-article-title="天天都会写接口,但它的用途和好处有多少人能说得清楚?" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500655&idx=3&sn=e427caef249276c80f4a3ed8e5f74905&chksm=9bd36ef7aca4e7e17ae0c3499093214690e567178785b99acb9e37a385076003b2549ddbbaf6#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500655&idx=3&sn=e427caef249276c80f4a3ed8e5f74905&chksm=9bd36ef7aca4e7e17ae0c3499093214690e567178785b99acb9e37a385076003b2549ddbbaf6&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" data-recommend-content="t" style="width: 100%;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;border-bottom: 1px dashed #c6e2ff;padding: 6px;font-size: 13px;font-weight: 400;color: #2c5f95;line-height: 18px;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">天天都会写接口,但它的用途和好处有多少人能说得清楚?</p> </section></a> </section> <section data-mpa-template="t" data-recommend-article-type="list-title" data-recomment-template-id="6" data-recommend-article-id="2247500524_1" data-recommend-article-time="1598920260" data-recommend-article-cover="http://mmbiz.qpic.cn/mmbiz_jpg/R3InYSAIZkHdlg23ib1C8gC6AEPtA1elU6AcNMJNAFNrqWmWdPTYJZ1q4RUiaRhoAN5wEdByLbTk7MafbhkRQMMw/0?wx_fmt=jpeg" data-recommend-article-title="卧槽!竟然可以直接白嫖 Github Action 的 2C7G 服务器!" data-recommend-article-content-url="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500524&idx=1&sn=fdaf9590ca0f1ad8ab5b869e2a757f25&chksm=9bd36f74aca4e662f3cba571b0529f9b21436ee908bf468ce50373295dcf6ad821506491dc98#rd"> <a href="http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247500524&idx=1&sn=fdaf9590ca0f1ad8ab5b869e2a757f25&chksm=9bd36f74aca4e662f3cba571b0529f9b21436ee908bf468ce50373295dcf6ad821506491dc98&scene=21#wechat_redirect" data-linktype="2"> <section data-recommend-title="t" data-recommend-content="t" style="width: 100%;display: flex;justify-content: center;align-items: center;flex-wrap: nowrap;border-bottom: 1px dashed #c6e2ff;padding: 6px;font-size: 13px;font-weight: 400;color: #2c5f95;line-height: 18px;border-bottom:none !important;" data-mid=""> <p style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 100%;" data-mid="">卧槽!竟然可以直接白嫖 Github Action 的 2C7G 服务器!</p> </section></a> </section> </section> </section> </section> <p><span style="color: rgb(62, 62, 62);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 2px;text-align: left;word-spacing: 2px;"></span><br></p> <p style="margin-bottom: 10px;max-width: 100%;min-height: 1em;white-space: normal;text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;color: rgb(255, 104, 39);font-size: 14px;overflow-wrap: break-word !important;box-sizing: border-box !important;"><strong style="max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">离职后与大家在星球聊了很多,</strong><strong style="max-width: 100%;color: rgb(0, 128, 255);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;">你不来看看?</strong></span></p> <p style="max-width: 100%;min-height: 1em;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><img data-type="png" data-ratio="0.5453333333333333" data-w="750" src="/upload/f7f418ec5d0483ff3a935bc09553a1ea.png" style="overflow-wrap: break-word !important;box-sizing: border-box !important;visibility: visible !important;width: 677px !important;"></p> <p style="max-width: 100%;min-height: 1em;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;font-size: 14px;overflow-wrap: break-word !important;box-sizing: border-box !important;">我的星球是否适合你?</span></p> <p style="max-width: 100%;min-height: 1em;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;font-size: 14px;overflow-wrap: break-word !important;box-sizing: border-box !important;">点击<strong style="max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max-width: 100%;color: rgb(255, 104, 39);overflow-wrap: break-word !important;box-sizing: border-box !important;">阅读原文</span></strong>看看我们都聊过啥?</span></p>
作者:微信小助手
<section style="display: none;" data-tools="新媒体管家" data-label="powered by xmt.cn"> <br> </section> <p style="text-align: center;"><span style="font-size: 14px;letter-spacing: 0.5440000295639038px;text-align: center;max-width: 100%;color: rgb(255, 41, 65);line-height: 22.4px;">(给</span><span style="font-size: 14px;letter-spacing: 0.5440000295639038px;text-align: center;max-width: 100%;line-height: 22.4px;color: rgb(0, 128, 255);">ImportNew</span><span style="font-size: 14px;letter-spacing: 0.5440000295639038px;text-align: center;max-width: 100%;color: rgb(255, 41, 65);line-height: 22.4px;">加星标,提高Java技能)</span></p> <blockquote> <p style="letter-spacing: 0.5440000295639038px;white-space: normal;background-color: rgb(255, 255, 255);max-width: 100%;min-height: 1em;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">转自:霁云HYY</span></p> <p style="letter-spacing: 0.5440000295639038px;white-space: normal;background-color: rgb(255, 255, 255);max-width: 100%;min-height: 1em;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">链接:blog.csdn.net/LO_YUN/article/details/103949317</span></p> </blockquote> <p><span style="font-size: 15px;background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 0.544px;text-align: left;">既然在项目中使用了MQ,那么就不可避免的需要考虑消息丢失问题。</span><span style="font-size: 15px;background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 0.544px;text-align: left;">在一些涉及到了金钱交易的场景下,消息丢失还是很致命的。</span><span style="font-size: 15px;background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 0.544px;text-align: left;">那么在RocketMQ中存在哪几种消息丢失的场景呢?</span></p> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">先来一张最简单的消费流程图:</span> </section> <p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.37076648841354726" data-s="300,640" src="/upload/3c6f32f1492e408ca9e1c55502049505.png" data-type="png" data-w="561" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 561px !important;visibility: visible !important;"></p> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">上图中大致包含了这么几种场景:</span> </section> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);overflow-wrap: break-word !important;"> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">生产者产生消息发送给RocketMQ</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">RocketMQ接收到了消息之后,必然需要存到磁盘中,否则断电或宕机之后会造成数据的丢失</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">消费者从RocketMQ中获取消息消费,消费成功之后,整个流程结束</span> </section></li> </ul> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">这三种场景都可能会产生消息的丢失,如下图所示:</span> </section> <p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.6053042121684867" data-s="300,640" src="/upload/15cc9a8f6c474d6a687a8afe4e201ff9.png" data-type="png" data-w="641" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 641px !important;visibility: visible !important;"></p> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">1、场景1中生产者将消息发送给Rocket MQ的时候,如果出现了网络抖动或者通信异常等问题,消息就有可能会丢失</span> </section> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">2、场景2中消息需要持久化到磁盘中,这时会有两种情况导致消息丢失</span> </section> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);overflow-wrap: break-word !important;"> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">RocketMQ为了减少磁盘的IO,会先将消息写入到os cache中,而不是直接写入到磁盘中,消费者从os cache中获取消息类似于直接从内存中获取消息,速度更快,过一段时间会由os线程异步的将消息刷入磁盘中,此时才算真正完成了消息的持久化。在这个过程中,如果消息还没有完成异步刷盘,RocketMQ中的Broker宕机的话,就会导致消息丢失</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">如果消息已经被刷入了磁盘中,但是数据没有做任何备份,一旦磁盘损坏,那么消息也会丢失</span> </section></li> </ul> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">3、消费者成功从RocketMQ中获取到了消息,还没有将消息完全消费完的时候,就通知RocketMQ我已经将消息消费了,然后消费者宕机,但是RocketMQ认为消费者已经成功消费了数据,所以数据依旧丢失了。</span> </section> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">那么如何保证消息的零丢失呢?</span> </section> <p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.463302752293578" data-s="300,640" src="/upload/408393fb08462f6c56dc0e3538a560fb.png" data-type="png" data-w="654" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 654px !important;visibility: visible !important;"></p> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">1、场景1中保证消息不丢失的方案是使用RocketMQ自带的事务机制来发送消息,大致流程为:</span> </section> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);overflow-wrap: break-word !important;"> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">首先生产者发送half消息到RocketMQ中,此时消费者是无法消费half消息的,若half消息就发送失败了,则执行相应的回滚逻辑</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">half消息发送成功之后,且RocketMQ返回成功响应,则执行生产者的核心链路</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">如果生产者自己的核心链路执行失败,则回滚,并通知RocketMQ删除half消息</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">如果生产者的核心链路执行成功,则通知RocketMQ commit half消息,让消费者可以消费这条数据</span> </section></li> </ul> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">其中还有一些RocketMQ长时间没有收到生产者是要commit/rollback操作的响应,回调生产者接口的细节,感兴趣的可以参考:</span> </section> <blockquote data-tool="mdnice编辑器" style="margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;border-left-color: rgb(239, 112, 96);color: rgb(106, 115, 125);font-size: 0.9em;max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;overflow: auto;background: rgb(255, 249, 249);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-size: 16px;color: black;line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">https://blog.csdn.net/LO_YUN/article/details/101673893</span></p> </blockquote> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">在使用了RocketMQ事务将生产者的消息成功发送给RocketMQ,就可以保证在这个阶段消息不会丢失</span> </section> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">2、在场景2中要保证消息不丢失,首先需要将os cache的异步刷盘策略改为同步刷盘,这一步需要修改Broker的配置文件,将flushDiskType改为SYNC_FLUSH同步刷盘策略,默认的是ASYNC_FLUSH异步刷盘。</span> </section> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">一旦同步刷盘返回成功,那么就一定保证消息已经持久化到磁盘中了;为了保证磁盘损坏不会丢失数据,我们需要对RocketMQ采用主从机构,集群部署,Leader中的数据在多个Follower中都存有备份,防止单点故障。</span> </section> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">3、在场景3中,消息到达了消费者,RocketMQ在代码中就能保证消息不会丢失<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span> </section> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;letter-spacing: 0.544px;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code style="padding: 15px 16px 16px;max-width: 100%;overflow-x: auto;color: rgb(221, 221, 221);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(39, 40, 34);border-radius: 5px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;"><span style="max-width: 100%;color: rgb(117, 113, 94);line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">//注册消息监听器处理消息</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">consumer.registerMessageListener(<span style="max-width: 100%;color: rgb(249, 38, 114);font-weight: bold;line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">new</span> MessageListenerConcurrently() {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(117, 113, 94);line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">@Override</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(249, 38, 114);font-weight: bold;line-height: 26px;">public</span> ConsumeConcurrentlyStatus <span style="max-width: 100%;color: rgb(166, 226, 46);font-weight: bold;line-height: 26px;">consumeMessage</span><span style="max-width: 100%;line-height: 26px;">(List<MessageExt> msgs, ConsumeConcurrentlyContext context)</span></span>{ <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(117, 113, 94);line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">//对消息进行处理</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(249, 38, 114);font-weight: bold;line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">return</span> ConsumeConcurrentlyStatus.CONSUME_SUCCESS;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">});<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></code></pre> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">上面这段代码中,RocketMQ在消费者中注册了一个监听器,当消费者获取到了消息,就会去回调这个监听器函数,去处理里面的消息</span> </section> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">当你的消息处理完毕之后,才会返回ConsumeConcurrentlyStatus.CONSUME_SUCCESS 只有返回了CONSUME_SUCCESS,消费者才会告诉RocketMQ我已经消费完了,此时如果消费者宕机,消息已经处理完了,也就不会丢失消息了</span> </section> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">如果消费者还没有返回CONSUME_SUCCESS时就宕机了,那么RocketMQ就会认为你这个消费者节点挂掉了,会自动故障转移,将消息交给消费者组的其他消费者去消费这个消息,保证消息不会丢失</span> </section> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">为了保证消息不会丢失,在consumeMessage方法中就直接写消息消费的业务逻辑就可以了,如果非要搞一些骚操作,比如下面的代码</span> </section> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;letter-spacing: 0.544px;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code style="padding: 15px 16px 16px;max-width: 100%;overflow-x: auto;color: rgb(221, 221, 221);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(39, 40, 34);border-radius: 5px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;"><span style="max-width: 100%;color: rgb(117, 113, 94);line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">//注册消息监听器处理消息</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">consumer.registerMessageListener(<span style="max-width: 100%;color: rgb(249, 38, 114);font-weight: bold;line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">new</span> MessageListenerConcurrently() {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(117, 113, 94);line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">@Override</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(249, 38, 114);font-weight: bold;line-height: 26px;">public</span> ConsumeConcurrentlyStatus <span style="max-width: 100%;color: rgb(166, 226, 46);font-weight: bold;line-height: 26px;">consumeMessage</span><span style="max-width: 100%;line-height: 26px;">(List<MessageExt> msgs, ConsumeConcurrentlyContext context)</span></span>{ <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(117, 113, 94);line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">//开启子线程异步处理消息</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(249, 38, 114);font-weight: bold;line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">new</span> Thread() {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(249, 38, 114);font-weight: bold;line-height: 26px;">public</span> <span style="max-width: 100%;color: rgb(249, 38, 114);font-weight: bold;line-height: 26px;">void</span> <span style="max-width: 100%;color: rgb(166, 226, 46);font-weight: bold;line-height: 26px;">run</span><span style="max-width: 100%;line-height: 26px;">()</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(117, 113, 94);line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">//对消息进行处理</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }.start(); <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="max-width: 100%;color: rgb(249, 38, 114);font-weight: bold;line-height: 26px;box-sizing: border-box !important;overflow-wrap: break-word !important;">return</span> ConsumeConcurrentlyStatus.CONSUME_SUCCESS;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">});<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></code></pre> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">如果新开子线程异步处理消息的话,就有可能出现消息还没有被消费完,消费者告诉RocketMQ消息已经被消费了,结果宕机丢失消息的情况。</span> </section> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">使用上面一整套的方案就可以在使用RocketMQ时保证消息零丢失,但是性能和吞吐量也将大幅下降</span> </section> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);overflow-wrap: break-word !important;"> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">使用事务机制传输消息,会比普通的消息传输多出很多步骤,耗费性能</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">同步刷盘相比异步刷盘,一个是存储在磁盘中,一个存储在内存中,速度完全不是一个数量级</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">主从机构的话,需要Leader将数据同步给Follower</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;color: rgb(1, 1, 1);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">消费时无法异步消费,只能等待消费完成再通知RocketMQ消费完成</span> </section></li> </ul> <section style="padding-top: 8px;padding-bottom: 8px;max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);line-height: 2em;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">消息零丢失是一把双刃剑,要想用好,还是要视具体的业务场景而定,选择合适的方案才是最好的。</span> </section> <section donone="shifuMouseDownCard('shifu_c_030')" label="Copyright Reserved by PLAYHUDONG." style="text-align: start;white-space: normal;margin-top: 1em;margin-bottom: 1em;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);border-width: 0px;border-style: initial;border-color: initial;"> <section style="margin-left: 1em;line-height: 1.4;"> <span style="padding: 3px 8px;border-top-left-radius: 4px;border-top-right-radius: 4px;border-bottom-right-radius: 4px;border-bottom-left-radius: 4px;color: rgb(255, 255, 255);background-color: rgb(255, 105, 31);font-family: inherit;text-align: inherit;text-decoration: inherit;font-size: 16px;">推荐阅读</span> <span style="margin-left: 4px;padding: 3px 8px;border-top-left-radius: 1.2em;border-top-right-radius: 1.2em;border-bottom-right-radius: 1.2em;border-bottom-left-radius: 1.2em;color: rgb(255, 255, 255);line-height: 1.2;background-color: rgb(204, 204, 204);font-family: inherit;text-align: inherit;text-decoration: inherit;border-color: rgb(249, 110, 87);font-size: 12px;">点击标题可跳转</span> </section> <section style="margin-top: -11px;padding: 22px 16px 16px;border-width: 1px;border-style: solid;border-color: rgb(255, 105, 31);color: rgb(51, 51, 51);font-size: 1em;font-family: inherit;text-align: inherit;text-decoration: inherit;"> <p><span style="font-size: 12px;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=2651489469&idx=2&sn=a8e696040b6a51d989b9b02d68cf055c&chksm=bd25e8c28a5261d4ad63b7d902bedf4ac630e325914a95964765363ae57b8a39f44b10a1ded5&scene=21#wechat_redirect" data-itemshowtype="11" tab="innerlink" data-linktype="2">Kafka和RocketMQ底层存储之那些你不知道的事</a><br></span></p> <p><span style="font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 12px;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=2651488457&idx=1&sn=0abdcd370c3d24e34758a738243eb348&chksm=bd25ecb68a5265a07b2fe1721aad3c464117365ec227b2d8e96a9b8ed7337a64a2d65444a9e0&scene=21#wechat_redirect" data-itemshowtype="11" tab="innerlink" data-linktype="2">面试官:你对Kafka比较熟?那说说kafka日志段如何读写的吧?</a><br></span></p> <p><span style="font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 12px;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=2651487456&idx=1&sn=d91242b06395894870b641826b5535f0&chksm=bd25109f8a529989d92ff075c2b0f6612d30721b8b3a1dca5c5393c1f8ddc88a6f205ee39366&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2">Kafka 消息丢失与消费精确一次性</a><br></span></p> </section> </section> <p style="caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);text-align: start;white-space: normal;"><br></p> <p style="white-space: normal;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;color: rgb(255, 169, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">看完本文有收获?请转发分享给更多人</span></p> <p style="white-space: normal;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;color: rgb(255, 169, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">关注「ImportNew」,提升Java技能</strong></p> <p style="white-space: normal;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img data-ratio="0.9166666666666666" data-s="300,640" src="/upload/899866149276fa5fddb73c61ae04be64.jpg" data-type="jpeg" data-w="600" style="box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;width: 600px !important;" width="auto"></p> <p style="text-align: right;"><span style="font-size: 14px;text-align: right;">好文章,我</span><span style="font-size: 14px;text-align: right;color: rgb(255, 41, 65);">在看</span><span style="font-size: 14px;text-align: right;">❤️</span></p>
作者:微信小助手
<p style="text-align: center;"><span style="font-size: 14px;letter-spacing: 0.5440000295639038px;text-align: center;max-width: 100%;color: rgb(255, 41, 65);line-height: 22.4px;">(给</span><span style="font-size: 14px;letter-spacing: 0.5440000295639038px;text-align: center;max-width: 100%;line-height: 22.4px;color: rgb(0, 128, 255);">ImportNew</span><span style="font-size: 14px;letter-spacing: 0.5440000295639038px;text-align: center;max-width: 100%;color: rgb(255, 41, 65);line-height: 22.4px;">加星标,提高Java技能)</span></p> <blockquote> <p style="letter-spacing: 0.5440000295639038px;white-space: normal;background-color: rgb(255, 255, 255);max-width: 100%;min-height: 1em;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">转自:小姐姐味道</span></p> </blockquote> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">目前</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">HTTP</span></code><span style="font-size: 15px;">协议,乃至</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">WebSocket</span></code><span style="font-size: 15px;">协议,乃至采用了</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">MQTT</span></code><span style="font-size: 15px;">协议的</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">WebSocket</span></code><span style="font-size: 15px;">协议,都不可避免的使用了</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">Nginx</span></code><span style="font-size: 15px;">。所谓病从口入,祸从口出。作为入口,Nginx承担的责任非常的重要。假如某个时刻不能用了,那可真是灾难。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">如何保证</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">Nginx</span></code><span style="font-size: 15px;">的高可用呢?这是个问题。不论你用什么样的方案,到最后总是要归为单一,很让人苦恼。</span></p> <p style="max-width: 100%;min-height: 1em;color: rgb(63, 63, 63);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.4020926756352765" data-s="300,640" data-type="png" data-w="669" src="/upload/f3790462fde552b8f226200c153e0efa.png" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 669px !important;visibility: visible !important;"></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">所谓的高可用,无非两种方式。一种方式就是在组件自身上做文章,另外一种方式,就是加入一个中间层。我们通常希望在高可用的时候,同时还能够负载均衡,典型的猫和狗都想要,贪婪的很。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">每当解决不了问题的时候,我们都会加入一个中间层,然后把希望寄托在这个新生的组件上。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">如果这个中间层解决不了问题,我们就可以加入另外一个中间层。就这样一层套一层,到最后系统高可用架构就会变得非常复杂。</span></p> <h2 data-tool="mdnice编辑器"><br></h2> <h2 data-tool="mdnice编辑器"><span style="color: rgb(123, 12, 0);font-size: 15px;"><strong>DNS保证高可用</strong></span></h2> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">第一种方式当然是要在DNS上做文章了。通过在DNS上,绑定多个Nginx的IP地址,即可完成高可用。不仅能够完成高可用,还能顺便完成负载均衡。</span></p> <p style="max-width: 100%;min-height: 1em;color: rgb(63, 63, 63);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.4020926756352765" data-s="300,640" data-type="png" data-w="669" src="/upload/54dfd6c48f188f08ed66770361f0d4b8.png" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 669px !important;visibility: visible !important;"></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">但这玩意有一个致命的问题,那就是故障的感知时间。<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">我们的浏览器在访问到真正的Nginx之前,需要把域名转化为真正的IP地址,DNS就是干</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">解析</span></code><span style="font-size: 15px;">这个动作的,每次需要耗费20-20ms不等。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">为了加快解析速度,一般都会有多级的缓存。比如浏览器就有DNS的缓存;你使用的PC机上也有这样的缓存;IPS服务提供商,也会有缓存;再加上有的企业为了加速访问所自建的DNS服务器,中间的缓存层就更多了。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">只有所有的缓存都不命中的情况下,DNS才会查询真正的IP地址。所以,如果有一台Nginx当机了,这个故障的感知能力就会特别的差。总有一部分用户的请求,会落在这台已经死亡的机器上。</span></p> <h2 data-tool="mdnice编辑器"><span style="color: rgb(123, 12, 0);font-size: 15px;"><strong><br></strong></span></h2> <h2 data-tool="mdnice编辑器"><span style="color: rgb(123, 12, 0);font-size: 15px;"><strong>硬件保证高可用</strong></span></h2> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">我们前面说了。解决不了的问题,就可以加中间层,即使这个中间层是硬件,比如</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">F5</span></code><span style="font-size: 15px;">。</span></p> <p style="max-width: 100%;min-height: 1em;color: rgb(63, 63, 63);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.44095665171898357" data-s="300,640" data-type="png" data-w="669" src="/upload/8d47087527ea6d5535cc495b535df4b5.png" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 669px !important;visibility: visible !important;"></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;max-width: 100%;letter-spacing: 0.034em;box-sizing: border-box !important;overflow-wrap: break-word !important;">这种架构一般的企业玩不起,只有那些采购有回扣有油水的公司,才会喜欢这个。互联网中用的很少,就不过多介绍了。<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">当然,F5同样有单点的问题。虽然硬件肯定要比软件稳定上一点,但是总归是一个隐患。就像Oracle无论再厉害,它还是有出问题的时候,到时候备机是必须的。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">有的厂商在卖硬件的时候,推荐你一次买3个!为啥呢?这也有理由。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">你的一台硬件正在服务,有两台备份机器。当你服务的这台机器出现问题时,就可以选取备份机中的其中一台作为主机,另一台依然是备机,集群还是高可用的。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">这理由真让人陶醉。按照这个逻辑,碰到傻子,我可以卖出100台!</span></p> <h2 data-tool="mdnice编辑器"><br></h2> <h2 data-tool="mdnice编辑器"><span style="color: rgb(123, 12, 0);font-size: 15px;"><strong>主备模式</strong></span></h2> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">硬的不行,就要来软的。采用主备的模式,使用软件来完成切换过程。</span></p> <p style="max-width: 100%;min-height: 1em;color: rgb(63, 63, 63);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.44095665171898357" data-s="300,640" data-type="png" data-w="669" src="/upload/4250f54124369e6343fd4299138608ec.png" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 669px !important;visibility: visible !important;"></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.034em;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如图,使用</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;letter-spacing: 0.034em;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">keepalived</span></code><span style="max-width: 100%;letter-spacing: 0.034em;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">组件,通过</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;letter-spacing: 0.034em;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">VRRP</span></code><span style="font-size: 15px;max-width: 100%;letter-spacing: 0.034em;box-sizing: border-box !important;overflow-wrap: break-word !important;">协议,即可完成最简单的高可用配置。<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">我们把</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">DNS</span></code><span style="font-size: 15px;">的地址绑定在</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">VIP</span></code><span style="font-size: 15px;">上,当正在服务的</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">Nginx</span></code><span style="font-size: 15px;">发生问题,</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">VIP</span></code><span style="font-size: 15px;">会发生</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">漂移</span></code><span style="font-size: 15px;">,转移到另外一台</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">Nginx</span></code><span style="font-size: 15px;">上。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">可以看到,备份的Nginx,正常情况下是无法进行服务的,它也叫做</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">影子节点</span></code><span style="font-size: 15px;">,只有主Nginx发生问题的时候才有用。如果你的节点非常多,这种模式下,会有非常大的浪费。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">除了浪费,还有一个非常大的问题。那就是,单台Nginx无论性能多么牛X,总是有上限的。当网卡的流量达到顶峰,接下来何去何从呢?</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">这种模式肯定是不满足需求的。</span></p> <h2 data-tool="mdnice编辑器"><br></h2> <h2 data-tool="mdnice编辑器"><span style="color: rgb(123, 12, 0);font-size: 15px;"><strong>简单组合模式</strong></span></h2> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">这个时候,我们就可以配合DNS解析,以及主备模式做文章了。如下图,DNS解析到两个VIP上,VIP本身也做了高可用。这样就能够缩短故障时间,同时也能够保证每个组件的高可用。</span></p> <p style="max-width: 100%;min-height: 1em;color: rgb(63, 63, 63);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="1.1853512705530642" data-s="300,640" data-type="png" data-w="669" src="/upload/86884c39e23faf85a871ae632728fcac.png" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 669px !important;visibility: visible !important;"></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">这种架构模式思路是非常清晰的,但依然存在影子节点的浪费。</span></p> <h2 data-tool="mdnice编辑器"><br></h2> <h2 data-tool="mdnice编辑器"><span style="color: rgb(123, 12, 0);font-size: 15px;"><strong>LVS+KeepAlived+Nginx</strong></span></h2> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">LVS 是 Linux Virtual Server 的简称,也就是 Linux 虚拟服务器。现在 LVS 已经是 Linux 标准内核的一部分,从 Linux2.4 内核以后,已经完全内置了 LVS 的各个功能模块,无需给内核打任何补丁,可以直接使用 LVS 提供的各种功能。</span></p> <p style="max-width: 100%;min-height: 1em;color: rgb(63, 63, 63);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.5692041522491349" data-s="300,640" data-type="png" src="/upload/c49c6da841727895d0889a88baf47506.png" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 657px !important;visibility: visible !important;" data-w=""></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">LVS工作在OSI模型的第4层:传输层,比如TCP/UDP,所以像7层网络的HTTP协议,它是识别不出来的。也就是说,我们不能拿HTTP协议的一些内容来控制路由,它的路由切入层次更低一些。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">如下图,LVS 架设的服务器集群系统有三个部分组成:</span></p> <ul data-tool="mdnice编辑器" class="list-paddingleft-2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;max-width: 100%;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: black;overflow-wrap: break-word !important;"> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;line-height: 26px;color: rgb(1, 1, 1);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">最前端的负载均衡层,用 Load Balancer 表示</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;line-height: 26px;color: rgb(1, 1, 1);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">中间的服务器集群层,用 Server Array 表示</span> </section></li> <li style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 15px;"> <section style="margin-top: 5px;margin-bottom: 5px;max-width: 100%;line-height: 26px;color: rgb(1, 1, 1);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;">最底端的数据共享存储层,用 Shared Storage 表示</span> </section></li> </ul> <p style="max-width: 100%;min-height: 1em;color: rgb(63, 63, 63);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.6313432835820896" data-s="300,640" data-type="png" data-w="670" src="/upload/90fb256e2716316472257ba78580719e.png" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 670px !important;visibility: visible !important;"></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">DR(直接路由)模式可将响应数据包直接返回给用户浏览器,避免负载均衡服务器网卡带宽成为瓶颈,是目前采用最为广泛的方式(数据不详,fullnat模式使用也比较广泛)。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">所以,配合DNS的负载均衡,加上LVS的负载均衡,可以实现双层的负载均衡和高可用。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">如图,DNS可以将请求绑定在VIP上。由于LVS DR模式的效率非常高,网卡要达到瓶颈也需要非常大的请求量(只有入口流量才走LVS),所以一般通过LVS做nginx的负载均衡就足够了。如果LVS还有瓶颈,那么就可以在DNS上再做文章。</span></p> <p style="max-width: 100%;min-height: 1em;color: rgb(63, 63, 63);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-ratio="0.6149253731343284" data-s="300,640" data-type="png" data-w="670" src="/upload/35db0ba01f8217c14e4d08ff7100079f.png" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 670px !important;visibility: visible !important;"></p> <h2 data-tool="mdnice编辑器"><br></h2> <h2 data-tool="mdnice编辑器"><span style="color: rgb(123, 12, 0);font-size: 15px;"><strong>还有哪些挑战?</strong></span></h2> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">其实,我们上面谈到的这些方案,大多数是在同机房的。如果在多个机房,如何让用户选择最快的节点、如何保证负载均衡,又是一个大的问题。另外,你可以看到数据包经过层层的转发和协调,还有多种负载均衡算法参与其中,如何保持会话,也是一个挑战。一般的,四层会话会通过IP地址去实现,七层会话会通过cookie或者头信息等去实现。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">开发人员一般情况下接触不到这么入口级的东西,但一旦遇到了,可能会受忙脚乱。本文是</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">xjjdog</span></code><span style="font-size: 15px;">根据一些即有的经验进行整理,希望你在公司需要一些高可用</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">方案</span></code><span style="font-size: 15px;">的时候,能够助你一臂之力。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 1em;padding-bottom: 8px;max-width: 100%;min-height: 1em;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;letter-spacing: 0.544px;text-align: left;white-space: normal;background-color: rgb(255, 255, 255);color: rgb(74, 74, 74);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 15px;">什么叫</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">方案</span></code><span style="font-size: 15px;">?你只需要 <strong style="max-width: 100%;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;">当时</strong> 把你的领导哄好,让他感觉很认同的样子就行了。至于要不要做,具体怎么做,那都是后面的事。君不见,扯了这么半天,很多企业其实一个</span><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(40, 202, 113);box-sizing: border-box !important;"><span style="font-size: 15px;">nginx</span></code><span style="font-size: 15px;">,就可以走天下。</span></p> <p><span style="font-size: 15px;text-align: left;"><br></span></p> <section donone="shifuMouseDownCard('shifu_c_030')" label="Copyright Reserved by PLAYHUDONG." style="text-align: start;white-space: normal;margin-top: 1em;margin-bottom: 1em;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);border-width: 0px;border-style: initial;border-color: initial;"> <section style="margin-left: 1em;line-height: 1.4;"> <span style="padding: 3px 8px;border-top-left-radius: 4px;border-top-right-radius: 4px;border-bottom-right-radius: 4px;border-bottom-left-radius: 4px;color: rgb(255, 255, 255);background-color: rgb(255, 105, 31);font-family: inherit;text-align: inherit;text-decoration: inherit;font-size: 16px;">推荐阅读</span> <span style="margin-left: 4px;padding: 3px 8px;border-top-left-radius: 1.2em;border-top-right-radius: 1.2em;border-bottom-right-radius: 1.2em;border-bottom-left-radius: 1.2em;color: rgb(255, 255, 255);line-height: 1.2;background-color: rgb(204, 204, 204);font-family: inherit;text-align: inherit;text-decoration: inherit;border-color: rgb(249, 110, 87);font-size: 12px;">点击标题可跳转</span> </section> <section style="margin-top: -11px;padding: 22px 16px 16px;border-width: 1px;border-style: solid;border-color: rgb(255, 105, 31);color: rgb(51, 51, 51);font-size: 1em;font-family: inherit;text-align: inherit;text-decoration: inherit;"> <p><span style="font-size: 12px;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=2651483191&idx=1&sn=fa01226837e5312736b980a807f5c098&chksm=bd2500488a52895ee41179555f5de08b982b0ba509c07e30c0fcecd48d8931798a563c913a2f&scene=21#wechat_redirect" data-itemshowtype="0" tab="innerlink" data-linktype="2">究竟啥才是互联网架构“高可用”</a><br></span></p> <p><span style="font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 12px;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=2651485955&idx=1&sn=008deedef2822bfa34aa9d33a68607f6&chksm=bd251b7c8a52926aa5ed17db2e6ac918a81b75af471e84b07f8577031d3e62bcb17395628cc8&scene=21#wechat_redirect" data-itemshowtype="11" tab="innerlink" data-linktype="2">为什么基础服务不应该高可用</a></span></p> </section> </section> <p style="caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);text-align: start;white-space: normal;"><br></p> <p style="white-space: normal;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;color: rgb(255, 169, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">看完本文有收获?请转发分享给更多人</span></p> <p style="white-space: normal;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;color: rgb(255, 169, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">关注「ImportNew」,提升Java技能</strong></p> <p style="white-space: normal;max-width: 100%;min-height: 1em;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img data-ratio="0.9166666666666666" data-s="300,640" data-type="jpeg" data-w="600" width="auto" src="/upload/899866149276fa5fddb73c61ae04be64.jpg" style="box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;width: 600px !important;"></p> <p style="text-align: right;"><span style="font-size: 14px;text-align: right;">好文章,我</span><span style="font-size: 14px;text-align: right;color: rgb(255, 41, 65);">在看</span><span style="font-size: 14px;text-align: right;">❤️</span></p> <p><br></p>