{"id":10020813,"date":"2025-06-05T19:20:27","date_gmt":"2025-06-05T11:20:27","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020813"},"modified":"2025-06-05T19:20:27","modified_gmt":"2025-06-05T11:20:27","slug":"vivo-pulsar%e4%b8%87%e4%ba%bf%e7%ba%a7%e6%b6%88%e6%81%af%e5%a4%84%e7%90%86%e5%ae%9e%e8%b7%b5%ef%bc%881%ef%bc%89-%e6%95%b0%e6%8d%ae%e5%8f%91%e9%80%81%e5%8e%9f%e7%90%86%e8%a7%a3%e6%9e%90%e5%92%8c","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020813","title":{"rendered":"vivo Pulsar\u4e07\u4ebf\u7ea7\u6d88\u606f\u5904\u7406\u5b9e\u8df5\uff081\uff09-\u6570\u636e\u53d1\u9001\u539f\u7406\u89e3\u6790\u548c\u6027\u80fd\u8c03\u4f18"},"content":{"rendered":"<blockquote>\n<p>\u4f5c\u8005\uff1avivo \u4e92\u8054\u7f51\u5927\u6570\u636e\u56e2\u961f- Quan Limin<\/p>\n<p>\u672c\u6587\u662fvivo\u4e92\u8054\u7f51\u5927\u6570\u636e\u56e2\u961f\u300avivo Pulsar\u4e07\u4ebf\u7ea7\u6d88\u606f\u5904\u7406\u5b9e\u8df5\u300b\u7cfb\u5217\u6587\u7ae0\u7b2c1\u7bc7\u3002<\/p>\n<p>\u6587\u7ae0\u4ee5Pulsar client\u6a21\u5757\u4e2d\u7684Producer\u4e3a\u89e3\u6790\u5bf9\u8c61\uff0c\u901a\u8fc7\u5bf9Producer\u6570\u636e\u53d1\u9001\u539f\u7406\u8fdb\u884c\u9010\u5c42\u5206\u6790\uff0c\u4ee5\u53ca\u5206\u4eab\u53c2\u6570\u8c03\u4f18\u5b9e\u6218\u6848\u4f8b\uff0c\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u4e0e\u4f7f\u7528\u597dProducer\uff0c\u5e76\u4f53\u4f1a\u5230Producer\u5bf9\u6d88\u606f\u4e2d\u95f4\u4ef6\u7cfb\u7edf\u7a33\u5b9a\u6027\u4ee5\u53ca\u5904\u7406\u6027\u80fd\u6240\u8d77\u5230\u7684\u5173\u952e\u4f5c\u7528\u3002<\/p>\n<\/blockquote>\n<h2>\u4e00\u3001Pulsar \u7b80\u8981\u4ecb\u7ecd<\/h2>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611512\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\"><\/p>\n<p>Pulsar\u662f\u65b0\u4e00\u4ee3\u7684\u4e91\u539f\u751f\u6d88\u606f\u4e2d\u95f4\u4ef6\uff0c\u7531Apache\u8f6f\u4ef6\u57fa\u91d1\u4f1a\u5b75\u5316\u548c\u5f00\u6e90\u3002\u5b83\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u4e3a\u4e86\u6ee1\u8db3\u73b0\u4ee3\u6570\u636e\u5904\u7406\u548c\u8ba1\u7b97\u5e94\u7528\u7a0b\u5e8f\u5bf9\u53ef\u6269\u5c55\u6027\u3001\u53ef\u9760\u6027\u548c\u9ad8\u6027\u80fd\u7684\u9700\u6c42\uff0c\u5177\u5907\u5b58\u50a8\u4e0e\u8ba1\u7b97\u5206\u79bb\u3001\u8282\u70b9\u5bf9\u7b49\u3001\u72ec\u7acb\u6269\u5c55\u3001\u5b9e\u65f6\u5747\u8861\u3001\u8282\u70b9\u6545\u969c\u5feb\u901f\u6062\u590d\u7b49\u7279\u6027\u3002<\/p>\n<p>Pulsar\u7531\u56db\u4e2a\u6838\u5fc3\u6a21\u5757\u7ec4\u6210\uff1abroker\u3001bookKeeper\u548cclient\uff08Producer\u548cConsumer\uff09\u3001zk\uff08\u5143\u6570\u636e\u7ba1\u7406\u548c\u8282\u70b9\u534f\u8c03\uff09\u3002broker\u63a5\u53d7\u6765\u81eaProducer\u7684\u6d88\u606f\uff0c\u5c06\u6d88\u606f\u8def\u7531\u5230\u5bf9\u5e94\u7684topic\uff1bbookKeeper\u7528\u4e8e\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8\u548c\u6570\u636e\u590d\u5236\uff1bConsumer\u6d88\u8d39topic\u4e0a\u7684\u6570\u636e\u3002Pulsar\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u548c\u534f\u8bae\uff08\u5982Java\u3001C++\u3001Go\u3001Python\u7b49\uff09\uff0c\u53ef\u4ee5\u8fd0\u884c\u5728\u4e91\u3001\u672c\u5730\u548c\u6df7\u5408\u73af\u5883\u4e2d\uff0c\u6269\u5c55\u6027\u597d\uff0c\u652f\u6301\u591a\u79df\u6237\u548c\u8de8\u6570\u636e\u4e2d\u5fc3\u590d\u5236\u7b49\u7279\u6027\u3002\u56e0\u6b64\uff0cPulsar\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u4e91\u8ba1\u7b97\u3001\u5927\u6570\u636e\u3001\u7269\u8054\u7f51\u7b49\u9886\u57df\u7684\u5b9e\u65f6\u6d88\u606f\u4f20\u9012\u548c\u5904\u7406\u5e94\u7528\u4e2d\u3002<\/p>\n<h2>\u4e8c\u3001Pulsar Producer\u89e3\u6790<\/h2>\n<p>\u9996\u5148\u9700\u8981\u4e86\u89e3Producer\u7684\u6570\u636e\u53d1\u9001\u6d41\u7a0b\uff0c\u8fd9\u91cc\u4ee5\u201c\u5f00\u542f\u538b\u7f29\u3001batch\u53d1\u9001\u6d88\u606f\u7ed9partitioned topic\u201c\u8fd9\u6837\u7684\u4e00\u4e2a\u7ebf\u4e0a\u5e38\u89c4\u573a\u666f\u4e3a\u4f8b\uff0c\u89e3\u6790\u6570\u636e\u7684\u53d1\u9001\u7684\u5173\u952e\u73af\u8282\u3002<\/p>\n<p><strong>tips\uff1a<\/strong><\/p>\n<p>\u5728Pulsar\u4e2d\u6709\u65e0\u5206\u533a\uff08Non-Partitioned\uff09Topic \u548c\u6709\u5206\u533a (Partitioned) \u7684 Topic\u4e4b\u5206\uff0cPartitioned topic\u6700\u5c0f\u5206\u533a\u6570\u4e3a1\uff0c\u4e3a\u6ee1\u8db3\u4efb\u52a1\u7684\u62d3\u5c55\u6027\uff0c\u5728\u7ebf\u4e0a\u4e00\u822c\u4f7f\u7528Partitioned topic\u3002<\/p>\n<h3>2.1 \u6d88\u606f\u751f\u4ea7\u4e0e\u53d1\u9001\u7684\u8be6\u7ec6\u6d41\u7a0b<\/h3>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611513\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>Producer\u53d1\u9001\u6570\u636e\u4e3b\u8981\u5206\u4e3a<strong>12\u4e2a\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>\u2460 \u521b\u5efaProducer\uff1a<\/strong><\/p>\n<p>partitioned topic\u521b\u5efa\u7684\u662f\u4e00\u4e2aPartitioned-<\/p>\n<p>ProducerImpl\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u4e86\u6240\u6709\u5206\u533a\u53ca\u5176\u5bf9\u5e94\u7684ProducerImpl\u5bf9\u8c61\uff0cProducerImpl\u5bf9\u8c61\u8d1f\u8d23\u6240\u5bf9\u5e94\u5206\u533a\u6570\u636e\u7684\u7ef4\u62a4\u548c\u53d1\u9001\u3002<\/p>\n<p><strong>\u2461 \u6784\u9020\u6d88\u606f\uff1a<\/strong><\/p>\n<p>\u4e00\u6761\u6d88\u606f\u88ab\u53d1\u9001\u524d\u9996\u5148\u4f1a\u88ab\u5c01\u88c5\u6210\u4e3a\u4e00\u4e2aMessage\u5bf9\u8c61\uff0c\u5bf9\u8c61\u4e2d\u5305\u542b\u4e86\u6240\u53d1\u9001\u7684topic name\u3001\u6d88\u606f\u4f53\u3001\u6d88\u606f\u5927\u5c0f\u3001schema\u7c7b\u578b\u3001metadata\uff08\u662f\u5426\u6307\u5b9akey\u7b49\uff09\u7b49\u4fe1\u606f\u3002<\/p>\n<p><strong>\u2462 \u786e\u5b9a\u76ee\u6807\u5206\u533a\uff1a<\/strong><\/p>\n<p>\u5728\u53d1\u9001\u6d88\u606f\u524d\u9700\u8981\u901a\u8fc7\u8def\u7531\u7b56\u7565\u51b3\u5b9a\u53d1\u5f80\u54ea\u4e00\u4e2a\u5206\u533a\uff0c\u9009\u62e9\u5bf9\u5e94\u5206\u533a\u7684ProducerImpl\u5bf9\u8c61\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<p><strong>\u2463 \u62e6\u622a\u5668\uff1a<\/strong><\/p>\n<p>Producer\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u7684\u62e6\u622a\u5668\uff0c\u62e6\u622a\u5668\u9700\u8981\u5b9e\u73b0producerInterceptor\u63a5\u53e3\uff0c\u5728\u6d88\u606f\u53d1\u9001\u524d\u53ef\u5bf9\u6d88\u606f\u8fdb\u884c\u62e6\u622a\u4fee\u6539\u3002<\/p>\n<p><strong>\u2464 \u6d88\u606f\u5806\u79ef\u63a7\u5236\uff1a<\/strong><\/p>\n<p>Producer\u53ef\u4ee5\u5904\u7406\u7684\u6d88\u606f\u662f\u6709\u9650\u7684\uff0c\u63a5\u6536\u65b0\u7684\u6d88\u606f\u65f6\u4f1a\u5206\u522b\u8fdb\u884c\u4fe1\u53f7\u91cf\u548c\u5185\u5b58\u4f7f\u7528\u7387\u6821\u9a8c\uff0c\u63a7\u5236\u63a5\u6536\u6d88\u606f\u7684\u901f\u7387\uff0c\u9632\u6b62\u6d88\u606f\u65e0\u9650\u5728\u672c\u5730\u5806\u79ef\u3002<\/p>\n<p><strong>\u2465 batch\u5bb9\u5668\u7ba1\u7406\uff1a<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5206\u597d\u533a\u7684\u6d88\u606f\u4e0d\u662f\u76f4\u63a5\u88ab\u53d1\u9001\uff0c\u800c\u662f\u653e\u5165\u4e86\u751f\u4ea7\u8005\u7684\u4e00\u4e2abatch\u7f13\u5b58\u5bb9\u5668\u4e2d\u91cc\u9762\u3002\u5728\u8fd9\u4e2a\u7f13\u5b58\u91cc\u9762\uff0c\u591a\u6761\u6d88\u606f\u4f1a\u88ab\u5c01\u88c5\u6210\u4e3a\u4e00\u4e2a\u6279\u6b21\uff08batch\uff09\u3002<\/p>\n<p><strong>\u2466 \u6d88\u606f\u5e8f\u5217\u5316\uff1a<\/strong><\/p>\n<p>\u00a0Pulsar \u7684\u6d88\u606f\u9700\u8981\u4ece\u5ba2\u6237\u7aef\u4f20\u5230\u670d\u52a1\u7aef\uff0c\u6d89\u53ca\u5230\u7f51\u7edc\u4f20\u8f93\uff0c\u56e0\u6b64Producer\u5c06batch\u7f13\u51b2\u533a\u4e2d\u7684\u6240\u6709\u6d88\u606f\u9010\u4e00\u8fdb\u884c\u5e8f\u5217\u5316\u3002<\/p>\n<p><strong>\u2467 \u538b\u7f29\uff1a<\/strong><\/p>\n<p>Pulsar\u5185\u7f6e\u4e86\u591a\u79cd\u538b\u7f29\u7b97\u6cd5\uff0c\u5728\u53d1\u9001\u524d\u4f1a\u6839\u636e\u6240\u9009\u62e9\u7684\u538b\u7f29\u7b97\u6cd5\u5bf9batch\u6574\u4f53\u8fdb\u884c\u538b\u7f29\uff0c\u8fd9\u5c06\u4f18\u5316\u7f51\u7edc\u4f20\u8f93\u4ee5\u63d0\u9ad8Pulsar\u6d88\u606f\u4f20\u8f93\u7684\u6027\u80fd\u3002<\/p>\n<p><strong>\u2468 \u6784\u5efa\u6d88\u606f\u53d1\u9001\u5bf9\u8c61\uff1a<\/strong><\/p>\n<p>\u65e0\u8bba\u662f\u5f00\u542fbatch\u7684\u6279\u6b21\u6d88\u606f\uff0c\u8fd8\u662f\u5173\u95edbatch\u7684\u5355\u6761\u6d88\u606f\uff0c\u90fd\u4f1a\u88ab\u5305\u88c5\u4e3a\u4e00\u4e2aOpSendMsg\u5bf9\u8c61\uff0cOpSendMsg\u4e5f\u662fProducer\u53d1\u9001\u548cpulsar broker\u63a5\u6536\u5904\u7406\u7684\u6700\u5c0f\u5355\u4f4d\u3002<\/p>\n<p><strong>\u2469 pending\u961f\u5217\uff1a<\/strong><\/p>\n<p>\u6240\u6709\u6784\u5efa\u597d\u7684OpSendMsg\u5728\u53d1\u9001\u524d\u90fd\u4f1a\u88ab\u653e\u5165pendingMessages\u961f\u5217\u4e2d\uff0c\u6d88\u606f\u5904\u7406\u5b8c\u6210\u540e\u624d\u4f1a\u4ece\u961f\u5217\u4e2d\u79fb\u9664\u3002<\/p>\n<p><strong>\u246a\u00a0\u6d88\u606f\u4f20\u8f93\uff1a<\/strong><\/p>\n<p>Pulsar \u4f7f\u7528netty\u5c06\u6d88\u606f\u5f02\u6b65\u7684\u4ece\u5ba2\u6237\u7aef\u53d1\u9001\u5230\u670d\u52a1\u7aef\uff0cBroker\u8282\u70b9\u5c06\u5728\u6536\u5230\u6d88\u606f\u540e\u5bf9\u5176\u8fdb\u884c\u786e\u8ba4\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u6307\u5b9a\u4e3b\u9898\u7684\u6301\u4e45\u5b58\u50a8\u4e2d\u3002<\/p>\n<p><strong>\u246b \u54cd\u5e94\u5904\u7406\uff1a<\/strong><\/p>\n<p>Pulsar Broker \u5728\u6536\u5230\u6d88\u606f\u65f6\u4f1a\u8fd4\u56de\u4e00\u4e2a\u54cd\u5e94\uff0c\u5982\u679c\u5199\u5165\u6210\u529f\uff0c\u6d88\u606f\u5c06\u4f1a\u4ecependingMessages\u961f\u5217\u4e2d\u79fb\u9664\u3002\u5982\u679c\u5199\u5165\u5931\u8d25\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\uff0c\u751f\u4ea7\u8005\u5728\u6536\u5230\u53ef\u91cd\u8bd5\u9519\u8bef\u4e4b\u540e\u4f1a\u5c1d\u8bd5\u91cd\u65b0\u53d1\u9001\u6d88\u606f\uff0c\u76f4\u5230\u91cd\u8bd5\u6210\u529f\u6216\u8d85\u65f6\u3002<\/p>\n<h3>2.2 \u5173\u952e\u73af\u8282\u539f\u7406\u5206\u6790<\/h3>\n<p>\u63a5\u4e0b\u6765\u4f1a\u5bf9\u4e0a\u8ff0\u6d41\u7a0b\u4e2d\u5173\u952e\u73af\u8282\u7684\u8bbe\u8ba1\u548c\u539f\u7406\u4f5c\u8fdb\u4e00\u6b65\u7684\u5256\u6790\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u7684\u7406\u89e3Producer\u3002<\/p>\n<h4>2.2.1 \u521b\u5efaProducer<\/h4>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611514\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u5728Pulsar\u4e2d\uff0cPartitionedProducerImpl\u7528\u4e8e\u5c06\u591a\u4e2aProducerImpl\u5bf9\u8c61\u5305\u88c5\u6210\u4e3a\u4e00\u4e2a\u903b\u8f91\u751f\u4ea7\u8005\uff0c\u4ee5\u4fbf\u5411Partitioned Topic\u53d1\u9001\u6d88\u606f\u65f6\u80fd\u591f\u6279\u91cf\u64cd\u4f5c\u3002\u5176\u4e2d\uff0cPartitionedProducerImpl.producers\u6210\u5458\u53d8\u91cf\u7ef4\u62a4\u4e86\u6bcf\u4e2a\u5206\u533a\u53ca\u5176\u5bf9\u5e94\u7684ProducerImpl\u5bf9\u8c61\uff0c\u8be5\u8bbe\u8ba1\u4e3b\u8981\u6709\u4ee5\u4e0b<strong>3\u4e2a\u597d\u5904\uff1a<\/strong><\/p>\n<p><strong>\u2460 \u6bcf\u4e2a\u5206\u533a\u5bf9\u5e94\u4e00\u4e2a\u5355\u72ec\u7684\u751f\u4ea7\u8005\uff1a<\/strong><\/p>\n<p>\u5728Pulsar\u4e2d\uff0cPartitioned Topic\u6309\u7167\u5206\u533a\uff08Partition\uff09\u5c06\u591a\u4e2a ProducerImpl \u5bf9\u8c61\u8fdb\u884c\u5206\u914d\uff0c\u4ee5\u4fbf\u80fd\u591f\u540c\u65f6\u53d1\u5f80\u591a\u4e2a Broker \u8282\u70b9\uff0c\u56e0\u6b64\u5bf9\u4e8e\u6bcf\u4e2a\u5206\u533a\uff0c\u9700\u8981\u62e5\u6709\u4e00\u4e2a\u5355\u72ec\u7684\u751f\u4ea7\u8005\u4ee5\u4fbf\u8fdb\u884c\u53d1\u9001\u64cd\u4f5c\u3002\u5728 PartitionedProducerImpl \u7c7b\u4e2d\uff0c\u9700\u8981\u4e3a\u6bcf\u4e2a\u5206\u533a\u7ef4\u62a4\u4e00\u4e2a ProducerImpl \u5bf9\u8c61\uff0c\u4ee5\u4fbf\u5728\u6d88\u606f\u88ab\u5206\u914d\u597d\u201c\u76ee\u6807\u5206\u533a\u201d\u540e\u53ef\u4ee5\u8c03\u7528\u5bf9\u5e94\u7684ProducerImpl\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p><strong>\u2461\u7b80\u5316\u4ee3\u7801\u903b\u8f91\uff1a<\/strong><\/p>\n<p>\u5728PartitionedProducerImpl\u4e2d\uff0c\u5c06\u6bcf\u4e2a\u5206\u533a\u53ca\u5176\u5bf9\u5e94\u7684ProducerImpl\u5bf9\u8c61\u7ef4\u62a4\u5728\u4e00\u4e2aHashMap\u4e2d\uff0c\u80fd\u591f\u66f4\u52a0\u65b9\u4fbf\u7684\u7ef4\u62a4\u5e76\u7ba1\u7406\u4e0d\u540c\u5206\u533a\u7684\u751f\u4ea7\u8005\uff0c\u4f7f\u5f97\u4ee3\u7801\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u7b80\u660e\u3002<\/p>\n<p><strong>\u2462 \u63d0\u9ad8\u5bb9\u9519\u6027\uff1a<\/strong><\/p>\n<p>\u5f53\u67d0\u4e2a\u5206\u533a\u7684ProducerImpl\u5bf9\u8c61\u65e0\u6cd5\u5de5\u4f5c\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5176\u4ed6\u53ef\u7528\u7684ProducerImpl\u5bf9\u8c61\uff0c\u4ece\u800c\u4fdd\u8bc1\u7cfb\u7edf\u6574\u4f53\u7684\u53ef\u7528\u6027\u3002\u7531\u4e8e\u5c06\u4e0d\u540c\u5206\u533a\u7684ProducerImpl\u5bf9\u8c61\u5206\u522b\u8fdb\u884c\u7ef4\u62a4\uff0c\u56e0\u6b64\u5177\u5907\u66f4\u52a0\u7075\u6d3b\u7684\u5bb9\u9519\u5904\u7406\u7b56\u7565\u3002<\/p>\n<p>\u5728\u7ebf\u4e0a\u5b9e\u8df5\u4e2d\u6211\u4eec\u4e5f\u57fa\u4e8e\u8be5\u8bbe\u8ba1\uff0c\u5728PartitionedProducerImpl\u5c42\u505a\u4e86\u8fdb\u4e00\u6b65\u4f18\u5316\uff0c\u901a\u8fc7\u611f\u77e5\u4e0b\u4e00\u5c42\u6bcf\u4e2aProducerImpl\u7684\u963b\u585e\u72b6\u6001\uff08\u4fe1\u53f7\u91cf\u7684\u4f7f\u7528\u60c5\u51b5\uff09\u6765\u51b3\u5b9a\u65b0\u7684\u6d88\u606f\u53d1\u9001\uff0c\u907f\u514d\u5c06\u6d88\u606f\u6301\u7eed\u53d1\u5f80\u963b\u585e\u8f83\u4e3a\u4e25\u91cd\u7684\u5206\u533a\uff0c\u89c4\u907f\u4e86topic\u88ab\u67d0\u4e00\u4e2a\u5206\u533a\u963b\u585e\u800c\u5f71\u54cd\u5230\u6574\u4f53\u53d1\u9001\u6027\u80fd\u7684\u60c5\u51b5\uff0c\u4e5f\u63d0\u9ad8\u4e86\u7ebf\u4e0a\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\uff0c\u5177\u4f53\u7684\u5b9e\u73b0\u53ef\u4ee5\u8be6\u89c1\u8fd9\u7bc7\u6587\u7ae0\u300a<a href=\"https:\/\/link.segmentfault.com\/?enc=gqhThzJK%2BZFjpCzXO%2BqDYw%3D%3D.DkeDjbBcpNhSSjQVGMDOWAHPS6s1kwfEkDwHxKCLozFeS8oMF0rIoLDmdHb62E7QzRpGEo0vOqHTkUekYzr6P3XTRU%2BIIA7fPYdftgRZarKGuLkSYRsu0Np07f1FIcntkwad7u1sOC4fK4Nu3FWXig%2B14H0K4CWft7gNMi0HICM2mT5GG4u7GpyNEh4a8R7i\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u6784\u5efa\u4e0b\u4e00\u4ee3\u4e07\u4ebf\u7ea7\u4e91\u539f\u751f\u6d88\u606f\u67b6\u6784\uff1aApache Pulsar \u5728 vivo \u7684\u63a2\u7d22\u4e0e\u5b9e\u8df5<\/a>\u300b\u3002<\/p>\n<p>\u5173\u952e\u4ee3\u7801\uff1a<\/p>\n<pre><code>\/\/\u5bf9\u6bcf\u4e00\u4e2a\u5206\u533a\u90fd\u521b\u5efa\u4e00\u4e2aProducerImpl\u5bf9\u8c61\n  private void start(List&lt;Integer&gt; indexList) {\n        AtomicReference&lt;Throwable&gt; createFail = new AtomicReference&lt;Throwable&gt;();\n        AtomicInteger completed = new AtomicInteger();\n \n        for (int partitionIndex : indexList) {\n            createProducer(partitionIndex).producerCreatedFuture().handle((prod, createException) -&gt; {\n.......\n            });\n        }\n    }\n \n    private ProducerImpl&lt;T&gt; createProducer(final int partitionIndex) {\n        return producers.computeIfAbsent(partitionIndex, (idx) -&gt; {\n            String partitionName = TopicName.get(topic).getPartition(idx).toString();\n            return client.newProducerImpl(partitionName, idx,\n                    conf, schema, interceptors, new CompletableFuture&lt;&gt;());\n        });\n    }<\/code><\/pre>\n<h4>2.2.2 \u786e\u5b9a\u76ee\u6807\u5206\u533a<\/h4>\n<p>\u5728\u53d1\u9001\u6d88\u606f\u524d\u9700\u8981\u51b3\u5b9a\u53d1\u5f80\u54ea\u4e00\u4e2a\u5206\u533a\uff0c\u786e\u5b9a\u597d\u5206\u533a\u540e\u4fbf\u8c03\u7528\u5bf9\u5e94\u5206\u533a\u7684ProducerImpl\u5bf9\u8c61\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u76ee\u6807\u5206\u533a\u7684\u786e\u5b9a\u4e3b\u8981\u8ddf\u201c\u8def\u7531\u7b56\u7565\u201d\u548c\u201c\u662f\u5426\u6307\u5b9akey\u201d\u6709\u5173\uff1a<\/p>\n<p><strong>\uff081\uff09\u5982\u679c\u6d88\u606f\u6ca1\u6709\u6307\u5b9akey\uff1a<\/strong>\u5219\u6309\u7167\u4e09\u79cd\u8def\u7531\u7b56\u7565\u7684\u6548\u679c\u9009\u62e9\u5206\u533a\u8fdb\u884c\u53d1\u9001\uff0c\u4e09\u79cd\u8def\u7531\u7b56\u7565\u5982\u4e0b\uff1a<\/p>\n<blockquote>\n<ul>\n<li><strong>SinglePartition\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u5982\u679c\u6d88\u606f\u6ca1\u6709\u6307\u5b9aKey\uff0cProducer\u4f1a\u968f\u673a\u9009\u62e9\u4e00\u4e2a Partition\uff0c\u7136\u540e\u628a\u6240\u6709\u7684\u6d88\u606f\u90fd\u53d1\u9001\u5230\u8fd9\u4e2a Partition\u4e0a\u3002<\/p>\n<ul>\n<li><strong>RoundRobinPartition\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u751f\u4ea7\u8005\u5c06\u4ee5\u8f6e\u8be2\u65b9\u5f0f\u5728\u6240\u6709 Partition\u4e4b\u95f4\u53d1\u5e03\u6d88\u606f\uff0c\u4ee5\u5b9e\u73b0\u6700\u5927\u541e\u5410\u91cf\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u5982\u679c\u5f00\u542f\u4e86batch\u53d1\u9001\uff0c\u5219\u8f6e\u8be2\u5c06\u4f1a\u4ee5\u6279\u4e3a\u5355\u4f4d\u8fdb\u884c\u6d88\u606f\u53d1\u9001\uff0c\u6279\u6b21\u53d1\u9001\u65f6\u6bcf\u9694partitionSwitchMs\u4f1a\u8f6e\u8be2\u4e00\u4e2a Partition\u3002\u5982\u679c\u5173\u95ed\u4e86\u6279\u91cf\u53d1\u9001\uff0c\u90a3\u4e48\u6bcf\u6761\u6d88\u606f\u53d1\u9001\u90fd\u4f1a\u8f6e\u8be2\u4e00\u4e2aPartition\u3002\uff08partitionSwitchMs\u81f3\u5c11\u4e3a\u4e00\u4e2abatchingMaxPublishDelay\u65f6\u95f4\uff09\u3002<\/p>\n<ul>\n<li><strong>CustomPartition\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u4f7f\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u6d88\u606f\u8def\u7531\u5b9e\u73b0\uff0c\u6839\u636e\u81ea\u5b9a\u4e49\u7684 Router\u5b9e\u73b0\u51b3\u5b9a\u6d88\u606f\u8981\u53d1\u5f80\u54ea\u4e2a\u5206\u533a\u3002\u7528\u6237\u81ea\u5b9a\u4e49\u7684 Router\u53ef\u4ee5\u901a\u8fc7 messageRoute\u53c2\u6570\u8bbe\u7f6e\u3002\u81ea\u5b9a\u4e49\u7684 Router\u9700\u8981\u5b9e\u73b0 MessageRouter\u63a5\u53e3\u7684 choosePartition\u65b9\u6cd5\u3002<\/p>\n<\/blockquote>\n<p><strong>\uff082\uff09\u5982\u679c\u6d88\u606f\u6307\u5b9akey\uff1a<\/strong>\u5219\u4f1a\u5bf9Key\u505a\u54c8\u5e0c\u5904\u7406,\u7136\u540e\u627e\u5230\u5bf9\u5e94\u7684 Partition\uff0c\u628akey\u6240\u5bf9\u5e94\u7684\u6d88\u606f\u90fd\u53d1\u9001\u5230\u540c\u4e00\u4e2a\u5206\u533a\uff1a<\/p>\n<blockquote><p>\u5bf9\u6d88\u606f\u7684Key\u8fdb\u884c\u54c8\u5e0c\u5904\u7406\u540e\u5982\u4f55\u627e\u5230\u5bf9\u5e94\u7684 Partition\u7684\uff1f\u5373\u4f7f\u7528Key\u7684\u54c8\u5e0c\u503c\u5bf9\u603b\u7684 Partition\u6570\u53d6\u6a21\uff1aN=(Key\u7684\u54c8\u5e0c\u503c%\u603b\u7684 Partition\u6570)\uff0c\u5f97\u5230\u7684N\u5c31\u662f\u7b2cN\u4e2a Partition\uff0cProducer\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e hashingscheme\u6765\u4f7f\u7528\u4e0d\u540c\u7684\u54c8\u5e0c\u7b97\u6cd5 \uff0c\u73b0\u5728\u5df2\u7ecf\u652f\u6301 JavastringHash\u548c Murmur3_32Hash\u4e24\u79cd\u54c8\u5e0c\u7b97\u6cd5\uff0c\u524d\u8005\u76f4\u63a5\u8c03\u7528String.hash.Code()\uff0c\u540e\u8005\u4f7f\u7528Murmur3\u3002<\/p><\/blockquote>\n<p>\u8def\u7531\u7b56\u7565\u7684\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/SinglePartition\u8def\u7531\u7b56\u7565\uff1a\npublic int choosePartition(Message&lt;?&gt; msg, TopicMetadata metadata) {\n    \/\/ If the message has a key, it supersedes the single partition routing policy\n    if (msg.hasKey()) {\n        return signSafeMod(hash.makeHash(msg.getKey()), metadata.numPartitions());\n    }\n \n    return partitionIndex;\n}<\/code><\/pre>\n<pre><code>\/\/RoundRobin\u8def\u7531\u7b56\u7565\uff1a\npublic int choosePartition(Message&lt;?&gt; msg, TopicMetadata topicMetadata) {\n    \/\/ If the message has a key, it supersedes the round robin routing policy\n    if (msg.hasKey()) {\n        return signSafeMod(hash.makeHash(msg.getKey()), topicMetadata.numPartitions());\n    }\n \n    if (isBatchingEnabled) { \/\/ if batching is enabled, choose partition on `partitionSwitchMs` boundary.\n        long currentMs = clock.millis();\n        return signSafeMod(currentMs \/ partitionSwitchMs + startPtnIdx, topicMetadata.numPartitions());\n    } else {\n        return signSafeMod(PARTITION_INDEX_UPDATER.getAndIncrement(this), topicMetadata.numPartitions());\n    }\n}<\/code><\/pre>\n<h4>2.2.3 \u6d88\u606f\u5806\u79ef\u63a7\u5236<\/h4>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611515\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>Producer\u4e0d\u53ef\u80fd\u65e0\u9650\u63a5\u6536\u65b0\u7684\u6d88\u606f\uff0c\u5982\u679c\u67d0\u4e9b\u5206\u533a\u6570\u636e\u53d1\u9001\u8f83\u6162\uff0c\u6d88\u606f\u5c31\u4f1a\u5806\u79ef\u5728Prouducer\u7f13\u5b58\u4e2d\uff0c\u5bfc\u81f4\u5df2\u7ecf\u963b\u585e\u7684\u5206\u533a\u5806\u79ef\u5927\u91cf\u7684\u6d88\u606f\uff0c\u53c8\u65e0\u6cd5\u91cd\u65b0\u53d1\u5f80\u5176\u4ed6\u5206\u533a\uff0c\u540c\u65f6\u4e5f\u53ef\u80fd\u56e0\u4e3a\u65e0\u9650\u5806\u79ef\u7684\u6d88\u606f\u5360\u7528\u4e86\u5927\u91cf\u7684\u5185\u5b58\uff0c\u4f7f\u5f97\u4efb\u52a1\u9891\u7e41GC\u751a\u81f3OOM\u3002<\/p>\n<p>\u5728Pulsar\u63d0\u4f9b\u4e86\u4e24\u4e2a\u6838\u5fc3\u7684\u901f\u7387\u9650\u5236\u7b56\u7565\u548c\u4e00\u4e2a\u963b\u585e\u65f6\u7684\u6d88\u606f\u5904\u7406\u7b56\u7565\uff1a<\/p>\n<blockquote>\n<ul>\n<li><strong>\u6d88\u606f\u6570\u91cf\u9650\u5236\uff1a<\/strong><\/li>\n<\/ul>\n<p>maxPendingMessages\u63a7\u5236\u6bcf\u4e2a\u5206\u533a\u67d0\u4e00\u65f6\u523b\u6700\u5927\u53ef\u5904\u7406\u6d88\u606f\u6570\u91cf\uff0c\u901a\u8fc7\u4fe1\u53f7\u91cf\u7684\u65b9\u5f0f\u63a7\u5236\u201c\u65b0\u8fdb\u5165\u7684\u6d88\u606f\u201d\u7684\u4fe1\u53f7\u91cf\u5206\u914d\u548c\u201c\u5904\u7406\u5b8c\u6210\u6d88\u606f\u201c\u7684\u4fe1\u53f7\u91cf\u91ca\u653e\uff0c\u9632\u6b62\u67d0\u4e2a\u5206\u533a\u7684\u6d88\u606f\u4e25\u91cd\u5806\u79ef\u3002<\/p>\n<ul>\n<li><strong>\u6d88\u606f\u5360\u7528\u5185\u5b58\u5927\u5c0f\u9650\u5236\uff1a<\/strong><\/li>\n<\/ul>\n<p>memoryLimit\u63a7\u5236\u6574\u4e2aPulsar client\u7684\u6d88\u606f\u6700\u5927\u5360\u7528\u5185\u5b58\u5927\u5c0f\uff0c\u901a\u8fc7\u8ba1\u6570\u5668\u65b9\u5f0f\u63a7\u5236\u201c\u65b0\u8fdb\u5165\u7684\u6d88\u606f\u201d\u6709\u6548\u8f7d\u8377\u7684\u5185\u5b58\u5206\u914d\u548c\u201c\u5904\u7406\u5b8c\u6210\u6d88\u606f\u201c\u6709\u6548\u8f7d\u8377\u7684\u5185\u5b58\u91ca\u653e\uff0c\u8fd9\u91cc\u9700\u8981\u7279\u6b8a\u8bf4\u660e\u7684\u662fmemoryLimit\u662fclient\u7684\u53c2\u6570\uff0c\u9488\u5bf9\u7684\u662f\u8be5client\u5bf9\u8c61\u4e0b\u7684\u6240\u6709topic\uff0c\u56e0\u6b64\u5e76\u4e0d\u5efa\u8bae\u4e00\u4e2aPulsar client\u5bf9\u8c61new\u591a\u4e2aProducer topic \uff0c\u56e0\u4e3a\u5f88\u5bb9\u6613\u51fa\u73b0\u67d0\u4e00\u4e2atopic\u5360\u7528\u5185\u5b58\u8fc7\u591a\uff0c\u5bfc\u81f4\u53e6\u4e00\u4e2atopic\u65e0\u7a7a\u95f4\u53ef\u5206\u914d\u7684\u60c5\u51b5\u3002<\/p>\n<ul>\n<li><strong>\u963b\u585e\u5904\u7406\u7b56\u7565\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u7531blockIfQueueFull\u8fdb\u884c\u63a7\u5236\uff0c\u5f53blockIfQueueFull\u4e3atrue\u65f6\uff0c\u4ee3\u8868\u963b\u585e\u7b49\u5f85\uff0cProducer\u4f1a\u7b49\u5f85\u83b7\u53d6\u4fe1\u53f7\u91cf\uff1b\u5f53blockIfQueueFull\u4e3afalse\u65f6\uff0c\u4e00\u65e6\u83b7\u53d6\u4e0d\u5230\u4fe1\u53f7\u91cf\uff0c\u5c31\u4f1a\u7acb\u523b\u5931\u8d25\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u5982\u679cblockIfQueueFull\u4e3afalse\uff0c\u4e1a\u52a1\u9700\u8981\u5904\u7406\u597d\u6d88\u606f\u5931\u8d25\u540e\u7684\u56de\u8c03\u7b56\u7565\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u6570\u636e\u5728Producer\u4e0a\u201c\u4e22\u5931\u201d\u3002<\/p>\n<\/blockquote>\n<p>\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>public void sendAsync(Message&lt;?&gt; message, SendCallback callback) {\n......\n        MessageImpl&lt;?&gt; msg = (MessageImpl&lt;?&gt;) message;\n        MessageMetadata msgMetadata = msg.getMessageBuilder();\n        ByteBuf payload = msg.getDataBuffer();\n        int uncompressedSize = payload.readableBytes();\n        \/\/\u5bf9\u53d1\u9001\u961f\u5217\u5927\u5c0f\u4ee5\u53caclient memory\u8fdb\u884c\u5224\u65ad\u662f\u5426\u6709\u7a7a\u95f4\u653e\u5165\u65b0\u7684\u6d88\u606f\n        if (!canEnqueueRequest(callback, message.getSequenceId(), uncompressedSize)) {\n            return;\n        }\n......\n    }\n \n    private boolean canEnqueueRequest(SendCallback callback, long sequenceId, int payloadSize) {\n        try {\n            if (conf.isBlockIfQueueFull()) {\n                \/\/\u5f53blockIfQueueFull\u4e3atrue\u65f6\uff0c\u7b49\u5f85\u83b7\u53d6\u4fe1\u53f7\u91cf\n                if (semaphore.isPresent()) {\n                    semaphore.get().acquire();\n                }\n                \/\/\u5206\u914d\u6d88\u606f\u6709\u6548\u8f7d\u8377\u6240\u9700\u8981\u7684\u5185\u5b58\u7a7a\u95f4\n                client.getMemoryLimitController().reserveMemory(payloadSize);\n            } else {\n                \/\/\u5f53blockIfQueueFull\u4e3afalse\u65f6\uff0c\u5982\u679c\u65e0\u6cd5\u83b7\u53d6\u5230\u4fe1\u53f7\u91cf\uff0c\u5219\u5feb\u901f\u5931\u8d25\n                if (!semaphore.map(Semaphore::tryAcquire).orElse(true)) {\n                    callback.sendComplete(new PulsarClientException.ProducerQueueIsFullError(&quot;Producer send queue is full&quot;, sequenceId));\n                    return false;\n                }\n                \/\/\u5982\u679c\u6ca1\u6709\u5982\u4f55\u7684\u5185\u5b58\u7a7a\u95f4\u7528\u4e8e\u6d88\u606f\u5206\u914d\uff0c\u5219\u62a5\u9519\n                if (!client.getMemoryLimitController().tryReserveMemory(payloadSize)) {\n                    semaphore.ifPresent(Semaphore::release);\n                    callback.sendComplete(new PulsarClientException.MemoryBufferIsFullError(&quot;Client memory buffer is full&quot;, sequenceId));\n                    return false;\n                }\n            }\n        } catch (InterruptedException e) {\n            Thread.currentThread().interrupt();\n            callback.sendComplete(new PulsarClientException(e, sequenceId));\n            return false;\n        }\n \n        return true;\n    }<\/code><\/pre>\n<h4>2.2.4 \u6d88\u606fbatch\u5bb9\u5668\u6253\u5305<\/h4>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611516\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p><strong>\uff081\uff09batch\u5173\u952e\u7ec4\u6210\u4fe1\u606f<\/strong><\/p>\n<blockquote>\n<ul>\n<li><strong>Messages:<\/strong> \u4fdd\u5b58\u6d88\u606f\u7684list\uff0c\u4fdd\u5b58\u8ddf\u8fd9\u4e2abatch\u76f8\u5173\u6240\u6709\u7684MessageImpl\u5bf9\u8c61\u3002<\/li>\n<li><strong>Metadata\uff1a<\/strong>\u4fdd\u5b58batch\u76f8\u5173\u7684\u5143\u6570\u636e\uff0c\u5982\u6279\u91cf\u6d88\u606f\u7684\u5e8f\u5217\u53f7\u3001\u6d88\u606f\u53d1\u9001\u7684\u65f6\u95f4\u6233\u7b49\u4fe1\u606f\u3002<\/li>\n<li><strong>Callback\uff1a<\/strong>\u4fdd\u5b58\u6d88\u606f\u56de\u8c03\u903b\u8f91\u7684\u96c6\u5408\uff0c\u8bb0\u5f55\u4e86\u6bcf\u4e00\u6761\u6d88\u606f\u5bf9\u5e94\u7684callback\u7b56\u7565\uff0c\u5728batch\u6d88\u606f\u53d1\u9001\u5e76\u7b49\u5230\u670d\u52a1\u7aef\u54cd\u5e94\u540e\uff0c\u4f9d\u6b21\u5bf9\u6d88\u606f\u7684\u56de\u8c03\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<p><strong>\uff082\uff09batch\u6253\u5305\u6761\u4ef6<\/strong><\/p>\n<p>batch\u6253\u5305\u6761\u4ef6\u7684\u4e09\u4e2a\u5173\u952e\u53c2\u6570\uff1a\u6ee1\u8db3\u5176\u4e00\u6570\u636e\u5c31\u4f1a\u88ab\u6253\u5305\u53d1\u9001\u51fa\u53bb\u3002<\/p>\n<blockquote>\n<ul>\n<li><strong>\u6279\u6b21\u5927\u5c0f\uff1a<\/strong>batchingMaxBytes<\/li>\n<li><strong>\u6279\u6b21\u6761\u6570\uff1a<\/strong>batchingMaxMessages<\/li>\n<li><strong>\u6279\u6b21\u5ef6\u8fdf\u53d1\u9001\u65f6\u95f4\uff1a<\/strong><\/li>\n<\/ul>\n<p>batchingMaxPublishDelay<\/p>\n<\/blockquote>\n<p>Pulsar\u4f7f\u7528\u4e24\u4e2a\u6a21\u5757\u8bbe\u8ba1\u6765\u5b9e\u73b0\u4e0a\u9762\u7684\u53c2\u6570\u63a7\u5236\uff1a<\/p>\n<blockquote>\n<ul>\n<li><strong>accumulator\uff1a<\/strong>\u5728 BatchMessage-<\/li>\n<\/ul>\n<p>ContainerImpl \u4e2d\u901a\u8fc7\u8ba1\u6570\u5668\u7684\u65b9\u5f0f\u53bb\u63a7\u5236batch\u7684\u5927\u5c0f\u548c\u6761\u6570\uff0cnumMessages-<\/p>\n<p>InBatch\u8bb0\u5f55\u5df2\u7ecf\u7f13\u5b58\u7684\u6d88\u606f\u6570\u91cf\uff0ccurrentBatchSizeBytes\u7528\u4e8e\u8bb0\u5f55\u5df2\u7f13\u5b58\u7684\u6d88\u606f\u7684\u5927\u5c0f\u3002\u5f53\u8fd9\u4e9b\u53d8\u91cf\u8fbe\u5230\u9608\u503c\u65f6\uff0cBatchMessageContainerImpl \u5c06\u4f1a\u89e6\u53d1\u6279\u91cf\u6d88\u606f\u7684\u53d1\u9001\u3002<\/p>\n<ul>\n<li><strong>batchTimerTask\uff1a<\/strong>\u5f53\u751f\u4ea7\u8005\u4f7f\u7528\u6279\u91cf\u6d88\u606f\u53d1\u9001\u6a21\u5f0f\u65f6\uff0cProducer\u5c06\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668\u4efb\u52a1\uff08batchTimerTask\uff09\uff0c\u5e76\u901a\u8fc7\u8ba1\u65f6\u5668\u7684\u65b9\u5f0f\u5b9a\u65f6\u5c06BatchMessageContainer\u5bb9\u5668\u4e2d\u7684\u6d88\u606f\u8fdb\u884c\u6279\u91cf\u53d1\u9001\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<h4>2.2.5 \u6d88\u606f\u538b\u7f29<\/h4>\n<p>\u5982\u679c\u5f00\u542f\u4e86\u6d88\u606f\u538b\u7f29\uff0c\u5728\u53d1\u9001\u524d\u90fd\u9700\u8981\u8fdb\u884c\u538b\u7f29\u5904\u7406\u3002\u5bf9\u4e8e\u5355\u6761\u6d88\u606f\u53d1\u9001\u7684\u573a\u666f\uff0c\u662f\u5bf9\u6bcf\u4e00\u6761\u6d88\u606f\u8fdb\u884c\u5355\u72ec\u538b\u7f29\u540e\u8fdb\u884c\u53d1\u9001\uff1b\u800c\u5982\u679c\u5f00\u542f\u4e86batch\u5219\u662f\u5bf9\u6574\u4e2abatch\u8fdb\u884c\u538b\u7f29\u540e\u518d\u6574\u4e2a\u8fdb\u884c\u53d1\u9001\u3002<\/p>\n<p>\u5728\u7ebf\u4e0a\u5b9e\u8df5\u4e2d\uff0c\u63a8\u8350\u5728\u4e0d\u5f71\u54cd\u4e1a\u52a1\u5ef6\u8fdf\u7684\u60c5\u51b5\u4e0bbatch\u8d8a\u5927\u8d8a\u597d\uff0c\u4e3b\u8981\u6709<strong>\u4e24\u4e2a\u7406\u7531<\/strong>\uff1a<\/p>\n<blockquote>\n<ul>\n<li><strong>\u53ef\u4ee5\u4f18\u5316\u7f51\u7edcIO\u964d\u4f4eCPU\u8d1f\u8f7d\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u4e0d\u8bbaProducer\u53d1\u9001\u7684\u662f\u4e00\u6761\u6d88\u606f\u8fd8\u662f\u4e00\u6279\u6d88\u606f\uff0c\u5728pulsar\u5ba2\u6237\u7aef\u90fd\u4f1a\u88ab\u6784\u5efa\u4e3a\u4e00\u4e2aOpSendMsg\u5bf9\u8c61\uff0c\u540c\u65f6pulsar broker\u63a5\u6536\u5230\u6d88\u606f\u8fdb\u884c\u5199\u5165\u5904\u7406\u65f6\uff0c\u4e5f\u662f\u6309\u7167OpSendMsg\u4e3a\u4e00\u4e2a\u5904\u7406\u5355\u4f4d\u5c06\u6d88\u606f\u5199\u5165\u78c1\u76d8\uff0c\u56e0\u6b64\u5f53\u6d88\u606f\u6570\u91cf\u4e00\u5b9a\u65f6\uff0cbatch\u8d8a\u5927\uff0c\u5219\u4ee3\u8868\u9700\u8981\u5904\u7406\u7684OpSendMsg\u8d8a\u5c11\u3002<\/p>\n<ul>\n<li><strong>batch\u8d8a\u5927\u201c\u538b\u7f29\u6548\u679c\u5219\u8d8a\u597d\u201d\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u538b\u7f29\u7b97\u6cd5\u5bf9\u5e94\u7684\u538b\u7f29\u7387\u5e76\u4e0d\u56fa\u5b9a\uff0c\u5b83\u901a\u5e38\u53d6\u51b3\u4e8e\u6240\u8981\u538b\u7f29\u7684\u6570\u636e\u5bf9\u8c61\u7684\u5185\u5bb9\u548c\u538b\u7f29\u7b97\u6cd5\u672c\u8eab\uff0c\u538b\u7f29\u7684\u672c\u8d28\u5728\u4e8e\u901a\u8fc7\u6d88\u9664\u6216\u5229\u7528\u6570\u636e\u4e2d\u5b58\u5728\u7684\u5197\u4f59\u6765\u5b9e\u73b0\u6570\u636e\u7684\u538b\u7f29\u548c\u91cd\u6784\u3002\u800cPulsar\u662f\u4ee5batch\u6765\u8fdb\u884c\u6253\u5305\u7684\uff0cbatch\u8d8a\u5927\uff0c\u538b\u7f29\u7684\u76ee\u6807\u5305\u4f53\u8d8a\u5927\u538b\u7f29\u6548\u679c\u5219\u53ef\u80fd\u8d8a\u597d\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u5c3d\u53ef\u80fd\u907f\u514d\u5355\u6761\u6d88\u606f\u56e0\u4e3a\u5305\u4f53\u8f83\u5c0f\u5bfc\u81f4\u8d8a\u538b\u7f29\u540e\u5305\u4f53\u8d8a\u5927\u7684\u60c5\u51b5\u51fa\u73b0\u3002<\/p>\n<\/blockquote>\n<p>\u4ee5\u4e0b\u662f\u5f00\u542f\u4e86batch\u60c5\u51b5\u4e0b\uff0c\u6784\u5efa\u53d1\u9001\u6d88\u606f\u548c\u538b\u7f29\u7684\u5173\u952e\u4ee3\u7801\uff1a<\/p>\n<pre><code>    public OpSendMsg createOpSendMsg() throws IOException {\n        \/\/\u5bf9\u6570\u636e\u8fdb\u884c\u538b\u7f29\u3001\u52a0\u5bc6\u7b49\u64cd\u4f5c\n        ByteBuf encryptedPayload = producer.encryptMessage(messageMetadata, getCompressedBatchMetadataAndPayload());\n......\n \n        ByteBufPair cmd = producer.sendMessage(producer.producerId, messageMetadata.getSequenceId(),\n                messageMetadata.getHighestSequenceId(), numMessagesInBatch, messageMetadata, encryptedPayload);\n        \/\/\u5bf9\u6574\u4e2abatch\u6784\u5efa\u4e00\u4e2aOpSendMsg\n        OpSendMsg op = OpSendMsg.create(messages, cmd, messageMetadata.getSequenceId(),\n                messageMetadata.getHighestSequenceId(), firstCallback);\n......\n        return op;\n    }\n \n    \/\/\u5bf9batch\u8fdb\u884c\u538b\u7f29\uff0c\u5e76\u5c06\u538b\u7f29\u540e\u4fe1\u606f\u66f4\u65b0\u5230messageMetadata\u4e2d\n    private ByteBuf getCompressedBatchMetadataAndPayload() {\n......\n        int uncompressedSize = batchedMessageMetadataAndPayload.readableBytes();\n        ByteBuf compressedPayload = compressor.encode(batchedMessageMetadataAndPayload);\n        batchedMessageMetadataAndPayload.release();\n......\n        return compressedPayload;\n    }<\/code><\/pre>\n<h4>2.2.6 pending\u961f\u5217<\/h4>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611517\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>Pulsar \u4e2d\u7684 pendingMessages\u961f\u5217\u662f\u5ba2\u6237\u7aef\u7528\u6765\u6682\u5b58\u201c\u672a\u5904\u7406\u5b8c\u6210\u7684\u6d88\u606f\u201d\u7684\u4e00\u4e2a\u7f13\u5b58\u961f\u5217\u3002\u7528\u4e8e\u5b58\u50a8\u5f53Producer\u8fde\u63a5\u5230 Broker \u670d\u52a1\u5668\u540e\uff0c\u8fd8\u672a\u53d1\u9001\u6216\u5c1a\u672a\u5f97\u5230 Broker \u7cfb\u7edf\u7684 ACK \u786e\u8ba4\u7684\u6240\u6709\u751f\u4ea7\u8005\uff08Producer\uff09\u7684\u6d88\u606f\u3002\u5728\u53d1\u9001\u6d88\u606f\u4e4b\u524d\uff0cProducer \u9996\u5148\u4f1a\u5c06\u6d88\u606f\u7f13\u5b58\u5230 pendingMessages \u961f\u5217\u4e2d\uff0c\u5e76\u8bb0\u5f55\u4fdd\u5b58\u7f13\u5b58\u6d88\u606f\u7684OpSendMsg\u5bf9\u8c61\uff0c\u76f4\u5230\u5b83\u88ab\u6210\u529f\u53d1\u9001\u5230\u4e86 Broker \u7aef\u5e76\u6536\u5230 Broker \u53d1\u9001\u7684ACK \u786e\u8ba4\u4e4b\u540e\uff0c\u76f8\u5173\u7684\u5143\u4fe1\u606f\u548c\u6d88\u606f\u4fe1\u606f\u624d\u4f1a\u4ece\u961f\u5217\u4e2d\u79fb\u9664\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<strong>pending\u961f\u5217\u7684\u672c\u8d28\u662f\u4e00\u4e2a\u56de\u8c03\u5904\u7406\u961f\u5217\uff0c\u800c\u4e0d\u662f\u53d1\u9001\u961f\u5217<\/strong>\uff0c\u6d88\u606f\u5728\u653e\u5165pending\u961f\u5217\u7684\u540c\u65f6\u5c31\u88ab\u5f02\u6b65\u53d1\u9001\u5230\u670d\u52a1\u7aef\u4e86\uff0c\u6240\u4ee5\u8fd9\u91cc\u9700\u8981\u91cd\u70b9\u7406\u89e3\u4ec0\u4e48\u662f\u201c\u672a\u5904\u7406\u5b8c\u6210\u7684\u6d88\u606f\u201d\u3002<\/p>\n<p>pendingMessages \u961f\u5217\u7684<strong>\u4f5c\u7528\u5728\u4e8e<\/strong>\uff1a\u5bf9\u4e8e\u5df2\u7ecf\u53d1\u9001\u4f46\u5c1a\u672a\u6536\u5230 ACK \u786e\u8ba4\u7684\u6d88\u606f\uff0c\u9632\u6b62\u5728\u8fde\u63a5\u51fa\u73b0\u5f02\u5e38\u65f6\u4e22\u5931\u6d88\u606f\u3002\u5f53\u8fde\u63a5\u4e2d\u65ad\u65f6\uff0c\u7f13\u5b58\u5728 pendingMessages \u961f\u5217\u4e2d\u7684\u672a\u786e\u8ba4\u6d88\u606f\u5c06\u88ab\u8ba4\u4e3a\u662f\u9700\u8981\u91cd\u53d1\u7684\uff0c\u5f53\u8fde\u63a5\u6062\u590d\u65f6\uff0c\u7f13\u5b58\u7684\u6d88\u606f\u5c06\u91cd\u65b0\u53d1\u9001\u5230 Broker \u7aef\uff0c\u4ee5\u786e\u4fdd\u751f\u4ea7\u8005\u751f\u4ea7\u7684\u6d88\u606f\u4e0d\u4f1a\u4e22\u5931\u3002<\/p>\n<p><strong>\u603b\u7684\u6765\u8bf4\uff0c<\/strong>pendingMessages \u961f\u5217\u662f Pulsar \u5ba2\u6237\u7aef\u4fdd\u8bc1\u6d88\u606f\u53ef\u9760\u6027\u548c\u4e00\u81f4\u6027\u7684\u5173\u952e\u529f\u80fd\u7ec4\u4ef6\uff0c\u5728 Pulsar \u7684\u751f\u4ea7\u8005\uff08Producer\uff09\u548c\u6d88\u606f\u786e\u8ba4\u7684\u673a\u5236\u4e2d\u62c5\u4efb\u7740\u975e\u5e38\u91cd\u8981\u7684\u89d2\u8272\u3002<\/p>\n<p>\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>add() \u65b9\u6cd5\u7528\u4e8e\u5728\u8ffd\u52a0\u6d88\u606f\u65f6\u5c06\u6307\u5b9a\u5143\u7d20\u63d2\u5165\u961f\u5217\u4e2d\u7684\u961f\u5c3e\uff0cremove() \u7528\u4e8e\u6d88\u606f\u5728\u5b8c\u6210\u540e\u79fb\u9664\u961f\u5217\u5934\u90e8\u7684\u5143\u7d20\u3002<\/p><\/blockquote>\n<pre><code>    protected void processOpSendMsg(OpSendMsg op) {\n        if (op == null) {\n            return;\n        }\n        try {\n            if (op.msg != null &amp;&amp; isBatchMessagingEnabled()) {\n                batchMessageAndSend();\n            }\n            \/\/\u5c06\u6d88\u606f\u653e\u5165\u201c\u5f85\u5904\u7406\u6d88\u606f\u961f\u5217\u201d\n            pendingMessages.add(op);\n......\n                \/\/ If we do have a connection, the message is sent immediately, otherwise we'll try again once a new\n                \/\/ connection is established\n                op.cmd.retain();\n                cnx.ctx().channel().eventLoop().execute(WriteInEventLoopCallback.create(this, cnx, op));\n                stats.updateNumMsgsSent(op.numMessagesInBatch, op.batchSizeByte);\n...... \n    }\n \n       \/\/\u6dfb\u52a0\u6d88\u606f\u5230pendingMessages\u961f\u5217\n       public boolean add(OpSendMsg o) {\n            \/\/ postpone adding to the queue while forEach iteration is in progress\n            \/\/batch\u7684\u8ba1\u6570\u662f\u6309\u7167batch\u4e2d\u6d88\u606f\u7684\u603b\u91cf\u8fdb\u884c\u8ba1\u6570\n            messagesCount.addAndGet(o.numMessagesInBatch);\n            if (forEachDepth &gt; 0) {\n                if (postponedOpSendMgs == null) {\n                    postponedOpSendMgs = new ArrayList&lt;&gt;();\n                }\n                return postponedOpSendMgs.add(o);\n            } else {\n                return delegate.add(o);\n            }\n        }\n        \/\/\u5c06\u6d88\u606f\u4ecependingMessages\u961f\u5217\u79fb\u9664\n        public void remove() {\n            OpSendMsg op = delegate.remove();\n            if (op != null) {\n                messagesCount.addAndGet(-op.numMessagesInBatch);\n            }\n        }<\/code><\/pre>\n<h4>2.2.7 \u6d88\u606f\u4f20\u8f93<\/h4>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611518\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>Producer\u548cbroker\u90fd\u7ef4\u62a4\u4e86\u5206\u533a\u7ef4\u5ea6\u7684pending\u961f\u5217\u6765\u4fdd\u8bc1\u6d88\u606f\u5904\u7406\u7684\u987a\u5e8f\u6027\uff0c\u4ee5\u53ca\u5b9e\u73b0\u6d88\u606f\u91cd\u65b0\u53d1\u9001\u3001\u91cd\u65b0\u5199\u5165\u6301\u4e45\u5316\u5b58\u50a8\u7684\u80fd\u529b\u3002\u5728Producer\u7aef\uff0c\u6d88\u606f\u88ab\u987a\u5e8f\u8ffd\u52a0\u5230pending\u961f\u5217\u5e76\u5f02\u6b65\u53d1\u9001\u5230\u670d\u52a1\u7aef\uff0c\u670d\u52a1\u7aef\u7684pending\u961f\u5217\u5728\u63a5\u6536\u5230\u6d88\u606f\u540e\uff0c\u6309\u7167\u987a\u5e8f\u8ffd\u52a0\u5230\u961f\u5217\u4e2d\uff0c\u5e76\u6309\u7167\u987a\u5e8f\u5c06\u6570\u636e\u5199\u5165bookie\u8fdb\u884c\u6301\u4e45\u5316\u5904\u7406\uff0c\u5904\u7406\u5b8c\u6210\u540e\u6309\u7167\u987a\u5e8f\u8fd4\u56de\u54cd\u5e94Producer\uff0c\u5e76\u5c06\u6d88\u606f\u4ecebroker pending\u548cproducer pending\u961f\u5217\u4e2d\u79fb\u9664\u3002<\/p>\n<p>\u53e6\u5916\u5728\u6570\u636e\u4f20\u8f93\u8fc7\u7a0b\u4e2d\uff0c\u65e0\u8bba\u662f\u4f7f\u7528Pulsar Producer\u7684\u540c\u6b65\u53d1\u9001\u8fd8\u662f\u5f02\u6b65\u53d1\u9001\uff0c\u5728\u6d88\u606f\u4f20\u8f93\u73af\u8282\u672c\u8d28\u4e0a\u90fd\u662f\u4f7f\u7528netty\u5c06\u6d88\u606f\u5f02\u6b65\u7684\u4ece\u5ba2\u6237\u7aef\u53d1\u9001\u5230\u670d\u52a1\u7aef\uff0c\u533a\u522b\u5728\u4e8esend() \u65b9\u6cd5\u5c01\u88c5\u4e86 sendAsync() \u65b9\u6cd5\uff0c\u4f7f\u5176\u53ef\u4ee5\u5728\u5411\u670d\u52a1\u5668\u53d1\u9001 Pulsar \u6d88\u606f\u65f6\u963b\u585e\u7b49\u5f85 Broker \u7684\u54cd\u5e94\uff0c\u76f4\u5230\u786e\u8ba4\u6d88\u606f\u5df2\u7ecf\u88ab Broker \u6210\u529f\u5904\u7406\u540e\u624d\u4f1a\u8fd4\u56de\uff0c\u5e38\u89c4\u60c5\u51b5\u4e0b\uff0c\u5efa\u8bae\u4f7f\u7528\u5f02\u6b65\u7684\u65b9\u5f0f\u53d1\u9001 Pulsar \u6d88\u606f\uff0c\u56e0\u4e3a\u540c\u6b65\u65b9\u5f0f\u5fc5\u987b\u5728 Broker \u7aef\u6210\u529f\u63a5\u6536\u5230\u6d88\u606f\u4e4b\u540e\u624d\u4f1a\u8fd4\u56de\uff0c\u56e0\u6b64\u4f1a\u5e26\u6765\u8f83\u5927\u7684\u6027\u80fd\u635f\u8017\u548c\u5ef6\u8fdf\u3002\u4f46\u662f\u5728\u90e8\u5206\u573a\u666f\u4e0b\uff0c\u9700\u8981\u4f7f\u7528\u540c\u6b65\u65b9\u5f0f\u6765\u4fdd\u8bc1\u53ef\u9760\u6027\uff0c\u4ee5\u9632 Broker \u7aef\u63a5\u6536\u5931\u8d25\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 send() \u65b9\u6cd5\u5b9e\u73b0\u540c\u6b65\u65b9\u5f0f\u7684\u65b9\u5f0f\u53d1\u9001 Pulsar \u6d88\u606f\u3002<\/p>\n<p>\u4f7f\u7528netty\u6267\u884c\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>    private static final class WriteInEventLoopCallback implements Runnable {  \n......\n        @Override\n        public void run() {\n            if (log.isDebugEnabled()) {\n                log.debug(&quot;[{}] [{}] Sending message cnx {}, sequenceId {}&quot;, producer.topic, producer.producerName, cnx,\n                        sequenceId);\n            }\n \n            try {\n                cnx.ctx().writeAndFlush(cmd, cnx.ctx().voidPromise());\n                op.updateSentTimestamp();\n            } finally {\n                recycle();\n            }\n        }\n......\n    \n    }<\/code><\/pre>\n<h4>2.2.8 \u5904\u7406\u54cd\u5e94<\/h4>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611519\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>Pulsar Producer \u4f7f\u7528\u201cACK \u8ddf\u8e2a\u673a\u5236\u201d\u6765\u5b9e\u73b0\u5bf9 Broker \u8fd4\u56de\u7684 ACK \u786e\u8ba4\u6d88\u606f\u7684\u5904\u7406\uff0c\u7528\u4e8e\u68c0\u6d4b\u548c\u5904\u7406\u5230\u8fbe\u751f\u4ea7\u8005\u7684\u5168\u90e8\u6d88\u606f\u72b6\u6001\u4fe1\u606f\u3002<\/p>\n<p>\u5bf9\u4e8eProducer\u53d1\u9001\u7684\u6d88\u606f\uff0cPulsar\u4f1a\u5bf9\u6bcf\u4e2a\u6d88\u606f\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684 sequenceId \u5e8f\u53f7\uff0c\u5e76\u8bb0\u5f55\u8be5\u6d88\u606f\u7684\u521b\u5efa\u65f6\u95f4\uff08createdAt\uff09\u7b49\u5143\u6570\u636e\u4fe1\u606f\u3002\u5f53 Broker \u786e\u8ba4\u6536\u5230\u67d0\u4e2a\u6d88\u606f\u65f6\uff0cProducer \u4f1a\u4f9d\u636e\u8fd4\u56de\u7684 ACK \u5e8f\u53f7\u548c Broker \u8fd4\u56de\u7684\u786e\u8ba4\u65f6\u95f4\u6765\u5224\u65ad\u5f53\u524d ACK \u662f\u5426\u6709\u6548\uff0c\u5e76\u4ece\u5df2\u7f13\u5b58\u7684 pendingMessages \u961f\u5217\u4e2d\u627e\u5230\u5bf9\u5e94\u7684\u6d88\u606f\u5143\u6570\u636e\u4fe1\u606f\uff0c\u4ee5\u8fdb\u884c\u786e\u8ba4\u5904\u7406\uff0c\u5728 Broker \u786e\u8ba4\u6d88\u606f\u63a5\u6536\u6210\u529f\u65f6\uff0cProducer \u5c06\u4ece\u7b49\u5f85\u786e\u8ba4\u7684\u6d88\u606f\u961f\u5217\u4e2d\u5220\u9664\u5bf9\u5e94\u7684\u6d88\u606f\u5143\u6570\u636e\u4fe1\u606f\uff0c\u5982\u679c Broker \u8fd4\u56de\u7684 ACK \u6d88\u606f\u4e0d\u7b26\u5408\u751f\u4ea7\u8005\u9884\u671f\u7684\u6d88\u606f\u72b6\u6001\u4fe1\u606f\uff0cProducer \u5c06\u4f1a\u91cd\u53d1\u6d88\u606f\uff0c\u76f4\u5230\u91cd\u8bd5\u6210\u529f\u6216\u591a\u6b21\u91cd\u8bd5\u5931\u8d25\u540e\u629b\u51fa\u5f02\u5e38\u540e\u518d\u4ece\u961f\u5217\u4e2d\u79fb\u9664\u5bf9\u5e94\u6d88\u606f\u5143\u6570\u636e\u4fe1\u606f\u5e76\u91ca\u653e\u5bf9\u5e94\u5185\u5b58\u3001\u4fe1\u53f7\u91cf\u7b49\u8d44\u6e90\u3002<\/p>\n<p>\u6d88\u606f\u91cd\u53d1\u7684\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>    private void resendMessages(ClientCnx cnx, long expectedEpoch) {\n        cnx.ctx().channel().eventLoop().execute(() -&gt; {\n            synchronized (this) {\n                \/\/\u5224\u65ad\u8fde\u63a5\u72b6\u6001\uff1a\u5f53\u8fde\u63a5\u6b63\u5728\u5173\u95ed\u6216\u8005\u5df2\u7ecf\u5173\u95ed\u5219\u4e0d\u8fdb\u884c\u91cd\u53d1\n                if (getState() == State.Closing || getState() == State.Closed) {\n                    \/\/ Producer was closed while reconnecting, close the connection to make sure the broker\n                    \/\/ drops the producer on its side\n                    cnx.channel().close();\n                    return;\n                }\n......\n                \/\/\u8c03\u7528\u91cd\u53d1\u6d88\u606f\u65b9\u6cd5\n                recoverProcessOpSendMsgFrom(cnx, null, expectedEpoch);\n            }\n        });\n    }\n \n \n   \/\/ Must acquire a lock on ProducerImpl.this before calling method.\n    private void recoverProcessOpSendMsgFrom(ClientCnx cnx, MessageImpl from, long expectedEpoch) {\n......\n        final boolean stripChecksum = cnx.getRemoteEndpointProtocolVersion() &lt; brokerChecksumSupportedVersion();\n        Iterator&lt;OpSendMsg&gt; msgIterator = pendingMessages.iterator();\n        OpSendMsg pendingRegisteringOp = null;\n        while (msgIterator.hasNext()) {\n            OpSendMsg op = msgIterator.next();\n......\n            op.cmd.retain();\n            if (log.isDebugEnabled()) {\n                log.debug(&quot;[{}] [{}] Re-Sending message in cnx {}, sequenceId {}&quot;, topic, producerName,\n                          cnx.channel(), op.sequenceId);\n            }\n            \/\/\u53d1\u9001\u6d88\u606f\n            cnx.ctx().write(op.cmd, cnx.ctx().voidPromise());\n            op.updateSentTimestamp();\n            stats.updateNumMsgsSent(op.numMessagesInBatch, op.batchSizeByte);\n        }\n        cnx.ctx().flush();\n......\n    }<\/code><\/pre>\n<h2>\u4e09\u3001Pulsar \u6570\u636e\u53d1\u9001\u7aef\u53c2\u6570\u8c03\u4f18\u5b9e\u8df5<\/h2>\n<p>\u6839\u636e\u4ee5\u4e0a\u5bf9\u539f\u7406\u89e3\u6790\uff0c\u6211\u4eec\u5bf9Producer\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2a\u5927\u81f4\u7406\u89e3\uff0c\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2aProducer\u53c2\u6570\u8c03\u4f18\u5b9e\u8df5\u6848\u4f8b\u6765\u5e2e\u52a9\u8bfb\u8005\u57fa\u4e8e\u539f\u7406\u8fdb\u4e00\u6b65\u7406\u89e3\u5ba2\u6237\u7aef\u53c2\u6570\u4e4b\u95f4\u7684\u8054\u7cfb\u3002<\/p>\n<h3>3.1 \u8c03\u4f18\u76ee\u7684<\/h3>\n<p>\u9996\u5148\u8981\u6e05\u695a\u4e3a\u4ec0\u4e48\u8981\u8fdb\u884c\u53c2\u6570\u8c03\u4f18\uff0c\u6709\u4ee5\u4e0b\u4e24\u4e2a\u76ee\u7684\uff1a<\/p>\n<blockquote>\n<ul>\n<li><strong>\u964d\u4f4e\u53c2\u6570\u4f7f\u7528\u95e8\u69db\uff1a<\/strong><\/li>\n<\/ul>\n<p>Pulsar client\u548cProducer\u7684\u51e0\u5341\u4e2a\u914d\u7f6e\u53c2\u6570\uff0c\u53c2\u6570\u591a\u4e14\u8054\u7cfb\u7d27\u5bc6\uff0c\u9700\u8981\u82b1\u8d39\u8f83\u591a\u7684\u65f6\u95f4\u6210\u672c\u53bb\u7406\u89e3\uff0c\u540c\u65f6\u53c2\u6570\u4e4b\u95f4\u5b58\u5728\u534f\u540c\u751f\u6548\u4e92\u76f8\u5f71\u54cd\u7684\u60c5\u51b5\uff0c\u5bf9\u666e\u901a\u4f7f\u7528\u8005\u800c\u8a00\u573a\u666f\u590d\u6742\u7406\u89e3\u95e8\u69db\u9ad8\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u6709\u4e00\u5957\u8f83\u4e3a\u901a\u7528\u7684\u53c2\u6570\u914d\u7f6e\uff0c\u6216\u6709\u516c\u5f0f\u5316\u7684\u53c2\u6570\u914d\u7f6e\u65b9\u6cd5\u8bba\u3002<\/p>\n<ul>\n<li><strong>\u63d0\u5347\u5355\u673a\u5904\u7406\u6027\u80fd\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u7ad9\u5728\u5ba2\u6237\u7aef\u7684\u89d2\u5ea6\uff0c\u76f8\u540c\u65f6\u95f4\u5185\u5904\u7406\u7684\u6570\u636e\u91cf\u8d8a\u591a\uff0c\u5219\u8ba4\u4e3a\u5355\u673a\u5904\u7406\u6027\u80fd\u66f4\u5f3a\u3002\u4f5c\u4e3a\u4e2d\u95f4\u4ef6\u7cfb\u7edf\u7684\u63d0\u4f9b\u8005\uff0c\u6211\u4eec\u7ecf\u5e38\u8ba4\u4e3a\u6027\u80fd\u63d0\u5347\u662f\u670d\u52a1\u7aef\u7684\u4e8b\u60c5\uff0c\u60f3\u5c3d\u529e\u6cd5\u5728pulsar\u7684broker\u548cbookie\u4e0a\u53bb\u63d0\u5347\u5355\u673a\u5904\u7406\u6027\u80fd\uff0c\u4f46pulsar client\u4f5c\u4e3a\u6574\u4e2a\u6d88\u606f\u4e2d\u95f4\u4ef6\u7cfb\u7edf\u7684\u6838\u5fc3\u7ec4\u4ef6\uff0c\u5b83\u80fd\u5426\u53d1\u9001\u597d\u4e00\u4efd\u6570\u636e\uff0c\u5bf9\u6574\u4e2a\u6d88\u606f\u4e2d\u95f4\u4ef6\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u4e5f\u53d1\u6325\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002<\/p>\n<\/blockquote>\n<h3>3.2 \u8c03\u4f18\u5b9e\u8df5<\/h3>\n<p>\u4e0b\u9762\u5c31\u56f4\u7ed5\u201c\u53c2\u6570\u901a\u7528\u6a21\u7248\u5316\u201d\u548c\u201c\u63d0\u5347\u5355\u673a\u5904\u7406\u6027\u80fd\u201d\u4e24\u4e2a\u76ee\u7684\u51fa\u53d1\u5e76\u7ed3\u5408\u4e0a\u8ff0\u8bb2\u89e3\u7684\u6570\u636e\u53d1\u9001\u539f\u7406\uff0c\u6765\u5206\u4eab\u4e00\u4e9b\u5b9e\u8df5\u7ecf\u9a8c\u3002<\/p>\n<h4>3.2.1 \u5173\u8054\u4e0e\u573a\u666f\u76f8\u5173\u7684\u91cd\u70b9\u53c2\u6570<\/h4>\n<p>Pulsar\u5ba2\u6237\u7aef\u53c2\u6570\u867d\u591a\u4f46\u90fd\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c\uff0c\u4e0d\u9700\u8981\u4e00\u4e00\u8c03\u6574\u3002\u53ea\u9700\u8981\u5bf9\u4e1a\u52a1\u573a\u666f\u76f8\u5173\u7684\u9488\u5bf9\u6027\u7684\u53bb\u8c03\u6574\u5373\u53ef\uff0c\u5982\u6211\u4eec\u672c\u6b21\u7684\u53c2\u6570\u8c03\u4f18\u76ee\u7684\u662f\u63d0\u5347\u5355\u673a\u5904\u7406\u6027\u80fd\uff0c\u5219\u91cd\u70b9\u5173\u6ce8\u54ea\u4e9b\u573a\u666f\u54ea\u4e9b\u53c2\u6570\u53ef\u4ee5\u63d0\u5347\u5ba2\u6237\u7aef\u7684\u53d1\u9001\u901f\u7387\u3001\u964d\u4f4e\u670d\u52a1\u7aef\u7684\u538b\u529b\uff0c\u8ba9\u670d\u52a1\u7aef\u53ef\u4ee5\u5904\u7406\u66f4\u591a\u7684\u6570\u636e\uff0c\u6709\u4ee5\u4e0b\u56db\u70b9\u6700\u4e3a\u5173\u952e\uff1a<\/p>\n<blockquote>\n<ul>\n<li><strong>batch\u6253\u5305\u53d1\u9001\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u6d88\u606f\u591a\u6761\u6279\u6b21\u53d1\u9001\uff0c\u5728\u964d\u4f4e\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u7f51\u7edcIO\u7684\u540c\u65f6\u4e5f\u964d\u4f4e\u4e86\u4e24\u8005\u7684cpu\u7684\u8d1f\u8f7d\u3002\u8fd9\u91cc\u9700\u5f3a\u8c03\u7684\u662f\u6211\u4eec\u5e0c\u671bbatch\u662f\u4e00\u4e2a\u5747\u5300\u7684\u3001\u201c\u5b8c\u6574\u201d\u7684\u5305\uff0c\u5982pending\u961f\u5217\u88ab\u6253\u6ee1\uff0cbatch\u53ea\u80fd\u7a7a\u7b49\u5230\u5ef6\u8fdf\u53d1\u9001\u65f6\u95f4\u8fc7\u540e\u88ab\u53d1\u9001\uff0c\u6ca1\u6709\u6784\u5efa\u51fa\u9884\u671f\u4e2d\u7684batch\uff0c\u90a3\u4e48\u53ef\u4ee5\u8ba4\u4e3a\u8fd9\u4e2abatch\u662f\u4e00\u4e2a\u4e0d\u5b8c\u6574\u7684\u5305\uff0c\u8fd9\u79cdbatch\u5305\u542b\u7684\u6570\u636e\u91cf\u5c11\uff0c\u5bf9\u53d1\u9001\u6548\u7387\u6709\u7740\u6781\u5927\u7684\u5f71\u54cd\u3002<\/p>\n<ul>\n<li><strong>\u6570\u636e\u538b\u7f29\uff1a<\/strong><\/li>\n<\/ul>\n<p>Pulsar\u662fIO\u5bc6\u96c6\u578b\u7cfb\u7edf\uff0c\u5e38\u89c4\u60c5\u51b5\u4e0b\u78c1\u76d8\u662f\u7cfb\u7edf\u7684\u4e3b\u8981\u74f6\u9888\uff0c\u5f00\u542f\u538b\u7f29\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u7f51\u7edcI\/O\uff0c\u63d0\u5347\u5904\u7406\u76f8\u540c\u6570\u636e\u91cf\u4e0b\u7684\u8bfb\u5199\u80fd\u529b\u3002\u7531\u4e8e\u538b\u7f29\u662f\u9488\u5bf9batch\u7684\uff0c\u5728\u53d1\u9001\u65f6\u95f4\u4e00\u5b9a\u7684\u60c5\u51b5\u4e0b\uff0cbatch\u8d8a\u5927\u5176\u538b\u7f29\u6548\u679c\u4e5f\u8d8a\u597d\uff0c\u4ee3\u8868\u7740\u5904\u7406\u7684\u6d88\u606f\u91cf\u4e5f\u66f4\u591a\u3002<\/p>\n<ul>\n<li><strong>RoundRobin\u53d1\u9001\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u5c06\u6570\u636e\u5747\u5300\u5730\u5206\u914d\u5230\u591a\u4e2a\u5206\u533a\u4e2d\u3002\u5b83\u7684\u57fa\u672c\u601d\u60f3\u662f\u8f6e\u8be2\u5c06\u65b0\u7684\u6570\u636e\u5199\u5165\u5230\u4e0d\u540c\u7684\u5206\u533a\u4e2d\uff0c\u4ee5\u5747\u8861\u5730\u5206\u6563\u8d1f\u8f7d\u3002<\/p>\n<ul>\n<li><strong>\u6d88\u606f\u5806\u79ef\u63a7\u5236\uff1a<\/strong><\/li>\n<\/ul>\n<p>maxPendingMessages\u4fe1\u53f7\u91cf\u548cmemoryLimit\u9650\u5236\u4e0d\u76f4\u63a5\u63d0\u5347\u53d1\u9001\u901f\u7387\uff0c\u4f46\u5b83\u80fd\u591f\u6709\u6548\u4fdd\u969c\u6211\u4eec\u5ba2\u6237\u7aef\u7684\u7a33\u5b9a\uff0c\u4e5f\u662f\u63a7\u5236\u6216\u9650\u5236\u53d1\u9001\u6548\u7387\u7684\u91cd\u8981\u53c2\u6570\u4e4b\u4e00\u3002<\/p>\n<\/blockquote>\n<p>\u6d89\u53ca\u7684\u5ba2\u6237\u7aef\u5173\u952e\u53c2\u6570\u4ee5\u53ca\u9ed8\u8ba4\u503c\u548c\u6211\u4eec\u7ebf\u4e0a\u8c03\u4f18\u540e\u8bbe\u7f6e\u7684\u6570\u503c\u5982\u4e0b\u8868\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611520\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<h4>3.2.2 \u7ed3\u5408Producer\u53d1\u9001\u539f\u7406\u5206\u6790\u53c2\u6570\u7684\u6548\u679c<\/h4>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u4ee5\u53c2\u6570\u7684\u6548\u7528\u89d2\u5ea6\u6765\u63cf\u8ff0\u4e00\u6761\u6d88\u606f\u4ece\u6784\u5efa\u5230\u53d1\u9001\u7684\u8fc7\u7a0b\uff0c\u8fdb\u4e00\u6b65\u89e3\u91ca\u53c2\u6570\u5982\u6b64\u8bbe\u7f6e\u7684\u610f\u4e49\uff1a<\/p>\n<p><strong>\uff081\uff09\u9009\u62e9\u5206\u533a<\/strong><\/p>\n<p>\u6784\u5efa\u6d88\u606f\u540e\uff0c\u901a\u8fc7messageRoutingMode\u53c2\u6570\u6240\u8bbe\u7f6e\u7684\u8def\u7531\u7b56\u7565\u6765\u9009\u62e9\u5206\u533a\uff0c\u8fd9\u91cc\u4ee5RoundRobinPartition\u4e3a\u8def\u7531\u7b56\u7565\uff0c\u5f00\u542fbatch\u65f6\u5219\u6bcf\u95f4\u9694partitionSwitchMs\u65f6\u95f4\u6362\u4e00\u4e2a\u5206\u533a\u8fdb\u884c\u6570\u636e\u53d1\u9001\uff0cpartitionSwitchMs\u7684\u503c\u4e3a\u201cbatchingPartitionSwitchFrequencyByPublish<\/p>\n<p>-Delay\u3001batchingMaxPublishDelayMicros\u201d\u8fd9\u4e24\u4e2aProducer\u53c2\u6570\u4e4b\u79ef\uff0c\u4e5f\u5c31\u662f\u6bcfbatchingPartition<\/p>\n<p>-SwitchFrequencyByPublishDelay\u4e2abatch\u7684\u6700\u5927\u6253\u5305\u65f6\u95f4\uff0c\u6d88\u606f\u5c31\u4f1a\u8f6e\u6362\u4e00\u4e2a\u5206\u533a\u53d1\u9001\u3002<\/p>\n<p>\u4e3a\u4e86\u80fd\u5728batchingMaxPublishDelayMicros\u5185\u5f97\u5230\u4e00\u4e2a\u8f83\u5927\u7684\u5305\uff0c\u6211\u4eec\u5e0c\u671b\u8fd9\u4e2abatch\u63a5\u6536\u7684\u6d88\u606f\u662f\u8fde\u7eed\u7684\uff0c\u56e0\u6b64batchingPartitionSwitchFrequency-<\/p>\n<p>ByPublishDelay\u4e0d\u80fd\u5c0f\u4e8e1\uff0c\u540c\u65f6\u4e5f\u5e0c\u671b\u4e00\u4e2a\u5206\u533a\u4e4b\u95f4\u6570\u636e\u662f\u8f83\u4e3a\u5747\u5300\u7684\uff0c\u6240\u4ee5batchingPartition-<\/p>\n<p>SwitchFrequencyByPublishDelay\u4e5f\u8981\u5c3d\u91cf\u5c0f\uff0c\u5426\u5219\u5206\u533a\u5bf9\u5e94\u7684\u4fe1\u53f7\u91cfmaxPendingMessages\u8017\u5c3d\u8fd8\u6ca1\u6709\u5207\u6362\u5206\u533a\uff0c\u5c31\u4f1a\u5bfc\u81f4batch\u5fc5\u987b\u7b49\u5f85\u4e00\u4e2abatchingMaxPublishDelayMicros\u65f6\u95f4\u3002\u56e0\u6b64\u5c06batchingPartitionSwitchFrequencyByPublishDelay\u4fee\u6539\u6210\u4e861\uff0c\u4fdd\u8bc1\u6253\u5305\u4e86\u4e00\u4e2abatch\u4e4b\u540e\u5c31\u5207\u6362\u5206\u533a\uff0c\u8fd9\u4e5f\u6781\u5927\u7684\u907f\u514d\u4e86\u5206\u533a\u4fe1\u53f7\u91cf\u8017\u5c3d\uff0c\u51fa\u73b0\u53d1\u9001\u963b\u585e\u3002<\/p>\n<p><strong>\uff082\uff09\u6d88\u606f\u5806\u79ef\u63a7\u5236<\/strong><\/p>\n<p>maxPendingMessages\u4f5c\u4e3a\u5206\u533a\u7684\u4fe1\u53f7\u91cf\uff0c\u4e5f\u662f\u201cpending\u961f\u5217\u201d\u7684\u5927\u5c0f\uff0c\u4ee3\u8868\u7740\u6bcf\u4e2a\u5206\u533a\u80fd\u591f\u540c\u65f6\u5904\u7406\u7684\u6700\u5927\u6d88\u606f\u4e0a\u9650\uff0c\u800cmaxPendingMessages-<\/p>\n<p>AcrossPartitions\u5219\u662f\u9488\u5bf9\u6574\u4e2atopic\u751f\u6548\u7684\uff0cmaxPendingMessages=min( maxPending-<\/p>\n<p>Messages,maxPendingMessagesAcrossPartitions\/Partition\uff09\uff0c\u7531\u4e8e\u7ebf\u4e0a\u5206\u533a\u53ef\u80fd\u4f1a\u53d8\u5316\uff0c\u6709\u4e0d\u786e\u5b9a\u6027\uff0c\u56e0\u6b64\u5c31\u4f7f\u7528\u4e0a\u800c\u8a00\u9664\u975e\u6709\u7279\u6b8a\u7684\u4f7f\u7528\u573a\u666f\uff0c\u5efa\u8bae\u5c06maxPendingMessagesAcrossPartitions\u8bbe\u7f6e\u7684\u6bd4\u8f83\u5927\uff0c\u8ba9maxPendingMessages\u751f\u6548\u5373\u53ef\u3002<\/p>\n<p>\u9664\u4e86maxPendingMessages\u4ee5\u5916\uff0c\u6d88\u606f\u80fd\u5426\u63a5\u6536\u88ab\u653e\u5165pending\u961f\u5217\u4e2d\uff0c\u8fd8\u8981\u770b\u5f53\u524d\u6b63\u5728\u5904\u7406\u7684\u6d88\u606f\u4f53\u5927\u5c0f\u603b\u548c\u662f\u5426\u8d85\u8fc7\u4e86memoryLimit\u53c2\u6570\u7684\u9650\u5236\uff0cmemoryLimit\u63a7\u5236\u4e86\u6d88\u606f\u5f85\u5904\u7406\u961f\u5217\u4e2d\u672a\u538b\u7f29\u524d\u7684\u6d88\u606f\u6709\u6548\u8377\u8f7d\u603b\u548c\uff0c\u53ef\u4ee5\u907f\u514d\u5728\u6d88\u606f\u6709\u6548\u8377\u8f7d\u975e\u5e38\u5927\u65f6\uff0c\u8fd8\u672a\u89e6\u53d1maxPendingMessages\u9650\u5236\uff0c\u5c31\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u8fc7\u591a\u51fa\u73b0\u9891\u7e41GC\u548coom\u7684\u95ee\u9898\u3002\u7531\u4e8ememoryLimit\u662fclient\u7ea7\u522b\u7684\u7b56\u7565\uff0c\u56e0\u6b64\u4e5f\u5efa\u8bae\u4e00\u4e2aclient\u5bf9\u5e94\u4e00\u4e2aPoducer\u3002<\/p>\n<p>\u603b\u800c\u8a00\u4e4bmaxPendingMessages\u63a7\u5236\u4e86\u6bcf\u4e2a\u5206\u533a\u53ef\u4ee5\u5904\u7406\u6d88\u606f\u6570\u91cf\u7684\u4e0a\u9650\uff0cmemoryLimit\u63a7\u5236\u4e86\u6240\u6709\u5206\u533a\u53ef\u4ee5\u6d88\u606f\u5360\u7528\u5185\u5b58\u7684\u4e0a\u9650\uff0c\u4e24\u8005\u76f8\u8f85\u76f8\u6210\u3002<\/p>\n<p><strong>\uff083\uff09\u6d88\u606fbatch\u5bb9\u5668\u6253\u5305<\/strong><\/p>\n<p>\u51b3\u5b9a\u4e00\u4e2abatch\u662f\u5426\u6253\u5305\u5b8c\u6210\u6709\u4e09\u4e2a\u6761\u4ef6\u63a7\u5236\uff0cbatchingMaxBytes\u3001batchingMaxMessages\u3001batchingMaxPublishDelayMicros\u6ee1\u8db3\u5176\u4e00\u5373\u53ef\uff0c\u6839\u636e\u8fd9\u4e09\u4e2a\u53c2\u6570\u7684\u542b\u4e49\u53bb\u8bbe\u7f6e\u503c\u770b\u4f3c\u662f\u5bb9\u6613\u7684\uff0c\u4f46\u5bb9\u6613\u5ffd\u7565\u7684\u662fbatch\u4e2d\u7528\u6765\u6253\u5305\u7684\u6d88\u606f\u4e5f\u662f\u53d7memoryLimit\u548cmaxPendingMessages\u5236\u7ea6\u7684\uff0c\u5e94\u8be5\u907f\u514d\u51fa\u73b0batch\u4e2d\u6d88\u606f\u7684\u6570\u91cf\u8d85\u8fc7memoryLimit\u548cmaxPendingMessages\u5bfc\u81f4batch\u6253\u5305\u6548\u7387\u53d7\u5f71\u54cd\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5f53maxPendingMessages\u8bbe\u7f6e\u4e3a500\uff0c\u800cbatchingMaxMessages\u8bbe\u7f6e1000\u65f6\uff0cbatch\u5c31\u6c38\u8fdc\u65e0\u6cd5\u6ee1\u8db3\u6d88\u606f\u6761\u6570\u8fbe\u52301000\u7684\u6761\u4ef6\uff0c\u53ea\u80fd\u7a7a\u7b49batchingMaxPublishDelayMicros\u6216\u8005batchingMaxBytes\u4e24\u8005\u751f\u6548\u3002<\/p>\n<h4>3.2.3 \u516c\u5f0f\u5316\u6a21\u7248<\/h4>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u5206\u6790\uff0c\u5927\u81f4\u4e86\u89e3\u4e86\u5173\u952e\u53c2\u6570\u7684\u751f\u6548\u6548\u679c\uff0c\u4e14\u5f7c\u6b64\u76f8\u4e92\u5173\u8054\uff0c\u6839\u636e\u8fd9\u4e9b\u5173\u7cfb\u5c31\u80fd\u591f\u8f93\u51fa\u4e00\u4e2a\u8f83\u4e3a\u7b80\u5355\u7684\u53c2\u6570\u8c03\u4f18\u6a21\u7248\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u53d1\u9001\u7684\u5355\u6761\u6d88\u606f\u5927\u5c0f\u4e3a\uff1amessageByte\uff1b\u5206\u533a\u6570\u91cf\u4e3a\uff1apartitionNum\u3002<\/p>\n<p>\u90a3\u4e48\u5bf9\u5e94\u53c2\u6570\u8c03\u6574\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/\u4e1a\u52a1\u53d1\u9001\u901f\u7387\u8d8a\u5927\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u7684\u503c\u8d8a\u5927\nmaxPendingMessages\uff1a\u4e00\u822c1000-2000\u4e4b\u95f4\n \n\/\/\u8fd9\u91cc\u503c\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u5927\u4e00\u4e9b\uff0c\u8ba9maxPendingMessages\u751f\u6548\u5373\u53ef\nmaxPendingMessagesAcrossPartitions = maxPendingMessages * partitionNum\n \n\/\/memoryLimit\u7684\u503c\u5c31\u662f\u6253\u7b97\u963b\u585e\u603b\u6d88\u606f\u5927\u5c0f\uff0c\u8fd9\u4e0e\u6d88\u606f\u4f53\u548cmaxPendingMessages\u6709\u5173\nmemoryLimit=(maxPendingMessages * partitionNum * messageByte)\n \n\/\/batch\u7684\u6761\u6570\u4e0d\u8d85\u8fc7\u201c\u5f85\u5904\u7406\u6d88\u606f\u961f\u5217\u201d\u5927\u5c0f\u7684\u4e00\u534a\nbatchingMaxMessages=maxPendingMessages\/2\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u5728\u6d88\u606f\u53d1\u9001\u7b49\u5f85ack\u7684\u65f6\u5019\uff0c\u8be5\u5206\u533a\u5269\u4e0b\u4e00\u534a\u7684\u7a7a\u95f4\u8fd8\u80fd\u7528\u6765\u6784\u5efa\u4e00\u4e2abatch\n \n\/\/batch\u5927\u5c0f\u540c\u7406\uff0cbatch\u5927\u5c0f\u4e0d\u8d85\u8fc7\u201c\u5f85\u5904\u7406\u6d88\u606f\u961f\u5217\u201d\u6d88\u606f\u5927\u5c0f\u7684\u4e00\u534a\nbatchingMaxBytes= Math.min(memoryLimit * 1024 * 1024 \/partitionNum\/2,1048576)\n \n\/\/\u4e1a\u52a1\u80fd\u591f\u63a5\u53d7\u7684\u5ef6\u8fdf\u5927\u5c0f\uff0c\u4e00\u822c\u5ef6\u8fdf\u65f6\u95f4\u8d8a\u5927\uff0cbatch\u8d8a\u5927\nbatchingMaxPublishDelayMicros=1ms-100m\u7686\u53ef\n \n\/\/\u6bcf\u6784\u5efa\u4e00\u4e2abatch\u5c31\u8f6c\u6362\u4e00\u4e2a\u5206\u533a\nbatchingPartitionSwitchFrequencyByPublishDelay=1<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u6839\u636e\u4e0a\u9762\u7684\u5206\u6790\uff0c\u53c2\u6570\u4e4b\u95f4\u662f\u6709\u4e00\u4e2a\u6a21\u7248\u5316\u7684\u516c\u5f0f\uff0c\u4f46\u8fd9\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\uff0c\u8bfb\u8005\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u4e1a\u52a1\u573a\u666f\u8fdb\u884c\u8c03\u6574\u3002\u5728\u771f\u5b9e\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u7ebf\u4e0a\u7684\u6d88\u606f\u5927\u5c0f\u4ee5\u53ca\u5206\u533a\u6570\u91cf\u5b9e\u9645\u4e0a\u662f\u4f1a\u53d8\u5316\u7684\uff0c\u56e0\u6b64\u771f\u6b63\u7684\u53c2\u6570\u8bbe\u7f6e\u8fd8\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u6765\u786e\u5b9a\uff0c\u6bd4\u5982\u6211\u4eec\u7ebf\u4e0a\u901a\u5e38\u7684\u505a\u6cd5\u662f\u6839\u636e\u673a\u5668\u914d\u7f6e\u5c06memoryLimit\u76f4\u63a5\u8bbe\u7f6e\u4e3a64M-256M\uff0c\u5206\u533a\u6570\u91cf\u6211\u4eec\u7ebf\u4e0a\u4e0d\u4f1a\u8d85\u8fc71000\uff0c\u90a3\u4e48\u8fd9\u91cc\u5c31\u5047\u8bbe\u4e3a1000\uff0c\u786e\u5b9a\u4e86\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u5176\u4ed6\u7684\u53c2\u6570\u7684\u503c\u4e5f\u5c31\u786e\u5b9a\u4e86\u3002<\/p>\n<h4>3.2.4 \u6548\u679c\u5bf9\u6bd4<\/h4>\n<p>\u4ee5\u7ebf\u4e0a\u4e00\u4e2a\u4e1a\u52a1\u53c2\u6570\u8c03\u4f18\u4e3a\u4f8b\uff0c\u524d\u540e\u90fd\u5f00\u542f\u538b\u7f29\u7684\u60c5\u51b5\u4e0b\u8c03\u6574\u4e0a\u8ff0\u53c2\u6570\u540e\u7684\u4e00\u4e2a\u6548\u679c\u3002<\/p>\n<p>\u670d\u52a1\u7aef\uff08Pulsar\uff09\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611521\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611522\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p><strong>\u4f18\u5316\u524d\u540e\u5bf9\u6bd4\u6570\u636e\uff1a<\/strong><\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046611523\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u76f8\u540c\u7684\u5199\u5165\u901f\u7387\uff0cPulsar\u670d\u52a1\u7aef\u7f51\u5361\u6d41\u91cf\u7f29\u51cf\u7ea650%\uff08batch\u5305\u4f53\u589e\u52a0\uff0c\u538b\u7f29\u6548\u679c\u63d0\u5347\uff09\uff0ccpu\u8d1f\u8f7d\u964d\u4f4e\u7ea690%\uff0cPulsar\u670d\u52a1\u7aef\u603b\u4f53\u6210\u672c\u76f8\u8f83\u4f18\u5316\u524d\u81f3\u5c11\u53ef\u964d\u4f4e50%\u4ee5\u4e0a\uff0c\u5ba2\u6237\u7aef\u4e5f\u6709\u4e00\u5b9a\u7a0b\u5ea6\u7684\u8d1f\u8f7d\u964d\u4f4e\u3002<\/p>\n<p>\u53c2\u6570\u8c03\u6574\u540e\uff0cCPU\u8d1f\u8f7d\u5f97\u5230\u660e\u663e\u964d\u4f4e\uff0c\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u907f\u514d\u4e86CPU\u6210\u4e3a\u7cfb\u7edf\u7684\u74f6\u9888\uff0c\u540c\u65f6\u7531\u4e8e\u538b\u7f29\u6548\u679c\u7684\u63d0\u5347\uff0cPulsar \u7684\u78c1\u76d8IO\u8d1f\u8f7d\u5f97\u5230\u663e\u8457\u964d\u4f4e\uff0c\u53ef\u4ee5\u7528\u66f4\u5c11\u7684\u673a\u5668\u5904\u7406\u66f4\u591a\u7684\u6570\u636e\u3002<\/p>\n<h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<p>\u7406\u89e3Producer\u53d1\u9001\u539f\u7406\u4ee5\u53ca\u6838\u5fc3\u53c2\u6570\u662f\u5199\u597d\u6570\u636e\u53d1\u9001\u7a0b\u5e8f\u6700\u4e3a\u6709\u6548\u7684\u624b\u6bb5\uff0c\u6700\u7b80\u5355\u7684\u5ba2\u6237\u7aef\u53c2\u6570\u4f18\u5316\u53cd\u800c\u9690\u85cf\u4e86\u5de8\u5927\u7684\u6536\u76ca\u3002\u672c\u6587\u901a\u8fc7\u5bf9Producer\u539f\u7406\u8fdb\u884c\u5256\u6790\u3001\u5bf9\u6d88\u606f\u7684\u6d41\u8f6c\u8fc7\u7a0b\u4e2d\u53c2\u6570\u6548\u7528\u8fdb\u884c\u8bb2\u89e3\uff0c\u5e76\u914d\u5408\u53c2\u6570\u8c03\u4f18\u5b9e\u8df5\u6848\u4f8b\uff0c\u4ecb\u7ecd\u4e86\u5177\u4f53\u7684\u5206\u6790\u601d\u8def\u548c\u8c03\u4f18\u7684\u65b9\u6cd5\uff0c\u5728\u5b9e\u9645\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u5bf9\u6838\u5fc3\u7684\u51e0\u4e2a\u4e0a\u6e38\u7cfb\u7edf\u8fdb\u884c\u8c03\u4f18\uff0c\u670d\u52a1\u7aef\u5355\u673a\u5904\u7406\u80fd\u529b\u81f3\u5c11\u63d0\u5347\u4e86\u4e00\u500d\u4ee5\u4e0a\uff0c\u6210\u672c\u5f97\u5230\u4e86\u6781\u5927\u7684\u964d\u4f4e\u3002<\/p>\n<blockquote>\n<p>\u53c2\u8003\u6587\u7ae0\uff1a<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=E7GoMVdN5Hz%2B5NQbkG7E6Q%3D%3D.vjkKc5iyqE9enbs4%2F%2FHqeOrDvBJahvetH16o7gxOyBj3QWFxa3R%2BRCKKsrcwqJHMKdy4MxwoK4U%2FdV9eM2EcMQ%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/pulsar.apache.org\/docs\/4.0.x\/concepts-overview\/<\/a><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046611510\">https:\/\/segmentfault.com\/a\/1190000046611510<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1avivo \u4e92\u8054\u7f51\u5927\u6570\u636e\u56e2\u961f- Quan Limin \u672c\u6587\u662fvivo\u4e92\u8054\u7f51\u5927\u6570\u636e\u56e2\u961f\u300avivo Pulsa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020813","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020813"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020813\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}