作者:微信小助手
<section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;" data-mpa-powered-by="yiban.io"> <section style="margin-bottom: -1.4em;box-sizing: border-box;"> <section style="box-sizing:border-box;max-width: 0.62em !important;width: 0.62em;height: 0.62em;border-left-width: 1px;border-left-style: solid;border-left-color: #0075a8;border-top-width: 1px;border-top-style: solid;border-top-color: #0075a8;"> <br> </section> <section style="box-sizing: border-box;margin-top: -0.31em;margin-left: 0.31em;width: 2.5em;height: 1.4em;border-left: 1px solid rgb(0, 117, 168);border-top: 1px solid rgb(0, 117, 168);max-width: 2.5em !important;"> <section> <svg viewbox="0 0 1 1" style="box-sizing:border-box;max-width:0 !important;float:left;line-height:0;width:0;vertical-align:top;"></svg> </section> </section> </section> <section style="padding-right: 0.31em;padding-left: 0.31em;box-sizing: border-box;"> <section style="box-sizing:border-box;padding: 10px;display: inline-block;vertical-align: top;width: 568.09375px;"> <section style="text-align: left;box-sizing: border-box;"> <p style="vertical-align:inherit;box-sizing: border-box;"><span style="color: rgb(0, 117, 168);box-sizing: border-box;font-size: 14px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">作者简介</span></p> <p style="text-align: justify;line-height: 150%;"><span style="color: rgb(117, 117, 117);font-size: 14px;caret-color: red;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;">万绍远,CNCF 基金会官方认证 Kubernetes CKA&CKS 工程师,云原生解决方案架构师。对 ceph、Openstack、Kubernetes、prometheus 技术和其他云原生相关技术有较深入的研究。参与设计并实施过多个金融、保险、制造业等多个行业 IaaS 和 PaaS 平台设计和应用云原生改造指导。</span><span style="color: rgb(117, 117, 117);font-size: 14px;caret-color: red;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;"></span></p> </section> </section> </section> <section style="box-sizing: border-box;margin-top: -1.4em;margin-left: auto;width: 2.5em;max-width: 2.5em !important;"> <section style="box-sizing: border-box;margin-bottom: -0.31em;margin-left: -0.31em;width: 2.5em;height: 1.4em;border-right: 1px solid rgb(0, 117, 168);border-bottom: 1px solid rgb(0, 117, 168);max-width: 2.5em !important;"> <section> <svg viewbox="0 0 1 1" style="box-sizing:border-box;max-width:0 !important;float:left;line-height:0;width:0;vertical-align:top;"></svg> </section> </section> </section> <section style="box-sizing:border-box;max-width: 0.62em !important;margin-left: auto;width: 0.62em;height: 0.62em;border-right-width: 1px;border-right-style: solid;border-right-color: #0075a8;border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #0075a8;"> <br> </section> </section> <p><br></p> <table width="863" style="width: 768px;"> <thead style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box;"> <tr style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box;"> <th style="margin: 0px;padding: 8px;border-width: 1px;border-style: solid;border-color: rgb(217, 217, 217);background: rgb(247, 247, 247);max-width: 100%;box-sizing: border-box;text-align: inherit;line-height: 20px;vertical-align: middle;font-weight: 700;overflow-wrap: break-word !important;"><span style="font-size: 15px;">软件</span></th> <th style="margin: 0px;padding: 8px;border-width: 1px;border-style: solid;border-color: rgb(217, 217, 217);background: rgb(247, 247, 247);max-width: 100%;box-sizing: border-box;text-align: inherit;line-height: 20px;vertical-align: middle;font-weight: 700;overflow-wrap: break-word !important;"><span style="font-size: 15px;">版本</span></th> </tr> </thead> <tbody style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box;"> <tr style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box;"> <td style="margin: 0px;padding: 8px;border-width: 1px;border-style: solid;border-color: rgb(217, 217, 217);max-width: 100%;box-sizing: border-box;line-height: 20px;vertical-align: middle;overflow-wrap: break-word !important;"><span style="font-size: 15px;">Rancher</span></td> <td style="margin: 0px;padding: 8px;border-width: 1px;border-style: solid;border-color: rgb(217, 217, 217);max-width: 100%;box-sizing: border-box;line-height: 20px;vertical-align: middle;overflow-wrap: break-word !important;"><span style="font-size: 15px;">2.6.4</span></td> </tr> <tr style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box;background-color: rgba(181, 181, 181, 0.1);"> <td style="margin: 0px;padding: 8px;border-width: 1px;border-style: solid;border-color: rgb(217, 217, 217);max-width: 100%;box-sizing: border-box;line-height: 20px;vertical-align: middle;overflow-wrap: break-word !important;"><span style="font-size: 15px;">Kubernetes</span></td> <td style="margin: 0px;padding: 8px;border-width: 1px;border-style: solid;border-color: rgb(217, 217, 217);max-width: 100%;box-sizing: border-box;line-height: 20px;vertical-align: middle;overflow-wrap: break-word !important;"><span style="font-size: 15px;">1.22.7+rke2r2</span></td> </tr> </tbody> </table> <h2 data-line="7" style="margin: 0px;padding: 0px;font-weight: 400;font-size: 16px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></h2> <p style="margin: 0px;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;font-size: 16px;text-align: center;line-height: 1.5em;"><strong style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;font-size: 15px;"><img class="rich_pages wxw-img" data-ratio="0.8814814814814815" data-s="300,640" src="/upload/8b182f7f5c6f0ec9e371c32cab57af9c.jpg" data-type="jpeg" data-w="1080" style="margin: 0px;padding: 0px;max-width: 100%;height: 37px;overflow-wrap: break-word !important;vertical-align: bottom;width: 42px;"></span></strong></p> <p style="margin: 0px;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;font-size: 16px;text-align: center;line-height: 1.5em;"><span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(0, 117, 168);"><strong style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">概 述</strong></span></p> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;">Rancher 2.6 监控启用方式与之前版本存在较大差异,属于原生的 Prometheus-Operator,通过抽象化一些 Kubernetes CRD 资源,可以更好地把监控告警功能整合起来,提高易用性。Prometheus-operator 包括以下 CRD 资源对象:</span> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br> </section> <ul class="list-paddingleft-1" style="margin: 0px;padding: 0px 0px 0px 1.2em;box-sizing: border-box !important;width: 577.422px;max-width: 100%;overflow-wrap: break-word !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;list-style-type: disc;"> <li style="margin: 0px;padding: 0px;clear: both;max-width: 100%;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;">PrometheusRules:定义告警规则</span> </section></li> <li style="margin: 0px;padding: 0px;clear: both;max-width: 100%;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;">Alert Managers:Altermanager 启动 CRD,用于 Altermanager 启动副本</span> </section></li> <li style="margin: 0px;padding: 0px;clear: both;max-width: 100%;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;">Receivers:配置告警接收媒介 CRD</span> </section></li> <li style="margin: 0px;padding: 0px;clear: both;max-width: 100%;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;">Routers:将告警规则和告警媒介进行匹配</span> </section></li> <li style="margin: 0px;padding: 0px;clear: both;max-width: 100%;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;">ServiceMonitor:定义 Prometheus 采集的监控指标地址</span> </section></li> <li style="margin: 0px;padding: 0px;clear: both;max-width: 100%;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;">Pod Monitor:更细粒化的对 POD 进行监控</span> <span style="margin: 0px;padding: 0px;max-width: 100%;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"></span> </section></li> </ul> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> </section> <p style="margin: 0px 0px 0em;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;text-align: center;"><img class="rich_pages wxw-img js_insertlocalimg" data-ratio="0.58203125" data-s="300,640" src="/upload/3a0a31b094fcb96a42c94f44b093b86f.png" data-type="png" data-w="1280" style="margin: 0px;padding: 0px;max-width: 100%;height: auto !important;overflow-wrap: break-word !important;vertical-align: bottom;"></p> <p style="margin: 0px;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p> <p style="margin: 0px;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;font-size: 16px;text-align: center;line-height: 1.5em;"><strong style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;font-size: 15px;"><img class="rich_pages wxw-img" data-ratio="0.8814814814814815" data-s="300,640" src="/upload/8b182f7f5c6f0ec9e371c32cab57af9c.jpg" data-type="jpeg" data-w="1080" style="margin: 0px;padding: 0px;max-width: 100%;height: 37px;overflow-wrap: break-word !important;vertical-align: bottom;width: 42px;"></span></strong></p> <p style="margin: 0px;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;font-size: 16px;text-align: center;line-height: 1.5em;"><span style="margin: 0px;padding: 0px;max-width: 100%;color: rgb(0, 117, 168);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;"><strong style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">配置使用</strong></span><span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(0, 117, 168);"><strong style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></strong></span></p> <p style="margin: 0px;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p> <h3 data-line="22" style="margin: 0px;padding: 0px;font-weight: 400;font-size: 16px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"><span style="margin: 0px;padding: 0px;max-width: 100%;color: rgb(0, 117, 168);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;"><strong style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="color: rgb(0, 117, 168);margin: 0px;padding: 0px;max-width: 100%;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;">启用监控</span></strong></span></h3> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;">切换到对应集群,选择左下角 clusterTools 启用 Prometheus:</span> <span style="margin: 0px;padding: 0px;max-width: 100%;font-size: 15px;overflow-wrap: break-word !important;box-sizing: border-box !important;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;"></span> </section> <p style="margin: 0px;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p> <p style="margin: 0px 0px 0em;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;text-align: center;"><img class="rich_pages wxw-img js_insertlocalimg" data-ratio="0.478125" data-s="300,640" src="/upload/868af76ea889279cd865ee76a334f410.png" data-type="png" data-w="1280" style="margin: 0px;padding: 0px;max-width: 100%;height: auto !important;overflow-wrap: break-word !important;vertical-align: bottom;"></p> <p style="margin: 0px;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;">部署到 System 项目中,勾选自定义 helm 参数:</span> <span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;font-size: 15px;"></span> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <img class="rich_pages wxw-img" data-ratio="0.48083989501312335" src="/upload/4b32bab565c97c170c9ea1f6785510b6.png" data-type="png" data-w="3810" style="margin: 0px;padding: 0px;max-width: 100%;height: auto;overflow-wrap: break-word !important;vertical-align: middle;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;width: auto;"> <br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box;"> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;">根据实际需求修改部署要求:</span> <span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;font-size: 15px;"></span> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <img class="rich_pages wxw-img" data-ratio="0.5168340224453633" src="/upload/4150c76483956a55a99c66cda263ee5e.png" data-type="png" data-w="3386" style="margin: 0px;padding: 0px;max-width: 100%;height: auto;overflow-wrap: break-word !important;vertical-align: middle;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;width: auto;"> <br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box;"> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <img class="rich_pages wxw-img" data-ratio="0.4932392710170488" src="/upload/2c21c75b173ff8de7ac529b56d64d22a.png" data-type="png" data-w="3402" style="margin: 0px;padding: 0px;max-width: 100%;height: auto;overflow-wrap: break-word !important;vertical-align: middle;box-sizing: border-box;border-width: 0px;border-style: initial;border-color: initial;width: auto;"> <br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box;"> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;">如果需要对接远端存储(如 infuxdb),就需要修改 yaml 配置,并配置指向 influxdb:</span> <span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;font-size: 15px;"></span> </section> <pre style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);font-size: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;"> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="javascript"><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">remoteRead:</span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> - url: http:<span class="code-snippet__comment" style="margin: 0px;padding: 0px;max-width: 100%;color: rgb(175, 175, 175);font-style: italic;overflow-wrap: break-word !important;box-sizing: border-box !important;">//192.168.0.7:8086/api/v1/prom/read?db=prometheusremoteWrite:</span></span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> - url: http:<span class="code-snippet__comment" style="margin: 0px;padding: 0px;max-width: 100%;color: rgb(175, 175, 175);font-style: italic;overflow-wrap: break-word !important;box-sizing: border-box !important;">//192.168.0.7:8086/api/v1/prom/write?db=prometheus</span></span></code></pre> </section></pre> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;">默认 node-exporter 资源 limit 配置较低,长时间运行后容易被 OOM KILL 掉,需要修改默认的内存限制为 150Mi:</span> <span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;font-size: 15px;"></span> </section> <pre style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);font-size: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;"> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="properties"><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <span class="code-snippet__attr" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">podLabels</span>:</span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <span class="code-snippet__attr" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">jobLabel</span>: <span class="code-snippet__string" style="margin: 0px;padding: 0px;max-width: 100%;color: rgb(221, 17, 68);overflow-wrap: break-word !important;box-sizing: border-box !important;">node-exporter</span></span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <span class="code-snippet__attr" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">resources</span>:</span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <span class="code-snippet__attr" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">limits</span>:</span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <span class="code-snippet__attr" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">cpu</span>: <span class="code-snippet__string" style="margin: 0px;padding: 0px;max-width: 100%;color: rgb(221, 17, 68);overflow-wrap: break-word !important;box-sizing: border-box !important;">200m</span></span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <span class="code-snippet__attr" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">memory</span>: <span class="code-snippet__string" style="margin: 0px;padding: 0px;max-width: 100%;color: rgb(221, 17, 68);overflow-wrap: break-word !important;box-sizing: border-box !important;">150Mi</span></span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <span class="code-snippet__attr" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">requests</span>:</span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <span class="code-snippet__attr" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">cpu</span>: <span class="code-snippet__string" style="margin: 0px;padding: 0px;max-width: 100%;color: rgb(221, 17, 68);overflow-wrap: break-word !important;box-sizing: border-box !important;">100m</span></span></code><code style="white-space:pre-wrap;margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;text-align: left;font-size: 14px;display: flex;font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"> <span class="code-snippet__attr" style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">memory</span>: <span class="code-snippet__string" style="margin: 0px;padding: 0px;max-width: 100%;color: rgb(221, 17, 68);overflow-wrap: break-word !important;box-sizing: border-box !important;">30Mi</span></span></code></pre> </section></pre> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <br> </section> <section style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;color: rgb(51, 51, 51);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: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;line-height: 1.5em;"> <span style="margin: 0px;padding: 0px;max-width: 100%;font-size: 15px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;">可以点击如�
作者:微信小助手
<p style="-webkit-print-color-adjust: exact;margin-right: 0px;margin-bottom: 15px;margin-left: 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;margin-top: 0px !important;">很多软件系统由于性能问题导致了失败,在开发生命周期和性能测试生命周期的每个阶段都存在导致性能失败的原因。有时候,性能问题是无法控制的,它不在项目经理、技术架构师或性能工程师的控制范围之内。从业务和个人层面来看,大多数的系统性能失败仅仅是因为性能工程师、开发人员、 DBA、业务团队和利益相关者之间从一开始就缺乏沟通,这导致了许多其他问题,这些问题将直接影响应用程序的性能和 ROI。对任何应用/产品进行有效性能测试的唯一目标是实现令人满意的投资回报。性能测试和软件工程是有风险的,并且总是需要从开发的早期阶段开始,进行大量的反复试验。</p> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;text-decoration: none;"><span style="background-color: rgb(255, 255, 255);">系统性能的失败必须与其他业务问题进行类似</span>的<span style="background-color: rgb(255, 255, 255);">处理。</span><span style="background-color: rgb(255, 255, 255);">了解问题出在哪里,为什么会出问题,以及如何预防。</span><span style="background-color: rgb(255, 255, 255);">在大多数场景中,需要每个人都了解/理解端到端全生命周期实现中的性能挑战。</span><span style="background-color: rgb(255, 255, 255);">他山之石,根据老码农的经验,总结了一个导致系统性能失败的原因列表。</span></p> <p style="text-align: center;margin-bottom: 0em;"><img class="rich_pages wxw-img" data-galleryid="" data-ratio="0.5625" data-s="300,640" data-type="jpeg" data-w="800" style="height: auto !important;" src="/upload/92290226bca907dc3dd7e04c6ea9c6ef.jpg"></p> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;text-decoration: none;"><span style="background-color: rgb(255, 255, 255);"></span></p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">1. 对最终用户反馈的置若罔闻</h2> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">作为最终用户,才会意识到的现有潜在性能问题。为了理解生产系统中现有的性能问题,需要从最终用户那里获得关于应用程序在不同的预期负载条件下如何运行的持续反馈。总是有很多用户在生产环境中使用某个功能,即使这一功能不能满足他们期望的性能,他们也不会质疑它,而且会假设它是正确的,当用户可以同时从多个位置访问时,这可能是一个大问题。因此,如果想提高应用程序的性能,就必须让最终用户参与进来,以获得关于应用程序或系统在生产环境中性能的持续反馈。当然,与最终用户的交互需要时间和精力,尽管如此,为了使产品/应用提供最佳性能,这绝对是值得的。</p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">2. 不关注性能目标</h2> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">设定目标是确定系统性能的最重要方面之一。许多团队经常无法实现他们的性能目标以求改进,于是花费了大量时间修复系统中现有的和隐藏的性能问题。性能测试的完美目标应该在最现实的条件下定义、设计和执行,比如真实的浏览器、设备和多个地理位置。确定正确的指标来监控,定义每个指标的最小阈值,执行性能测试来得到基线结果,所有这些数字对于确定什么样的变化可以创造性能改进是必要的。在软件开发生命周期中尽早开始性能测试是一种很好的做法,可以首先消除瓶颈,并确保在用户负载很重的情况下不断检查应用程序的性能。</p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">3. 不清晰及不完整的非功能性需求</h2> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">收集完整的非功能性需求比功能性需求更复杂,因为它们被视为第二类甚至第三类需求。因此,它们经常被误解和忽视,只有少数组织将非功能性需求作为一等公民。这会在系统架构/设计中导致严重的问题,经常导致项目崩溃和网站崩溃,使系统无法使用。在大多数情况下,非功能性需求文档不完整、不一致,或者在大多数不成功的项目中不存在。性能测试第一步是对应用程序/系统进行可行性分析,并创建一组明确的非功能性需求。一个可靠的非功能性需求文档将确定产品/应用具有最佳性能的所有标准。此外,还需要:</p> <ul style="-webkit-print-color-adjust: exact;margin: 15px 0px;padding-left: 30px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;orphans: auto;text-align: start;text-indent: 0px;text-transform: none;white-space: normal;widows: auto;word-spacing: 0px;-webkit-text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;" class="list-paddingleft-1"> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">为产品/应用和系统建立明确的性能目标和期望</p></li> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">必须让所有人(开发团队、QA、管理团队、 DBA、涉众和业务团队)达成一致。</p></li> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">在技术团队、项目团队和业务团队之间建立沟通,以了解生产环境中最终用户的实际性能问题</p></li> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">使用分析工具,获取生产流量统计数据,以创建合适的工作负载模型</p></li> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">在收集非功能性需求时,了解应用程序/产品的体系结构、设计、问题和现有的性能问题</p></li> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">非功能性需求应该从软件开发过程的开始并在整个生命周期中进行讨论; 如果应用程序是全新的,基线和基准测试对于性能测试是必要的。</p></li> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">获取所有涉及的内部和外部组件的完整信息,并了解它们如何通信(CDN、防火墙、 DNS、负载均衡器、服务器、网络和系统、缓存等)</p></li> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">了解应用程序内存占用和第三方体系结构限制</p></li> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">与涉众和业务团队交谈,以了解目标并确定什么是基本的、现有的遗留系统性能问题、平台约束和竞争对手。</p></li> <li style="-webkit-print-color-adjust: exact;margin: 0px;"><p style="text-align: justify;">有必要记录所有内容,并让业务和其他涉众一起参加会议,以确定现有的非功能性需求是否合适,并就定义的 SLA 达成一致。</p></li> </ul> <p style="text-align: center;margin-bottom: 0em;"><img class="rich_pages wxw-img" data-galleryid="" data-ratio="0.7475409836065574" data-s="300,640" data-type="jpeg" data-w="610" style="height: auto !important;" src="/upload/fce6f98af885464b6e5e4c0b491a0b75.jpg"></p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">4. 糟糕的架构设计</h2> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">最初,糟糕的架构设计只会导致一些小问题,这些问题在开始时会比较少,但是会逐渐累积起来。简单维护是一个挑战,在一个区域中的任何更改都会破坏应用程序的其他部分。如果在架构设计阶段作出了不恰当的决定,应用/系统可能会出现严重的性能下降,导致过多的网络延迟和其他问题。由于不了解明确定义的系统架构,在负载测试执行阶段会存在太多不确定性和复杂性的高风险,这可能会给性能测试和工程团队带来意想不到的性能问题。在软件开发生命周期的应用程序设计和开发阶段,由于性能方面的挑战,软件发布可能会推迟。</p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">5. 对技术依赖缺乏预见</h2> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">依赖关系是允许更多应用和功能组件之间的连接。特定的操作系统版本、应用服务器、数据库服务器或 Java 虚拟机、通用语言运行库和框架都是依赖关系的例子。然而,有些依赖关系更为复杂,比如由 Linux、 Java 中的各种包组成的依赖关系,以及 Python 和 Ruby 等脚本语言组成的依赖关系。理解每个技术在设计和基础设施方面对每个组件的依赖性,使用哪些技术,以及使用哪些框架和工具来开发应用程序,对于系统性能来说,以期望的结果完成性能测试是至关重要的。</p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">6.新功能的过度扩展</h2> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">过度的新功能扩展是软件开发人员经常遇到的一个主要障碍。处理这种情况的有效方法是定期举行面向用户体验的会议和讨论,每个团队成员都参与其中,以验证每个功能,并确保它有意义地解决了设定的问题。性能测试团队必须从规划发布的时间表开始,并且应该主动地公布需要的时间,以防在发布前的最后一分钟添加任何新功能。如果项目有一个固定的最后期限,就需要提前计划环境需求,以确保意外的环境延迟不会影响性能测试的进度。如果在最后一刻继续添加了新功能,交付的质量大概率会受到影响。最终,客户可能会拒绝最终的可交付成果,从而产生返工和额外资源短缺的情况。</p> <p style="text-align: center;margin-bottom: 0em;"><img class="rich_pages wxw-img" data-galleryid="" data-ratio="0.666015625" data-s="300,640" data-type="png" data-w="1024" style="height: auto !important;" src="/upload/3a1e544429c5945da2d627c1645fb6d5.png"></p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">7. 推崇好大喜功</h2> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">在性能测试执行的最初版本中,直接关注目标 SLA 以达到可接受的限制可能是不现实的。性能测试是一个迭代过程,需要大量持续的性能测试来识别和消除所有的性能瓶颈。需要花费额外的时间优化每一行代码和组件,以提高系统/应用程序的性能。在性能测试中,每个 SLA 和 KPI 都是必要的,并且只有通过持续的性能测试、代码分析、内存分析、性能工程、监控以及客户端和服务器端的调优才能获得所需的响应时间、吞吐量、网络延迟和资源利用率,这有时需要花费很长的时间。分析所有的性能结果和降低,并从用户级、操作系统级、系统级、网络级和服务器级使用适当的指标收集数据,对所有导致性能问题根本原因的分析是至关重要的。</p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">8. 容量规划的匮乏</h2> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">许多基础设施未能实施有效的规划,简单地说,容量规划过程并不简单直接。我们可以创建一个场景、添加流量、评估结果、解决性能问题,然后重复,直到满意为止,但是实际的问题往往伴随着糟糕的容量规划。糟糕的容量计划增加了性能缺失的可能性,风险会完全暴露,最终导致失败。所有这些都可以通过仔细的容量规划来适当解决。来自基础设施领域的系统工程师、来自数据库领域的DBA和来自应用程序开发领域的程序员是最需要参与有效容量规划过程的三类人。许多人有时会将容量管理与容量计划混淆,不能准确地预测和错误预测未来的工作负载。需要确保识别准确的资源需求(CPU、内存、磁盘空间和网络带宽) ,以支持当前和未来增加的工作负载,以满足业务需求并避免容量规划失败。使用正确的度量标准进行持续监控将帮助我们进行有效的容量规划,并且还有助于处理流量增加后未预料到的工作负载。</p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">9. 性能问题没有完全解决</h2> <p style="-webkit-print-color-adjust: exact;margin: 15px 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">当应用的用户量增加时,往往会看到更多的性能问题。随着时间的推移,系统中隐藏的性能问题和已知的性能问题是导致性能持续下降的主要原因。必须与项目中的每个团队成员讨论确定的每个瓶颈,以成功地确保客户 SLA 的性能。当涉及到性能问题时,每一秒都很重要,如果忽略现有的性能问题,系统将会变慢,甚至更糟。例如,某些服务可能会停止在严重超载的服务器上运行,从而使应用程序无法访问。找出监控数据,检查服务的健康状态,一般就能找出性能问题的常见原因。</p> <h2 style="-webkit-print-color-adjust: exact;margin: 20px 0px 10px;padding: 0px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;font-style: normal;font-variant-caps: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;">10. 方法论的缺失</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-right: 0px;margin-left: 0px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;font-style: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: normal;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;word-spacing: 0px;text-size-adjust: auto;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration: none;margin-bottom: 0px !important;">缺乏合适的方法来建立性能测试策略及其覆盖范围的话,会很难获得有效的性能测试结果。理解性能测试方法和过程将帮助团队中的每个工程师,特别是当性能问题出现时,为每个发生问题的瓶颈提供正确的修复。性能测试过程应该有良好的计划和定义,并且文档化。好的文档可以在开发人员、 DBA和QA之间建立有效的沟通。随着软件变得更加复杂和多元化,并且有越来越多的平台和位置需要测试,需要有一个强有力的性能测试方法,以确保正在开发的软件系统经过充分的性能测试,以确保它们满足特定的业务要求,并且能够在所有预期的负载条件和环境中高性能地运行。</p> <p style="text-align: justify;"><br></p> <p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;color: rgb(34, 34, 34);font-family: system-ui, -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: 0.544px;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">【关联阅读】</p> <ul class="list-paddingleft-1" style="margin: 0px;padding: 0px 0px 0px 1.5em;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;color: rgb(34, 34, 34);font-size: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: 0.544px;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);list-style-type: square;"> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658977232&idx=1&sn=46b4a559ccff7dbd261c668c31863754&chksm=80d358f6b7a4d1e0d45deed31fc6cd6fa67fb9f65dc88ae79f1aa95f70d5e453f813420002fe&scene=21#wechat_redirect" textvalue="系统性能设计的10个反模式" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" style="margin: 0px;padding: 0px;color: rgb(87, 107, 149);text-decoration: none;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: 0.544px;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);" data-linktype="2">系统性能设计的10个反模式</a><br></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658975476&idx=1&sn=6b912551bddce66214a80987042fe963&chksm=80d323d2b7a4aac476e2582c6521da4a0f089aba147874898b842e1d63d3c3ca43bd0850e019&scene=21#wechat_redirect" textvalue="没有被了解的API?一个老码农眼中的API世界" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">没有被了解的API?一个老码农眼中的API世界</a></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658972665&idx=1&sn=a3b8cc3538099530270910ae12267795&chksm=80d32edfb7a4a7c9ffcf631c8b788580ec07139b05c66954daea33ec5674018953da946a14c3&scene=21#wechat_redirect" textvalue="区块链性能提升:链上设计之道" linktype="text" imgurl="" imgdata="null" data-itemshowtype="11" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">区块链性能提升 —— 链上设计之道</a></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658977217&idx=1&sn=453a828b108364ed897209f939a6971e&chksm=80d358e7b7a4d1f11ab3d0af9be0c02ac4a28ab0d059773a2d5e049fc682988ea1549d2a745e&scene=21#wechat_redirect" textvalue="API设计中性能提升的10个建议" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);">API设计——性能提升的10个建议</a></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658977171&idx=1&sn=7227b19fac3bcc80d7fedbfced7ec5c2&chksm=80d358b5b7a4d1a3c4b797196805074b972e31964d628a8b17e49d4e98216730e05ecc59823e&scene=21#wechat_redirect" textvalue="熟悉而陌生——那些个系统抽象" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">熟悉而陌生——那些个系统抽象</a></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658976882&idx=1&sn=4795e73502372530b8b441875a7b6f01&chksm=80d35954b7a4d0420b03b65ce8261c76a8b6bf98f016b926218ddc337e87af7d3ba8aa4472bd&scene=21#wechat_redirect" textvalue="服务器性能监控的温故知新" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">服务器——性能监控的温故知新</a></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658977196&idx=1&sn=fe9aa8e7cd81f09d74a0ab13feecce5d&chksm=80d3588ab7a4d19c487f7e60a4f2b8d0dba9e3107b802e10450a3de1004eb8971f0fb9e7d068&scene=21#wechat_redirect" textvalue="性能:关键路径的延迟分析" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">性能——关键路径的延迟分析</a><br style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658972628&idx=1&sn=406445883c2a9c2eaaa2480eb2cded74&chksm=80d32ef2b7a4a7e46038d5b52a044487fabd8af6a6463d9a996c03d805119b0785286ff28459&scene=21#wechat_redirect" textvalue="IOT语义互操作性之API接口" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">IOT语义互操作性之API接口</a></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658974209&idx=1&sn=b1c59331b5923154324c81253099eaa9&chksm=80d32727b7a4ae31c1f71edd00fb5d8db83ab8194c5cf34b5b25ce16d20202e149003e0a4f12&scene=21#wechat_redirect" textvalue="智能音箱场景下的性能优化" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">智能音箱场景下的性能优化</a></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658976859&idx=1&sn=028f4bd7340b38b327a26442eb55d76b&chksm=80d3597db7a4d06bea3d151e5cacf4820f6ef2a83a24d1c54661ec7f00ba50d337cf53003a57&scene=21#wechat_redirect" textvalue="软件系统的多维性能模型" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;">软件系统的多维性能模型</a><br style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658975379&idx=1&sn=e931610ee8e2ef865518a177f91e4869&chksm=80d323b5b7a4aaa3cb87d890c4d4c2175c24e67a091fa911b002f9069299f147856532ff5c28&scene=21#wechat_redirect" textvalue="性能,10点系统性思考" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">性能,10点系统性思考</a><br style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-size: 14px;"><p style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;"><span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658976943&idx=1&sn=c189603ac170298ea3a6d0f527d00988&chksm=80d35989b7a4d09f69d0fb4a3a92cca7d56775c46573246b617259d4a45146629a5d689b7080&scene=21#wechat_redirect" textvalue="API的性能约定" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(87, 107, 149);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">API的性能约定</a></span></p></li> </ul> <p style="text-align: justify;"><br></p>
作者:微信小助手
<p style="-webkit-print-color-adjust: exact;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">从用户界面到应用程序,从驱动程序到操作系统的内核,几乎所有软件都存在着系统性能上的缺陷,许多看起来完全不同的性能问题实际上有着相同的根本原因。对于成功经验的抽象一般被称为软件模式或者设计模式,那么导致系统性能问题的行为方式和做法则可以称为性能设计的反模式。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">有些反模式的根源在于硬件问题,有些是开发或管理实践不佳的结果,还有一些只是常见的错误。这里列出了10个影响系统性能的反模式, 它们产生的原因是什么?如何发现以及如何避免呢?</p> <p style="text-align: center;margin-bottom: 0em;"><img class="rich_pages wxw-img" data-backh="532" data-backw="578" data-galleryid="" data-ratio="0.9196787148594378" data-s="300,640" src="/upload/8543978a3e36d66da04c06646264c14f.jpg" data-type="jpeg" data-w="747" style="text-align: center;white-space: normal;width: 100%;height: auto;"></p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">1. 项目结束时来修复性能</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">在软件项目的开发过程中,一个经常被忽视的领域就是性能的测量和评估。很多时候,团队都会争分夺秒地开发新特性并修复 bug,而性能工作则会被抛在脑后。人们常常无法制定性能目标或基准,而且开发人员第一次考虑性能则是在收到性能问题的报告之后。。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">这种反模式看起来很明显,但是许多项目总是在重蹈覆辙。如果团队不费心去建模或测量软件性能,或者等到项目接近尾声才开始,不太可能得到好的结果,即便成功也是偶然的。</p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">2.测量和比较的不当</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">选择一个基准和比较结果在软件项目开始阶段是一个简单的问题,但在持续的过程中会发生很多问题。例如,在软件系统基准测试上,性能指标比以前的版本降低不超过2% ,这就是一个典型的错误,这意味着系统性能会随着时间的推移而下降。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">那么,如何选择度量的基准呢? 一个好基准的特点是:</p> <ul style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;padding-left: 30px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: start;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);" class="list-paddingleft-1"> <li style="-webkit-print-color-adjust: exact;"><p style="text-align: justify;">可重复性,比较实验可以相对容易地进行,而且精确度合理。</p></li> <li style="-webkit-print-color-adjust: exact;"><p style="text-align: justify;">可观察性,如果表现不佳,开发者有一个可以开始发现的地方。</p></li> <li style="-webkit-print-color-adjust: exact;"><p style="text-align: justify;">可持续性,如果和竞品进行比较,维护以前版本的性能历史记录是一个有价值的帮助。</p></li> <li style="-webkit-print-color-adjust: exact;"><p style="text-align: justify;">易于表达,让每个人都能在简短的演示中理解比较的结果。</p></li> <li style="-webkit-print-color-adjust: exact;"><p style="text-align: justify;">真实性,使测量真实地反映了客户的体验。</p></li> <li style="-webkit-print-color-adjust: exact;"><p style="text-align: justify;">可执行性,所有开发人员能够迅速确定其更改的效果。</p></li> </ul> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">并不是所有被选中的基准都能满足所有这些标准,但避免选择不能真正代表用户体验的基准,并抵制为基准进行优化的诱惑。</p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">3. 对算法的厌恶</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">对于许多软件开发者来说,可能有着『算法恐惧症』。实际上,很多性能问题的根本原因是在于软件中所采用的算法,真正重大的改进都源于算法的改变,例如,性能提高1倍以上。算法选择的一个关键部分是手头有一个现实的基准或测量实践来支撑,而不是直觉或者所谓的最佳实践。这意味着执行性能最有效时间是在项目的早期阶段,这可能与通常发生的情况正好相反。在处理o (n2)算法时,所有的编译选项几乎是没啥用的。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">为了弥补硬件差异,根据运行环境做出明显不同的选择。捕捉这类问题的方法之一是,原始开发人员既要记录影响代码性能的外部性的假设,又要提供某种程序化的方法来验证这些假设。例如,在判断哈希算法的时候,跟踪哈希表上的最大哈希链长度以及哈希表总数,这样就可以轻松识别哈希函数的优劣。另一种方式是当假设被违背时强制一个报错,这可能不适合某些应用程序。软件重用是一个很好的目标,但是要注意不要违背在其开发过程中所做的假设。</p> <p style="text-align: center;margin-bottom: 0em;"><img class="rich_pages wxw-img" data-backh="280" data-backw="500" data-galleryid="" data-ratio="0.56" data-s="300,640" src="/upload/367a71f78b1ab70bab4faaba25a94563.jpg" data-type="jpeg" data-w="500" style="width: 100%;height: auto;"></p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">4. 递归的泛滥</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">如果你的应用程序正在做一些不需要或不值得欣赏的工作,比如多次重新绘制屏幕、过于频繁地计算统计数据等等,那么消除这种浪费就是性能提升的重要领域。对于应用程序来说,通常最重要的是程序的结束状态,而不是到达结束状态所需的一系列步骤。通常有一条捷径可以让我们更快地达到目标,这就像缩短赛道而不是加速赛车,除了正确的使用预测预取之外,软件中加快速度的唯一方法就是做得更少。</p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">5. 过早地进行低级别的优化</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">过早的优化可能对实际基准测试的性能产生负面影响,低层次的周期调整不是在最初的代码开发阶段。即便如此,也应该仔细记录进行调优的条件,以帮助其他人以后评估这些条件是否仍然有效。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">如前所述,最有效的软件性能工作侧重于算法,而不是低级别的细节,手工编码的汇编链表是愚蠢的事情。另外,这种低级别的优化不是一个好主意,对于需要在不同的系统或处理器集上良好运行的软件,这些技术往往需要每个平台的不同版本,从开发、可移植性和测试角度来看,这是一种痛苦而昂贵的方法。基于实际实验的结果,而不是直觉,直到确信这是一种提高性能的经济有效的方法。Donald Knuth 说过: “过早的优化是万恶之源。”</p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">6.关注表象而不是真正到问题</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">人们经常要求确定应用程序性能不佳的原因,通常认为一定有某种潜在bug导致了性能问题的出现。尽管这有时确实如此,但在大多数情况下,问题实际上出在应用程序本身,而且往往出在用户使用应用程序的方式上。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">应用程序顶层的每一行代码通常都会导致软件堆栈深处的大量工作,顶层的低效率会有一个很大的杠杆系数,放大了它们的影响。然而,由于缺乏观察应用程序行为的合适工具,工程师一般会尝试使用 trace 或诸如 iostat 等各种性能监视命令等低级工具来诊断性能问题,这常常会导致增加系统调用或 i/o 操作,而不是修改应用程序以减少正在进行的调用数量。例如,一个基于数据库的应用程序有性能问题,那么首先查看 SQL; 一旦调优了 SQL,数据库正确地编制了索引等,那么也许是时候查看磁盘利用率了。</p> <p style="text-align: center;margin-bottom: 0em;"><img class="rich_pages wxw-img" data-backh="310" data-backw="578" data-cropselx1="0" data-cropselx2="578" data-cropsely1="0" data-cropsely2="343" data-galleryid="" data-ratio="0.5365344467640919" data-s="300,640" src="/upload/d2f10cfa444ad67b78b4bc4ae6a8396d.jpg" data-type="jpeg" data-w="479" style="height: auto;text-align: center;white-space: normal;width: 100%;"></p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">7.线程数量过多<br></h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">一旦程序员熟悉了线程或者多个协程,一个更常见的错误是决定对每个连接的工作单元使用一个线程(或进程)。无可否认,这是一个简单的编程模型,任务状态可以方便地保存在线程堆栈中。这在小型或本地测试环境中工作得很好,一旦这个应用程序被部署到成千上万个缓慢的连接上时,就会发现这些成千上万的线程并没有真正很好地执行,因为这台机器现在有大量的 TLB 和来自所有这些堆栈的缓存压力。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">一个经验性的答案是将线程数量限制在一个更合理的数量(接近 cpu 数量) ,并使用工作堆模型和异步 i/o 针对要完成的任务多路传送。这些类型的应用程序架构更容易扩展,并且在重负载下表现得更优雅。毕竟,如果应用程序的净吞吐量开始下降,超过一定的负载水平,这种情况本身就是不稳定的。</p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">8. 非对称硬件利用率</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">一些处理器设计使用三级缓存来隐藏内存访问的延迟,多级TLB现在也变得越来越普遍。这些缓存和 TLB使用不同程度的关联方式来跨缓存分散应用程序的负载,但是这种技术常常被其他性能优化意外地阻碍。一个简单的例子是一个性能工具,它将一个共享库中的函数重新排序,将最常用的函数放在库的开头,这是一个显然合理的策略,可以减少 ITLB的错误率和分页次数。然而,当应用于许多共享库时,这将导致每个共享库的段开头被访问的频率远远高于其他部分。如果是64kb 的对齐方式,这意味着那些属于64kb 边界的 TLB 条目更为常用,有效地将 ITLB 的大小减少了8倍。另一个例子发生在数据库上,如果该数据库代码以 L2缓存大小的倍数分配了大块共享内存,通过在每个大块中使用类似的访问模式,显著降低了 L2缓存的命中率。热点检测还可能发生在物理内存上,如果一个内存区域优于另一个区域,则可能导致平均内存访问时间显著增加。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">检测和避免这一问题可能很困难,因为硬件计数器和工具往往缺乏直接观测这些影响的能力; 一旦检测到,如果没有应用程序可见的影响,则很难避免这种热点定位,通常需要有意识地将随机性注入分配模式、内存布局等。</p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">9. CPU之间无需交换缓存</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">在多处理器上,精心设计的硬件协议确保系统中只有一个缓存包含修改版本的内存; 多个缓存可能包含未修改的内存副本。当一个 CPU 试图写入当前位于另一个 CPU 缓存中的内存时,会发生缓存到缓存的传输,以便在 CPU 之间移动该缓存的所有权。在大型多处理器上,这可能需要大量的时间和可用带宽; 最小化这些传输的数量可以提高可测量性。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">一个经常看到的例子是一个简单的计数器,它受到读取锁的保护。锁被获取,计数器读取,锁被丢弃。这些锁除了毫无必要地降低可伸缩性之外,几乎什么都不做。经常影响性能的问题是锁的滥用。如果读取器锁的持有时间很短 ,那么我们最好只使用简单的互斥锁,当锁被长时间持有时,读写锁才可能是有意义的。</p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">检测缓存到缓存的传输或错误共享可能非常困难。一种技术是查看在执行时间概要文件中引用的内存和语句; 如果大多数加载没有错过缓存,这种方法就可以很好地工作。对于开发工具来说,需要与编译器和运行时数据收集进行仔细的集成才能正确地解决这个问题。</p> <p style="text-align: center;margin-bottom: 0em;"><br></p> <p style="text-align: center;margin-bottom: 0em;"><img class="rich_pages wxw-img" data-backh="324" data-backw="578" data-galleryid="" data-ratio="0.5614973262032086" data-s="300,640" src="/upload/e701090261068b0b487a7e736c7e8ba1.jpg" data-type="jpeg" data-w="748" style="text-align: center;white-space: normal;width: 100%;height: auto;"></p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">10. 没有针对常见的情况进行优化<br></h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;"><span style="background-color: rgb(255, 255, 255);">一般地,频繁的操作比不频繁的操作多出几个数量级,设计算法来利用这种不对称性可以产生显著的收益。一个简单的例子是使用哈希表锁,这提高了对表进行简单搜索、插入或删除的可伸缩性,同时</span>监督<span style="background-color: rgb(255, 255, 255);">需要访问整个表的操作,如调整大小。</span><span style="background-color: rgb(255, 255, 255);">一个复杂的示例是锁定内核中的 CPU ,当前 CPU 的锁定实现利用了读访问和写访问的巨大优势,线程只需防止自己的抢占,这只需要一个本地内存的</span>引<span style="background-color: rgb(255, 255, 255);">用。</span></p> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;margin-bottom: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">常见场景和用例才是性能优化的核心关注点,对于应用层的软件更是如此。</p> <h2 style="-webkit-print-color-adjust: exact;margin-top: 20px;margin-bottom: 10px;font-weight: bold;-webkit-font-smoothing: antialiased;cursor: text;font-size: 24px;border-bottom: 1px solid rgb(204, 204, 204);color: black;font-family: Helvetica, arial, sans-serif;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">小结</h2> <p style="-webkit-print-color-adjust: exact;margin-top: 15px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Helvetica, arial, sans-serif;font-size: 14px;text-align: justify;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);">这10个问题应该有助于我们研究系统的性能设计,至少能更快地认识到这些问题。尽管并非所有项目的性能都具有挑战性,但是避免这些反模式将使有限的资源更加有效。其核心思想是,在项目开始时在基准、算法和数据结构选择方面所做的性能工作将在以后带来巨大的好处。</p> <p style="text-align: justify;"><br></p> <p style="text-align: justify;">【关联阅读】</p> <ul class="list-paddingleft-1" style="padding-left: 1.5em;outline: 0px;max-width: 100%;color: rgb(34, 34, 34);font-family: -apple-system, system-ui, "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);list-style-type: square;overflow-wrap: break-word !important;"> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658975476&idx=1&sn=6b912551bddce66214a80987042fe963&chksm=80d323d2b7a4aac476e2582c6521da4a0f089aba147874898b842e1d63d3c3ca43bd0850e019&scene=21#wechat_redirect" textvalue="没有被了解的API?一个老码农眼中的API世界" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">没有被了解的API?一个老码农眼中的API世界</a></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658972665&idx=1&sn=a3b8cc3538099530270910ae12267795&chksm=80d32edfb7a4a7c9ffcf631c8b788580ec07139b05c66954daea33ec5674018953da946a14c3&scene=21#wechat_redirect" textvalue="区块链性能提升:链上设计之道" linktype="text" imgurl="" imgdata="null" data-itemshowtype="11" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">区块链性能提升 —— 链上设计之道</a></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658972665&idx=1&sn=a3b8cc3538099530270910ae12267795&chksm=80d32edfb7a4a7c9ffcf631c8b788580ec07139b05c66954daea33ec5674018953da946a14c3&scene=21#wechat_redirect" textvalue="区块链性能提升:链上设计之道" linktype="text" imgurl="" imgdata="null" data-itemshowtype="11" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></a><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658977217&idx=1&sn=453a828b108364ed897209f939a6971e&chksm=80d358e7b7a4d1f11ab3d0af9be0c02ac4a28ab0d059773a2d5e049fc682988ea1549d2a745e&scene=21#wechat_redirect" textvalue="API设计中性能提升的10个建议" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" style="font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);" data-linktype="2">API设计——性能提升的10个建议</a></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658977171&idx=1&sn=7227b19fac3bcc80d7fedbfced7ec5c2&chksm=80d358b5b7a4d1a3c4b797196805074b972e31964d628a8b17e49d4e98216730e05ecc59823e&scene=21#wechat_redirect" textvalue="熟悉而陌生——那些个系统抽象" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">熟悉而陌生——那些个系统抽象</a></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658976882&idx=1&sn=4795e73502372530b8b441875a7b6f01&chksm=80d35954b7a4d0420b03b65ce8261c76a8b6bf98f016b926218ddc337e87af7d3ba8aa4472bd&scene=21#wechat_redirect" textvalue="服务器性能监控的温故知新" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">服务器——性能监控的温故知新</a></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658977196&idx=1&sn=fe9aa8e7cd81f09d74a0ab13feecce5d&chksm=80d3588ab7a4d19c487f7e60a4f2b8d0dba9e3107b802e10450a3de1004eb8971f0fb9e7d068&scene=21#wechat_redirect" textvalue="性能:关键路径的延迟分析" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">性能——关键路径的延迟分析</a><br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658972628&idx=1&sn=406445883c2a9c2eaaa2480eb2cded74&chksm=80d32ef2b7a4a7e46038d5b52a044487fabd8af6a6463d9a996c03d805119b0785286ff28459&scene=21#wechat_redirect" textvalue="IOT语义互操作性之API接口" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">IOT语义互操作性之API接口</a></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658974209&idx=1&sn=b1c59331b5923154324c81253099eaa9&chksm=80d32727b7a4ae31c1f71edd00fb5d8db83ab8194c5cf34b5b25ce16d20202e149003e0a4f12&scene=21#wechat_redirect" textvalue="智能音箱场景下的性能优化" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">智能音箱场景下的性能优化</a></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658976859&idx=1&sn=028f4bd7340b38b327a26442eb55d76b&chksm=80d3597db7a4d06bea3d151e5cacf4820f6ef2a83a24d1c54661ec7f00ba50d337cf53003a57&scene=21#wechat_redirect" textvalue="软件系统的多维性能模型" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;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;overflow-wrap: break-word !important;">软件系统的多维性能模型</a><br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658975379&idx=1&sn=e931610ee8e2ef865518a177f91e4869&chksm=80d323b5b7a4aaa3cb87d890c4d4c2175c24e67a091fa911b002f9069299f147856532ff5c28&scene=21#wechat_redirect" textvalue="性能,10点系统性思考" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">性能,10点系统性思考</a><br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p></li> <li style="outline: 0px;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a target="_blank" href="http://mp.weixin.qq.com/s?__biz=MzAwOTcyNzA0OQ==&mid=2658976943&idx=1&sn=c189603ac170298ea3a6d0f527d00988&chksm=80d35989b7a4d09f69d0fb4a3a92cca7d56775c46573246b617259d4a45146629a5d689b7080&scene=21#wechat_redirect" textvalue="API的性能约定" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2" wah-hotarea="click" hasload="1" style="outline: 0px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">API的性能约定</a></span></p></li> </ul> <p style="text-align: justify;"><br></p>
作者:微信小助手
<section data-mpa-powered-by="yiban.io"> <p style="white-space: normal;margin-bottom: 0px;"><span style="font-size: 15px;"></span><span style="font-size: 15px;">今天这篇文章,其实也是我曾经面试中遇到过的真题。</span><br></p> <p style="white-space: normal;margin-bottom: 0px;"><span style="font-size: 15px;"><br></span></p> <section style="text-align: left;margin-bottom: 0px;"> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">分库分表大家可能听得多了,但读扩散问题大家了解吗?</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">这里涉及到几个问题。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <ul class="list-paddingleft-1" style="white-space: normal;list-style-type: disc;"> <li style="font-size: 15px;"><p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 15px;">分库分表是什么?</span></p></li> <li style="font-size: 15px;"><p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 15px;">读扩散问题是什么?</span></p></li> <li style="font-size: 15px;"><p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 15px;">分库分表为什么会引发读扩散问题?</span></p></li> <li style="font-size: 15px;"><p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 15px;">怎么解决读扩散问题?</span></p></li> </ul> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><br></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">这些问题还是比较有意思的。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">相信兄弟们也一定有机会遇到哈哈哈。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">我们先从分库分表的话题聊起吧。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p> <h3 style="white-space: normal;text-align: left;margin-bottom: 0px;"><strong><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;color: rgb(171, 25, 66);">分库分表</span></strong></h3> <p style="white-space: normal;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">我们平时做项目开发。一开始,通常都先用一张数据表,而一般来说数据表写到两千万条数据之后,底层 B+ 树的层级结构就可能会变高,不同层级的数据页一般都放在磁盘里不同的地方,换言之,磁盘 IO 就会增多,带来的便是查询性能变差。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">于是,当我们单表需要管理的数据变得越来越多,就不得不考虑数据库分表。而这里的分表,分为水平分表和垂直分表。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">垂直分表的原理比较简单,一般就是把某几列拆成一个新表,这样单行数据就会变小, B+ 树里的单个数据页(固定 16KB)内能放入的行数就会变多,从而使单表能放入更多的数据。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">垂直分表没有太多可以说的点。下面,我们重点说说最常见的水平分表。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">水平分表有好几种做法,但不管是哪种,本质上都是将原来的 user 表,变成 user_0, user1, user2 .... userN 这样的 N 多张小表。</span></p> <p style="text-align: left;margin-bottom: 0px;"><br></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">从读写一张 user 大表,变成读写 user_1 … userN 这样的 N 张小表。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <figure style="white-space: normal;text-align: center;margin-bottom: 0px;"> <img class="rich_pages wxw-img" data-ratio="1.0810810810810811" src="/upload/e89fa6a18a892f6c30b473455776e1c0.png" data-type="png" data-w="1110" style="white-space: normal;text-align: left;margin-bottom: 0px;width: 406px;height: 439px;" title="分表3"> <figcaption style="white-space: normal;text-align: left;margin-bottom: 0px;"> <br> </figcaption> <figcaption style="white-space: normal;text-align: left;margin-bottom: 0px;"> <strong><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;color: rgb(171, 25, 66);">分表</span></strong> </figcaption> </figure> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">每一张小表里,只保存一部分数据,但具体保存多少,这个自己定,一般就定 500 万~ 两千万。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">那分表具体怎么做?</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p> <h4 style="white-space: normal;text-align: left;margin-bottom: 0px;"><strong><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">根据 id 范围分表</span></strong></h4> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><br></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">我认为最好用的,是根据 id 范围进行分表。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">我们假设每张分表能放<span style="color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;float: none;display: inline !important;">两千万</span>行数据。那</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <ul class="list-paddingleft-1" style="list-style-type: disc;"> <li style="font-size: 15px;"><p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 15px;">user0 放主键 id 为 1~<span style="color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;float: none;display: inline !important;">2千万</span>的数据;</span></p></li> <li style="font-size: 15px;"><p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 15px;">user1 定 id 为 <span style="color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;float: none;display: inline !important;">两千万 </span>+1 ~ 4<span style="color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;float: none;display: inline !important;">千万</span>;</span></p></li> <li style="font-size: 15px;"><p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 15px;">user2 定 id 为 4千万 +1 ~ 6千万;</span></p></li> <li style="font-size: 15px;"><p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 15px;"> userN 放 2N千万+1 ~ 2(N+1)千万。</span></p></li> </ul> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <figure style="white-space: normal;text-align: center;margin-bottom: 0px;"> <img class="rich_pages wxw-img" data-ratio="0.7117516629711752" src="/upload/63bb26c0dcd6f68d2e1a80f749be71a4.png" data-type="png" data-w="1353" style="white-space: normal;text-align: left;margin-bottom: 0px;width: 429px;height: 305px;" title="根据id范围分表"> <figcaption style="white-space: normal;text-align: center;margin-bottom: 0px;"> <span style="text-align: justify;font-size: 12px;color: rgb(136, 136, 136);">根据id范围分表</span> </figcaption> <figcaption style="white-space: normal;text-align: left;margin-bottom: 0px;"> <span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span> </figcaption> </figure> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">假设现在有条数据,id=3 千万,将这个 3 千万除 2 千万= 1.5,向下取整得到 1,那就可以得到这条数据属于 user1 表。于是去读写 user1 表就行了。这就完成了数据的路由逻辑,我们把这部分逻辑封装起来,放在数据库和业务代码之间。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">这样。对于业务代码来说,它只知道自己在读写一张 user 表,根本不知道底下还分了那么多张小表。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">对于数据库来说,它并不知道自己被分表了,它只知道有那么几张表,正好名字长得比较像而已。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">这还只是在一个数据库里做分表,如果范围再搞大点,还能在多个数据库里做分表,这就是所谓的分库分表。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">不管是单库分表还是分库分表,都可以通过这样一个中间层逻辑做路由。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">还真的就应了那句话,<strong>没有什么是加中间层不能解决的</strong>。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">如果有,就多加一层。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">至于这个中间层的实现方式就更灵活了,它既可以像第三方orm库那样加在业务代码中。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <figure style="white-space: normal;text-align: center;margin-bottom: 0px;"> <img class="rich_pages wxw-img" data-ratio="0.5833333333333334" src="/upload/b772f82901675bf9d44a4f7bcdca92c6.png" data-type="png" data-w="1800" style="white-space: normal;text-align: left;margin-bottom: 0px;width: 428px;height: 250px;" title="通过orm读写分表.drawio"> <figcaption style="white-space: normal;text-align: center;margin-bottom: 0px;"> <span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 12px;color: rgb(136, 136, 136);">通过 ORM 读写分表</span> </figcaption> </figure> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><br></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">也可以在 MySQL 和业务代码之间加个 Proxy 服务。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">如果是通过第三方 ORM 库的方式来做的话,那需要根据不同语言实现不同的代码库,所以不少厂都选择后者加个 <span style="color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;float: none;display: inline !important;">Proxy </span>的方式,这样就不需要关心上游服务用的是什么语言。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <figure style="white-space: normal;text-align: center;margin-bottom: 0px;"> <img class="rich_pages wxw-img" data-ratio="0.5833333333333334" src="/upload/a91c7977f2642264700e58b3a31df49d.png" data-type="png" data-w="1800" style="white-space: normal;text-align: left;margin-bottom: 0px;width: 445px;height: 260px;" title="通过proxy管理分表.drawio"> <figcaption style="white-space: normal;text-align: center;margin-bottom: 0px;"> <span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;color: rgb(136, 136, 136);font-size: 12px;">通过 P</span> <span style="color: rgb(136, 136, 136);font-size: 12px;text-align: justify;">roxy 管理分表</span> </figcaption> </figure> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p> <h4 style="white-space: normal;text-align: left;margin-bottom: 0px;"><strong><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">根据 id 取模分表</span></strong></h4> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><br></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">这时候就有兄弟要提出问题了,"我看很多方案都对id取模,你这个方案是不是不完整?"</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">取模的方案也是很常见的。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">比如一个 id=31 进来,我们一共分了 5 张表,分别是 user0 到 user4。对 31%5=1,取模得 1,于是就能知道应该读写 user1 表。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <figure style="white-space: normal;text-align: center;margin-bottom: 0px;"> <img class="rich_pages wxw-img" data-ratio="0.7111111111111111" src="/upload/86a9278d9b6592f7fb8a1dfb308cb38e.png" data-type="png" data-w="1350" style="white-space: normal;text-align: left;margin-bottom: 0px;width: 441px;height: 314px;" title="根据id取模分表.drawio"> <figcaption style="white-space: normal;text-align: center;margin-bottom: 0px;"> <span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;font-size: 12px;color: rgb(136, 136, 136);">根据 id 取模分表</span> </figcaption> </figure> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">优点当然是比较简单。而且读写数据都可以很均匀的分摊到每个分表上。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">但缺点也比较明显,如果想要扩展表的个数,比如从 5 张表变成 8 张表。那同样还是 id=31 的数据,31%8 = 7,就需要读写 user7 这张表。跟原来就对不上了。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">这就需要<strong>考虑数据迁移的问题</strong>。很头秃。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">为了避免后续扩展的问题,我见过一些业务一开始就将数据预估得很大,然后心一横,分成 100 张表,一张表如果存个两千万条,那也能存 20 亿条数据了。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">也不是说这样不行吧,就是这个业务直到最后放弃的时候,也就存了百万条数据,每次打开数据库表能看到茫茫多的 user_xx,就是不太舒服。专业点叫做<strong>增</strong><strong>加了程序员的心智负担</strong>。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">而上面一种方式,根据 id 范围去分表,就能很好地解决这些问题。数据少的时候,表也少。随着数据增多,表会慢慢变多。而且这样表还可以无限扩展。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">那是不是说取模的做法就用不上了呢?</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">也不是。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><br style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p> <h4 style="white-space: normal;text-align: left;margin-bottom: 0px;"><strong><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">将上面两种方式结合起来</span></strong></h4> <p><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">id 取模的做法,最大的好处是,新写入的数据都是实实在在地分散到了多张表上。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">而根据 id 范围去做分表,因为 id 是递增的,那新写入的数据一般都会落到某一张表上,如果你的业务场景写数据特别频繁,那这张表就会出现写热点的问题。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">这时候就可以将 id 取模和 id 范围分表的方式结合起来。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">我们可以在某个 id 范围里,引入取模的功能。比如 以前 2千万~4<span style="color: rgb(51, 51, 51);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;float: none;display: inline !important;">千万 </span>是 user1 表。现在可以在这个范围再分成 5 个表,也就是引 入user1-0、user1-2 到 user1-4,在这 5 个表里取模。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">举个例子,id=3千万,根据范围,会分到 user1 表,然后再进行取模 3千万 % 5 = 0,也就是读写 user1-0 表。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">这样就可以<strong>将写单表分摊为写多表</strong>。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;">这在分库的场景下优势会更明显。不同的库,可以把服务部署到不同的机器上,这样各个机器的性能都能被用起来。</span></p> <p style="white-space: normal;text-align: left;margin-bottom: 0px;"><span style="font-size: 15px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-align: justify;"><br></span></p> <figure style="white-space: normal;text-align: center;margin-bottom: 0px;"> <img class="rich_pages wxw-img" data-ratio="0.5757575757575758" src="/upload/4023a898599e86bf7a5c508e6a7f5098.png" data-type="png" data-w="1980" style="white-space: normal;text-align: left;margin-bottom: 0px;width: 472px;height: 272px;" title="根据id范围分表后再取模.drawio"> <figcaption style="white-space: normal;text-align: center;margin-bottom: 0px;"> <span style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-
作者:微信小助手
<section data-tool="mdnice编辑器" data-website="https://www.mdnice.com"> <hr data-tool="mdnice编辑器" style="margin: 10px 0px;height: 1px;padding: 0px;border-width: initial;border-style: none;border-color: initial;text-align: center;background-image: linear-gradient(to right, rgba(248, 57, 41, 0), rgb(14, 136, 235), rgba(248, 57, 41, 0));"> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">前两天在工作中忙的焦头烂额,涉及到<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>对于事务的控制,便仔细研究了一下,颇有所获,花费好了几天测试整理,今天才发表出来,希望看到博客的老铁们能有所获吧。话不多说直奔正题。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;"><strong style="font-weight: border;color: #0e88eb;">先简单介绍一下Spring事务的传播行为:</strong></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">所谓事务的传播行为是指,如果在开始当前事务之前,一个事务上下文已经存在,此时有若干选项可以指定一个事务性方法的执行行为。在<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">TransactionDefinition</code>定义中包括了如下几个表示传播行为的常量:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;list-style-type: disc;" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;text-align: left;color: rgb(1,1,1);font-weight: 500;font-size: 15px;"> <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">TransactionDefinition.PROPAGATION_REQUIRED</code>:如果当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。这是默认值。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;text-align: left;color: rgb(1,1,1);font-weight: 500;font-size: 15px;"> <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">TransactionDefinition.PROPAGATION_REQUIRES_NEW</code>:创建一个新的事务,如果当前存在事务,则把当前事务挂起。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;text-align: left;color: rgb(1,1,1);font-weight: 500;font-size: 15px;"> <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">TransactionDefinition.PROPAGATION_SUPPORTS</code>:如果当前存在事务,则加入该事务;如果当前没有事务,则以非事务的方式继续运行。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;text-align: left;color: rgb(1,1,1);font-weight: 500;font-size: 15px;"> <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">TransactionDefinition.PROPAGATION_NOT_SUPPORTED</code>:以非事务方式运行,如果当前存在事务,则把当前事务挂起。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;text-align: left;color: rgb(1,1,1);font-weight: 500;font-size: 15px;"> <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">TransactionDefinition.PROPAGATION_NEVER</code>:以非事务方式运行,如果当前存在事务,则抛出异常。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;text-align: left;color: rgb(1,1,1);font-weight: 500;font-size: 15px;"> <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">TransactionDefinition.PROPAGATION_MANDATORY</code>:如果当前存在事务,则加入该事务;如果当前没有事务,则抛出异常。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;text-align: left;color: rgb(1,1,1);font-weight: 500;font-size: 15px;"> <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">TransactionDefinition.PROPAGATION_NESTED</code>:如果当前存在事务,则创建一个事务作为当前事务的嵌套事务来运行;如果当前没有事务,则该取值等价于 <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">TransactionDefinition.PROPAGATION_REQUIRED</code>。 </section></li> </ul> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;"><strong style="font-weight: border;color: #0e88eb;">然后说一下Spring事务的回滚机制:</strong></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">Spring的AOP即声明式事务管理默认是针对<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">unchecked exception</code>回滚。Spring的事务边界是在调用业务方法之前开始的,业务方法执行完毕之后来执行<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">commit or rollback</code>(Spring默认取决于是否抛出<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">runtimeException</code>)。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">如果你在方法中有<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">try{}catch(Exception e){}</code>处理,那么try里面的代码块就脱离了事务的管理,若要事务生效需要在catch中<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">throw new RuntimeException ("xxxxxx");</code>这一点也是面试中会问到的事务失效的场景。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">再简单介绍一下<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>注解底层实现方式吧,毫无疑问,是通过动态代理,那么动态代理又分为JDK自身和CGLIB,这个也不多赘述了,毕竟今天的主题是如何将<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>对于事物的控制应用到炉火纯青。哈哈~</p> <hr data-tool="mdnice编辑器" style="margin: 10px 0px;height: 1px;padding: 0px;border-width: initial;border-style: none;border-color: initial;text-align: center;background-image: linear-gradient(to right, rgba(248, 57, 41, 0), rgb(14, 136, 235), rgba(248, 57, 41, 0));"> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">第一点要注意的就是在<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>注解的方法中,再调用本类中的其他方法method2时,那么method2方法上的<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>注解是不!会!生!效!的!但是加上也并不会报错,拿图片简单帮助理解一下吧。这一点也是面试中会问到的事务失效的场景。</p> <figure data-tool="mdnice编辑器" style="margin: 0;margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"> <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" textvalue="你已选中了添加链接的内容" linktype="text" imgurl="" imgdata="null" tab="innerlink" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 20px 41.8542px;right: auto;bottom: auto;"><img class="rich_pages wxw-img" data-ratio="0.4764705882352941" src="/upload/33ba8ae9fb71aa3a9ea537e0666a2864.png" data-type="png" data-w="680" style="max-width: 100%;border-radius: 0px 0px 5px 5px;display: block;margin: 0px;width: 85%;height: 100%;object-fit: contain;box-shadow: rgb(132, 161, 168) 0px 10px 15px;"></span></a> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">通过代理对象在目标对象前后进行方法增强,也就是事务的开启提交和回滚。那么继续调用本类中其他方法是怎样呢,如下图:</p> <figure data-tool="mdnice编辑器" style="margin: 0;margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"> <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" textvalue="你已选中了添加链接的内容" linktype="text" imgurl="" imgdata="null" tab="innerlink" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 20px 41.8542px;right: auto;bottom: auto;"><img class="rich_pages wxw-img" data-ratio="0.6455882352941177" src="/upload/fd127b8caa13d1663cc48cdf62df5776.png" data-type="png" data-w="680" style="max-width: 100%;border-radius: 0px 0px 5px 5px;display: block;margin: 0px;width: 85%;height: 100%;object-fit: contain;box-shadow: rgb(132, 161, 168) 0px 10px 15px;"></span></a> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">可见目标对象内部的自我调用,也就是通过this.指向的目标对象将不会执行方法的增强。</p> <hr data-tool="mdnice编辑器" style="margin: 10px 0px;height: 1px;padding: 0px;border-width: initial;border-style: none;border-color: initial;text-align: center;background-image: linear-gradient(to right, rgba(248, 57, 41, 0), rgb(14, 136, 235), rgba(248, 57, 41, 0));"> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">先说第二点需要注意的地方,等下说如何解决上面第一点的问题。第二点就是<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>注解的方法必须是公共方法,就是必须是public修饰符!!!</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">至于这个的原因,发表下个人的理解吧,因为JVM的动态代理是基于接口实现的,通过代理类将目标方法进行增强,想一下也是啦,没有权限访问那么你让我怎么进行,,,好吧,这个我也没有深入研究底层,个人理解个人理解。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">在这里我也放个问题吧,希望有高手可以回复指点指点我,因为JVM动态代理是基于接口实现的,那么是不是service层都要按照接口和实现类的开发模式,注解才会生效呢,就是说<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">controller</code>层直接调用没有接口的service层,加了注解也一样不起作用吧,这个懒了,没有测试,其一是因为没有人会这么开发吧,其二是我就认为是不起作用的,哈哈</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;"><strong style="font-weight: border;color: #0e88eb;">下面来解决一下第一点的问题,如何在方法中调用本类中其他方法呢。</strong></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">通过<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">AopContext.currentProxy ()</code>获取到本类的代理对象,再去调用就好啦。因为这个是CGLIB实现,所以要开启AOP,当然也很简单,在springboot启动类上加上注解<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@EnableAspectJAutoProxy(exposeProxy = true)</code>就可以啦,这个依赖大家自行搜一下就好啦。要注意,注意,代理对象调用的方法也要是public修饰符,否则方法中获取不到注入的bean,会报空指针错误。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">emmmm,我先把调用的方式和结果说下吧。自己简单写了代码,有点粗糙,就不要介意啦,嘿嘿。。。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">Controller中调用Service</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASpWUOnrrmsP2K5dOicLBbIIunfWiavibk92kNGbjkldawma7CyX6PKhNOBLicKoHK7hiciaE5F5rTvlytz/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #9B9B9B;line-height: 26px;">@RestController</span><br><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #B8D7A3;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">class</span> <span style="color: #DCDCDC;line-height: 26px;">TransactionalController</span> </span>{<br> <br> <span style="color: #9B9B9B;line-height: 26px;">@Autowired</span><br> <span style="color: #569CD6;line-height: 26px;">private</span> TransactionalService transactionalService;<br> <br> <span style="color: #9B9B9B;line-height: 26px;">@PostMapping</span>(<span style="color: #D69D85;line-height: 26px;">"transactionalTest"</span>)<br> <span style="color: #DCDCDC;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #569CD6;line-height: 26px;">void</span> <span style="color: #DCDCDC;line-height: 26px;">transacionalTest</span><span style="color: #DCDCDC;line-height: 26px;">()</span></span>{<br> transactionalService.transactionalMethod();<br> }<br>}<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">Service中实现对事务的控制:接口</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASpWUOnrrmsP2K5dOicLBbIIunfWiavibk92kNGbjkldawma7CyX6PKhNOBLicKoHK7hiciaE5F5rTvlytz/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #B8D7A3;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">interface</span> <span style="color: #DCDCDC;line-height: 26px;">TransactionalService</span> </span>{<br> <span style="color: #DCDCDC;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">void</span> <span style="color: #DCDCDC;line-height: 26px;">transactionalMethod</span><span style="color: #DCDCDC;line-height: 26px;">()</span></span>;<br>}<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">Service中实现对事务的控制:实现类(各种情况的说明都写在图片里了,这样方便阅读,有助于快速理解吧)</p> <figure data-tool="mdnice编辑器" style="margin: 0;margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"> <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" textvalue="你已选中了添加链接的内容" linktype="text" imgurl="" imgdata="null" tab="innerlink" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 20px 41.8542px;right: auto;bottom: auto;"><img class="rich_pages wxw-img" data-ratio="0.6690017513134852" src="/upload/ece4986dfaf6cad859ffb9cfaa255889.png" data-type="png" data-w="571" style="max-width: 100%;border-radius: 0px 0px 5px 5px;display: block;margin: 0px;width: 85%;height: 100%;object-fit: contain;box-shadow: rgb(132, 161, 168) 0px 10px 15px;"></span></a> </figure> <figure data-tool="mdnice编辑器" style="margin: 0;margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"> <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" textvalue="你已选中了添加链接的内容" linktype="text" imgurl="" imgdata="null" tab="innerlink" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 20px 41.8542px;right: auto;bottom: auto;"><img class="rich_pages wxw-img" data-ratio="0.5143229166666666" src="/upload/71cfae8b269c8f5a49a2ae459f8aa771.png" data-type="png" data-w="768" style="max-width: 100%;border-radius: 0px 0px 5px 5px;display: block;margin: 0px;width: 85%;height: 100%;object-fit: contain;box-shadow: rgb(132, 161, 168) 0px 10px 15px;"></span></a> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">上面两种情况不管使不使用代理调用方法1和方法2,方法<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">transactionalMethod</code>都处在一个事务中,四条更新操作全部失败。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">那么有人可能会有疑问了,在方法1和方法2上都加<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>注解呢?答案是结果和上面是一致的。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">小结只要方法<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">transactionalMethod</code>上有注解,并且方法1和方法2都处于当前事务中(不使用代理调用,方法1和方法2上的<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>注解是不生效的;使用代理,需要方法1和方法2都处在<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">transactionalMethod</code>方法的事务中,默认或者嵌套事务均可,当然也可以不加<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>注解),那么整体保持事务一致性。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">如果想要方法1和方法2均单独保持事务一致性怎么办呢,刚说过了,如果不是用代理调用<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>注解是不生效的,所以一定要使用代理调用实现,然后让方法1和方法2分别单独开启新的事务,便OK啦。下面摆上图片。</p> <figure data-tool="mdnice编辑器" style="margin: 0;margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"> <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" textvalue="你已选中了添加链接的内容" linktype="text" imgurl="" imgdata="null" tab="innerlink" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 20px 41.8542px;right: auto;bottom: auto;"><img class="rich_pages wxw-img" data-ratio="0.5866495507060334" src="/upload/33efba5d44e705209ce8bb8bee2f780.png" data-type="png" data-w="779" style="max-width: 100%;border-radius: 0px 0px 5px 5px;display: block;margin: 0px;width: 85%;height: 100%;object-fit: contain;box-shadow: rgb(132, 161, 168) 0px 10px 15px;"></span></a> </figure> <figure data-tool="mdnice编辑器" style="margin: 0;margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"> <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" textvalue="你已选中了添加链接的内容" linktype="text" imgurl="" imgdata="null" tab="innerlink" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 20px 41.8542px;right: auto;bottom: auto;"><img class="rich_pages wxw-img" data-ratio="0.5692695214105793" src="/upload/e4e81d40fc1fb16276e0675e40cbb776.png" data-type="png" data-w="794" style="max-width: 100%;border-radius: 0px 0px 5px 5px;display: block;margin: 0px;width: 85%;height: 100%;object-fit: contain;box-shadow: rgb(132, 161, 168) 0px 10px 15px;"></span></a> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">这两种情况都是方法1和方法2均处在单独的事务中,各自保持事务的一致性。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">接下来进行进一步的优化,可以在<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">transactionalMethod</code>方法中分别对方法1和方法2进行控制。要将代码的艺术发挥到极致嘛,下面装逼开始。</p> <figure data-tool="mdnice编辑器" style="margin: 0;margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"> <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" textvalue="你已选中了添加链接的内容" linktype="text" imgurl="" imgdata="null" tab="innerlink" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="top: auto;left: auto;margin: 20px 41.8542px;right: auto;bottom: auto;"><img class="rich_pages wxw-img" data-ratio="0.5684608120868744" src="/upload/9d01f8b5b99ad8a8f90d1eb6f02d935e.png" data-type="png" data-w="1059" style="max-width: 100%;border-radius: 0px 0px 5px 5px;display: block;margin: 0px;width: 85%;height: 100%;object-fit: contain;box-shadow: rgb(132, 161, 168) 0px 10px 15px;"></span></a> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">代码太长了,超过屏幕了,粘贴出来截的图,红框注释需要仔细看,希望不要影响你的阅读体验,至此,本篇关于<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactioinal</code>注解的使用就到此为止啦,</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">简单总结一下吧:</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">1、就是<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Transactional</code>注解保证的是每个方法处在一个事务,如果有try一定在catch中抛出运行时异常。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">2、方法必须是public修饰符。否则注解不会生效,但是加了注解也没啥毛病,不会报错,只是没卵用而已。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">3、this.本方法的调用,被调用方法上注解是不生效的,因为无法再次进行切面增强。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">如果有更细致的讨论欢迎评论,感谢阅读。</p> </section>
作者:微信小助手
<p data-mpa-powered-by="yiban.io"><strong style="max-width: 100%;color: rgb(74, 74, 74);font-family: 微软雅黑, 'Microsoft YaHei';text-align: left;white-space: normal;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);font-size: 18px;box-sizing: border-box !important;overflow-wrap: break-word !important;">大家好,我是苏三。</strong></p> <p><strong style="max-width: 100%;color: rgb(74, 74, 74);font-family: 微软雅黑, 'Microsoft YaHei';text-align: left;white-space: normal;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);font-size: 18px;box-sizing: border-box !important;overflow-wrap: break-word !important;"></strong></p> <p style="margin: 0px 0px 24px;padding: 0px;clear: both;min-height: 1em;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;outline: 0px;color: rgb(34, 34, 34);font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size: 17px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: normal;letter-spacing: 0.544px;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);visibility: visible;"><span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;outline: 0px;color: rgb(255, 169, 0);font-size: 18px;font-weight: bold;font-family: Roboto, Oxygen, Ubuntu, Cantarell, PingFangSC-regular, PingFangTC-regular, 'Open Sans', 'Helvetica Neue', sans-serif;letter-spacing: 0px;text-align: left;visibility: visible;">sql语句的执行顺序:</span><span style="margin: 0px;padding: 0px;max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;outline: 0px;color: rgb(255, 169, 0);font-size: 18px;font-weight: bold;font-family: Roboto, Oxygen, Ubuntu, Cantarell, PingFangSC-regular, PingFangTC-regular, 'Open Sans', 'Helvetica Neue', sans-serif;letter-spacing: 0px;text-align: left;visibility: visible;"></span></p> <section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="margin: -1em 0px 24px;padding: 0px;max-width: 100%;overflow-wrap: break-wor
作者:微信小助手
<section data-tool="mdnice编辑器" data-website="https://www.mdnice.com"> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">实际业务开发过程中,业务逻辑可能非常复杂,核心业务 + N个子业务。如果都放到一块儿去做,代码可能会很长,耦合度不断攀升,维护起来也麻烦,甚至头疼。还有一些业务场景不需要在一次请求中同步完成,比如邮件发送、短信发送等。</p> <blockquote data-tool="mdnice编辑器" style="border-width: initial;border-style: none;border-color: initial;display: block;font-size: 0.9em;overflow: auto;margin-bottom: 20px;margin-top: 20px;font-style: normal;padding: 10px;line-height: 1.8;border-radius: 0px 0px 10px 10px;color: rgb(14, 136, 235);background: rgb(255, 255, 255);box-shadow: rgb(132, 161, 168) 0px 10px 15px;"> <p style="padding-top: 8px;padding-bottom: 8px;letter-spacing: 0.2em;word-spacing: 0.1em;margin: 0px;line-height: 26px;color: #0e88eb;font-size: 15px;display: block;">MQ 确实可以解决这个问题,但 MQ 重啊,非必要不提升架构复杂度。针对这些问题,我们了解一下 Spring Event。</p> </blockquote> <blockquote data-tool="mdnice编辑器" style="border-width: initial;border-style: none;border-color: initial;display: block;font-size: 0.9em;overflow: auto;margin-bottom: 20px;margin-top: 20px;font-style: normal;padding: 10px;line-height: 1.8;border-radius: 0px 0px 10px 10px;color: rgb(14, 136, 235);background: rgb(255, 255, 255);box-shadow: rgb(132, 161, 168) 0px 10px 15px;"> <p style="padding-top: 8px;padding-bottom: 8px;letter-spacing: 0.2em;word-spacing: 0.1em;margin: 0px;line-height: 26px;color: #0e88eb;font-size: 15px;display: block;">基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能。</p> <p style="padding-top: 8px;padding-bottom: 8px;letter-spacing: 0.2em;word-spacing: 0.1em;margin: 0px;line-height: 26px;color: #0e88eb;font-size: 15px;display: block;">项目地址:https://github.com/YunaiV/ruoyi-vue-pro</p> </blockquote> <h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;color: black;font-size: 22px;text-align: left;margin: 20px 10px 0px 0px;"><span style="display: none;"></span><span style="font-family: STHeitiSC-Light;font-size: 22px;color: #0e88eb;font-weight: bolder;display: inline-block;padding-left: 10px;border-left: 5px solid #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">Spring Event 同步使用</a></span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">Spring Event(<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">Application Event</code>)其实就是一个观察者设计模式,一个 Bean 处理完成任务后希望通知其它 Bean 或者说一个 Bean 想观察监听另一个Bean 的行为。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">Spring Event 用来解耦业务真的贼好用!</p> <blockquote data-tool="mdnice编辑器" style="border-width: initial;border-style: none;border-color: initial;display: block;font-size: 0.9em;overflow: auto;margin-bottom: 20px;margin-top: 20px;font-style: normal;padding: 10px;line-height: 1.8;border-radius: 0px 0px 10px 10px;color: rgb(14, 136, 235);background: rgb(255, 255, 255);box-shadow: rgb(132, 161, 168) 0px 10px 15px;"> <p style="padding-top: 8px;padding-bottom: 8px;letter-spacing: 0.2em;word-spacing: 0.1em;margin: 0px;line-height: 26px;color: #0e88eb;font-size: 15px;display: block;">Demo 地址:https://gitee.com/csps/mingyue-springboot-learning</p> </blockquote> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">1.自定义事件</a></span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">定义事件,继承 <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">ApplicationEvent</code> 的类成为一个事件类</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #569CD6;line-height: 26px;">import</span> lombok.Data;<br><span style="color: #569CD6;line-height: 26px;">import</span> lombok.ToString;<br><span style="color: #569CD6;line-height: 26px;">import</span> org.springframework.context.ApplicationEvent;<br><br><span style="color: #57A64A;font-style: italic;line-height: 26px;">/**<br> * <span style="color: #608B4E;line-height: 26px;">@author</span> Strive<br> * <span style="color: #608B4E;line-height: 26px;">@date</span> 2022/4/22 18:00<br> * <span style="color: #608B4E;line-height: 26px;">@description</span><br> */</span><br><span style="color: #9B9B9B;line-height: 26px;">@Data</span><br><span style="color: #9B9B9B;line-height: 26px;">@ToString</span><br><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #B8D7A3;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">class</span> <span style="color: #DCDCDC;line-height: 26px;">OrderProductEvent</span> <span style="color: #569CD6;line-height: 26px;">extends</span> <span style="color: #DCDCDC;line-height: 26px;">ApplicationEvent</span> </span>{<br><br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">/** 该类型事件携带的信息 */</span><br> <span style="color: #569CD6;line-height: 26px;">private</span> String orderId;<br><br> <span style="color: #DCDCDC;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #DCDCDC;line-height: 26px;">OrderProductEvent</span><span style="color: #DCDCDC;line-height: 26px;">(Object source, String orderId)</span> </span>{<br> <span style="color: #569CD6;line-height: 26px;">super</span>(source);<br> <span style="color: #569CD6;line-height: 26px;">this</span>.orderId = orderId;<br> }<br>}<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">2.定义监听器</a></span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">监听并处理事件,实现 <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">ApplicationListener</code> 接口或者使用 <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@EventListener</code> 注解</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #569CD6;line-height: 26px;">import</span> com.csp.mingyue.event.events.OrderProductEvent;<br><span style="color: #569CD6;line-height: 26px;">import</span> lombok.SneakyThrows;<br><span style="color: #569CD6;line-height: 26px;">import</span> lombok.extern.slf4j.Slf4j;<br><span style="color: #569CD6;line-height: 26px;">import</span> org.springframework.context.ApplicationListener;<br><span style="color: #569CD6;line-height: 26px;">import</span> org.springframework.stereotype.Component;<br><br><span style="color: #57A64A;font-style: italic;line-height: 26px;">/**<br> * 实现 ApplicationListener 接口,并指定监听的事件类型<br> *<br> * <span style="color: #608B4E;line-height: 26px;">@author</span> Strive<br> * <span style="color: #608B4E;line-height: 26px;">@date</span> 2022/4/24 09:09<br> * <span style="color: #608B4E;line-height: 26px;">@description</span><br> */</span><br><span style="color: #9B9B9B;line-height: 26px;">@Slf</span>4j<br><span style="color: #9B9B9B;line-height: 26px;">@Component</span><br><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #B8D7A3;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">class</span> <span style="color: #DCDCDC;line-height: 26px;">OrderProductListener</span> <span style="color: #569CD6;line-height: 26px;">implements</span> <span style="color: #DCDCDC;line-height: 26px;">ApplicationListener</span><<span style="color: #DCDCDC;line-height: 26px;">OrderProductEvent</span>> </span>{<br><br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">/** 使用 onApplicationEvent 方法对消息进行接收处理 */</span><br> <span style="color: #9B9B9B;line-height: 26px;">@SneakyThrows</span><br> <span style="color: #9B9B9B;line-height: 26px;">@Override</span><br> <span style="color: #DCDCDC;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #569CD6;line-height: 26px;">void</span> <span style="color: #DCDCDC;line-height: 26px;">onApplicationEvent</span><span style="color: #DCDCDC;line-height: 26px;">(OrderProductEvent event)</span> </span>{<br> String orderId = event.getOrderId();<br> <span style="color: #569CD6;line-height: 26px;">long</span> start = System.currentTimeMillis();<br> Thread.sleep(<span style="color: #B8D7A3;line-height: 26px;">2000</span>);<br> <span style="color: #569CD6;line-height: 26px;">long</span> end = System.currentTimeMillis();<br> log.info(<span style="color: #D69D85;line-height: 26px;">"{}:校验订单商品价格耗时:({})毫秒"</span>, orderId, (end - start));<br> }<br>}<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">3.定义发布者</a></span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">发布事件,通过 <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">ApplicationEventPublisher</code> 发布事件</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #569CD6;line-height: 26px;">import</span> com.csp.mingyue.event.events.OrderProductEvent;<br><span style="color: #569CD6;line-height: 26px;">import</span> lombok.RequiredArgsConstructor;<br><span style="color: #569CD6;line-height: 26px;">import</span> lombok.extern.slf4j.Slf4j;<br><span style="color: #569CD6;line-height: 26px;">import</span> org.springframework.context.ApplicationContext;<br><span style="color: #569CD6;line-height: 26px;">import</span> org.springframework.stereotype.Service;<br><br><span style="color: #57A64A;font-style: italic;line-height: 26px;">/**<br> * <span style="color: #608B4E;line-height: 26px;">@author</span> Strive<br> * <span style="color: #608B4E;line-height: 26px;">@date</span> 2022/4/24 09:25<br> * <span style="color: #608B4E;line-height: 26px;">@description</span><br> */</span><br><span style="color: #9B9B9B;line-height: 26px;">@Slf</span>4j<br><span style="color: #9B9B9B;line-height: 26px;">@Service</span><br><span style="color: #9B9B9B;line-height: 26px;">@RequiredArgsConstructor</span><br><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #B8D7A3;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">class</span> <span style="color: #DCDCDC;line-height: 26px;">OrderService</span> </span>{<br><br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">/** 注入ApplicationContext用来发布事件 */</span><br> <span style="color: #569CD6;line-height: 26px;">private</span> <span style="color: #569CD6;line-height: 26px;">final</span> ApplicationContext applicationContext;<br><br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">/**<br> * 下单<br> *<br> * <span style="color: #608B4E;line-height: 26px;">@param</span> orderId 订单ID<br> */</span><br> <span style="color: #DCDCDC;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">public</span> String <span style="color: #DCDCDC;line-height: 26px;">buyOrder</span><span style="color: #DCDCDC;line-height: 26px;">(String orderId)</span> </span>{<br> <span style="color: #569CD6;line-height: 26px;">long</span> start = System.currentTimeMillis();<br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">// 1.查询订单详情</span><br><br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">// 2.检验订单价格 (同步处理)</span><br> applicationContext.publishEvent(<span style="color: #569CD6;line-height: 26px;">new</span> OrderProductEvent(<span style="color: #569CD6;line-height: 26px;">this</span>, orderId));<br><br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">// 3.短信通知(异步处理)</span><br><br> <span style="color: #569CD6;line-height: 26px;">long</span> end = System.currentTimeMillis();<br> log.info(<span style="color: #D69D85;line-height: 26px;">"任务全部完成,总耗时:({})毫秒"</span>, end - start);<br> <span style="color: #569CD6;line-height: 26px;">return</span> <span style="color: #D69D85;line-height: 26px;">"购买成功"</span>;<br> }<br>}<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">4.单测执行</a></span><span style="display: none;"></span></h3> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #569CD6;line-height: 26px;">import</span> org.junit.jupiter.api.Test;<br><span style="color: #569CD6;line-height: 26px;">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span style="color: #569CD6;line-height: 26px;">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span style="color: #57A64A;font-style: italic;line-height: 26px;">/**<br> * <span style="color: #608B4E;line-height: 26px;">@author</span> Strive<br> * <span style="color: #608B4E;line-height: 26px;">@date</span> 2022/4/24 09:28<br> * <span style="color: #608B4E;line-height: 26px;">@description</span><br> */</span><br><span style="color: #9B9B9B;line-height: 26px;">@SpringBootTest</span><br><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #B8D7A3;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">class</span> <span style="color: #DCDCDC;line-height: 26px;">OrderServiceTest</span> </span>{<br> <span style="color: #9B9B9B;line-height: 26px;">@Autowired</span> <span style="color: #569CD6;line-height: 26px;">private</span> OrderService orderService;<br><br> <span style="color: #9B9B9B;line-height: 26px;">@Test</span><br> <span style="color: #DCDCDC;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #569CD6;line-height: 26px;">void</span> <span style="color: #DCDCDC;line-height: 26px;">buyOrderTest</span><span style="color: #DCDCDC;line-height: 26px;">()</span> </span>{<br> orderService.buyOrder(<span style="color: #D69D85;line-height: 26px;">"732171109"</span>);<br> }<br>}<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">执行结果如下:</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #B8D7A3;line-height: 26px;">2022</span>-<span style="color: #B8D7A3;line-height: 26px;">04</span>-<span style="color: #B8D7A3;line-height: 26px;">24</span> <span style="color: #B8D7A3;line-height: 26px;">10</span>:<span style="color: #B8D7A3;line-height: 26px;">13</span>:<span style="color: #B8D7A3;line-height: 26px;">17.535</span> INFO <span style="color: #B8D7A3;line-height: 26px;">44272</span> --- [ main] c.c.m.e.listener.OrderProductListener : <span style="color: #B8D7A3;line-height: 26px;">732171109</span>:校验订单商品价格耗时:(<span style="color: #B8D7A3;line-height: 26px;">2008</span>)毫秒<br><span style="color: #B8D7A3;line-height: 26px;">2022</span>-<span style="color: #B8D7A3;line-height: 26px;">04</span>-<span style="color: #B8D7A3;line-height: 26px;">24</span> <span style="color: #B8D7A3;line-height: 26px;">10</span>:<span style="color: #B8D7A3;line-height: 26px;">13</span>:<span style="color: #B8D7A3;line-height: 26px;">17.536</span> INFO <span style="color: #B8D7A3;line-height: 26px;">44272</span> --- [ main] c.c.mingyue.event.service.OrderService : 任务全部完成,总耗时:(<span style="color: #B8D7A3;line-height: 26px;">2009</span>)毫秒<br></code></pre> <blockquote data-tool="mdnice编辑器" style="border-width: initial;border-style: none;border-color: initial;display: block;font-size: 0.9em;overflow: auto;margin-bottom: 20px;margin-top: 20px;font-style: normal;padding: 10px;line-height: 1.8;border-radius: 0px 0px 10px 10px;color: rgb(14, 136, 235);background: rgb(255, 255, 255);box-shadow: rgb(132, 161, 168) 0px 10px 15px;"> <p style="padding-top: 8px;padding-bottom: 8px;letter-spacing: 0.2em;word-spacing: 0.1em;margin: 0px;line-height: 26px;color: #0e88eb;font-size: 15px;display: block;">基于微服务的思想,构建在 B2C 电商场景下的项目实战。核心技术栈,是 Spring Boot + Dubbo 。未来,会重构成 Spring Cloud Alibaba 。</p> <p style="padding-top: 8px;padding-bottom: 8px;letter-spacing: 0.2em;word-spacing: 0.1em;margin: 0px;line-height: 26px;color: #0e88eb;font-size: 15px;display: block;">项目地址:https://github.com/YunaiV/onemall</p> </blockquote> <h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;color: black;font-size: 22px;text-align: left;margin: 20px 10px 0px 0px;"><span style="display: none;"></span><span style="font-family: STHeitiSC-Light;font-size: 22px;color: #0e88eb;font-weight: bolder;display: inline-block;padding-left: 10px;border-left: 5px solid #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">Spring Event 异步使用</a></span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">有些业务场景不需要在一次请求中同步完成,比如邮件发送、短信发送等。</p> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">1.自定义事件</a></span><span style="display: none;"></span></h3> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #569CD6;line-height: 26px;">import</span> lombok.AllArgsConstructor;<br><span style="color: #569CD6;line-height: 26px;">import</span> lombok.Data;<br><br><span style="color: #57A64A;font-style: italic;line-height: 26px;">/**<br> * <span style="color: #608B4E;line-height: 26px;">@author</span> Strive<br> * <span style="color: #608B4E;line-height: 26px;">@date</span> 2022/4/24 10:18<br> * <span style="color: #608B4E;line-height: 26px;">@description</span><br> */</span><br><span style="color: #9B9B9B;line-height: 26px;">@Data</span><br><span style="color: #9B9B9B;line-height: 26px;">@AllArgsConstructor</span><br><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #B8D7A3;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">class</span> <span style="color: #DCDCDC;line-height: 26px;">MsgEvent</span> </span>{<br><br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">/** 该类型事件携带的信息 */</span><br> <span style="color: #569CD6;line-height: 26px;">public</span> String orderId;<br>}<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">2.定义监听器</a></span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">推荐使用 <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@EventListener</code> 注解</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #569CD6;line-height: 26px;">import</span> com.csp.mingyue.event.events.MsgEvent;<br><span style="color: #569CD6;line-height: 26px;">import</span> lombok.SneakyThrows;<br><span style="color: #569CD6;line-height: 26px;">import</span> lombok.extern.slf4j.Slf4j;<br><span style="color: #569CD6;line-height: 26px;">import</span> org.springframework.context.event.EventListener;<br><span style="color: #569CD6;line-height: 26px;">import</span> org.springframework.stereotype.Component;<br><br><span style="color: #57A64A;font-style: italic;line-height: 26px;">/**<br> * <span style="color: #608B4E;line-height: 26px;">@author</span> Strive<br> * <span style="color: #608B4E;line-height: 26px;">@date</span> 2022/4/24 10:20<br> * <span style="color: #608B4E;line-height: 26px;">@description</span><br> */</span><br><span style="color: #9B9B9B;line-height: 26px;">@Slf</span>4j<br><span style="color: #9B9B9B;line-height: 26px;">@Component</span><br><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #B8D7A3;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">class</span> <span style="color: #DCDCDC;line-height: 26px;">MsgListener</span> </span>{<br><br> <span style="color: #9B9B9B;line-height: 26px;">@SneakyThrows</span><br> <span style="color: #9B9B9B;line-height: 26px;">@EventListener</span>(MsgEvent<span style="color: #B8D7A3;line-height: 26px;">.<span style="color: #569CD6;line-height: 26px;">class</span>)<br> <span style="color: #DCDCDC;line-height: 26px;">public</span> <span style="color: #DCDCDC;line-height: 26px;">void</span> <span style="color: #DCDCDC;line-height: 26px;">sendMsg</span>(<span style="color: #DCDCDC;line-height: 26px;">MsgEvent</span> <span style="color: #DCDCDC;line-height: 26px;">event</span>) </span>{<br> String orderId = event.getOrderId();<br> <span style="color: #569CD6;line-height: 26px;">long</span> start = System.currentTimeMillis();<br> log.info(<span style="color: #D69D85;line-height: 26px;">"开发发送短信"</span>);<br> log.info(<span style="color: #D69D85;line-height: 26px;">"开发发送邮件"</span>);<br> Thread.sleep(<span style="color: #B8D7A3;line-height: 26px;">4000</span>);<br> <span style="color: #569CD6;line-height: 26px;">long</span> end = System.currentTimeMillis();<br> log.info(<span style="color: #D69D85;line-height: 26px;">"{}:发送短信、邮件耗时:({})毫秒"</span>, orderId, (end - start));<br> }<br>}<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">3.定义发布者</a></span><span style="display: none;"></span></h3> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #57A64A;font-style: italic;line-height: 26px;">/**<br> * 下单<br> *<br> * <span style="color: #608B4E;line-height: 26px;">@param</span> orderId 订单ID<br> */</span><br><span style="color: #DCDCDC;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">public</span> String <span style="color: #DCDCDC;line-height: 26px;">buyOrder</span><span style="color: #DCDCDC;line-height: 26px;">(String orderId)</span> </span>{<br> <span style="color: #569CD6;line-height: 26px;">long</span> start = System.currentTimeMillis();<br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">// 1.查询订单详情</span><br><br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">// 2.检验订单价格 (同步处理)</span><br> applicationContext.publishEvent(<span style="color: #569CD6;line-height: 26px;">new</span> OrderProductEvent(<span style="color: #569CD6;line-height: 26px;">this</span>, orderId));<br><br> <span style="color: #57A64A;font-style: italic;line-height: 26px;">// 3.短信通知(异步处理)</span><br> applicationContext.publishEvent(<span style="color: #569CD6;line-height: 26px;">new</span> MsgEvent(orderId));<br><br> <span style="color: #569CD6;line-height: 26px;">long</span> end = System.currentTimeMillis();<br> log.info(<span style="color: #D69D85;line-height: 26px;">"任务全部完成,总耗时:({})毫秒"</span>, end - start);<br> <span style="color: #569CD6;line-height: 26px;">return</span> <span style="color: #D69D85;line-height: 26px;">"购买成功"</span>;<br>}<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">4.单测执行(同步)</a></span><span style="display: none;"></span></h3> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #9B9B9B;line-height: 26px;">@Test</span><br><span style="color: #DCDCDC;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #569CD6;line-height: 26px;">void</span> <span style="color: #DCDCDC;line-height: 26px;">buyOrderTest</span><span style="color: #DCDCDC;line-height: 26px;">()</span> </span>{<br> orderService.buyOrder(<span style="color: #D69D85;line-height: 26px;">"732171109"</span>);<br>}<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">执行结果如下:</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #B8D7A3;line-height: 26px;">2022</span>-<span style="color: #B8D7A3;line-height: 26px;">04</span>-<span style="color: #B8D7A3;line-height: 26px;">24</span> <span style="color: #B8D7A3;line-height: 26px;">10</span>:<span style="color: #B8D7A3;line-height: 26px;">24</span>:<span style="color: #B8D7A3;line-height: 26px;">13.905</span> INFO <span style="color: #B8D7A3;line-height: 26px;">54848</span> --- [ main] c.c.m.e.listener.OrderProductListener : <span style="color: #B8D7A3;line-height: 26px;">732171109</span>:校验订单商品价格耗时:(<span style="color: #B8D7A3;line-height: 26px;">2004</span>)毫秒<br><span style="color: #B8D7A3;line-height: 26px;">2022</span>-<span style="color: #B8D7A3;line-height: 26px;">04</span>-<span style="color: #B8D7A3;line-height: 26px;">24</span> <span style="color: #B8D7A3;line-height: 26px;">10</span>:<span style="color: #B8D7A3;line-height: 26px;">24</span>:<span style="color: #B8D7A3;line-height: 26px;">13.906</span> INFO <span style="color: #B8D7A3;line-height: 26px;">54848</span> --- [ main] c.c.mingyue.event.listener.MsgListener : 开发发送短信<br><span style="color: #B8D7A3;line-height: 26px;">2022</span>-<span style="color: #B8D7A3;line-height: 26px;">04</span>-<span style="color: #B8D7A3;line-height: 26px;">24</span> <span style="color: #B8D7A3;line-height: 26px;">10</span>:<span style="color: #B8D7A3;line-height: 26px;">24</span>:<span style="color: #B8D7A3;line-height: 26px;">13.907</span> INFO <span style="color: #B8D7A3;line-height: 26px;">54848</span> --- [ main] c.c.mingyue.event.listener.MsgListener : 开发发送邮件<br><span style="color: #B8D7A3;line-height: 26px;">2022</span>-<span style="color: #B8D7A3;line-height: 26px;">04</span>-<span style="color: #B8D7A3;line-height: 26px;">24</span> <span style="color: #B8D7A3;line-height: 26px;">10</span>:<span style="color: #B8D7A3;line-height: 26px;">24</span>:<span style="color: #B8D7A3;line-height: 26px;">17.908</span> INFO <span style="color: #B8D7A3;line-height: 26px;">54848</span> --- [ main] c.c.mingyue.event.listener.MsgListener : <span style="color: #B8D7A3;line-height: 26px;">732171109</span>:发送短信、邮件耗时:(<span style="color: #B8D7A3;line-height: 26px;">4002</span>)毫秒<br><span style="color: #B8D7A3;line-height: 26px;">2022</span>-<span style="color: #B8D7A3;line-height: 26px;">04</span>-<span style="color: #B8D7A3;line-height: 26px;">24</span> <span style="color: #B8D7A3;line-height: 26px;">10</span>:<span style="color: #B8D7A3;line-height: 26px;">24</span>:<span style="color: #B8D7A3;line-height: 26px;">17.908</span> INFO <span style="color: #B8D7A3;line-height: 26px;">54848</span> --- [ main] c.c.mingyue.event.service.OrderService : 任务全部完成,总耗时:(<span style="color: #B8D7A3;line-height: 26px;">6008</span>)毫秒<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">5.开启异步</a></span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">启动类增加 <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@EnableAsync</code> 注解</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #9B9B9B;line-height: 26px;">@EnableAsync</span><br><span style="color: #9B9B9B;line-height: 26px;">@SpringBootApplication</span><br><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #B8D7A3;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">class</span> <span style="color: #DCDCDC;line-height: 26px;">MingYueSpringbootEventApplication</span> </span>{<br><br> <span style="color: #DCDCDC;line-height: 26px;"><span style="color: #569CD6;line-height: 26px;">public</span> <span style="color: #569CD6;line-height: 26px;">static</span> <span style="color: #569CD6;line-height: 26px;">void</span> <span style="color: #DCDCDC;line-height: 26px;">main</span><span style="color: #DCDCDC;line-height: 26px;">(String[] args)</span> </span>{<br> SpringApplication.run(MingYueSpringbootEventApplication<span style="color: #B8D7A3;line-height: 26px;">.<span style="color: #569CD6;line-height: 26px;">class</span>, <span style="color: #DCDCDC;line-height: 26px;">args</span>)</span>;<br> }<br>}<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;"><code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">Listener</code> 类需要开启异步的方法增加 <code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">@Async</code> 注解</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/hzVGicX27IG2kREVLZKwASorZF9aCvdIW9P6NjvsiaAMWcqmkRAZMunm1oHFgYoibxrK1pTvUEbAA9QPX8SYsMyV77gI0NsicIY0/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(30, 30, 30);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #DCDCDC;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #1E1E1E;border-radius: 5px;"><span style="color: #9B9B9B;line-height: 26px;">@Async</span><br><span style="color: #9B9B9B;line-height: 26px;">@SneakyThrows</span><br><span style="color: #9B9B9B;line-height: 26px;">@EventListener</span>(MsgEvent<span style="color: #B8D7A3;line-height: 26px;">.<span style="color: #569CD6;line-height: 26px;">class</span>)<br><span style="color: #DCDCDC;line-height: 26px;">public</span> <span style="color: #DCDCDC;line-height: 26px;">void</span> <span style="color: #DCDCDC;line-height: 26px;">sendMsg</span>(<span style="color: #DCDCDC;line-height: 26px;">MsgEvent</span> <span style="color: #DCDCDC;line-height: 26px;">event</span>) </span>{<br> String orderId = event.getOrderId();<br> <span style="color: #569CD6;line-height: 26px;">long</span> start = System.currentTimeMillis();<br> log.info(<span style="color: #D69D85;line-height: 26px;">"开发发送短信"</span>);<br> log.info(<span style="color: #D69D85;line-height: 26px;">"开发发送邮件"</span>);<br> Thread.sleep(<span style="color: #B8D7A3;line-height: 26px;">4000</span>);<br> <span style="color: #569CD6;line-height: 26px;">long</span> end = System.currentTimeMillis();<br> log.info(<span style="color: #D69D85;line-height: 26px;">"{}:发送短信、邮件耗时:({})毫秒"</span>, orderId, (end - start));<br>}<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;font-size: 18px;color: #0e88eb;"><span style="display: none;"></span><span style="font-size: 18px;color: #0e88eb;"><a href="https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247487551&idx=1&sn=18f64ba49f3f0f9d8be9d1fdef8857d9&scene=21#wechat_redirect" style="text-decoration: none;word-wrap: break-word;font-weight: bold;color: #0e88eb;border-bottom: 0px solid #ff3502;font-family: STHeitiSC-Light;" data-linktype="2">6.单测执行(异步)</a></span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;color: black;margin: 10px 10px;line-height: 1.75;letter-spacing: 0.2em;font-size: 15px;word-spacing: 0.1em;">发送短信的线程显示 ,主线程结束后(总耗时:(2017)毫秒)控制台停止打印了</p> </section>
作者:微信小助手
作者:微信小助手
<section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="color: black;line-height: 1.6;word-spacing: 0px;letter-spacing: 0px;word-break: break-word;overflow-wrap: break-word;text-align: left;font-size: 16px;padding: 0px 13px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;" data-mpa-powered-by="yiban.io"> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">MyISAM 仅仅支持表级锁(table-level locking),一锁就锁整张表,这在并发写的情况下性非常差。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">InnoDB 不光支持表级锁(table-level locking),还支持行级锁(row-level locking),默认为行级锁。行级锁的粒度更小,仅对相关的记录上锁即可(对一行或者多行记录加锁),所以对于并发写入操作来说, InnoDB 的性能更高。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;"><strong style="font-weight: 700;color: #3e4ca3;">表级锁和行级锁对比</strong> :</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;list-style-type: disc;" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">表级锁:</strong> MySQL 中锁定粒度最大的一种锁,是针对非索引字段加的锁,对当前操作的整张表加锁,实现简单,资源消耗也比较少,加锁快,不会出现死锁。其锁定粒度最大,触发锁冲突的概率最高,并发度最低,MyISAM 和 InnoDB 引擎都支持表级锁。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">行级锁:</strong> MySQL 中锁定粒度最小的一种锁,是针对索引字段加的锁,只针对当前操作的记录进行加锁。行级锁能大大减少数据库操作的冲突。其加锁粒度最小,并发度高,但加锁的开销也最大,加锁慢,会出现死锁。 </section></li> </ul> <h3 data-tool="mdnice编辑器" style="letter-spacing: 0.07em;margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;color: black;font-size: 20px;text-align: left;line-height: 1.4;margin: 27px 0 27px;"><span style="background-image: url("https://mmbiz.qpic.cn/mmbiz_svg/qbvaL9taELswnEhChUF51DDOMH1Fshw6eV5bGdng9GFic0UKouRaGlibbQOOQialibccJIRCqibQ6hqfoLz1XGp0jQILIdJcibhWWW/640?wx_fmt=svg");background-size: 15px 15px;display: inline-block;width: 15px;height: 15px;line-height: 15px;"></span><span style="display: none;"></span><span style="font-size: 18px;font-weight: bold;display: inline-block;margin-left: 8px;color: #3e4ca3;">行级锁的使用有什么注意事项?</span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">InnoDB 的行锁是针对索引字段加的锁,表级锁是针对非索引字段加的锁。当我们执行 <code style="word-wrap: break-word;border-radius: 4px;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;letter-spacing: 0.07em;font-size: 13px;color: #9b6e23;background-color: #FFF9E3;padding: 3px;margin: 0px;">UPDATE</code>、<code style="word-wrap: break-word;border-radius: 4px;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;letter-spacing: 0.07em;font-size: 13px;color: #9b6e23;background-color: #FFF9E3;padding: 3px;margin: 0px;">DELETE</code> 语句时,如果 <code style="word-wrap: break-word;border-radius: 4px;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;letter-spacing: 0.07em;font-size: 13px;color: #9b6e23;background-color: #FFF9E3;padding: 3px;margin: 0px;">WHERE</code>条件中字段没有命中索引或者索引失效的话,就会导致扫描全表对表中的所有记录进行加锁。这个在我们日常工作开发中经常会遇到,一定要多多注意!!!</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">不过,很多时候即使用了索引也有可能会走全表扫描,这是因为 MySQL 优化器的原因。</p> <h3 data-tool="mdnice编辑器" style="letter-spacing: 0.07em;margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;color: black;font-size: 20px;text-align: left;line-height: 1.4;margin: 27px 0 27px;"><span style="background-image: url("https://mmbiz.qpic.cn/mmbiz_svg/qbvaL9taELswnEhChUF51DDOMH1Fshw6eV5bGdng9GFic0UKouRaGlibbQOOQialibccJIRCqibQ6hqfoLz1XGp0jQILIdJcibhWWW/640?wx_fmt=svg");background-size: 15px 15px;display: inline-block;width: 15px;height: 15px;line-height: 15px;"></span><span style="display: none;"></span><span style="font-size: 18px;font-weight: bold;display: inline-block;margin-left: 8px;color: #3e4ca3;">共享锁和排他锁呢?</span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">不论是表级锁还是行级锁,都存在共享锁(Share Lock,S 锁)和排他锁(Exclusive Lock,X 锁)这两类:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;list-style-type: disc;" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">共享锁(S 锁)</strong> :又称读锁,事务在读取记录的时候获取共享锁,允许多个事务同时获取(锁兼容)。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">排他锁(X 锁)</strong> :又称写锁/独占锁,事务在修改记录的时候获取排他锁,不允许多个事务同时获取。如果一个记录已经被加了排他锁,那其他事务不能再对这条事务加任何类型的锁(锁不兼容)。 </section></li> </ul> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">排他锁与任何的锁都不兼容,共享锁仅和共享锁兼容。</p> <section data-tool="mdnice编辑器" style="overflow-x: auto;"> <table> <thead> <tr style="border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(204, 204, 204);background-color: white;"> <th style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-weight: bold;background-color: rgb(240, 240, 240);color: rgb(62, 76, 163);font-size: 13px;letter-spacing: 0.07em;min-width: 85px;text-align: left;"><br></th> <th style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-weight: bold;background-color: rgb(240, 240, 240);color: rgb(62, 76, 163);font-size: 13px;letter-spacing: 0.07em;min-width: 85px;text-align: left;">S 锁</th> <th style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-weight: bold;background-color: rgb(240, 240, 240);color: rgb(62, 76, 163);font-size: 13px;letter-spacing: 0.07em;min-width: 85px;text-align: left;">X 锁</th> </tr> </thead> <tbody style="border-width: 0px;border-style: initial;border-color: initial;"> <tr style="border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(204, 204, 204);background-color: white;"> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;letter-spacing: 0.07em;min-width: 85px;text-align: left;">S 锁</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;letter-spacing: 0.07em;min-width: 85px;text-align: left;">不冲突</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;letter-spacing: 0.07em;min-width: 85px;text-align: left;">冲突</td> </tr> <tr style="border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(204, 204, 204);background-color: rgb(248, 248, 248);"> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;letter-spacing: 0.07em;min-width: 85px;text-align: left;">X 锁</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;letter-spacing: 0.07em;min-width: 85px;text-align: left;">冲突</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;letter-spacing: 0.07em;min-width: 85px;text-align: left;">冲突</td> </tr> </tbody> </table> </section> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">由于 MVCC 的存在,对于一般的 <code style="word-wrap: break-word;border-radius: 4px;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;letter-spacing: 0.07em;font-size: 13px;color: #9b6e23;background-color: #FFF9E3;padding: 3px;margin: 0px;">SELECT</code> 语句,InnoDB 不会加任何锁。不过, 你可以通过以下语句显式加共享锁或排他锁。</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/qbvaL9taELswnEhChUF51DDOMH1Fshw6ugesRicVvLbfNN8OibcB8Oyl2cLeUVd3QOw1pOPnw7d1ksQSrlibLVVbNYHWzoYBibBO/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(40, 44, 52);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: #abb2bf;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #282c34;border-radius: 5px;"><span style="color: #5c6370;font-style: italic;line-height: 26px;"># 共享锁</span><br><span style="color: #c678dd;line-height: 26px;">SELECT</span> ... <span style="color: #c678dd;line-height: 26px;">LOCK</span> <span style="color: #c678dd;line-height: 26px;">IN</span> <span style="color: #c678dd;line-height: 26px;">SHARE</span> <span style="color: #c678dd;line-height: 26px;">MODE</span>;<br><span style="color: #5c6370;font-style: italic;line-height: 26px;"># 排他锁</span><br><span style="color: #c678dd;line-height: 26px;">SELECT</span> ... <span style="color: #c678dd;line-height: 26px;">FOR</span> <span style="color: #c678dd;line-height: 26px;">UPDATE</span>;<br></code></pre> <h3 data-tool="mdnice编辑器" style="letter-spacing: 0.07em;margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;color: black;font-size: 20px;text-align: left;line-height: 1.4;margin: 27px 0 27px;"><span style="background-image: url("https://mmbiz.qpic.cn/mmbiz_svg/qbvaL9taELswnEhChUF51DDOMH1Fshw6eV5bGdng9GFic0UKouRaGlibbQOOQialibccJIRCqibQ6hqfoLz1XGp0jQILIdJcibhWWW/640?wx_fmt=svg");background-size: 15px 15px;display: inline-block;width: 15px;height: 15px;line-height: 15px;"></span><span style="display: none;"></span><span style="font-size: 18px;font-weight: bold;display: inline-block;margin-left: 8px;color: #3e4ca3;">意向锁有什么作用?</span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">如果需要用到表锁的话,如何判断表中的记录没有行锁呢?一行一行遍历肯定是不行,性能太差。我们需要用到一个叫做意向锁的东东来快速判断是否可以对某个表使用表锁。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">意向锁是表级锁,共有两种:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;list-style-type: disc;" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">意向共享锁(Intention Shared Lock,IS 锁)</strong>:事务有意向对表中的某些加共享锁(S 锁),加共享锁前必须先取得该表的 IS 锁。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">意向排他锁(Intention Exclusive Lock,IX 锁)</strong>:事务有意向对表中的某些记录加排他锁(X 锁),加排他锁之前必须先取得该表的 IX 锁。 </section></li> </ul> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">意向锁是有数据引擎自己维护的,用户无法手动操作意向锁,在为数据行加共享 / 排他锁之前,InooDB 会先获取该数据行所在在数据表的对应意向锁。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">意向锁之间是互相兼容的。</p> <section data-tool="mdnice编辑器" style="overflow-x: auto;"> <table> <thead> <tr style="border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(204, 204, 204);background-color: white;"> <th style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-weight: bold;background-color: rgb(240, 240, 240);color: rgb(62, 76, 163);font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;"><br></th> <th style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-weight: bold;background-color: rgb(240, 240, 240);color: rgb(62, 76, 163);font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">IS 锁</th> <th style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-weight: bold;background-color: rgb(240, 240, 240);color: rgb(62, 76, 163);font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">IX 锁</th> </tr> </thead> <tbody style="border-width: 0px;border-style: initial;border-color: initial;"> <tr style="border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(204, 204, 204);background-color: white;"> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">IS 锁</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">兼容</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">兼容</td> </tr> <tr style="border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(204, 204, 204);background-color: rgb(248, 248, 248);"> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">IX 锁</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">兼容</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">兼容</td> </tr> </tbody> </table> </section> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">意向锁和共享锁和排它锁互斥(这里指的是表级别的共享锁和排他锁,意向锁不会与行级的共享锁和排他锁互斥)。</p> <section data-tool="mdnice编辑器" style="overflow-x: auto;"> <table> <thead> <tr style="border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(204, 204, 204);background-color: white;"> <th style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-weight: bold;background-color: rgb(240, 240, 240);color: rgb(62, 76, 163);font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;"><br></th> <th style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-weight: bold;background-color: rgb(240, 240, 240);color: rgb(62, 76, 163);font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">IS 锁</th> <th style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-weight: bold;background-color: rgb(240, 240, 240);color: rgb(62, 76, 163);font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">IX 锁</th> </tr> </thead> <tbody style="border-width: 0px;border-style: initial;border-color: initial;"> <tr style="border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(204, 204, 204);background-color: white;"> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">S 锁</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">兼容</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">互斥</td> </tr> <tr style="border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(204, 204, 204);background-color: rgb(248, 248, 248);"> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">X 锁</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">互斥</td> <td style="border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 5px 10px;font-size: 13px;text-align: center;letter-spacing: 0.07em;min-width: 85px;">互斥</td> </tr> </tbody> </table> </section> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">《MySQL 技术内幕 InnoDB 存储引擎》这本书对应的描述应该是笔误了。</p> <figure data-tool="mdnice编辑器" style="display: flex;flex-direction: column;justify-content: center;align-items: center;margin: 0;margin-top: 20px;margin-bottom: 20px;"> <img class="rich_pages wxw-img" data-ratio="0.6470588235294118" src="/upload/492a29dde7458d8f39f47cd67d61808f.png" data-type="png" data-w="850" style="margin: 0 auto;max-width: 100%;width: 100%;border-radius: 5px;display: block;margin-bottom: 10px;height: auto;border-radius: 5px;"> </figure> <h3 data-tool="mdnice编辑器" style="letter-spacing: 0.07em;margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;color: black;font-size: 20px;text-align: left;line-height: 1.4;margin: 27px 0 27px;"><span style="background-image: url("https://mmbiz.qpic.cn/mmbiz_svg/qbvaL9taELswnEhChUF51DDOMH1Fshw6eV5bGdng9GFic0UKouRaGlibbQOOQialibccJIRCqibQ6hqfoLz1XGp0jQILIdJcibhWWW/640?wx_fmt=svg");background-size: 15px 15px;display: inline-block;width: 15px;height: 15px;line-height: 15px;"></span><span style="display: none;"></span><span style="font-size: 18px;font-weight: bold;display: inline-block;margin-left: 8px;color: #3e4ca3;">InnoDB 有哪几类行锁?</span><span style="display: none;"></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">MySQL InnoDB 支持三种行锁定方式:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;list-style-type: disc;" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">记录锁(Record Lock)</strong> :也被称为记录锁,属于单个行记录上的锁。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">间隙锁(Gap Lock)</strong> :锁定一个范围,不包括记录本身。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">临键锁(Next-key Lock)</strong> :Record Lock+Gap Lock,锁定一个范围,包含记录本身。记录锁只能锁住已经存在的记录,为了避免插入新记录,需要依赖间隙锁。 </section></li> </ul> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;font-size: 16px;margin: 1.8em 0 1.8em;padding: 0;line-height: 1.8em;color: #000000;letter-spacing: 0.07em;">InnoDB 的默认隔离级别 REPEATABLE-READ(可重读)是可以解决幻读问题发生的,主要有下面两种情况:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;list-style-type: disc;" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">快照读</strong> :由 MVCC 机制来保证不出现幻读。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;text-align: left;font-weight: 500;color: #000000;line-height: 2em;letter-spacing: 0.07em;"> <strong style="font-weight: 700;color: #3e4ca3;">当前读</strong> :使用 Next-Key Lock 进行加锁来保证不出现幻读。 </section></li> </ul> <p data-tool="mdnice编辑器" style="margin: 0px 0px 16px;padding: 8px 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;color: rgb(0, 0, 0);font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif;text-size-adjust: auto;line-height: 1.75em;"><strong style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 2px;color: rgb(52, 152, 219);"><span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;color: rgb(136, 136, 136);font-family: PingFangSC-Light;font-size: 11px;letter-spacing: 0.544px;word-spacing: 2px;caret-color: rgb(0, 0, 0);">·········· END ··············</span></strong></p> <p data-tool="mdnice编辑器" style="margin: 0px 0px 16px;padding: 8px 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;color: rgb(0, 0, 0);font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif;text-size-adjust: auto;line-height: 1.75em;"><span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 2px;text-align: left;color: rgb(255, 76, 65);">欢迎加入我的</span><a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247519201&idx=1&sn=b910e72ec34a1447ecece7e993bc977d&chksm=cea1c42af9d64d3c69ba4849fc9175b26a2219d67c2aa6dabb7b73d95f4c41b8a9461e818243&token=1878783075&lang=zh_CN&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(0, 132, 255);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 2px;text-align: left;border-bottom: 1px solid rgb(0, 132, 255);">知识星球</a><span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 2px;text-align: left;color: rgb(255, 76, 65);">获取更多面试干货</span><span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 2px;text-align: left;">,</span><a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247519384&idx=1&sn=bc7e71af75350b755f04ca4178395b1a&chksm=cea1c353f9d64a458f797696d4144b4d6e58639371a4612b8e4d106d83a66d2289e7b2cd7431&token=660789642&lang=zh_CN&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 0px;outline: 0px;color: rgb(0, 132, 255);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 2px;text-align: left;border-bottom: 1px solid rgb(0, 132, 255);">《Java 面试指北》</a><span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;letter-spacing: 2px;text-align: left;color: rgb(255, 76, 65);">持续更新完善中!</span></p> <section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="margin: 0px;padding: 0px 10px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;color: rgb(34, 34, 34);background-color: rgb(255, 255, 255);letter-spacing: 2px;word-break: break-word;line-height: 1.75em;"> <figure data-tool="mdnice编辑器" style="margin: 10px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;display: flex;flex-direction: column;justify-content: center;align-items: center;"> <img class="rich_pages wxw-img" data-ratio="0.7566191446028513" src="/upload/b08317605b5794dd9da468f28af47459.png" data-type="png" data-w="982" style="margin: 0px auto;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;vertical-align: bottom;height: auto !important;display: block;visibility: visible !important;width: 677px !important;"> <figcaption style="margin: 5px 0px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;text-align: center;color: rgb(136, 136, 136);font-size: 14px;"> 《Java 面试指北》目录 </figcaption> </figure> </section> <section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="margin: 0px;padding: 0px 13px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;color: black;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: 0px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;background-color: rgb(255, 255, 255);line-height: 1.6;word-break: break-word;"> <p data-tool="mdnice编辑器" style="margin: 1.8em 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;font-size: 16px;line-height: 1.8em;color: rgb(0, 0, 0);letter-spacing: 0.07em;"><strong style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-weight: 700;color: rgb(62, 76, 163);">近期文章精选</strong> :</p> <ul data-tool="mdnice编辑器" class="list-paddingleft-1" style="margin: 8px 0px;padding: 0px 0px 0px 25px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;color: black;list-style-type: disc;"> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin: 5px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;text-align: left;font-weight: 500;color: rgb(0, 0, 0);line-height: 2em;letter-spacing: 0.07em;"> <a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247523521&idx=1&sn=2b8c20b0bea9102173141e22a6cbc8dd&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 1.5px 0px;outline: 0px;color: rgb(62, 76, 163);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;font-weight: bold;border-bottom: 1px solid rgb(62, 76, 163);">手写一个简化版的 Spring Cloud!</a> </section></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin: 5px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;text-align: left;font-weight: 500;color: rgb(0, 0, 0);line-height: 2em;letter-spacing: 0.07em;"> <a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247523262&idx=1&sn=d460a69f3c4b5bf0887129494f483af5&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 1.5px 0px;outline: 0px;color: rgb(62, 76, 163);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;font-weight: bold;border-bottom: 1px solid rgb(62, 76, 163);">去过外包是不是就废了?</a> </section></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin: 5px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;text-align: left;font-weight: 500;color: rgb(0, 0, 0);line-height: 2em;letter-spacing: 0.07em;"> <a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247522784&idx=1&sn=23d7ff9d51158e0e0cd085c327eb5a60&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 1.5px 0px;outline: 0px;color: rgb(62, 76, 163);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;font-weight: bold;border-bottom: 1px solid rgb(62, 76, 163);">3 年 Java 工作经验裸辞</a> </section></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin: 5px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;text-align: left;font-weight: 500;color: rgb(0, 0, 0);line-height: 2em;letter-spacing: 0.07em;"> <a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247520849&idx=1&sn=cc6631b532239ffcf615df7a79c018da&chksm=cea1dd9af9d6548cece4e4819a9ebf5002ace11fe61aed7e10c50a2ac29ae1b525587b508db8&token=2094715744&lang=zh_CN&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 1.5px 0px;outline: 0px;color: rgb(62, 76, 163);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;font-weight: bold;border-bottom: 1px solid rgb(62, 76, 163);">上岸美团、华为、字节!</a> </section></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin: 5px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;text-align: left;font-weight: 500;color: rgb(0, 0, 0);line-height: 2em;letter-spacing: 0.07em;"> <a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247521040&idx=1&sn=d075c86309fe0e1b46517062557df04f&chksm=cea1dcdbf9d655cdd16e38749b46a12428c996b56543f693dc696026773801133a86ba83bc2e&token=787347680&lang=zh_CN&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 1.5px 0px;outline: 0px;color: rgb(62, 76, 163);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;font-weight: bold;border-bottom: 1px solid rgb(62, 76, 163);">顺利找到工作了</a> </section></li> </ul> <p data-tool="mdnice编辑器" style="margin: 1.8em 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;font-size: 16px;line-height: 1.8em;color: rgb(0, 0, 0);letter-spacing: 0.07em;"><strong style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;font-weight: 700;color: rgb(62, 76, 163);">走近作者</strong> :</p> <ul data-tool="mdnice编辑器" class="list-paddingleft-1" style="margin: 8px 0px;padding: 0px 0px 0px 25px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;color: black;list-style-type: disc;"> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin: 5px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;text-align: left;font-weight: 500;color: rgb(0, 0, 0);line-height: 2em;letter-spacing: 0.07em;"> <a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247521759&idx=1&sn=60965fc8c51f65f7cd271f59fd19e124&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 1.5px 0px;outline: 0px;color: rgb(62, 76, 163);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;font-weight: bold;border-bottom: 1px solid rgb(62, 76, 163);">害,毕业三年了!</a> </section></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin: 5px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;text-align: left;font-weight: 500;color: rgb(0, 0, 0);line-height: 2em;letter-spacing: 0.07em;"> <a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247523015&idx=1&sn=fced64478481bd1270863d6ecad09843&chksm=cea1d50cf9d65c1ae9ae02b356fff872d36611443abb4c6a1fd75e6d9dc95c7003a6eafabf57&token=1526867871&lang=zh_CN&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 1.5px 0px;outline: 0px;color: rgb(62, 76, 163);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;font-weight: bold;border-bottom: 1px solid rgb(62, 76, 163);">我的网站又又又升级了!</a> </section></li> <li style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section style="margin: 5px 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;text-align: left;font-weight: 500;color: rgb(0, 0, 0);line-height: 2em;letter-spacing: 0.07em;"> <a href="https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247501206&idx=1&sn=b04d30f26380602a2b9029ba91169e64&scene=21#wechat_redirect" data-linktype="2" wah-hotarea="click" style="margin: 0px;padding: 1.5px 0px;outline: 0px;color: rgb(62, 76, 163);text-decoration: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;font-weight: bold;border-bottom: 1px solid rgb(62, 76, 163);">1049天,100K!简单复盘!</a> </section></li> </ul> <p data-tool="mdnice编辑器" style="margin: 1.8em 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;clear: both;min-height: 1em;font-size: 16px;line-height: 1.8em;color: rgb(0, 0, 0);letter-spacing: 0.07em;">如果本文对你有帮助的话,欢迎点赞&在看&分享,这对我继续分享&创作优质文章非常重要。感谢🙏🏻</p> </section> </section>
作者:微信小助手
<p style="text-align: center;margin-bottom: 8px;" data-mpa-powered-by="yiban.io"><img class="rich_pages wxw-img" data-backh="324" data-backw="578" data-ratio="0.5604203152364273" src="/upload/c2ed6bbb1d810a0bca0960668469976f.png" data-type="png" data-w="1142" style="width: 100%;height: auto;"></p> <section powered-by="xiumi.us" style="margin-bottom: 0px;"> <section> <section> <section powered-by="xiumi.us" style="letter-spacing: 0.544px;"> <section> <section label="Copyright Reserved by PLAYHUDONG." donone="shifuMouseDownCard('shifu_c_008')"> <section powered-by="xiumi.us" style="letter-spacing: 0.612px;"> <section> <section> <section powered-by="xiumi.us"> <section powered-by="xiumi.us"> <section label="Copyright Reserved by PLAYHUDONG." donone="shifuMouseDownCard('shifu_c_008')"> <section powered-by="xiumi.us"> <section> <section> <section powered-by="xiumi.us"> <section powered-by="xiumi.us"> <section label="Copyright Reserved by PLAYHUDONG." donone="shifuMouseDownCard('shifu_c_008')" style="margin-right: 0em;margin-left: 0em;padding: 0.5em 1em;border-style: none;background-color: rgb(235, 235, 235);"> <section style="text-align: left;line-height: 1.5em;"> <span style="color: rgb(136, 136, 136);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: 0.612px;text-align: justify;">导读:本文基于日常开发的版本管理做简单梳理,来分析软件版本的定义相关问题,总结本篇文章希望对从事相关工作的同学能够有所帮助或者启发。</span> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> </section> <p data-lake-id="eda075be9dd973cbb6819cbe9ee5fe25" style="font-size: 15px;color: rgb(64, 64, 64);line-height: 1.74;letter-spacing: 0.008em;outline-style: none;margin-bottom: 0px;"><br style="letter-spacing: 0.12px;white-space: normal;"></p> <article data-clipboard-cangjie="["root",{"copyFrom":1007547796},["p",{"uuid":"l3o5br0t78szqbrrcb7"},["span",{"data-type":"text"},["span",{"data-type":"leaf"},""]]],["p",{"uuid":"l3o5f8j2bosc8p2w90c"},["span",{"data-type":"text"},["span",{"data-type":"leaf"},""]],["img",{"id":"698dvc","name":"image.png","size":55094,"width":649,"height":221.55517241379312,"src":"https://alidocs.oss-cn-zhangjiakou.aliyuncs.com/res/E8K4nyRpv8ozqLbj/img/73504b75-8ea6-4235-b126-ae415862eb5c.png","extraData":{"resourceId":"e204a6e1-7166-4f61-92e1-e47e0d5741cf"}},["span",{"data-type":"text"},["span",{"data-type":"leaf"},""]]],["span",{"data-type":"text"},["span",{"data-type":"leaf"},""]]],["p",{"uuid":"l3o5f8wvgu13770fak6"},["span",{"data-type":"text"}