{"id":2970,"date":"2021-07-08T22:44:48","date_gmt":"2021-07-08T14:44:48","guid":{"rendered":"\/?p=2970"},"modified":"2021-07-12T22:44:58","modified_gmt":"2021-07-12T14:44:58","slug":"6-2-%e6%b6%88%e6%81%af%e6%8b%89%e5%8f%96%e4%b8%80%e6%a6%82%e8%a7%88","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=2970","title":{"rendered":"6.2 \u6d88\u606f\u62c9\u53d6(\u4e00):\u6982\u89c8"},"content":{"rendered":"<p>\u6211\u4eec\u57fa\u4e8e\u4e86PUSH\u7684\u65b9\u5f0f,\u8bb2\u89e3\u62c9\u53d6,\u56e0\u4e3aPull\u6a21\u5f0f\u4e0b,\u62c9\u53d6\u66f4\u52a0\u7684\u57fa\u7840,\u4ece\u96be\u5012\u6613\u7f62\u4e86<\/p>\n<p>\u6d88\u606f\u6d88\u8d39\u5206\u4e3a\u4e86\u5e7f\u64ad\u6a21\u5f0f\u548c\u96c6\u7fa4\u6a21\u5f0f,\u5bf9\u4e8e\u5e7f\u64ad\u6a21\u5f0f\u6765\u8bf4,\u6bcf\u4e00\u4e2a\u6d88\u8d39\u8005\u90fd\u4f1a\u53bb\u62c9\u53d6\u8ba2\u9605\u4e3b\u9898\u4e0b\u7684\u6240\u6709\u6d88\u8d39\u961f\u5217\u7684\u6d88\u606f<\/p>\n<p>\u96c6\u7fa4\u6a21\u5f0f\u4e0b,\u540c\u4e00\u4e2a\u6d88\u8d39\u7ec4\u5185\u6709\u591a\u4e2a\u6d88\u606f\u6d88\u8d39\u8005,\u5b58\u5728\u7740\u591a\u4e2a\u6d88\u8d39\u961f\u5217,\u5982\u4f55\u8fdb\u884c\u8d1f\u8f7d\u5747\u8861\u7684\u5462?<\/p>\n<p>\u8fd9\u6b21\u6211\u4eec\u8fdb\u884c\u5b66\u4e60,\u5c31\u662f\u4ece<\/p>\n<p>\u4e0a\u9762start\u7684\u6700\u540e,mqClientFactory\u4e2d\u7684start\u5f00\u59cb<\/p>\n<p>\u5728mqClientFactory\u4e2d<\/p>\n<table>\n<tbody>\n<tr>\n<td>\/\/ Start pull service<\/p>\n<p>this.<strong>pullMessageService<\/strong>.start();<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u8fd9\u4e2apullMessageService\u4e2d,\u6211\u4eec\u7ee7\u627f\u4e86ServiceThread<\/p>\n<p>\u670d\u52a1\u7ebf\u7a0b ,\u901a\u8fc7run\u65b9\u6cd5\u542f\u52a8<\/p>\n<table>\n<tbody>\n<tr>\n<td>@Override<\/p>\n<p>public void run() {<\/p>\n<p><strong>log<\/strong>.info(this.getServiceName() + &#8221; service started&#8221;);<\/p>\n<p>while (!this.isStopped()) {<\/p>\n<p>try {<\/p>\n<p>\/\/\u4e0d\u65ad\u7684\u4ece\u5185\u90e8\u963b\u585e\u961f\u5217\u4e2d\u62ff\u53d6\u8bf7\u6c42<\/p>\n<p>PullRequest pullRequest = this.<strong>pullRequestQueue<\/strong>.take();<\/p>\n<p>this.pullMessage(pullRequest);<\/p>\n<p>} catch (InterruptedException ignored) {<\/p>\n<p>} catch (Exception e) {<\/p>\n<p><strong>log<\/strong>.error(&#8220;Pull Message Service Run Method exception&#8221;, e);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><strong>log<\/strong>.info(this.getServiceName() + &#8221; service end&#8221;);<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728PullMessageService\u4e2d,\u5b9e\u73b0\u5176\u5b9e\u5f88\u7b80\u5355\u5c31\u662f\u4e0d\u65ad\u7684\u4ece\u963b\u585e\u961f\u5217\u4e2d\u62ff\u53d6\u6570\u636e<\/p>\n<p>\u90a3\u4e48\u95ee\u9898\u6765\u4e86,PullRequest\u662f\u4f55\u65f6\u653e\u8fdb\u53bb\u7684\u5462?<\/p>\n<p>\u56e0\u4e3a<strong>pullRequestQueue<\/strong>\u662f\u4e2a\u5185\u90e8\u5c5e\u6027,\u6545\u653e\u5165\u7684\u51fd\u6570\u5fc5\u7136\u5728\u672c\u7c7b\u4e4b\u4e2d<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"437\" height=\"51\" class=\"wp-image-2971\" src=\"\/wp-content\/uploads\/2021\/07\/unnamed-file-5.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/07\/unnamed-file-5.png 437w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/07\/unnamed-file-5-300x35.png 300w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><\/p>\n<p>\u5728PullMessageService\u4e2d,\u63d0\u4f9b\u4e86\u5ef6\u8fdf\u6dfb\u52a0\u548c\u7acb\u5373\u6dfb\u52a0\u4e24\u79cd\u65b9\u5f0f\u5c06PullRequest\u65b9\u6cd5\u653e\u5165Queue\u4e2d<\/p>\n<p>\u6839\u636e\u4e0a\u9762\u7684<\/p>\n<p>executePullRequestImmediately,\u67e5\u627e\u5176\u8c03\u7528<\/p>\n<p>\u53ef\u4ee5\u53d1\u73b0,\u4eceDefaultMQPushConsumer\u4e2d\u53ef\u4ee5\u770b\u5230<\/p>\n<table>\n<tbody>\n<tr>\n<td>public void executePullRequestImmediately(final PullRequest pullRequest) {<\/p>\n<p>this.<strong>mQClientFactory<\/strong>.getPullMessageService().executePullRequestImmediately(pullRequest);<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u53careblanceImpl\u4e2d\u653e\u5165\u4e86queue<\/p>\n<p>\u6211\u4eec\u5148\u8bf4DefaultMQPushConsumer\u4e2d\u7684\u8c03\u7528<\/p>\n<p>\u4ece\u6574\u4f53\u7684\u5165\u53e3\u7c7b\u4e2d\u770b<\/p>\n<table>\n<tbody>\n<tr>\n<td>private void pullMessage(final PullRequest pullRequest) {<\/p>\n<p>final MQConsumerInner consumer = this.<strong>mQClientFactory<\/strong>.selectConsumer(pullRequest.getConsumerGroup());<\/p>\n<p>if (consumer != null) {<\/p>\n<p>DefaultMQPushConsumerImpl impl = (DefaultMQPushConsumerImpl) consumer;<\/p>\n<p>impl.pullMessage(pullRequest);<\/p>\n<p>} else {<\/p>\n<p><strong>log<\/strong>.warn(&#8220;No matched consumer for the PullRequest {}, drop it&#8221;, pullRequest);<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ecemqClientFactory\u4e2d\u83b7\u53d6\u5230ConsumerInner,\u7136\u540e\u8f6c\u6362\u4e00\u4e0b<\/p>\n<p>\u8fdb\u884c\u76f8\u5bf9\u5e94\u7684\u62c9\u53d6<\/p>\n<p>\u5728\u7ee7\u7eed\u8d70\u5176\u8c03\u7528\u4e4b\u524d,\u5bf9\u4e8ePullRequest\u7684\u6838\u5fc3\u5c5e\u6027,\u6211\u4eec\u9700\u8981\u8fdb\u884c\u4e00\u4e0b\u4e86\u89e3<\/p>\n<table>\n<tbody>\n<tr>\n<td>cprivate String <strong>consumerGroup<\/strong>;<\/p>\n<p>private MessageQueue <strong>messageQueue<\/strong>;<\/p>\n<p>private ProcessQueue <strong>processQueue<\/strong>;<\/p>\n<p>private long <strong>nextOffset<\/strong>;<\/p>\n<p>private boolean <strong>lockedFirst <\/strong>= false;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>consumerGroup \u6d88\u8d39\u8005\u7ec4<\/p>\n<p>messageQueue \u5f85\u6d88\u8d39\u961f\u5217<\/p>\n<p>processQueue \u6d88\u606f\u5904\u7406\u961f\u5217,\u4eceBorker\u83b7\u53d6\u7684\u6570\u636e\u5148\u653e\u5230\u6b64\u961f\u5217,\u7136\u540e\u8fdb\u884c\u6d88\u8d39<\/p>\n<p>nextOffset \u5f85\u62c9\u53d6\u7684MessageQueue\u504f\u79fb\u91cf<\/p>\n<p>lockedFirtst \u662f\u5426\u88ab\u9501\u5b9a<\/p>\n<p>\u5176\u4e2d\u7684processQueue,\u662fMessage\u5b58\u653e\u7684\u5730\u65b9,\u56e0\u4e3aPullMessageService\u9ed8\u8ba4\u6bcf\u6b21\u62c9\u53d632\u6761\u6d88\u606f,\u5728\u8fd9\u4e2a\u961f\u5217\u4e2d,\u6309\u7167\u504f\u79fb\u91cf\u4e00\u6b21\u5b58\u653e\u5728\u5176\u4e2d,\u7136\u540e\u8fdb\u884c\u63d0\u4ea4\u5230\u6d88\u8d39\u8005\u6d88\u8d39\u7ebf\u7a0b\u6c60,\u8fdb\u884c\u6d88\u606f\u6d88\u8d39<\/p>\n<p>\u8fd9\u4e2aProcessQueue \u4e5f\u662f\u5c01\u88c5\u540e\u7684\u6570\u636e\u7ed3\u6784,\u5176\u5185\u90e8\u5305\u542b\u5c5e\u6027\u5982\u4e0b<\/p>\n<table>\n<tbody>\n<tr>\n<td>\/\/\u63a7\u5236\u4e0b\u9762\u7684msgTreeMap,consumingMsgOrderlyTreeMap<\/p>\n<p>private final ReadWriteLock <strong>lockTreeMap <\/strong>= new ReentrantReadWriteLock();<\/p>\n<p>\/\/\u5b58\u50a8\u5bb9\u5668,\u952e\u4e3a\u6d88\u606f\u5728ConsumerQueue\u7684\u504f\u79fb\u91cf,MessageExt \u6d88\u606f\u5b9e\u4f53<\/p>\n<p>private final TreeMap&lt;Long, MessageExt&gt; <strong>msgTreeMap <\/strong>= new TreeMap&lt;Long, MessageExt&gt;();<\/p>\n<p>\/\/ProcessQueue\u4e2d\u603b\u6d88\u606f\u6570<\/p>\n<p>private final AtomicLong <strong>msgCount <\/strong>= new AtomicLong();<\/p>\n<p>private final AtomicLong <strong>msgSize <\/strong>= new AtomicLong();<\/p>\n<p>private final Lock <strong>lockConsume <\/strong>= new ReentrantLock();<\/p>\n<p><em>\/**<\/em><\/p>\n<p><em> * A subset of msgTreeMap, will only be used when orderly consume<\/em><\/p>\n<p><em> *\/<\/em><\/p>\n<p>\/\/\u6d88\u606f\u4e34\u65f6\u5b58\u50a8\u5bb9\u5668,\u7528\u4e8e\u5b58\u50a8\u6d88\u606f,\u5728\u6d88\u8d39\u524d,\u4e34\u65f6\u5b58\u50a8\u5728\u8fd9\u91cc\u9762,\u4e5f\u662f\u4e3a\u4e86\u5904\u7406\u987a\u5e8f\u6d88\u606f<\/p>\n<p>private final TreeMap&lt;Long, MessageExt&gt; <strong>consumingMsgOrderlyTreeMap <\/strong>= new TreeMap&lt;Long, MessageExt&gt;();<\/p>\n<p>private final AtomicLong <strong>tryUnlockTimes <\/strong>= new AtomicLong(0);<\/p>\n<p>\/\/\u6700\u5927\u961f\u5217\u504f\u79fb\u91cf<\/p>\n<p>private volatile long <strong>queueOffsetMax <\/strong>= 0L;<\/p>\n<p>\/\/\u5f53\u524dQueue\u662f\u5426\u88ab\u629b\u5f03<\/p>\n<p>private volatile boolean <strong>dropped <\/strong>= false;<\/p>\n<p>\/\/\u4e0a\u4e00\u6b21\u6d88\u606f\u62c9\u53d6\u65f6\u95f4\u6233<\/p>\n<p>private volatile long <strong>lastPullTimestamp <\/strong>= System.<em>currentTimeMillis<\/em>();<\/p>\n<p>\/\/\u4e0a\u4e00\u6b21\u6d88\u606f\u6d88\u8d39\u65f6\u95f4\u6233<\/p>\n<p>private volatile long <strong>lastConsumeTimestamp <\/strong>= System.<em>currentTimeMillis<\/em>();<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5bf9\u4e8e\u5176\u5305\u542b\u7684\u6838\u5fc3\u65b9\u6cd5,\u4e3b\u8981\u6709<\/p>\n<table>\n<tbody>\n<tr>\n<td>public boolean isLockExpired()<\/p>\n<p>\u9501\u662f\u5426\u8fc7\u671f<\/p>\n<p>public boolean isPullExpired()<\/p>\n<p>\u5224\u65adPullMessageService\u662f\u5426\u7a7a\u95f2<\/p>\n<p>public void cleanExpiredMsg(DefaultMQPushConsumer pushConsumer)<\/p>\n<p>\u79fb\u9664\u6d88\u8d39\u8d85\u65f6\u7684\u6d88\u606f.<\/p>\n<p>public boolean putMessage(final List&lt;MessageExt&gt; msgs)<\/p>\n<p>PullMessage\u62c9\u53d6\u6d88\u606f\u540e,\u8c03\u7528\u8be5\u65b9\u6cd5\u8bb2\u6d88\u606f\u6dfb\u52a0\u5230ProcessQueue<\/p>\n<p>public long getMaxSpan()<\/p>\n<p>\u6d88\u606f\u6700\u5927\u95f4\u9694,getMaxSpan()\/20 \u5e76\u4e0d\u80fd\u8bf4\u660eProcequeue\u5305\u542b\u7684\u6d88\u606f\u4e2a\u6570<\/p>\n<p>public long removeMessage(final List&lt;MessageExt&gt; msgs)<\/p>\n<p>\u79fb\u9664\u6d88\u606f<\/p>\n<p>public void rollback()<\/p>\n<p>\u5c06\u6240\u6709\u6d88\u606f\u91cd\u65b0\u653e\u5165msgTreeMap,\u6e05\u9664consumingMsgOrderlyTreeMap<\/p>\n<p>public long commit()<\/p>\n<p>\u63d0\u4ea4\u6d88\u606f<\/p>\n<p>public void makeMessageToConsumeAgain(List&lt;MessageExt&gt; msgs)<\/p>\n<p>\u91cd\u65b0\u6d88\u8d39\u4e00\u6279\u6d88\u606f,\u5c06\u5176\u4eceOrderMap\u4e2d\u79fb\u9664,\u5e76\u52a0\u5165\u5230msgTreeMap<\/p>\n<p>public List&lt;MessageExt&gt; takeMessages(final int batchSize)<\/p>\n<p>\u53d6\u51fabatchSize\u6761\u6d88\u606f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8bf4\u5b8c\u4e86ProcessQueue\u7684\u6d41\u7a0b\u4e4b\u540e,\u4f9d\u6b21\u770b\u6d88\u606f\u62c9\u53d6\u7684\u6d41\u7a0b<\/p>\n<p>\u6574\u4f53\u5206\u4e3a\u4e86<\/p>\n<p>1.\u5ba2\u6237\u7aef\u5c01\u88c5\u62c9\u53d6\u6d88\u606f\u8bf7\u6c42<\/p>\n<p>2.\u6d88\u606f\u670d\u52a1\u7aef\u67e5\u627e\u5e76\u8fd4\u56de\u6d88\u606f<\/p>\n<p>3.\u6d88\u606f\u62c9\u53d6\u5ba2\u6237\u7aef\u5904\u7406\u8fd4\u56de\u7684\u6d88\u606f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u57fa\u4e8e\u4e86PUSH\u7684\u65b9\u5f0f,\u8bb2\u89e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/2970"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2970"}],"version-history":[{"count":0,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/2970\/revisions"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2970"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}