﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-xiaomage234</title><link>http://www.blogjava.net/xiaomage234/</link><description>生命本就是一次凄美的漂流，记忆中放不下的，永远是孩提时代的那一份浪漫与纯真！</description><language>zh-cn</language><lastBuildDate>Fri, 24 Apr 2026 03:33:12 GMT</lastBuildDate><pubDate>Fri, 24 Apr 2026 03:33:12 GMT</pubDate><ttl>60</ttl><item><title>如何系统性地学习分布式系统？[转]</title><link>http://www.blogjava.net/xiaomage234/archive/2020/11/26/435735.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 26 Nov 2020 08:20:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2020/11/26/435735.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/435735.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2020/11/26/435735.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/435735.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/435735.html</trackback:ping><description><![CDATA[<h2>from：https://www.infoq.cn/article/orvjbfycnrito5qiyfhf<br />前言</h2><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">学习一个知识之前，我觉得比较好的方式是先理解它的来龙去脉：即这个知识产生的过程，它解决了什么问题，它是怎么样解决的，还有它引入了哪些新的问题（没有银弹），这样我们才能比较好的抓到它的脉络和关键点，不会一开始就迷失在细节中。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">所以，在学习分布式系统之前，我们需要解决的第一个问题是：分布式系统解决了什么问题？</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h2>分布式系统解决了什么问题？</h2><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">第一个是单机性能瓶颈导致的成本问题，由于摩尔定律失效，廉价 PC 机性能的瓶颈无法继续突破，小型机和大型机能提高更高的单机性能，但是成本太大高，一般的公司很难承受；</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">第二个是用户量和数据量爆炸性的增大导致的成本问题，进入互联网时代，用户量爆炸性的增大，用户产生的数据量也在爆炸性的增大，但是单个用户或者单条数据的价值其实比软件时代（比如银行用户）的价值是只低不高，所以必须寻找更经济的方案；</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">第三个是业务高可用的要求，对于互联网的产品来说，都要求 7 * 24 小时提供服务，无法容忍停止服务等故障，而要提供高可用的服务，唯一的方式就是增加冗余来完成，这样就算单机系统可以支撑的服务，因为高可用的要求，也会变成一个分布式系统。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">基于上面的三个原因可以看出，在互联网时代，单机系统是无法解决成本和高可用问题的，但是这两个问题对几乎对所有的公司来说都是非常关键的问题，所以，从单机系统到分布式系统是无法避免的技术大潮流。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h2>分布式系统是怎么来解决问题的？</h2><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">那么，分布式系统是怎么来解决单机系统面临的成本和高可用问题呢？</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">其实思路很简单，就是将一些廉价的 PC 机通过网络连接起来，共同完成工作，并且在系统中提供冗余来解决高可用的问题。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h2>分布式系统引入了哪些新的问题？</h2><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">我们来看分布式系统的定义：分布式系统是由一组通过网络进行通信、为了完成共同的任务而协调工作的计算机节点组成的系统。在定义中，我们可用看出，分布式系统它通过多工作节点来解决单机系统面临的成本和可用性问题，但是它引入了对分布式系统内部工作节点的协调问题。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">我们经常说掌握一个知识需要理解它的前因后果，对于分布式系统来说，前因是「分布式系统解决了什么问题」，后果是「它是怎么做内部工作节点的协调」，所以我们要解决的第二个问题是：分布式系统是怎么做内部工作节点协调的？</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h2>分布式计算引入了哪些新的问题？</h2><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">先从简单的情况入手，对于分布式计算（无状态）的情况，系统内部的协调需要做哪些工作：</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>1.怎么样找到服务？</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">在分布式系统内部，会有不同的服务（角色），服务 A 怎么找到服务 B 是需要解决的问题，一般来说服务注册与发现机制是常用的思路，所以可以了解一下服务注册发现机制实现原理，并且可以思考服务注册发现是选择做成 AP 还是 CP 系统更合理（严格按 CAP 理论说，我们目前使用的大部分系统很难满足 C 或者 A 的，所以这里只是通常意义上的 AP 或者 CP）；</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>2.怎么样找到实例？</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">找到服务后，当前的请求应该选择发往服务的哪一个实例呢？一般来说，如果同一个服务的实例都是完全对等的（无状态），那么按负载均衡策略来处理就足够（轮询、权重、hash、一致性 hash，fair 等各种策略的适用场景）；如果同一个服务的实例不是对等的（有状态），那么需要通过路由服务（元数据服务等）先确定当前要访问的请求数据做哪一个实例上，然后再进行访问。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>3.怎么样避免雪崩？</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">系统雪崩是指故障的由于正反馈循序导致不断扩大规则的故障。一次雪崩通常是由于整个系统中一个很小的部分出现故障于引发，进而导致系统其它部分也出现故障。比如系统中某一个服务的一个实例出现故障，导致负载均衡将该实例摘除而引起其它实例负载升高，最终导致该服务的所有实例像多米诺骨牌一样一个一个全部出现故障。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">避免雪崩总体的策略比较简单，只要是两个思路，一个是快速失败和降级机制（熔断、降级、限流等），通过快速减少系统负载来避免雪崩的发生；另一个为弹性扩容机制，通过快速增加系统的服务能力来避免雪崩的发生。这个根据不同的场景可以做不同的选择，或者两个策略都使用。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">一般来说，快速失败会导致部分的请求失败，如果分布式系统内部对一致性要求很高的话，快速失败会带来系统数据不一致的问题，弹性扩容会是一个比较好的选择，但是弹性扩容的实现成本和响应时间比快速失败要大得多。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>4.怎么样监控告警？</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">对于一个分布式系统，如果我们不能很清楚地了解内部的状态，那么高可用是没有办法完全保障的，所以对分布式系统的监控（比如接口的时延和可用性等信息），分布式追踪 Trace，模拟故障的混沌工程，以及相关的告警等机制是一定要完善的；</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h2>分布式存储引入了哪些新的问题？</h2><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">接下来我们再来看分布式存储（有状态）的内部的协调是怎么做的，同时，前面介绍的分布式计算的协调方式在分布式存储中同样适用，就不再重复了：</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>1.分布式系统的理论与衡权</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">ACID、BASE 和 CAP 理论，了解这三个主题，推荐这一篇文章以及文章后面相关的参考文献：</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">英文版本：<a href="https://www.infoq.com/articles/cap-twelve-years-later-how-the-rules-have-changed/" title="" data-type="link" target="_blank" style="transition: all 0.3s ease 0s; text-decoration-line: none; overflow-wrap: break-word; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: #1458d4;">https://www.infoq.com/articles/cap-twelve-years-later-how-the-rules-have-changed/</a><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">中文版本：<a href="https://www.infoq.cn/article/cap-twelve-years-later-how-the-rules-have-changed/" title="" data-type="link" target="_blank" style="transition: all 0.3s ease 0s; text-decoration-line: none; overflow-wrap: break-word; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: #1458d4;">https://www.infoq.cn/article/cap-twelve-years-later-how-the-rules-have-changed/</a><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>2.怎么样做数据分片？</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">单机的存储能力是不可能存储所有的数据的，所以需要解决怎么将数据按一定的规则分别存储到不同的机器上，目前使用比较多的方案为：Hash、Consistent Hash 和 Range Based 分片策略，可以了解一下它们的优缺点和各自的应用场景；</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>3.怎么样做数据复制？</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">为什么满足系统的高可用要求，需要对数据做冗余处理，目前的方案主要为：中心化方案（主从复制、一致性协议比如 Raft 和 Paxos 等）和 去中心化的方案（Quorum 和 Vector Clock）了解一下它们的优缺点和各自的应用场景，以及对系统外部表现出来的数据一致性级别（线性一致性、顺序一致性、最终一致性等）；</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>4.怎么样做分布式事务？</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">对于分布式系统来说，要实现事务，首先需要有对并发事务进行排序的能力，这样在事务冲突的时候，确认哪个事务提供成功，哪个事务提交失败。对于单机系统来说这个完全不是问题，简单通过时间戳加序号的方式就可以实现，但是对于分布式系统来说，系统中机器的时间不能完全同步，并且单台机器序号也没用全局意义，按上面的方式说行不通的。不过整个系统选一台机器按单机的模式生产事务 ID 是可以的，同城多中心和短距离的异地多中心都没有问题，不过想做成全球分布式系统的话，那么每一次事务都要去一个节点去获取事务 ID 的成本太高（比如中国杭州到美国东部的 RTT 为 200 + ms ），Google 的 Spanner 是通过 GPS 和原子钟实现 TrueTime API 来解决这个问题从而实现全球分布式数据库的。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">有了事务 ID 后，通过 2PC 或者 3PC 协议来实现分布式事务的原子性，其他部分和单机事务差别不大，就不再细说来。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h2>进阶学习阶段</h2><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">到这里，对分布式系统脉络上有了基本的概念，接下来开始进入细节学习阶段，这也是非常幸苦的阶段，对于分布式系统的理解深入与否，对细节的深入度是很重要的评价指标，毕竟魔鬼在细节。这里可以往两个方面进行系统的学习：</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>1.从实践出发</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">研究目前比较常用的分布式系统的设计，HDFS 或者 GFS（分布式文件系统）、Kafka 和 Pulsar（分布式消息队列），Redis Cluster 和 Codis（分布式缓存），MySQL 的分库分表（传统关系型数据库的分布式方案），MongoDB 的 Replica Set 和 Sharing 机制集以及去中心化的 Cassandra（NoSQL 数据库），中心化的 TiDB 和去中心化的 CockroachDB（NewSQL），以及一些微服务框架等；</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h3>2.从理论出发</h3><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">从理论出发，研究分布式相关的论文，这里推荐一本书「Designing Data-Intensive Applications」（中文版本：数据密集型应用系统设计），先整体看书，对比较感兴趣的章节，再读一读该章节中涉及到的相关参考文献。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><h2>总结</h2><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;">本文从分布式系统解决的问题开始，再讨论它是怎么样来解决问题的，最后讨论了它引入了哪些新的问题，并且讨论这些新问题的解决办法，这个就是分布式系统大概的知识脉络。掌握这个知识脉络后，那么就可以从实践和理论两个角度结合起来深入细节研究分布式系统了。</p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><span data-type="strong" style="font-weight: 700;">参考</span></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><a href="https://www.zhihu.com/question/320812569/answer/1386491563" title="" data-type="link" target="_blank" style="transition: all 0.3s ease 0s; text-decoration-line: none; overflow-wrap: break-word; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: #1458d4;">知乎 | 如何系统性的学习分布式系统</a><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><a href="https://book.douban.com/subject/26197294/" title="" data-type="link" target="_blank" style="transition: all 0.3s ease 0s; text-decoration-line: none; overflow-wrap: break-word; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: #1458d4;">Martin Kleppmann.Designing Data-Intensive Applications</a><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><br /></p><p data-type="paragraph" style="margin: 0px; padding: 0px; line-height: 1.875; font-size: 16px; min-height: 30px; white-space: pre-wrap; color: #303030; font-family: &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, Verdana, &quot;Microsoft Yahei&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Sans Serif&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; background-color: #ffffff;"><a href="https://www.infoq.com/articles/cap-twelve-years-later-how-the-rules-have-changed/" title="" data-type="link" target="_blank" style="transition: all 0.3s ease 0s; text-decoration-line: none; overflow-wrap: break-word; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: #1458d4;">CAP Twelve Years Later: How the &#8220;Rules&#8221; Have Changed</a></p><img src ="http://www.blogjava.net/xiaomage234/aggbug/435735.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2020-11-26 16:20 <a href="http://www.blogjava.net/xiaomage234/archive/2020/11/26/435735.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基带、射频，到底是干什么用的？</title><link>http://www.blogjava.net/xiaomage234/archive/2020/11/24/435733.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Tue, 24 Nov 2020 02:41:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2020/11/24/435733.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/435733.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2020/11/24/435733.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/435733.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/435733.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: from:https://www.huxiu.com/article/351920.html本文来自微信公众号：&nbsp;鲜枣课堂（ID：xzclasscom），作者：小枣君，题图来自：视觉中国大家好，我是小枣君。今天我们来聊聊基带和射频。说起基带和射频，相信大家都不陌生。它们是通信行业里的两个常见概念，经常出现在我们面前。不过，越是常见的概念，网上的资料就越混乱，错误也就越多。这些错误给很多初...&nbsp;&nbsp;<a href='http://www.blogjava.net/xiaomage234/archive/2020/11/24/435733.html'>阅读全文</a><img src ="http://www.blogjava.net/xiaomage234/aggbug/435733.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2020-11-24 10:41 <a href="http://www.blogjava.net/xiaomage234/archive/2020/11/24/435733.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cat.1 现状[转]</title><link>http://www.blogjava.net/xiaomage234/archive/2020/11/06/435716.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Fri, 06 Nov 2020 06:36:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2020/11/06/435716.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/435716.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2020/11/06/435716.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/435716.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/435716.html</trackback:ping><description><![CDATA[from：https://mp.weixin.qq.com/s/s-AuC_IDS5GEamRwX286MQ<br /><br /><br /><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; clear: both; min-height: 1em; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;">随着紫光展锐、ASR 等芯片厂商发布性价比更高的 Cat.1 芯片之后，Cat.1 模组厂商扎堆发布了自家的模组，<br />使得市场上的 Cat.1 模组价格已经迅速降至 45-60 元，玩家众多，竞争惨烈，基本重走 NB-IOT 的老路 &#8212;&#8212; 量未起，价已跌。</p><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; clear: both; min-height: 1em; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;">Cat.1 芯片原厂：</p><ul style="margin: 0px; padding: 0px 0px 0px 32px; max-width: 100%; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;"><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">高通 MDM9207-1(2016 年发布)</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">紫光展锐春藤 8910DM（28nm工艺，集成蓝牙和WiFi 室内定位）</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">翱捷 ASR3601</span></p></li></ul><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; clear: both; min-height: 1em; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;">Cat.1 模组厂商（不完全统计）：</p><ul style="margin: 0px; padding: 0px 0px 0px 32px; max-width: 100%; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;"><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">中移物联网</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">移远通信</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">合宙电子</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">移柯通信</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">域格信息</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">广和通</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">芯讯通</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">高新兴物联</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">美格智能</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">有方科技</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">有人信息</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">信位通讯</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">锐骐(厦门)电子</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">深圳信可通讯</span></p></li></ul><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; clear: both; min-height: 1em; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;">Cat.1 优势</p><ul style="margin: 0px; padding: 0px 0px 0px 32px; max-width: 100%; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;"><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">相对 NB-IOT，其通信速率优势明显</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">相对 eMTC，其网络成本低</span></p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 0px; padding: 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; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;">相对 Cat.4，其具有一定的成本优势</span></p></li></ul><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; clear: both; min-height: 1em; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;">Cat.1 劣势：</p><ul style="margin: 0px; padding: 0px 0px 0px 32px; max-width: 100%; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;"><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; font-size: inherit; color: inherit; line-height: inherit;">现阶段芯片厂家少</p><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; font-size: inherit; color: inherit; line-height: inherit;">国外以高通为主，辅以 Sequans、Altair。</p><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; font-size: inherit; color: inherit; line-height: inherit;">国内主要是展锐和翱捷。</p></li><li style="margin: 0px 0px 0.5em; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; font-size: inherit; color: inherit; line-height: inherit;">现阶段价格偏高</p><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; font-size: inherit; color: inherit; line-height: inherit;">NB-IoT、Cat.1、Cat.4 模组价格：</p><figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; color: inherit; line-height: inherit;"><br /></figure></li></ul><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; clear: both; min-height: 1em; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;">cat1 的主要市场和应用场景：</p><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; clear: both; min-height: 1em; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;">Cat.1 仍处于商用初期，落地的应用场景和案例还较少，一些明确的场景包括了共享、金融支付、工业控制、车载支付、公网对讲、POS 等等。</p><h2><span style="margin: 0px 3px 0px 0px; padding: 3px 10px 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: inherit; line-height: inherit; display: inline-block; font-weight: normal; background: #ec4444; color: #ffffff; border-top-right-radius: 3px; border-top-left-radius: 3px;">总结</span></h2><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; clear: both; min-height: 1em; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;">工信部办公厅发布了《关于深入推进移动物联网全面发展的通知》（以下简称《通知》）同时为 NB-IOT 和 Cat.1 站台，未来 NB-IOT 依旧很香，Cat.1 则前途大好。</p><p style="margin: 1.5em 0px; padding: 0px; max-width: 100%; clear: both; min-height: 1em; color: inherit; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: inherit; text-align: justify; background-color: #ffffff; line-height: inherit; box-sizing: border-box !important; overflow-wrap: break-word !important;">随着新基建的启动，5G 打头，未来将是 NB-IOT、4G（包括 Cat.1）、5G 共同承载蜂窝物联网的连接，以应对不同层次的物联网业务需求。</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/435716.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2020-11-06 14:36 <a href="http://www.blogjava.net/xiaomage234/archive/2020/11/06/435716.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QoS等级 与 会话[转]</title><link>http://www.blogjava.net/xiaomage234/archive/2020/10/12/435690.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Mon, 12 Oct 2020 06:19:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2020/10/12/435690.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/435690.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2020/10/12/435690.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/435690.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/435690.html</trackback:ping><description><![CDATA[from：https://www.cnblogs.com/schips/p/12262587.html<br /><br /><br /><h2>背景</h2><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">QoS 等级 与 通信的流程有关，直接影响了整个通信。而且篇幅比较长，所以我觉得应该单独拎出来讲一下。</p><h2>概念</h2><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">QoS 代表了 服务质量等级。 设置上，由2 位 的二进制控制，且值不允许为 3(0x11)。</p><div style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px 0px 20px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff; overflow: auto;"><table style="-webkit-tap-highlight-color: transparent; width: 868px; border-style: solid; border-color: #dfdfdf; margin: 0px auto; border-collapse: collapse; word-break: break-word; overflow-x: auto !important;"><thead style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; border-top: 2px solid var(--color-text-primary)  !important; border-bottom: 2px solid var(--color-text-primary)  !important; background: var(--BackgroundColor)  !important;"><tr style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; background-color: #eeeeee; border-top: 1px solid var(--color-text-primary)  !important;"><th style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; background-color: #fafafa; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border: none !important; background-image: initial !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important;"><span style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: bolder;">QoS值</span></th><th style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; background-color: #fafafa; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border: none !important; background-image: initial !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important;"><span style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: bolder;">Bit 2</span></th><th style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; background-color: #fafafa; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border: none !important; background-image: initial !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important;"><span style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: bolder;">Bit 1</span></th><th style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; background-color: #fafafa; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border: none !important; background-image: initial !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important;"><span style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: bolder;">描述</span></th></tr></thead><tbody style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><tr style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; background-color: var(--color-white); border-top: 1px solid var(--color-text-primary)  !important;"><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">0</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">0</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">0</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">最多分发一次</td></tr><tr style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; background-color: var(--color-white); border-top: 1px solid var(--color-text-primary)  !important;"><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">1</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">0</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">1</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">至少分发一次</td></tr><tr style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; background-color: var(--color-white); border-top: 1px solid var(--color-text-primary)  !important;"><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">2</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">1</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">0</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">只分发一次</td></tr><tr style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; background-color: var(--color-white); border-top: 1px solid var(--color-text-primary)  !important; border-bottom: 2px solid var(--color-text-primary)  !important;"><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">-</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">1</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">1</td><td style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 8px 14px; border-collapse: collapse; min-width: 50px; word-break: break-all; white-space: nowrap; border-width: initial !important; border-style: none !important; border-color: initial !important; background: transparent !important;">保留位</td></tr></tbody></table></div><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">要注意的是，QoS 是&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Sender</code>&nbsp;和&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Receiver</code>&nbsp;之间达成的协议，不是&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Publisher</code>&nbsp;和&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Subscriber</code>&nbsp;之间达成的协议。</p><blockquote style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; background-color: #ffffff; margin-right: 0px; margin-left: 0px; border-top: 0px; border-bottom: 0px; border-right: 0px; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; border-left-width: 0.25em !important; border-left-color: #dfe2e5 !important; padding: 0px 1em !important; margin-top: 0px !important; margin-bottom: 16px !important; color: #6a737d !important;"><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px;">也就是说&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Publisher</code>&nbsp;发布一条 QoS1 的消息，只能保证&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Broker</code>&nbsp;能至少收到一次这个消息；至于对应的&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Subscriber</code>&nbsp;能否至少收到一次这个消息，还要取决于&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Subscriber</code>&nbsp;在&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Subscribe</code>&nbsp;的时候和&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Broker</code>&nbsp;协商的 QoS 等级。</p><blockquote style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-right: 0px; margin-left: 0px; border-top: 0px; border-bottom: 0px; border-right: 0px; border-left-width: 0.25em !important; border-left-color: #dfe2e5 !important; padding: 0px 1em !important; margin-top: 0px !important; margin-bottom: 0px !important; color: #6a737d !important;"><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px;">这里又牵扯出一个概念："QoS 降级"：在 MQTT 协议中，从 Broker 到 Subscriber 这段消息传递的实际 QoS 等于 "Publisher 发布消息时指定的 QoS 等级和 Subscriber 在订阅时与 Broker 协商的 QoS 等级，这两个 QoS 等级中的最小那一个。"</p></blockquote></blockquote><h3>QoS 0 的通信时序图</h3><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">此时，整个过程中的&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Sender</code>&nbsp;不关心&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Receiver</code>&nbsp;是否收到消息，它"尽力"发完消息，至于是否有人收到，它不在乎。</p><div data-processed="true" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; white-space: pre-wrap; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;"><svg id="mermaid-1602471087294" width="100%" xmlns="http://www.w3.org/2000/svg" height="100%" style="max-width:675px;" viewbox="-75 -50 675 336"><g></g><g><line id="actor0" x1="75" y1="5" x2="75" y2="285" stroke-width="0.5px" stroke="#999"></line><rect x="0" y="0" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="75" y="32.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="75" dy="0">发布者</tspan></text></g><g><line id="actor1" x1="275" y1="5" x2="275" y2="285" stroke-width="0.5px" stroke="#999"></line><rect x="200" y="0" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="275" y="32.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="275" dy="0">服务器</tspan></text></g><g><line id="actor2" x1="475" y1="5" x2="475" y2="285" stroke-width="0.5px" stroke="#999"></line><rect x="400" y="0" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="475" y="32.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="475" dy="0">订阅者</tspan></text></g><defs><marker id="arrowhead" refx="5" refy="2" markerwidth="6" markerheight="4" orient="auto"></marker></defs><defs><marker id="crosshead" markerwidth="15" markerheight="8" orient="auto" refx="16" refy="4"></marker></defs><g><text x="175" y="93" style="text-anchor: middle;">PUBLISH (QoS0,Msg-A)</text><line x1="75" y1="100" x2="275" y2="100" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="fill: none;"></line></g><g></g><g><text x="377.5" y="128" style="text-anchor: middle;">PUBLISH(QoS0,Msg-A)</text><line x1="280" y1="135" x2="475" y2="135" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="fill: none;"></line></g><g><text x="75" y="163" style="text-anchor: middle;">Delete Msg-A</text></g><g><rect x="0" y="220" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="75" y="252.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="75" dy="0">发布者</tspan></text></g><g><rect x="200" y="220" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="275" y="252.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="275" dy="0">服务器</tspan></text></g><g><rect x="400" y="220" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="475" y="252.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="475" dy="0">订阅者</tspan></text></g><text x="187.5" y="-25">QoS 0：At most one(Fire and forget)</text></svg></div><h3>QoS1 的通信时序图</h3><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">此时，<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Sender</code>&nbsp;发送的一条消息，<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Receiver</code>&nbsp;至少能收到一次，也就是说&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Sender</code>&nbsp;向&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Receiver</code>&nbsp;发送消息，如果发送失败，会继续重试，直到&nbsp;<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Receiver</code>&nbsp;收到消息为止，但是因为重传的原因，<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace !important; font-size: 12px !important; line-height: 1.8; margin: 0px 4px !important; vertical-align: middle; display: inline; overflow-x: auto; background-color: rgba(27, 31, 35, 0.05) !important; border: none !important; padding: 0.2em 0.4em !important; border-radius: 3px !important;">Receiver</code>&nbsp;有可能会收到重复的消息；</p><div data-processed="true" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; white-space: pre-wrap; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;"><svg id="mermaid-1602471087312" width="100%" xmlns="http://www.w3.org/2000/svg" height="100%" style="max-width:675px;" viewbox="-75 -50 675 601"><g></g><g><line id="actor3" x1="75" y1="5" x2="75" y2="550" stroke-width="0.5px" stroke="#999"></line><rect x="0" y="0" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="75" y="32.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="75" dy="0">发布者</tspan></text></g><g><line id="actor4" x1="275" y1="5" x2="275" y2="550" stroke-width="0.5px" stroke="#999"></line><rect x="200" y="0" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="275" y="32.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="275" dy="0">服务器</tspan></text></g><g><line id="actor5" x1="475" y1="5" x2="475" y2="550" stroke-width="0.5px" stroke="#999"></line><rect x="400" y="0" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="475" y="32.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="475" dy="0">订阅者</tspan></text></g><defs><marker id="arrowhead" refx="5" refy="2" markerwidth="6" markerheight="4" orient="auto"></marker></defs><defs><marker id="crosshead" markerwidth="15" markerheight="8" orient="auto" refx="16" refy="4"></marker></defs><g><text x="75" y="93" style="text-anchor: middle;">Store (Msg-A)</text></g><g><text x="175" y="158" style="text-anchor: middle;">PUBLISH (QoS1,Msg-A)</text><line x1="75" y1="165" x2="275" y2="165" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="fill: none;"></line></g><g><text x="275" y="193" style="text-anchor: middle;">Store (Msg-A)</text></g><g><text x="375" y="258" style="text-anchor: middle;">PUBLISH (QoS1,Msg-A)</text><line x1="275" y1="265" x2="475" y2="265" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="fill: none;"></line></g><g><text x="175" y="293" style="text-anchor: middle;">PUBACK (QoS1)</text><line x1="275" y1="300" x2="75" y2="300" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="stroke-dasharray: 3, 3; fill: none;"></line></g><g><text x="75" y="328" style="text-anchor: middle;">Delete (Msg-A)</text></g><g><text x="375" y="393" style="text-anchor: middle;">PUBACK (QoS1,Msg-A)</text><line x1="475" y1="400" x2="275" y2="400" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="stroke-dasharray: 3, 3; fill: none;"></line></g><g><text x="275" y="428" style="text-anchor: middle;">Delete (Msg-A)</text></g><g><rect x="0" y="485" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="75" y="517.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="75" dy="0">发布者</tspan></text></g><g><rect x="200" y="485" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="275" y="517.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="275" dy="0">服务器</tspan></text></g><g><rect x="400" y="485" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="475" y="517.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="475" dy="0">订阅者</tspan></text></g><text x="187.5" y="-25">QoS 1：At least one</text></svg></div><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">1）Sender 向 Receiver 发送一个带有消息数据的 PUBLISH 包， 并在本地保存这个 PUBLISH 包。</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">2）Receiver 收到 PUBLISH 包以后，向 Sender 发送一个 PUBACK 数据包，PUBACK 数据包没有消息体（Payload），在可变头中（Variable header）中有一个包标识（Packet Identifier），和它收到的 PUBLISH 包中的 Packet Identifier 一致。</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">3）Sender 收到 PUBACK 之后，根据 PUBACK 包中的 Packet Identifier 找到本地保存的 PUBLISH 包，然后丢弃掉，一次消息的发送完成。</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">4）如果 Sender 在一段时间内没有收到 PUBLISH 包对应的 PUBACK，它将该 PUBLISH 包的 DUP 标识设为 1（代表是重新发送的 PUBLISH 包），然后重新发送该 PUBLISH 包。重复这个流程，直到收到 PUBACK，然后执行第 3 步。</p><h3>QoS 2 的通信时序图</h3><blockquote style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; background-color: #ffffff; margin-right: 0px; margin-left: 0px; border-top: 0px; border-bottom: 0px; border-right: 0px; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; border-left-width: 0.25em !important; border-left-color: #dfe2e5 !important; padding: 0px 1em !important; margin-top: 0px !important; margin-bottom: 16px !important; color: #6a737d !important;"><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px;">QoS2 不仅要确保 Receiver 能收到 Sender 发送的消息，还要保证消息不重复。它的重传和应答机制就要复杂一些，同时开销也是最大的。</p></blockquote><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">Sender 发送的一条消息，Receiver 确保能收到而且只收到一次，也就是说 Sender 尽力向 Receiver 发送消息，如果发送失败，会继续重试，直到 Receiver 收到消息为止，同时保证 Receiver 不会因为消息重传而收到重复的消息。</p><div data-processed="true" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; white-space: pre-wrap; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;"><svg id="mermaid-1602471087341" width="100%" xmlns="http://www.w3.org/2000/svg" height="100%" style="max-width:700px;" viewbox="-75 -50 700 936"><g></g><g><line id="actor6" x1="75" y1="5" x2="75" y2="885" stroke-width="0.5px" stroke="#999"></line><rect x="0" y="0" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="75" y="32.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="75" dy="0">发布者</tspan></text></g><g><line id="actor7" x1="275" y1="5" x2="275" y2="885" stroke-width="0.5px" stroke="#999"></line><rect x="200" y="0" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="275" y="32.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="275" dy="0">服务器</tspan></text></g><g><line id="actor8" x1="475" y1="5" x2="475" y2="885" stroke-width="0.5px" stroke="#999"></line><rect x="400" y="0" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="475" y="32.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="475" dy="0">订阅者</tspan></text></g><defs><marker id="arrowhead" refx="5" refy="2" markerwidth="6" markerheight="4" orient="auto"></marker></defs><defs><marker id="crosshead" markerwidth="15" markerheight="8" orient="auto" refx="16" refy="4"></marker></defs><g><text x="75" y="93" style="text-anchor: middle;">Store (Msg-A)</text></g><g><text x="175" y="158" style="text-anchor: middle;">PUBLISH (QoS2,Msg-A,DUP=0)</text><line x1="75" y1="165" x2="275" y2="165" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="fill: none;"></line></g><g><text x="275" y="193" style="text-anchor: middle;">Store (Msg-A)</text></g><g><text x="175" y="258" style="text-anchor: middle;">PUBREC (QoS2,Msg-A)</text><line x1="275" y1="265" x2="75" y2="265" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="stroke-dasharray: 3, 3; fill: none;"></line></g><g><text x="175" y="293" style="text-anchor: middle;">PUBREL (QoS2,Msg-A)</text><line x1="75" y1="300" x2="275" y2="300" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="fill: none;"></line></g><g><text x="375" y="328" style="text-anchor: middle;">PUBLISH (QoS2,Msg-A,DUP=0)</text><line x1="275" y1="335" x2="475" y2="335" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="fill: none;"></line></g><g><text x="175" y="363" style="text-anchor: middle;">PUBCOMP (QoS2,Msg-A)</text><line x1="275" y1="370" x2="75" y2="370" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="stroke-dasharray: 3, 3; fill: none;"></line></g><g><text x="75" y="398" style="text-anchor: middle;">Delete (Msg-A)</text></g><g><text x="475" y="463" style="text-anchor: middle;">Store (Msg-A)</text></g><g><text x="375" y="528" style="text-anchor: middle;">PUBREC (QoS2,Msg-A)</text><line x1="475" y1="535" x2="275" y2="535" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="stroke-dasharray: 3, 3; fill: none;"></line></g><g><text x="375" y="563" style="text-anchor: middle;">PUBREL (QoS2,Msg-A)</text><line x1="275" y1="570" x2="475" y2="570" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="fill: none;"></line></g><g><text x="475" y="598" style="text-anchor: middle;">Notify (Msg-A)</text></g><g><text x="375" y="663" style="text-anchor: middle;">PUBCOMP (QoS2,Msg-A)</text><line x1="475" y1="670" x2="275" y2="670" stroke-width="2" stroke="black" marker-end="url(#arrowhead)" style="stroke-dasharray: 3, 3; fill: none;"></line></g><g><text x="275" y="698" style="text-anchor: middle;">Delete (Msg-A)</text></g><g><text x="475" y="763" style="text-anchor: middle;">Delete (Msg-A)</text></g><g><rect x="0" y="820" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="75" y="852.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="75" dy="0">发布者</tspan></text></g><g><rect x="200" y="820" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="275" y="852.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="275" dy="0">服务器</tspan></text></g><g><rect x="400" y="820" fill="#eaeaea" stroke="#666" width="150" height="65" rx="3" ry="3"></rect><text x="475" y="852.5" dominant-baseline="central" alignment-baseline="central" style="text-anchor: middle;"><tspan x="475" dy="0">订阅者</tspan></text></g><text x="200" y="-25">QoS 2：Exactly one</text></svg></div><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">QoS 使用 2 套请求/应答流程（一个 4 段的握手）来确保 Receiver 收到来自 Sender 的消息，且不重复：</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">1）Sender 发送 QoS 为 2 的 PUBLISH 数据包，数据包 Packet Identifier 为 P，并在本地保存该 PUBLISH 包；</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">2）Receiver 收到 PUBLISH 数据包以后，在本地保存 PUBLISH 包的 Packet Identifier P，并回复 Sender 一个 PUBREC 数据包，PUBREC 数据包可变头中的 Packet Identifier 为 P，没有消息体（Payload）；</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">3）当 Sender 收到 PUBREC，它就可以安全地丢弃掉初始的 Packet Identifier 为 P 的 PUBLISH 数据包，同时保存该 PUBREC 数据包，同时回复 Receiver 一个 PUBREL 数据包，PUBREL 数据包可变头中的 Packet Identifier 为 P，没有消息体；如果 Sender 在一定时间内没有收到 PUBREC，它会把 PUBLISH 包的 DUP 标识设为 1，重新发送该 PUBLISH 数据包（Payload）；</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">4）当 Receiver 收到 PUBREL 数据包，它可以丢弃掉保存的 PUBLISH 包的 Packet Identifier P，并回复 Sender 一个 PUBCOMP 数据包，PUBCOMP 数据包可变头中的 Packet Identifier 为 P，没有消息体（Payload）；</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">5）当 Sender 收到 PUBCOMP 包，那么它认为数据包传输已完成，它会丢弃掉对应的 PUBREC 包。如果 Sender 在一定时间内没有收到 PUBCOMP 包，它会重新发送 PUBREL 数据包。</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">我们可以看到在 QoS2 中，完成一次消息的传递，Sender 和 Reciever 之间至少要发送四个数据包，QoS2 是最安全也是最慢的一种 QoS 等级了。</p><h2>QoS 和会话（Session）</h2><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">客户端的会话状态包括：</p><ul style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding-left: 0px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff; margin-top: 0px !important; margin-bottom: 16px !important; margin-left: 0px !important;"><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">已经发送给服务端，但是还没有完成确认的QoS 1和QoS 2级别的消息</li><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">已从服务端接收，但是还没有完成确认的QoS 2级别的消息。</li></ul><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">服务端的会话状态包括：</p><ul style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding-left: 0px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff; margin-top: 0px !important; margin-bottom: 16px !important; margin-left: 0px !important;"><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">会话是否存在，即使会话状态的其它部分都是空。</li><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">客户端的订阅信息。</li><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">已经发送给客户端，但是还没有完成确认的QoS 1和QoS 2级别的消息。</li><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">即将传输给客户端的QoS 1和QoS 2级别的消息。</li><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">已从客户端接收，但是还没有完成确认的QoS 2级别的消息。</li><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">可选，准备发送给客户端的QoS 0级别的消息。</li></ul><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">保留消息不是服务端会话状态的一部分，会话终止时<span style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: bolder;">不能</span>删除保留消息。</p><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">如果 Client 想接收离线消息，必须使用持久化的会话（CONNECT报文中可变头（byte8[1]）Clean Session = 0）连接到 Broker，这样 Broker 才会存储 Client 在离线期间没有确认接收的 QoS 大于 1 的消息。</p><h2>QoS 等级的选择</h2><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;"><span style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: bolder;">在以下情况下你可以选择 QoS0</span>：</p><ul style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding-left: 0px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff; margin-top: 0px !important; margin-bottom: 16px !important; margin-left: 0px !important;"><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">Client 和 Broker 之间的网络连接非常稳定，例如一个通过有线网络连接到 Broker 的测试用 Client；</li><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">可以接受丢失部分消息，比如你有一个传感器以非常短的间隔发布状态数据，所以丢一些也可以接受；</li><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">你不需要离线消息。</li></ul><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;"><span style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: bolder;">在以下情况下你应该选择 QoS1：</span></p><ul style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding-left: 0px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff; margin-top: 0px !important; margin-bottom: 16px !important; margin-left: 0px !important;"><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">你需要接收所有的消息，而且你的应用可以接受并处理重复的消息；</li><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">你无法接受 QoS2 带来的额外开销，QoS1 发送消息的速度比 QoS2 快很多。</li></ul><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;"><span style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: bolder;">在以下情况下你应该选择 QoS2：</span></p><ul style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; padding-left: 0px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff; margin-top: 0px !important; margin-bottom: 16px !important; margin-left: 0px !important;"><li style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; list-style-type: disc;">你的应用必须接收到所有的消息，而且你的应用在重复的消息下无法正常工作，同时你也能接受 QoS2 带来的额外开销。</li></ul><p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 10px; margin-bottom: 10px; color: #555555; font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; background-color: #ffffff;">实际上，QoS1 是应用最广泛的 QoS 等级，QoS1 发送消息的速度很快，而且能够保证消息的可靠性。虽然使用 QoS1 可能会收到重复的消息，但是在应用程序里面处理重复消息，通常并不是件难事。</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/435690.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2020-10-12 14:19 <a href="http://www.blogjava.net/xiaomage234/archive/2020/10/12/435690.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>性能问题定位：工具使用【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2019/04/19/433725.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Fri, 19 Apr 2019 03:04:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2019/04/19/433725.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/433725.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2019/04/19/433725.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/433725.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/433725.html</trackback:ping><description><![CDATA[from:<a href="http://zhulongchao.com/blog/performance-trace.html">http://zhulongchao.com/blog/performance-trace.html<br /><br /><h2>1.网速测试</h2><h3>安装iperf</h3><div style="margin: 0px; box-sizing: border-box; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"><div style="margin: 0px; box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; padding: 9.5px; background: #f5f5f5; font-size: 0.8rem; line-height: 1.42857; border-radius: 4px; border: 1px solid #cccccc; color: #333333; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace;"><code style="margin: 0px auto; box-sizing: border-box; padding: 0px 0.2em; max-width: 760px; width: 739px; display: block; font-size: 0.8rem; background-color: #f4f4f4; border: none; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; border-radius: 4px; line-height: 21.76px;">yum install epel-release 从epel源中安装 yum install -y  iperf </code></pre></div></div><h3>带宽检测</h3><div style="margin: 0px; box-sizing: border-box; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"><div style="margin: 0px; box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; padding: 9.5px; background: #f5f5f5; font-size: 0.8rem; line-height: 1.42857; border-radius: 4px; border: 1px solid #cccccc; color: #333333; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace;"><code style="margin: 0px auto; box-sizing: border-box; padding: 0px 0.2em; max-width: 760px; width: 739px; display: block; font-size: 0.8rem; background-color: #f4f4f4; border: none; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; border-radius: 4px; line-height: 21.76px;">iperf -s 开启服务端  iperf -c ip </code></pre></div></div><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;"><img src="http://images.cnblogs.com/cnblogs_com/zhulongchao/771893/o_iperf.png" alt="" style="margin: 1.5em auto 1.6em; box-sizing: border-box; padding: 0px; max-width: 100%; display: block; height: auto;" /></p><h3>丢包问题</h3><div style="margin: 0px; box-sizing: border-box; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"><div style="margin: 0px; box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; padding: 9.5px; background: #f5f5f5; font-size: 0.8rem; line-height: 1.42857; border-radius: 4px; border: 1px solid #cccccc; color: #333333; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace;"><code style="margin: 0px auto; box-sizing: border-box; padding: 0px 0.2em; max-width: 760px; width: 739px; display: block; font-size: 0.8rem; background-color: #f4f4f4; border: none; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; border-radius: 4px; line-height: 21.76px;">tcpdump进行抓包  tcpdump -i eth0 -s 3000 port 8080 -w /home/tomcat.pcap  对于抓包文件采用wireshark进行分析  丢包(TCP DUP ACK) 重传(retransmission)，超时重传， </code></pre></div></div><h2>2.cdn性能测试</h2><div style="margin: 0px; box-sizing: border-box; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"><div style="margin: 0px; box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; padding: 9.5px; background: #f5f5f5; font-size: 0.8rem; line-height: 1.42857; border-radius: 4px; border: 1px solid #cccccc; color: #333333; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace;"><code style="margin: 0px auto; box-sizing: border-box; padding: 0px 0.2em; max-width: 760px; width: 739px; display: block; font-size: 0.8rem; background-color: #f4f4f4; border: none; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; border-radius: 4px; line-height: 21.76px;">cdn 缓存，回源问题    304请求，浏览器是否使用本地缓存。比较last_modified 和if_modified_since  通过实践戳来判断，浏览器缓存和cdn缓存 </code></pre></div></div><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;"><img src="http://img4.tbcdn.cn/L1/461/1/2118052340.jpg" alt="" style="margin: 1.5em auto 1.6em; box-sizing: border-box; padding: 0px; max-width: 100%; display: block; height: auto;" /></p><h2>3.DNS基础</h2><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">路由解析</p><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">泛域名解析</p><h2>4.分布式服务链路追踪</h2><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;"><img src="http://images.cnblogs.com/cnblogs_com/zhulongchao/771893/o_traceid.png" alt="" style="margin: 1.5em auto 1.6em; box-sizing: border-box; padding: 0px; max-width: 100%; display: block; height: auto;" /></p><div style="margin: 0px; box-sizing: border-box; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"><div style="margin: 0px; box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; padding: 9.5px; background: #f5f5f5; font-size: 0.8rem; line-height: 1.42857; border-radius: 4px; border: 1px solid #cccccc; color: #333333; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace;"><code style="margin: 0px auto; box-sizing: border-box; padding: 0px 0.2em; max-width: 760px; width: 739px; display: block; font-size: 0.8rem; background-color: #f4f4f4; border: none; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; border-radius: 4px; line-height: 21.76px;">http入口产生一个traceId  分发到rpc调用，cache，db，jms调用链路中  google的著名论文dapper和zipkin  日志聚合，绑定链路日志和业务日志  采样采集，慢请求，异常服务。  日志量大。日志异步写入，环状数组，日志组件自研  共享信息放在ThreadLocal中。比如traceId </code></pre></div></div><h2>5.网卡性能问题定位</h2><div style="margin: 0px; box-sizing: border-box; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"><div style="margin: 0px; box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; padding: 9.5px; background: #f5f5f5; font-size: 0.8rem; line-height: 1.42857; border-radius: 4px; border: 1px solid #cccccc; color: #333333; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace;"><code style="margin: 0px auto; box-sizing: border-box; padding: 0px 0.2em; max-width: 760px; width: 739px; display: block; font-size: 0.8rem; background-color: #f4f4f4; border: none; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; border-radius: 4px; line-height: 21.76px;">tsar -l  -i 1 --traffic 查看网卡的进出流量 </code></pre></div></div><h2>6.CPU性能问题定位</h2><div style="margin: 0px; box-sizing: border-box; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"><div style="margin: 0px; box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; padding: 9.5px; background: #f5f5f5; font-size: 0.8rem; line-height: 1.42857; border-radius: 4px; border: 1px solid #cccccc; color: #333333; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace;"><code style="margin: 0px auto; box-sizing: border-box; padding: 0px 0.2em; max-width: 760px; width: 739px; display: block; font-size: 0.8rem; background-color: #f4f4f4; border: none; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; border-radius: 4px; line-height: 21.76px;">tsar -l  -i 1 --cpu  软件问题定位，perf 采样所有进程数据  perf record -F 99 -a -g -- sleep 30  java进程的函数map：java -cp attach-main.jar:$JAVA_HOME/lib/tools.jar net.virtualvoid.perf.AttachOnce PID  输出函数和地址的map  输出火焰图 perf script | stackcollapse-perf.pl | flamegraph.pl --color=java --hash &gt; flamegraph.svg </code></pre></div></div><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;"><img src="http://images.cnblogs.com/cnblogs_com/zhulongchao/771893/o_QQ%e6%88%aa%e5%9b%be20170724162019.png" alt="" style="margin: 1.5em auto 1.6em; box-sizing: border-box; padding: 0px; max-width: 100%; display: block; height: auto;" /></p><h2>7.内存性能问题定位</h2><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">-堆内内存问题，</p><div style="margin: 0px; box-sizing: border-box; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"><div style="margin: 0px; box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; padding: 9.5px; background: #f5f5f5; font-size: 0.8rem; line-height: 1.42857; border-radius: 4px; border: 1px solid #cccccc; color: #333333; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace;"><code style="margin: 0px auto; box-sizing: border-box; padding: 0px 0.2em; max-width: 760px; width: 739px; display: block; font-size: 0.8rem; background-color: #f4f4f4; border: none; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; border-radius: 4px; line-height: 21.76px;">采用jmap dump内存，采用离线工具分析  jprofile、mat </code></pre></div></div><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">-堆外内存问题</p><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">a.google-perftools</p><div style="margin: 0px; box-sizing: border-box; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"><div style="margin: 0px; box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; padding: 9.5px; background: #f5f5f5; font-size: 0.8rem; line-height: 1.42857; border-radius: 4px; border: 1px solid #cccccc; color: #333333; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace;"><code style="margin: 0px auto; box-sizing: border-box; padding: 0px 0.2em; max-width: 760px; width: 739px; display: block; font-size: 0.8rem; background-color: #f4f4f4; border: none; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; border-radius: 4px; line-height: 21.76px;">yum install -y google-perftools graphviz  export LD_PRELOAD=/usr/lib64/libtcmalloc.so.4  export HEAPPROFILE=/home/testGperf.prof  执行程序，结束程序，生成prof  分析prof  生成svg, pdf,text pprof --svg $JAVA_HOME/bin/java testGperf.prof.0001.heap &gt; test.svg  pprof --pdf $JAVA_HOME/bin/java testGperf.prof.0001.heap &gt; test.pdf  pprof --text $JAVA_HOME/bin/java testGperf.prof.0001.heap &gt; test.txt </code></pre></div></div><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">b.jemalloc定位（优势，适合长时间trace）</p><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;"><img src="http://images.cnblogs.com/cnblogs_com/zhulongchao/771893/o_QQ%e6%88%aa%e5%9b%be20170724161232.png" alt="" style="margin: 1.5em auto 1.6em; box-sizing: border-box; padding: 0px; max-width: 100%; display: block; height: auto;" /></p><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">sudo apt-get install graphviz 编译安装 ./configure &#8211;enable-prof &#8211;enable-stats &#8211;enable-debug &#8211;enable-fill make make install</p><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">运行配置 export MALLOC_CONF=&#8221;prof:true,prof_gdump:true,prof_prefix:/home/jedump/jez,lg_prof_interval:30,lg_prof_sample:17&#8221;</p><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">export LD_PRELOAD=/usr/local/lib/libjemalloc.so.2 运行 java -jar target/spring-boot-jemalloc-example-0.0.1-SNAPSHOT.jar</p><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">jeprof &#8211;show_bytes &#8211;svg jez.*.heap &gt; app-profiling.svg</p><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">注明：如果在docker容器中，推荐用pprof，jemalloc只显示函数地址，不显示函数名</p><h2>8.机器资源配额问题</h2><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">/etc/security/limits.conf</p><ul style="margin: 0px 0px 0px 20px; box-sizing: border-box; padding: 0px; font-size: 0.9rem; text-align: justify; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;"><li style="margin: 0px; box-sizing: border-box; padding: 0px;">soft nofile 65536</li><li style="margin: 0px; box-sizing: border-box; padding: 0px;">hard nofile 65536</li></ul><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">控制该用户文件句柄数</p><h2>9.磁盘性能问题定位</h2><p style="margin: 20px 0px; box-sizing: border-box; padding: 0px; text-align: justify; font-size: 0.9rem; color: #222222; font-family: Georgia, &quot;Linux Libertine&quot;, -apple-system, &quot;Helvetica Neue&quot;, Helvetica, &quot;Nimbus Sans L&quot;, Arial, &quot;Liberation Sans&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Source Han Sans CN&quot;, &quot;Source Han Sans SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Wenquanyi Micro Hei&quot;, &quot;WenQuanYi Zen Hei&quot;, &quot;ST Heiti&quot;, SimHei, &quot;WenQuanYi Zen Hei Sharp&quot;, sans-serif; background-color: #ffffff;">tsar -l -i 1 &#8211;io</p></a><img src ="http://www.blogjava.net/xiaomage234/aggbug/433725.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2019-04-19 11:04 <a href="http://www.blogjava.net/xiaomage234/archive/2019/04/19/433725.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java堆外内存排查小结【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2019/03/30/433695.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Sat, 30 Mar 2019 03:44:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2019/03/30/433695.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/433695.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2019/03/30/433695.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/433695.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/433695.html</trackback:ping><description><![CDATA[from:<a href="https://blog.csdn.net/lycyingO/article/details/80854669">https://blog.csdn.net/lycyingO/article/details/80854669<br /><br /><br /><div>&nbsp;版权声明：微信公众号《小姐姐味道》，转载注明出处<span style="white-space:pre">	</span>https://blog.csdn.net/lycyingO/article/details/80854669</div><div></div><div></div><div>简介</div><div>JVM堆外内存难排查但经常会出现问题，这可能是目前最全的JVM堆外内存排查思路。</div><div></div><div>通过本文，你应该了解：</div><div></div><div>pmap 命令</div><div>gdb 命令</div><div>perf 命令</div><div>内存 RSS、VSZ的区别</div><div>java NMT</div><div>起因</div><div>这几天遇到一个比较奇怪的问题，觉得有必要和大家分享一下。我们的一个服务，运行在docker上，在某个版本之后，占用的内存开始增长，直到docker分配的内存上限，但是并不会OOM。版本的更改如下：</div><div></div><div>升级了基础软件的版本</div><div>将docker的内存上限由4GB扩展到8GB</div><div>上上个版本的一项变动是使用了EhCache的Heap缓存</div><div>没有读文件，也没有mmap操作</div><div>使用jps 查看启动参数，发现分配了大约3GB的堆内存</div><div></div><div>[root]$ jps -v</div><div>75 Bootstrap -Xmx3000m -Xms3000m&nbsp; -verbose:gc -Xloggc:/home/logs/gc.log -XX:CMSInitiatingOccupancyFraction=80 -XX:+UseCMSCompactAtFullCollection -XX:MaxTenuringThreshold=10 -XX:MaxPermSize=128M -XX:SurvivorRatio=3 -XX:NewRatio=2 -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+UseParNewGC -XX:+UseConcMarkSweepGC</div><div>使用ps查看进程使用的内存和虚拟内存 ( Linux内存管理 )。除了虚拟内存比较高达到17GB以外，实际使用的内存RSS也夸张的达到了7GB，远远超过了-Xmx的设定。</div><div></div><div>[root]$ ps -p 75 -o rss,vsz&nbsp;&nbsp;</div><div>&nbsp;</div><div>RSS&nbsp; &nbsp; VSZ 7152568 17485844</div><div>原创文章，转载注明出处 (http://sayhiai.com)</div><div>排查过程</div><div>明显的，是有堆外内存的使用，不太可能是由于EhCache引起的（因为我们使用了heap方式）。了解到基础软件的升级涉及到netty版本升级，netty会用到一些DirectByteBuffer，第一轮排查我们采用如下方式：</div><div></div><div>jmap -dump:format=b,file=75.dump 75 通过分析堆内存找到DirectByteBuffer的引用和大小</div><div>部署一个升级基础软件之前的版本，持续观察</div><div>部署另一个版本，更改EhCache限制其大小到1024M</div><div>考虑到可能由Docker的内存分配机制引起，部署一实例到实体机</div><div>结果4个环境中的服务，无一例外的都出现了内存超用的问题。问题很奇怪，宝宝睡不着觉。</div><div></div><div>pmap</div><div>为了进一步分析问题，我们使用pmap查看进程的内存分配，通过RSS升序序排列。结果发现除了地址000000073c800000上分配的3GB堆以外，还有数量非常多的64M一块的内存段，还有巨量小的物理内存块映射到不同的虚拟内存段上。但到现在为止，我们不知道里面的内容是什么，是通过什么产生的。</div><div></div><div>[root]$ pmap -x 75&nbsp; | sort -n -k3</div><div>&nbsp;</div><div>.....省略N行</div><div>&nbsp;</div><div>0000000040626000&nbsp; &nbsp;55488&nbsp; &nbsp;55484&nbsp; &nbsp;55484 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa07c000000&nbsp; &nbsp;65536&nbsp; &nbsp;55820&nbsp; &nbsp;55820 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa044000000&nbsp; &nbsp;65536&nbsp; &nbsp;55896&nbsp; &nbsp;55896 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa0c0000000&nbsp; &nbsp;65536&nbsp; &nbsp;56304&nbsp; &nbsp;56304 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9db8000000&nbsp; &nbsp;65536&nbsp; &nbsp;56360&nbsp; &nbsp;56360 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa0b8000000&nbsp; &nbsp;65536&nbsp; &nbsp;56836&nbsp; &nbsp;56836 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa084000000&nbsp; &nbsp;65536&nbsp; &nbsp;57916&nbsp; &nbsp;57916 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9ec4000000&nbsp; &nbsp;65532&nbsp; &nbsp;59752&nbsp; &nbsp;59752 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa008000000&nbsp; &nbsp;65536&nbsp; &nbsp;60012&nbsp; &nbsp;60012 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9e58000000&nbsp; &nbsp;65536&nbsp; &nbsp;61608&nbsp; &nbsp;61608 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9f18000000&nbsp; &nbsp;65532&nbsp; &nbsp;61732&nbsp; &nbsp;61732 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa018000000&nbsp; &nbsp;65532&nbsp; &nbsp;61928&nbsp; &nbsp;61928 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa088000000&nbsp; &nbsp;65536&nbsp; &nbsp;62336&nbsp; &nbsp;62336 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa020000000&nbsp; &nbsp;65536&nbsp; &nbsp;62428&nbsp; &nbsp;62428 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9e44000000&nbsp; &nbsp;65536&nbsp; &nbsp;64352&nbsp; &nbsp;64352 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9ec0000000&nbsp; &nbsp;65528&nbsp; &nbsp;64928&nbsp; &nbsp;64928 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa050000000&nbsp; &nbsp;65532&nbsp; &nbsp;65424&nbsp; &nbsp;65424 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9e08000000&nbsp; &nbsp;65512&nbsp; &nbsp;65472&nbsp; &nbsp;65472 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9de0000000&nbsp; &nbsp;65524&nbsp; &nbsp;65512&nbsp; &nbsp;65512 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9dec000000&nbsp; &nbsp;65532&nbsp; &nbsp;65532&nbsp; &nbsp;65532 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9dac000000&nbsp; &nbsp;65536&nbsp; &nbsp;65536&nbsp; &nbsp;65536 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9dc8000000&nbsp; &nbsp;65536&nbsp; &nbsp;65536&nbsp; &nbsp;65536 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9e30000000&nbsp; &nbsp;65536&nbsp; &nbsp;65536&nbsp; &nbsp;65536 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007f9eb4000000&nbsp; &nbsp;65536&nbsp; &nbsp;65536&nbsp; &nbsp;65536 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa030000000&nbsp; &nbsp;65536&nbsp; &nbsp;65536&nbsp; &nbsp;65536 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>00007fa0b0000000&nbsp; &nbsp;65536&nbsp; &nbsp;65536&nbsp; &nbsp;65536 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>000000073c800000 3119140 2488596 2487228 rwx--&nbsp; &nbsp; [ anon ]</div><div>&nbsp;</div><div>total kB&nbsp; &nbsp; &nbsp; &nbsp; 17629516 7384476 7377520</div><div>通过google，找到以下资料 Linux glibc &gt;= 2.10 (RHEL 6) malloc may show excessive virtual memory usage)</div><div></div><div>文章指出造成应用程序大量申请64M大内存块的原因是由Glibc的一个版本升级引起的，通过export MALLOC_ARENA_MAX=4可以解决VSZ占用过高的问题。虽然这也是一个问题，但却不是我们想要的，因为我们增长的是物理内存，而不是虚拟内存。</div><div></div><div>NMT</div><div>幸运的是 JDK1.8有Native Memory Tracker可以帮助定位。通过在启动参数上加入-XX:NativeMemoryTracking=detail就可以启用。在命令行执行jcmd可查看内存分配。</div><div></div><div>#jcmd 75 VM.native_memory summary</div><div>&nbsp;</div><div>Native Memory Tracking: Total: reserved=5074027KB, committed=3798707KB -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Java Heap (reserved=3072000KB, committed=3072000KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (mmap: reserved=3072000KB, committed=3072000KB) -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Class (reserved=1075949KB, committed=28973KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (classes #4819)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (malloc=749KB #13158)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (mmap: reserved=1075200KB, committed=28224KB) -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread (reserved=484222KB, committed=484222KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (thread #470)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (stack: reserved=482132KB, committed=482132KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (malloc=1541KB #2371)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (arena=550KB #938) -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Code (reserved=253414KB, committed=25070KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (malloc=3814KB #5593)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (mmap: reserved=249600KB, committed=21256KB) -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GC (reserved=64102KB, committed=64102KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (malloc=54094KB #255)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (mmap: reserved=10008KB, committed=10008KB) -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Compiler (reserved=542KB, committed=542KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (malloc=411KB #543)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (arena=131KB #3) -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Internal (reserved=50582KB, committed=50582KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (malloc=50550KB #13713)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (mmap: reserved=32KB, committed=32KB) -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Symbol (reserved=6384KB, committed=6384KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (malloc=4266KB #31727)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (arena=2118KB #1) -&nbsp; &nbsp; Native Memory Tracking (reserved=1325KB, committed=1325KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (malloc=208KB #3083)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (tracking overhead=1117KB) -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Arena Chunk (reserved=231KB, committed=231KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (malloc=231KB) -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Unknown (reserved=65276KB, committed=65276KB)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (mmap: reserved=65276KB, committed=65276KB)</div><div>虽然pmap得到的内存地址和NMT大体能对的上，但仍然有不少内存去向成谜。虽然是个好工具但问题并不能解决。</div><div></div><div>gdb</div><div>非常好奇64M或者其他小内存块中是什么内容，接下来通过gdbdump出来。读取/proc目录下的maps文件，能精准的知晓目前进程的内存分布。</div><div></div><div>以下脚本通过传入进程id，能够将所关联的内存全部dump到文件中（会影响服务，慎用）。</div><div></div><div>grep rw-p /proc/$1/maps | sed -n 's/^\([0-9a-f]*\)-\([0-9a-f]*\) .*$/\1 \2/p' | while read start stop; do gdb --batch --pid $1 -ex "dump memory $1-$start-$stop.dump 0x$start 0x$stop"; done</div><div>更多时候，推荐之dump一部分内存。(再次提醒操作会影响服务，注意dump的内存块大小，慎用)。</div><div></div><div>gdb --batch --pid 75 -ex "dump memory a.dump 0x7f2bceda1000 0x7f2bcef2b000</div><div>[root]$ du -h *</div><div>dump 4.0K</div><div>55-00600000-00601000.dump 400K</div><div>55-00eb7000-00f1b000.dump 0</div><div>55-704800000-7c0352000.dump 47M</div><div>55-7f2840000000-7f2842eb8000.dump 53M</div><div>55-7f2848000000-7f284b467000.dump 64M</div><div>55-7f284c000000-7f284fffa000.dump 64M</div><div>55-7f2854000000-7f2857fff000.dump 64M</div><div>55-7f285c000000-7f2860000000.dump 64M</div><div>55-7f2864000000-7f2867ffd000.dump 1016K</div><div>55-7f286a024000-7f286a122000.dump 1016K</div><div>55-7f286a62a000-7f286a728000.dump 1016K</div><div>55-7f286d559000-7f286d657000.dump</div><div>是时候查看里面的内容了</div><div></div><div>[root]$ view 55-7f284c000000-7f284fffa000.dump</div><div>^@^@X+^?^@^@^@^@^@d(^?^@^@^@ &#255;^C^@^@^@^@^@ &#255;^C^@^@^@^@^@^@^@^@^@^@^@^@&#177;&lt;97&gt;p^C^@^@^@^@ 8^^Z+^?^@^@ ^@^@d(^?^@^@ 8^^Z+^?^@^@ ^@^@d(^?^@^@</div><div>achine":524993642,"timeSecond":1460272569,"inc":2145712868,"new":false},"device":{"client":"android","uid":"xxxxx","version":881},"</div><div>device_android":{"BootSerialno":"xxxxx","CpuInfo":"0-7","MacInfo":"2c:5b:b8:b0:d5:10","RAMSize":"4027212","SdcardInfo":"xxxx","Serialno":"xxxx",</div><div>"android_id":"488aedba19097476","buildnumber":"KTU84P/1416486236","device_ip":"0.0.0.0","mac":"2c:5b:b8:b0:d5:10","market_source":"12","model":"OPPO ...more</div><div>纳尼？这些内容不应该在堆里面么？为何还会使用额外的内存进行分配？上面已经排查netty申请directbuffer的原因了，那么还有什么地方在分配堆外内存呢？</div><div></div><div>perf</div><div>传统工具失灵，快到了黔驴技穷的时候了，是时候祭出神器perf了。</div><div></div><div>使用 perf record -g -p 55 开启监控栈函数调用。运行一段时间后Ctrl+C结束，会生成一个文件perf.data。</div><div></div><div>执行perf report -i perf.data查看报告。</div><div></div><div></div><div>如图，进程大量执行bzip相关函数。搜索zip，结果如下：</div><div></div><div></div><div></div><div>-.-!</div><div></div><div>进程调用了Java_java_util_zip_Inflater_inflatBytes() 申请了内存，仅有一小部分调用Deflater释放内存。与pmap内存地址相比对，确实是bzip在搞鬼。</div><div></div><div>原创文章，转载注明出处 (http://sayhiai.com)</div><div>解决</div><div>java项目搜索zip定位到代码，发现确实有相关bzip压缩解压操作，而且GZIPInputStream有个地方没有close。</div><div></div><div>GZIPInputStream使用Inflater申请堆外内存，Deflater释放内存，调用close()方法来主动释放。如果忘记关闭，Inflater对象的生命会延续到下一次GC。在此过程中，堆外内存会一直增长。</div><div></div><div>原代码：</div><div></div><div>public byte[] decompress ( byte[] input) throws IOException {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream out = new ByteArrayOutputStream();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(input)), out);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return out.toByteArray();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>修改后：</div><div></div><div>&nbsp;public byte[] decompress(byte[] input) throws IOException {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream out = new ByteArrayOutputStream();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(input));</div><div>&nbsp; &nbsp; &nbsp; &nbsp; IOUtils.copy(gzip, out);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; gzip.close();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return out.toByteArray();</div><div>&nbsp; &nbsp; }</div><div>经观察，问题解决。</div><div>---------------------&nbsp;</div><div>作者：lycyingO&nbsp;</div><div>来源：CSDN&nbsp;</div><div>原文：https://blog.csdn.net/lycyingO/article/details/80854669&nbsp;</div><div>版权声明：本文为博主原创文章，转载请附上博文链接！</div></a><img src ="http://www.blogjava.net/xiaomage234/aggbug/433695.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2019-03-30 11:44 <a href="http://www.blogjava.net/xiaomage234/archive/2019/03/30/433695.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POSIX 线程详解- 一种支持内存共享的简捷工具【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2019/02/16/433640.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Sat, 16 Feb 2019 03:37:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2019/02/16/433640.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/433640.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2019/02/16/433640.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/433640.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/433640.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: from:https://www.ibm.com/developerworks/cn/linux/thread/posix_thread1/index.htmlhttps://www.ibm.com/developerworks/cn/linux/thread/posix_thread2/index.htmlhttps://www.ibm.com/developerworks/cn/linux/t...&nbsp;&nbsp;<a href='http://www.blogjava.net/xiaomage234/archive/2019/02/16/433640.html'>阅读全文</a><img src ="http://www.blogjava.net/xiaomage234/aggbug/433640.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2019-02-16 11:37 <a href="http://www.blogjava.net/xiaomage234/archive/2019/02/16/433640.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MySQL加锁分析【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2019/02/13/433636.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Wed, 13 Feb 2019 09:07:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2019/02/13/433636.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/433636.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2019/02/13/433636.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/433636.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/433636.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: from:http://www.fanyilun.me/2017/04/20/MySQL%E5%8A%A0%E9%94%81%E5%88%86%E6%9E%90/MySQL加锁分析目录前言MySQL的锁如何查看事务的加锁情况不同语句的加锁情况1. 查询命中聚簇索引（主键索引）2. 查询命中唯一索引3. 查询命中二级索引（非唯一索引）4. 查询没有命中索引5. 对索引键值有修改6. 插入数据隐式锁一...&nbsp;&nbsp;<a href='http://www.blogjava.net/xiaomage234/archive/2019/02/13/433636.html'>阅读全文</a><img src ="http://www.blogjava.net/xiaomage234/aggbug/433636.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2019-02-13 17:07 <a href="http://www.blogjava.net/xiaomage234/archive/2019/02/13/433636.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MySQL 小心使用 replace into【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2018/12/25/433561.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Tue, 25 Dec 2018 11:19:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2018/12/25/433561.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/433561.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2018/12/25/433561.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/433561.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/433561.html</trackback:ping><description><![CDATA[<p data-spm-anchor-id="a2c4e.11153940.blogcont57855.i1.46fe7101Rdg0IU" style="box-sizing: border-box; margin: 0px 0px 32px; padding: 20px 32px; background: #f9f9f9; font-size: 12px; color: #73777a; line-height: 24px; overflow: hidden; overflow-wrap: break-word; word-break: break-all; font-family: PingFangSC, &quot;helvetica neue&quot;, &quot;hiragino sans gb&quot;, arial, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif;"><span data-spm-anchor-id="a2c4e.11153940.blogcont57855.i5.46fe7101Rdg0IU" style="box-sizing: border-box; font-weight: 700; color: #373d41;">摘要：</span>&nbsp;MySQL replace into 错误案例 背景 * MySQL5.7 * ROW模式 * 表结构 CREATE TABLE `test` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `col_1` varc</p><div markdown-body"="" style="box-sizing: border-box; margin: 0px; text-size-adjust: 100%; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 32px; overflow-wrap: break-word; overflow: hidden; word-break: break-all;"><h1>MySQL replace into 错误案例</h1><h2>背景</h2><pre style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: #f8f8f2; word-break: break-all; overflow-wrap: normal; background-color: #333333; border: 1px solid #cccccc; border-radius: 3px;"><code perl"="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; background: transparent; border-radius: 3px; padding: 0px; margin: 0px; display: inline; overflow: visible; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal;">* MySQL5.<span style="box-sizing: border-box; color: #ae81ff;">7</span>  * ROW模式   * 表结构 CREATE TABLE <span style="box-sizing: border-box; color: #e6db74;">`test`</span> (   <span style="box-sizing: border-box; color: #e6db74;">`id`</span> <span style="box-sizing: border-box; color: #f92672;">int</span>(<span style="box-sizing: border-box; color: #ae81ff;">10</span>) unsigned NOT NULL AUTO_INCREMENT,   <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   <span style="box-sizing: border-box; color: #e6db74;">`col_2`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   <span style="box-sizing: border-box; color: #e6db74;">`col_3`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   PRIMARY KEY (<span style="box-sizing: border-box; color: #e6db74;">`id`</span>),   UNIQUE KEY <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> (<span style="box-sizing: border-box; color: #e6db74;">`col_1`</span>) ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 </code></pre><h2>错误场景一</h2><blockquote style="box-sizing: border-box; padding: 0px 1em; margin: 0px 0px 16px; font-size: 17.5px; border-left-width: 0.25em; border-left-color: #dfe2e5; color: #6a737d;"><p data-spm-anchor-id="a2c4e.11153940.blogcont57855.i3.46fe7101Rdg0IU" style="box-sizing: border-box; margin: 0px; padding: 0px;">其他字段value莫名其妙的没了</p></blockquote><ul style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-weight: 600;">step1 初始化记录</span></li></ul><pre style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: #f8f8f2; word-break: break-all; overflow-wrap: normal; background-color: #333333; border: 1px solid #cccccc; border-radius: 3px;"><code ruby"="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; background: transparent; border-radius: 3px; padding: 0px; margin: 0px; display: inline; overflow: visible; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal;"><span style="box-sizing: border-box; color: #66d9ef;">mater:</span>lc&gt; REPLACE INTO test (col_1,col_2,col_3) values(<span style="box-sizing: border-box; color: #e6db74;">'a'</span>,<span style="box-sizing: border-box; color: #e6db74;">'a'</span>,<span style="box-sizing: border-box; color: #e6db74;">'a'</span>); Query OK, <span style="box-sizing: border-box; color: #ae81ff;">1</span> row affected (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec) --注意，这里是影响了<span style="box-sizing: border-box; color: #ae81ff;">1</span>条记录  <span style="box-sizing: border-box; color: #66d9ef;">master:</span>lc&gt; REPLACE INTO test (col_1,col_2,col_3) values(<span style="box-sizing: border-box; color: #e6db74;">'b'</span>,<span style="box-sizing: border-box; color: #e6db74;">'b'</span>,<span style="box-sizing: border-box; color: #e6db74;">'b'</span>); Query OK, <span style="box-sizing: border-box; color: #ae81ff;">1</span> row affected (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec) --注意，这里是影响了<span style="box-sizing: border-box; color: #ae81ff;">1</span>条记录  <span style="box-sizing: border-box; color: #66d9ef;">master:</span>lc&gt; REPLACE INTO test (col_1,col_2,col_3) values(<span style="box-sizing: border-box; color: #e6db74;">'c'</span>,<span style="box-sizing: border-box; color: #e6db74;">'c'</span>,<span style="box-sizing: border-box; color: #e6db74;">'c'</span>); Query OK, <span style="box-sizing: border-box; color: #ae81ff;">1</span> row affected (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec) --注意，这里是影响了<span style="box-sizing: border-box; color: #ae81ff;">1</span>条记录   master &gt; show create table test  <span style="box-sizing: border-box;">| test  |</span> CREATE TABLE <span style="box-sizing: border-box; color: #e6db74;">`test`</span> (   <span style="box-sizing: border-box; color: #e6db74;">`id`</span> int(<span style="box-sizing: border-box; color: #ae81ff;">10</span>) unsigned NOT NULL AUTO_INCREMENT,   <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   <span style="box-sizing: border-box; color: #e6db74;">`col_2`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   <span style="box-sizing: border-box; color: #e6db74;">`col_3`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   PRIMARY KEY (<span style="box-sizing: border-box; color: #e6db74;">`id`</span>),   UNIQUE KEY <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> (<span style="box-sizing: border-box; color: #e6db74;">`col_1`</span>) ) ENGINE=InnoDB AUTO_INCREMENT=<span style="box-sizing: border-box; color: #ae81ff;">4</span> DEFAULT CHARSET=utf8 <span style="box-sizing: border-box;">|   mater &gt; select * from test; +----+-------+-------+-------+ |</span> id <span style="box-sizing: border-box;">| col_1 |</span> col_2 <span style="box-sizing: border-box;">| col_3 |</span> +----+-------+-------+-------+ <span style="box-sizing: border-box;">|  1 |</span> a     <span style="box-sizing: border-box;">| a     |</span> a     <span style="box-sizing: border-box;">| |</span>  <span style="box-sizing: border-box; color: #ae81ff;">2</span> <span style="box-sizing: border-box;">| b     |</span> b     <span style="box-sizing: border-box;">| b     |</span> <span style="box-sizing: border-box;">|  3 |</span> c     <span style="box-sizing: border-box;">| c     |</span> c     <span style="box-sizing: border-box;">| +----+-------+-------+-------+ 3 rows <span style="box-sizing: border-box; color: #f92672;">in</span> set (0.00 sec)  </span></code></pre><ul style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-weight: 600;">step2 构造错误场景</span></li></ul><pre style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: #f8f8f2; word-break: break-all; overflow-wrap: normal; background-color: #333333; border: 1px solid #cccccc; border-radius: 3px;"><code ruby"="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; background: transparent; border-radius: 3px; padding: 0px; margin: 0px; display: inline; overflow: visible; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal;"><span style="box-sizing: border-box; color: #66d9ef;">master:</span>lc&gt; replace into test(col_1,col_2) values(<span style="box-sizing: border-box; color: #e6db74;">'c'</span>,<span style="box-sizing: border-box; color: #e6db74;">'cc'</span>); Query OK, <span style="box-sizing: border-box; color: #ae81ff;">2</span> rows affected (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec)  <span style="box-sizing: border-box; color: #66d9ef;">dba:</span>lc&gt; select * from test; +----+-------+-------+-------+ <span style="box-sizing: border-box;">| id |</span> col_1 <span style="box-sizing: border-box;">| col_2 |</span> col_3 <span style="box-sizing: border-box;">| +----+-------+-------+-------+ |</span>  <span style="box-sizing: border-box; color: #ae81ff;">1</span> <span style="box-sizing: border-box;">| a     |</span> a     <span style="box-sizing: border-box;">| a     |</span> <span style="box-sizing: border-box;">|  2 |</span> b     <span style="box-sizing: border-box;">| b     |</span> b     <span style="box-sizing: border-box;">| |</span>  <span style="box-sizing: border-box; color: #ae81ff;">4</span> <span style="box-sizing: border-box;">| c     |</span> cc    <span style="box-sizing: border-box;">| NULL  |</span> +----+-------+-------+-------+ <span style="box-sizing: border-box; color: #ae81ff;">3</span> rows <span style="box-sizing: border-box; color: #f92672;">in</span> set (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec)  </code></pre><ul style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-weight: 600;">总结</span></li></ul><ol style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;">col_3 的值，从原来的c，变成了NULL，天呐，数据不见了。 id 也变了。</li><li style="box-sizing: border-box; margin: 0.25em 0px 0px; padding: 0px;">用户原本的需求，应该是如果col_1='c' 存在，那么就改变col_2='cc'，其余的记录保持不变，结果id,col_3都变化了</li><li style="box-sizing: border-box; margin: 0.25em 0px 0px; padding: 0px;">解决方案就是：将replace into 改成 INSERT INTO &#8230; ON DUPLICATE KEY UPDATE</li></ol><p style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px;">但是你以为这样就完美的解决了吗？ 马上就会带来另外一场灾难，请看下面的错误场景</p><h2>错误场景二</h2><blockquote style="box-sizing: border-box; padding: 0px 1em; margin: 0px 0px 16px; font-size: 17.5px; border-left-width: 0.25em; border-left-color: #dfe2e5; color: #6a737d;"><p style="box-sizing: border-box; margin: 0px; padding: 0px;">ERROR 1062 (23000): Duplicate entry 'x' for key 'PRIMARY'</p></blockquote><ul style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-weight: 600;">step1 初始化记录</span></li></ul><pre style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: #f8f8f2; word-break: break-all; overflow-wrap: normal; background-color: #333333; border: 1px solid #cccccc; border-radius: 3px;"><code perl"="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; background: transparent; border-radius: 3px; padding: 0px; margin: 0px; display: inline; overflow: visible; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal;"> mater:<span style="box-sizing: border-box; color: #f92672;">lc</span>&gt; REPLACE INTO test (col_1,col_2) <span style="box-sizing: border-box; color: #f92672;">values</span>(<span style="box-sizing: border-box; color: #e6db74;">'a'</span>,<span style="box-sizing: border-box; color: #e6db74;">'a'</span>); Query OK, <span style="box-sizing: border-box; color: #ae81ff;">1</span> row affected (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec) --注意，这里是影响了<span style="box-sizing: border-box; color: #ae81ff;">1</span>条记录  master:<span style="box-sizing: border-box; color: #f92672;">lc</span>&gt; REPLACE INTO test (col_1,col_2) <span style="box-sizing: border-box; color: #f92672;">values</span>(<span style="box-sizing: border-box; color: #e6db74;">'b'</span>,<span style="box-sizing: border-box; color: #e6db74;">'b'</span>); Query OK, <span style="box-sizing: border-box; color: #ae81ff;">1</span> row affected (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec) --注意，这里是影响了<span style="box-sizing: border-box; color: #ae81ff;">1</span>条记录  master:<span style="box-sizing: border-box; color: #f92672;">lc</span>&gt; REPLACE INTO test (col_1,col_2) <span style="box-sizing: border-box; color: #f92672;">values</span>(<span style="box-sizing: border-box; color: #e6db74;">'c'</span>,<span style="box-sizing: border-box; color: #e6db74;">'c'</span>); Query OK, <span style="box-sizing: border-box; color: #ae81ff;">1</span> row affected (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec) --注意，这里是影响了<span style="box-sizing: border-box; color: #ae81ff;">1</span>条记录   master &gt; show create table test  | test  | CREATE TABLE <span style="box-sizing: border-box; color: #e6db74;">`test`</span> (   <span style="box-sizing: border-box; color: #e6db74;">`id`</span> <span style="box-sizing: border-box; color: #f92672;">int</span>(<span style="box-sizing: border-box; color: #ae81ff;">10</span>) unsigned NOT NULL AUTO_INCREMENT,   <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   <span style="box-sizing: border-box; color: #e6db74;">`col_2`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   PRIMARY KEY (<span style="box-sizing: border-box; color: #e6db74;">`id`</span>),   UNIQUE KEY <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> (<span style="box-sizing: border-box; color: #e6db74;">`col_1`</span>) ) ENGINE=InnoDB AUTO_INCREMENT=<span style="box-sizing: border-box; color: #ae81ff;">4</span> DEFAULT CHARSET=utf8 |   slave &gt; show create table test  | test  | CREATE TABLE <span style="box-sizing: border-box; color: #e6db74;">`test`</span> (   <span style="box-sizing: border-box; color: #e6db74;">`id`</span> <span style="box-sizing: border-box; color: #f92672;">int</span>(<span style="box-sizing: border-box; color: #ae81ff;">10</span>) unsigned NOT NULL AUTO_INCREMENT,   <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   <span style="box-sizing: border-box; color: #e6db74;">`col_2`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   PRIMARY KEY (<span style="box-sizing: border-box; color: #e6db74;">`id`</span>),   UNIQUE KEY <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> (<span style="box-sizing: border-box; color: #e6db74;">`col_1`</span>) ) ENGINE=InnoDB AUTO_INCREMENT=<span style="box-sizing: border-box; color: #ae81ff;">4</span> DEFAULT CHARSET=utf8 |</code></pre><ul style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-weight: 600;">step2 构造错误场景</span></li></ul><pre style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: #f8f8f2; word-break: break-all; overflow-wrap: normal; background-color: #333333; border: 1px solid #cccccc; border-radius: 3px;"><code ruby"="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; background: transparent; border-radius: 3px; padding: 0px; margin: 0px; display: inline; overflow: visible; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal;">* master  <span style="box-sizing: border-box; color: #66d9ef;">mater:</span>lc&gt; REPLACE INTO test (col_1,col_2) values(<span style="box-sizing: border-box; color: #e6db74;">'c'</span>,<span style="box-sizing: border-box; color: #e6db74;">'cc'</span>); Query OK, <span style="box-sizing: border-box; color: #ae81ff;">2</span> rows affected (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec)  --注意，这里是影响了两条记录  <span style="box-sizing: border-box; color: #66d9ef;">mater:</span>lc&gt; show create table test  <span style="box-sizing: border-box;">| test  |</span> CREATE TABLE <span style="box-sizing: border-box; color: #e6db74;">`test`</span> (   <span style="box-sizing: border-box; color: #e6db74;">`id`</span> int(<span style="box-sizing: border-box; color: #ae81ff;">10</span>) unsigned NOT NULL AUTO_INCREMENT,   <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   <span style="box-sizing: border-box; color: #e6db74;">`col_2`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   PRIMARY KEY (<span style="box-sizing: border-box; color: #e6db74;">`id`</span>),   UNIQUE KEY <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> (<span style="box-sizing: border-box; color: #e6db74;">`col_1`</span>) ) ENGINE=InnoDB AUTO_INCREMENT=<span style="box-sizing: border-box; color: #ae81ff;">5</span> DEFAULT CHARSET=utf8 <span style="box-sizing: border-box;">|  master:lc&gt; select * from test +----+-------+-------+ |</span> id <span style="box-sizing: border-box;">| col_1 |</span> col_2 <span style="box-sizing: border-box;">| +----+-------+-------+ |</span>  <span style="box-sizing: border-box; color: #ae81ff;">1</span> <span style="box-sizing: border-box;">| a     |</span> a     <span style="box-sizing: border-box;">| |</span>  <span style="box-sizing: border-box; color: #ae81ff;">2</span> <span style="box-sizing: border-box;">| b     |</span> b     <span style="box-sizing: border-box;">| |</span>  <span style="box-sizing: border-box; color: #ae81ff;">4</span> <span style="box-sizing: border-box;">| c     |</span> cc    <span style="box-sizing: border-box;">| +----+-------+-------+ 3 rows <span style="box-sizing: border-box; color: #f92672;">in</span> set (0.00 sec)  * slave  slave:lc&gt; show create table test  |</span> test  <span style="box-sizing: border-box;">| CREATE TABLE `test` (   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `col_1` varchar(100) DEFAULT NULL,   `col_2` varchar(100) DEFAULT NULL,   PRIMARY KEY (`id`),   UNIQUE KEY `col_1` (`col_1`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |</span>  <span style="box-sizing: border-box; color: #66d9ef;">slave:</span>lc&gt; select * from test +----+-------+-------+ <span style="box-sizing: border-box;">| id |</span> col_1 <span style="box-sizing: border-box;">| col_2 |</span> +----+-------+-------+ <span style="box-sizing: border-box;">|  1 |</span> a     <span style="box-sizing: border-box;">| a     |</span> <span style="box-sizing: border-box;">|  2 |</span> b     <span style="box-sizing: border-box;">| b     |</span> <span style="box-sizing: border-box;">|  4 |</span> c     <span style="box-sizing: border-box;">| cc    |</span> +----+-------+-------+ <span style="box-sizing: border-box; color: #ae81ff;">3</span> rows <span style="box-sizing: border-box; color: #f92672;">in</span> set (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec) </code></pre><ul style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-weight: 600;">step3 错误案例产生</span></li></ul><pre style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: #f8f8f2; word-break: break-all; overflow-wrap: normal; background-color: #333333; border: 1px solid #cccccc; border-radius: 3px;"><code perl"="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; background: transparent; border-radius: 3px; padding: 0px; margin: 0px; display: inline; overflow: visible; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal;">* 假设有一天，master 挂了， 由slave 提升为 new mater  原slave:<span style="box-sizing: border-box; color: #f92672;">lc</span>&gt; show create table test  | test  | CREATE TABLE <span style="box-sizing: border-box; color: #e6db74;">`test`</span> (   <span style="box-sizing: border-box; color: #e6db74;">`id`</span> <span style="box-sizing: border-box; color: #f92672;">int</span>(<span style="box-sizing: border-box; color: #ae81ff;">10</span>) unsigned NOT NULL AUTO_INCREMENT,   <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   <span style="box-sizing: border-box; color: #e6db74;">`col_2`</span> varchar(<span style="box-sizing: border-box; color: #ae81ff;">100</span>) DEFAULT NULL,   PRIMARY KEY (<span style="box-sizing: border-box; color: #e6db74;">`id`</span>),   UNIQUE KEY <span style="box-sizing: border-box; color: #e6db74;">`col_1`</span> (<span style="box-sizing: border-box; color: #e6db74;">`col_1`</span>) ) ENGINE=InnoDB AUTO_INCREMENT=<span style="box-sizing: border-box; color: #ae81ff;">4</span> DEFAULT CHARSET=utf8 |  原slave:<span style="box-sizing: border-box; color: #f92672;">lc</span>&gt; <span style="box-sizing: border-box; color: #f92672;">select</span> * from test +----+-------+-------+ | id | col_1 | col_2 | +----+-------+-------+ |  <span style="box-sizing: border-box; color: #ae81ff;">1</span> | a     | a     | |  <span style="box-sizing: border-box; color: #ae81ff;">2</span> | b     | b     | |  <span style="box-sizing: border-box; color: #ae81ff;">4</span> | c     | cc    | +----+-------+-------+ <span style="box-sizing: border-box; color: #ae81ff;">3</span> rows in set (<span style="box-sizing: border-box; color: #ae81ff;">0</span>.<span style="box-sizing: border-box; color: #ae81ff;">00</span> sec)   ===注意==  root:<span style="box-sizing: border-box; color: #f92672;">lc</span>&gt; REPLACE INTO test (col_1,col_2) <span style="box-sizing: border-box; color: #f92672;">values</span>(<span style="box-sizing: border-box; color: #e6db74;">'d'</span>,<span style="box-sizing: border-box; color: #e6db74;">'d'</span>); ERROR <span style="box-sizing: border-box; color: #ae81ff;">1062</span> (<span style="box-sizing: border-box; color: #ae81ff;">23000</span>): Duplicate entry <span style="box-sizing: border-box; color: #e6db74;">'4'</span> <span style="box-sizing: border-box; color: #f92672;">for</span> key <span style="box-sizing: border-box; color: #e6db74;">'PRIMARY'</span>  </code></pre><ul style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-weight: 600;">总结</span></li></ul><pre data-spm-anchor-id="a2c4e.11153940.blogcont57855.i4.46fe7101Rdg0IU" style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: #f8f8f2; word-break: break-all; overflow-wrap: normal; background-color: #333333; border: 1px solid #cccccc; border-radius: 3px;"><code markdown"="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; background: transparent; border-radius: 3px; padding: 0px; margin: 0px; display: inline; overflow: visible; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal;"><span style="box-sizing: border-box; color: #ae81ff;">* </span>Row 模式，主从情况下，replace into 和 INSERT INTO &#8230; ON DUPLICATE KEY UPDATE 都会导致以上问题的发生 <span style="box-sizing: border-box; color: #ae81ff;">* </span>解决方案： 最后可以通过alter table auto_increment值解决，但是这样已经造成mater的表很长时间没有写入了。。。</code></pre><h2>最后总结</h2><ul style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-weight: 600;">replace with unique key</span></li></ul><pre style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: #f8f8f2; word-break: break-all; overflow-wrap: normal; background-color: #333333; border: 1px solid #cccccc; border-radius: 3px;"><code sql"="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; background: transparent; border-radius: 3px; padding: 0px; margin: 0px; display: inline; overflow: visible; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal;">1. 禁止 <span style="box-sizing: border-box; color: #f92672;">replace</span> <span style="box-sizing: border-box; color: #f92672;">into</span> (错误一，错误二 都会发生) <span style="box-sizing: border-box; color: #ae81ff;">2.</span> 禁止 <span style="box-sizing: border-box; color: #f92672;">INSERT</span> <span style="box-sizing: border-box; color: #f92672;">INTO</span> &#8230; <span style="box-sizing: border-box; color: #f92672;">ON</span> <span style="box-sizing: border-box; color: #f92672;">DUPLICATE</span> <span style="box-sizing: border-box; color: #f92672;">KEY</span> <span style="box-sizing: border-box; color: #f92672;">UPDATE</span> （错误二 会发生）</code></pre><ul style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 0px 0px 2em; list-style-position: initial; list-style-image: initial;"><li style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-weight: 600;">replace with primary key</span></li></ul><pre data-spm-anchor-id="a2c4e.11153940.blogcont57855.i6.46fe7101Rdg0IU" style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; line-height: 1.45; color: #f8f8f2; word-break: break-all; overflow-wrap: normal; background-color: #333333; border: 1px solid #cccccc; border-radius: 3px; margin-bottom: 0px !important;"><code sql"="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; background: transparent; border-radius: 3px; padding: 0px; margin: 0px; display: inline; overflow: visible; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal;">1. 禁止 <span style="box-sizing: border-box; color: #f92672;">replace</span> <span style="box-sizing: border-box; color: #f92672;">into</span> (会发生错误场景一的案例，丢失部分字段数据) <span style="box-sizing: border-box; color: #ae81ff;">2.</span> 可以使用<span style="box-sizing: border-box; color: #f92672;">INSERT</span> <span style="box-sizing: border-box; color: #f92672;">INTO</span> &#8230; <span style="box-sizing: border-box; color: #f92672;">ON</span> <span style="box-sizing: border-box; color: #f92672;">DUPLICATE</span> <span style="box-sizing: border-box; color: #f92672;">KEY</span> <span style="box-sizing: border-box; color: #f92672;">UPDATE</span> 代替 <span style="box-sizing: border-box; color: #f92672;">replace</span> <span style="box-sizing: border-box; color: #f92672;">into</span></code></pre></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/433561.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2018-12-25 19:19 <a href="http://www.blogjava.net/xiaomage234/archive/2018/12/25/433561.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MySQL 开发实践 8 问，你能 hold 住几个？【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2018/12/03/433534.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Mon, 03 Dec 2018 07:55:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2018/12/03/433534.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/433534.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2018/12/03/433534.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/433534.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/433534.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: from:https://cloud.tencent.com/developer/article/1004475最近研发的项目对 DB 依赖比较重，梳理了这段时间使用MySQL遇到的8个比较具有代表性的问题，答案也比较偏自己的开发实践，没有 DBA专业和深入，有出入的请使劲拍砖！MySQL读写性能是多少，有哪些性能相关的配置参数？MySQL负载高时，如何找到是由哪些SQL引起的？如何针对具体的SQ...&nbsp;&nbsp;<a href='http://www.blogjava.net/xiaomage234/archive/2018/12/03/433534.html'>阅读全文</a><img src ="http://www.blogjava.net/xiaomage234/aggbug/433534.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2018-12-03 15:55 <a href="http://www.blogjava.net/xiaomage234/archive/2018/12/03/433534.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>