{"id":10020896,"date":"2025-07-10T14:09:42","date_gmt":"2025-07-10T06:09:42","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020896"},"modified":"2025-07-10T14:09:42","modified_gmt":"2025-07-10T06:09:42","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%883%ef%bc%89-kop%e6%8c%87%e6%a0%87%e5%bc%82%e5%b8%b8%e4%bf%ae%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020896","title":{"rendered":"vivo Pulsar \u4e07\u4ebf\u7ea7\u6d88\u606f\u5904\u7406\u5b9e\u8df5\uff083\uff09-KoP\u6307\u6807\u5f02\u5e38\u4fee\u590d"},"content":{"rendered":"<blockquote>\n<p>\u4f5c\u8005\uff1avivo \u4e92\u8054\u7f51\u5927\u6570\u636e\u56e2\u961f- Chen Jianbo<\/p>\n<p>\u672c\u6587\u662f\u300avivo Pulsar\u4e07\u4ebf\u7ea7\u6d88\u606f\u5904\u7406\u5b9e\u8df5\u300b\u7cfb\u5217\u6587\u7ae0\u7b2c3\u7bc7\u3002<\/p>\n<p>Pulsar\u662fApache\u57fa\u91d1\u4f1a\u7684\u5f00\u6e90\u5206\u5e03\u5f0f\u6d41\u5904\u7406\u5e73\u53f0\u548c\u6d88\u606f\u4e2d\u95f4\u4ef6\uff0c\u5b83\u5b9e\u73b0\u4e86Kafka\u7684\u534f\u8bae\uff0c\u53ef\u4ee5\u8ba9\u4f7f\u7528Kafka API\u7684\u5e94\u7528\u76f4\u63a5\u8fc1\u79fb\u81f3Pulsar\uff0c\u8fd9\u4f7f\u5f97Pulsar\u5728Kafka\u751f\u6001\u7cfb\u7edf\u4e2d\u66f4\u52a0\u5bb9\u6613\u88ab\u63a5\u53d7\u548c\u4f7f\u7528\u3002KoP\u63d0\u4f9b\u4e86\u4eceKafka\u5230Pulsar\u7684\u65e0\u7f1d\u8f6c\u6362\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528Kafka API\u64cd\u4f5cPulsar\u96c6\u7fa4\uff0c\u4fdd\u7559\u4e86Kafka\u7684\u5e7f\u6cdb\u7528\u6237\u57fa\u7840\u548c\u4e30\u5bcc\u751f\u6001\u7cfb\u7edf\u3002\u5b83\u4f7f\u5f97Pulsar\u53ef\u4ee5\u66f4\u597d\u5730\u4e0eKafka\u8fdb\u884c\u6574\u5408\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u6d88\u606f\u4f20\u8f93\u6027\u80fd\u3001\u66f4\u5f3a\u7684\u517c\u5bb9\u6027\u53ca\u53ef\u6269\u5c55\u6027\u3002vivo\u5728\u4f7f\u7528Pulsar KoP\u7684\u8fc7\u7a0b\u4e2d\u9047\u5230\u8fc7\u4e00\u4e9b\u95ee\u9898\uff0c\u672c\u7bc7\u4e3b\u8981\u5206\u4eab\u4e00\u4e2a\u5206\u533a\u6d88\u8d39\u6307\u6807\u7f3a\u5931\u7684\u95ee\u9898\u3002<\/p>\n<\/blockquote>\n<p>\u7cfb\u5217\u6587\u7ae0\uff1a<\/p>\n<ol>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=r7bLfjbUkKpAGkGbKSQDiQ%3D%3D.K6ztDGBFUzdsBsKrkjYUgqtx6w8SMCSrZlQY3Klzy%2BS5xWjTQNDG9V0B6cXyCoc%2FmefTjPiuAWVctnsH0cycYV9lWiQJz8Qb3ipBZwlKaGl8h9m0Wvma%2Bxr4ZLQ9esnZHDAe8S8z5cFGOGth0DArgqF4btQnIG1HEyHtxN3ihk3DWmVgHlUkEnDQlOt5lunH\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">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<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=C88klIcEy27hPlCgkC3PNw%3D%3D.gU%2FAJOjGOkP1wTtVaRSTUTqeGdgB2y%2Btsf1su9XhaAM6f5QUMINO4xMMFtv1UTlByUaDeXQy%2FOlsUgfnPVJgdQeIFLL3ePsCHdT%2BwLN9C7aKo%2BLfFXVr%2BoVBb6EArUkeSxpYmUXOWlHGRF%2FfR4BWw6OJmSsiklC3%2FNzaBBMUHRV4Bv3dw5rnK6BRg%2FzPBMRv\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">vivo Pulsar\u4e07\u4ebf\u7ea7\u6d88\u606f\u5904\u7406\u5b9e\u8df5\uff082\uff09-\u4ece0\u52301\u5efa\u8bbePulsar\u6307\u6807\u76d1\u63a7\u94fe\u8def<\/a><\/li>\n<\/ol>\n<p>\u6587\u7ae0\u592a\u957f\uff1f1\u5206\u949f\u770b\u56fe\u6293\u4f4f\u6838\u5fc3\u89c2\u70b9\ud83d\udc47<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046854185\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\"><\/p>\n<h2>\u4e00\u3001\u95ee\u9898\u80cc\u666f<\/h2>\n<p>\u5728\u4e00\u6b21\u7248\u672c\u7070\u5ea6\u5347\u7ea7\u4e2d\uff0c\u6211\u4eec\u53d1\u73b0\u67d0\u4e2a\u4f7f\u7528KoP\u7684\u4e1a\u52a1topic\u7684\u6d88\u8d39\u901f\u7387\u51fa\u73b0\u4e86\u663e\u8457\u4e0b\u964d\uff0c\u5177\u4f53\u60c5\u51b5\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046854186\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u6b63\u5e38\u7684\u5347\u7ea7\u91cd\u542f\u670d\u52a1\u5668\u4f1a\u51fa\u73b0\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u76f4\u63a5\u67e5\u770b\u4e0a\u62a5\u91c7\u96c6\u7684\u6570\u636e\u62a5\u6587\uff1a<\/p>\n<pre><code>kop_server_MESSAGE_OUT{group=&quot;&quot;,partition=&quot;0&quot;,tenant=&quot;kop&quot;,topic=&quot;persistent:\/\/kop-tenant\/kop-ns\/service-raw-stats&quot;} 3\nkop_server_BYTES_OUT{group=&quot;&quot;,partition=&quot;0&quot;,tenant=&quot;kop&quot;,topic=&quot;persistent:\/\/kop-tenant\/kop-ns\/service-raw-stats&quot;} 188\n<\/code><\/pre>\n<p>\u6211\u4eec\u770b\u5230\uff0cKoP\u6d88\u8d39\u6307\u6807kop\/_server\/_MESSAGE<\/p>\n<p>\/_OUT\u3001kop\/_server\/_BYTES\/_OUT\u662f\u6709\u4e0a\u62a5\u7684\uff0c\u4f46\u6307\u6807\u6570\u636e\u91cc\u7684group\u6807\u7b7e\u53d8\u6210\u4e86\u7a7a\u4e32\uff08\u7f3a\u5c11\u6d88\u8d39\u7ec4\u540d\u79f0\uff09\uff0c\u5206\u533a\u7684\u6d88\u8d39\u6307\u6807\u5c31\u65e0\u6cd5\u5c55\u793a\u4e86\u3002\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u4e86\u6d88\u8d39\u7ec4\u540d\u79f0\u7f3a\u5931\uff1f<\/p>\n<h2>\u4e8c\u3001\u95ee\u9898\u5206\u6790<\/h2>\n<p><strong>1\u3001\u627e\u5230\u95ee\u9898\u4ee3\u7801<\/strong><\/p>\n<p>\u6211\u4eec\u53bb\u627e\u4e0b\u8fd9\u4e2a\u6d88\u8d39\u7ec4\u540d\u79f0\u662f\u5728\u54ea\u91cc\u83b7\u53d6\u7684\uff0c\u662f\u5426\u903b\u8f91\u5b58\u5728\u4ec0\u4e48\u95ee\u9898\u3002\u6839\u636edruid\u4e2d\u7684kop\/_subscription\u5bf9\u5e94\u7684\u6d88\u8d39\u6307\u6807kop\/_server_<\/p>\n<p>MESSAGE\/_OUT\u3001kop\/_server\/_BYTES\/_OUT\uff0c\u627e\u5230\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>private void handleEntries(final List&lt;Entry&gt; entries,\n                               final TopicPartition topicPartition,\n                               final FetchRequest.PartitionData partitionData,\n                               final KafkaTopicConsumerManager tcm,\n                               final ManagedCursor cursor,\n                               final AtomicLong cursorOffset,\n                               final boolean readCommitted) {\n....\n        \/\/ \u5904\u7406\u6d88\u8d39\u6570\u636e\u65f6\uff0c\u83b7\u53d6\u6d88\u8d39\u7ec4\u540d\u79f0\n        CompletableFuture&lt;String&gt; groupNameFuture = requestHandler\n                .getCurrentConnectedGroup()\n                .computeIfAbsent(clientHost, clientHost -&gt; {\n                    CompletableFuture&lt;String&gt; future = new CompletableFuture&lt;&gt;();\n                    String groupIdPath = GroupIdUtils.groupIdPathFormat(clientHost, header.clientId());\n                    requestHandler.getMetadataStore()\n                            .get(requestHandler.getGroupIdStoredPath() + groupIdPath)\n                            .thenAccept(getResultOpt -&gt; {\n                                if (getResultOpt.isPresent()) {\n                                    GetResult getResult = getResultOpt.get();\n                                    future.complete(new String(getResult.getValue() == null\n                                            ? new byte[0] : getResult.getValue(), StandardCharsets.UTF_8));\n                                } else {\n                                    \/\/ \u4ecezk\u8282\u70b9 \/client_group_id\/xxx \u83b7\u53d6\u4e0d\u5230\u6d88\u8d39\u7ec4\uff0c\u6d88\u8d39\u7ec4\u5c31\u662f\u7a7a\u7684\n                                    future.complete(&quot;&quot;);\n                                }\n                            }).exceptionally(ex -&gt; {\n                                future.completeExceptionally(ex);\n                                return null;\n                            });\n                    returnfuture;\n                });\n\n        \/\/ this part is heavyweight, and we should not execute in the ManagedLedger Ordered executor thread\n        groupNameFuture.whenCompleteAsync((groupName, ex) -&gt; {\n            if (ex != null) {\n                log.error(&quot;Get groupId failed.&quot;, ex);\n                groupName = &quot;&quot;;\n            }\n.....\n            \/\/ \u83b7\u5f97\u6d88\u8d39\u7ec4\u540d\u79f0\u540e\uff0c\u8bb0\u5f55\u6d88\u8d39\u7ec4\u5bf9\u5e94\u7684\u6d88\u8d39\u6307\u6807\n            decodeResult.updateConsumerStats(topicPartition,\n                    entries.size(),\n                    groupName,\n                    statsLogger);\n<\/code><\/pre>\n<p>\u4ee3\u7801\u7684\u903b\u8f91\u662f\uff0c\u4ecerequestHandler\u7684currentConnectedGroup(map)\u4e2d\u901a\u8fc7host\u83b7\u53d6groupName\uff0c\u4e0d\u5b58\u5728\u5219\u901a\u8fc7MetadataStore\uff08\u5e26\u7f13\u5b58\u7684zk\u5b58\u50a8\u5bf9\u8c61\uff09\u83b7\u53d6\uff0c\u5982\u679czk\u7f13\u5b58\u4e5f\u6ca1\u6709\uff0c\u518d\u53d1\u8d77zk\u8bfb\u8bf7\u6c42\uff08\u8def\u5f84\u4e3a\/client\/_group\/_id\/host-clientId\uff09\u3002\u8bfb\u53d6\u5230\u6d88\u8d39\u7ec4\u540d\u79f0\u540e\uff0c\u7528\u5b83\u6765\u66f4\u65b0\u6d88\u8d39\u7ec4\u6307\u6807\u3002\u4ece\u590d\u73b0\u7684\u96c6\u7fa4\u786e\u5b9a\u8d70\u7684\u662f\u8fd9\u4e2a\u5206\u652f\uff0c\u5373\u662f\u4ecemetadataStore(\u5e26\u7f13\u5b58\u7684zk\u5ba2\u6237\u7aef)\u83b7\u53d6\u4e0d\u5230\u5bf9\u5e94zk\u8282\u70b9\/client\/_group\/_id\/xxx\u3002<\/p>\n<p><strong>2\u3001\u67e5\u627e\u53ef\u80fd\u5bfc\u81f4zk\u8282\u70b9\/client\/_group\/_id\/xxx\u8282\u70b9\u83b7\u53d6\u4e0d\u5230\u7684\u539f\u56e0<\/strong><\/p>\n<p>\u6709\u4e24\u79cd\u53ef\u80fd\u6027\uff1a\u4e00\u662f\u6ca1\u5199\u8fdb\u53bb\uff0c\u4e8c\u662f\u5199\u8fdb\u53bb\u4f46\u662f\u88ab\u5220\u9664\u4e86\u3002<\/p>\n<pre><code>    @Override\n    protected void handleFindCoordinatorRequest(KafkaHeaderAndRequest findCoordinator,\n                                                CompletableFuture&lt;AbstractResponse&gt; resultFuture) {\n...\n        \/\/ Store group name to metadata store for current client, use to collect consumer metrics.\n        storeGroupId(groupId, groupIdPath)\n                .whenComplete((stat, ex) -&gt; {\n                    if (ex != null) {\n                        \/\/ \/client_group_id\/xxx\u8282\u70b9\u5199\u5165\u5931\u8d25\n                        log.warn(&quot;Store groupId failed, the groupId might already stored.&quot;, ex);\n                    }\n                    findBroker(TopicName.get(pulsarTopicName))\n                            .whenComplete((node, throwable) -&gt; {\n                                ....\n                            });\n                });\n...\n<\/code><\/pre>\n<p>\u4ece\u4ee3\u7801\u770b\u5230\uff0cclientId\u4e0egroupId\u7684\u5173\u8054\u5173\u7cfb\u662f\u901a\u8fc7handleFindCoordinatorRequest\uff08FindCoordinator\uff09\u5199\u8fdb\u53bb\u7684\uff0c\u800c\u4e14\u53ea\u6709\u8fd9\u4e2a\u65b9\u6cd5\u5165\u53e3\u3002\u7531\u4e8e\u6ca1\u6709\u627e\u5230warn\u65e5\u5fd7\uff0c\u6392\u9664\u4e86\u7b2c\u4e00\u79cd\u6ca1\u5199\u8fdb\u53bb\u7684\u53ef\u80fd\u6027\u3002\u770b\u770b\u5220\u9664\u7684\u903b\u8f91\uff1a<\/p>\n<pre><code>protected void close(){\n    if (isActive.getAndSet(false)) {\n        ...\n        currentConnectedClientId.forEach(clientId -&gt; {\n            String path = groupIdStoredPath + GroupIdUtils.groupIdPathFormat(clientHost, clientId);\n            \/\/ \u5220\u9664zk\u4e0a\u7684 \/client_group_id\/xxx \u8282\u70b9\n            metadataStore.delete(path, Optional.empty())\n                    .whenComplete((__, ex) -&gt; {\n                        if (ex != null) {\n                            if (ex.getCause() instanceof MetadataStoreException.NotFoundException) {\n                                if (log.isDebugEnabled()) {\n                                    log.debug(&quot;The groupId store path doesn't exist. Path: [{}]&quot;, path);\n                                }\n                                return;\n                            }\n                            log.error(&quot;Delete groupId failed. Path: [{}]&quot;, path, ex);\n                            return;\n                        }\n                        if (log.isDebugEnabled()) {\n                            log.debug(&quot;Delete groupId success. Path: [{}]&quot;, path);\n                        }\n                    });\n        });\n    }\n}\n<\/code><\/pre>\n<p>\u5220\u9664\u662f\u5728requsetHandler.close\u65b9\u6cd5\u4e2d\u6267\u884c\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fde\u63a5\u65ad\u5f00\u5c31\u4f1a\u89e6\u53d1zk\u8282\u70b9\u5220\u9664\u3002<\/p>\n<p>\u4f46\u6709\u51e0\u4e2a<strong>\u7591\u95ee\uff1a<\/strong><\/p>\n<ul>\n<li>\/client\/_group\/_id\/xxx \u5230\u5e95\u662f\u5e72\u561b\u7528\u7684\uff1f\u6d88\u8d39\u6307\u6807\u4e3a\u4ec0\u4e48\u8981\u4f9d\u8d56\u5b83<\/li>\n<li>\u4e3a\u4ec0\u4e48\u8981\u5728handleFindCoordinatorRequest\u5199\u5165\uff1f<\/li>\n<li>\u8282\u70b9\/client\/_group\/_id\/xxx\u4e3a\u4ec0\u4e48\u8981\u5220\u9664\uff0c\u800c\u4e14\u662f\u5728\u8fde\u63a5\u65ad\u5f00\u65f6\u5220\u9664\uff0c\u5220\u9664\u65f6\u673a\u662f\u5426\u6709\u95ee\u9898\uff1f<\/li>\n<\/ul>\n<p>\u9996\u5148\u56de\u7b54\u7b2c1\u4e2a\u95ee\u9898\uff0c\u901a\u8fc7\u9605\u8bfb\u4ee3\u7801\u53ef\u4ee5\u77e5\u9053\uff0c\/client\/_group\/_id\/xxx \u8fd9\u4e2azk\u8282\u70b9\u662f\u7528\u4e8e\u5728\u4e0d\u540cbroker\u5b9e\u4f8b\u95f4\u4ea4\u6362\u6570\u636e\u7528\u7684(\u76f8\u5f53redis cache)\uff0c\u7528\u4e8e\u4e34\u65f6\u5b58\u653eIP+clientId\u4e0egroupId\u7684\u6620\u5c04\u5173\u7cfb\u3002\u7531\u4e8efetch\u63a5\u53e3\uff08\u62c9\u53d6\u6570\u636e\uff09\u7684request\u6ca1\u6709groupId\u7684\uff0c\u53ea\u80fd\u4f9d\u8d56\u52a0\u5165Group\u8fc7\u7a0b\u4e2d\u7684\u5143\u6570\u636e\uff0c\u5728fetch\u6d88\u8d39\u65f6\u624d\u80fd\u77e5\u9053\u5f53\u524d\u62c9\u6570\u636e\u7684consumer\u662f\u54ea\u4e2a\u6d88\u8d39\u7ec4\u7684\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046854187\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>3\u3001\u590d\u73b0<\/strong><\/p>\n<p>\u82e5\u8981\u89e3\u51b3\u95ee\u9898\uff0c\u6700\u597d\u80fd\u591f\u7a33\u5b9a\u5730\u590d\u73b0\u51fa\u95ee\u9898\uff0c\u8fd9\u6837\u624d\u80fd\u786e\u5b9a\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\uff0c\u5e76\u4e14\u786e\u8ba4\u4fee\u590d\u662f\u5426\u5b8c\u6210\u3002<\/p>\n<p>\u56e0\u4e3a\u8282\u70b9\u662f\u5728requsetHandle.close\u65b9\u6cd5\u4e2d\u6267\u884c\u5220\u9664\uff0cbroker\u8282\u70b9\u5173\u95ed\u4f1a\u89e6\u53d1\u8fde\u63a5\u5173\u95ed\uff0c\u8fdb\u800c\u89e6\u53d1\u5220\u9664\u3002\u5047\u8bbe\uff1a\u5ba2\u6237\u7aef\u901a\u8fc7brokerA\u53d1\u8d77FindCoordinator\u8bf7\u6c42\uff0c\u5199\u5165zk\u8282\u70b9\/client_group<\/p>\n<p>\/_id\/xxx\uff0c\u540c\u65f6\u8bf7\u6c42\u8fd4\u56debrokerB\u4f5c\u4e3aCoordinator\uff0c\u540e\u7eed\u4e0ebrokerB\u8fdb\u884cjoinGroup\u3001syncGroup\u7b49\u4ea4\u4e92\u786e\u5b9a\u6d88\u8d39\u5173\u7cfb\uff0c\u5ba2\u6237\u7aef\u5728brokerA\u3001brokerB\u3001brokerC\u90fd\u6709\u5206\u533a\u6d88\u8d39\u3002\u8fd9\u65f6\u91cd\u542fbrokerA\uff0c\u5206\u533a\u5747\u8861\u5230BrokerC\u4e0a\uff0c\u4f46\u6b64\u65f6\/client\/_group_id\/xxx\u56e0\u5173\u95edbroker\u800c\u65ad\u5f00\u8fde\u63a5\u88ab\u5220\u9664\uff0cconsumer\u6d88\u8d39\u521a\u8f6c\u79fb\u5230topic1-partition-1\u7684\u5206\u533a\u5c31\u65e0\u6cd5\u83b7\u53d6\u5230groupId\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046854188\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6309\u7167\u5047\u8bbe\uff0c\u67093\u4e2abroker\uff0c\u5f00\u542f\u751f\u4ea7\u548c\u6d88\u8d39\uff0c\u901a\u8fc7\u5728FindCoordinator\u8fd4\u56de\u524d\u83b7\u53d6node.leader()\u7684\u8fd4\u56de\u8282\u70b9BrokerB\uff0c\u5173\u95edbrokerA\u540e\uff0cbrokerC\u51fa\u73b0\u65ad\u70b9\u590d\u73b0\uff0c\u518d\u5173\u95edbrokerC\uff0cbrokerA\u4e5f\u4f1a\u590d\u73b0\uff08\u5047\u8bbe\u5206\u533a\u5728brokerA\u4e0ebrokerC\u4e4b\u95f4\u8f6c\u79fb\uff09\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046854189\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u590d\u73b0\u8981\u51e0\u4e2a\u6761\u4ef6\uff1a<\/p>\n<ol>\n<li>broker\u6570\u91cf\u8981\u8db3\u591f\u591a(\u4e0d\u5c0f\u4e8e3\u4e2a\uff09<\/li>\n<li>broker\u5185\u90e8\u6709zk\u7f13\u5b58metadataCache\u9ed8\u8ba4\u4e3a5\u5206\u949f\uff0c\u53ef\u4ee5\u628a\u65f6\u95f4\u8c03\u5c0f\u4e3a1\u6beb\u79d2\uff0c\u76f8\u5f53\u4e8e\u6ca1\u6709cache<\/li>\n<li>findCoordinator\u8fd4\u56de\u7684\u5fc5\u987b\u662f\u5176\u4ed6broker\u7684IP<\/li>\n<li>\u91cd\u542f\u7684\u5fc5\u987b\u662f\u63a5\u6536\u5230findCoordinator\u8bf7\u6c42\u90a3\u53f0broker\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u7684coordinator\uff0c\u8fd9\u65f6\u4f1a\u4ecezk\u5220\u9664\u8282\u70b9<\/li>\n<li>\u5206\u533a\u8f6c\u79fb\u5230\u5176\u4ed6broker\uff0c\u8fd9\u65f6\u65b0\u7684broker\u4f1a\u91cd\u65b0\u8bfb\u53d6zk\u8282\u70b9\u6570\u636e<\/li>\n<\/ol>\n<p>\u5230\u6b64\uff0c\u6211\u4eec\u57fa\u672c\u4e0a\u6e05\u695a\u4e86\u95ee\u9898\u539f\u56e0\uff1a\u8fde\u63a5\u5173\u95ed\u5bfc\u81f4zk\u8282\u70b9\u88ab\u5220\u9664\u4e86\uff0c\u522b\u7684broker\u8282\u70b9\u9700\u8981\u65f6\u5c31\u8bfb\u53d6\u4e0d\u5230\u4e86\u3002\u90a3\u600e\u4e48\u89e3\u51b3\uff1f<\/p>\n<h2>\u4e09\u3001\u95ee\u9898\u89e3\u51b3<\/h2>\n<p><strong>\u65b9\u6848\u4e00<\/strong><\/p>\n<p>\u65e2\u7136\u77e5\u9053\u628a\u6d88\u8d39\u8005\u4e0eFindCoordinator\u7684\u8fde\u63a5\u8fdb\u884c\u7ed1\u5b9a\u4e0d\u5408\u9002\u7684\uff0c\u90a3\u4e48\u662f\u5426\u5e94\u8be5\u628aFindCoordinator\u5199\u5165zk\u8282\u70b9\u6362\u6210\u7531JoinGroup\u5199\u5165\uff0c\u65ad\u8fde\u5373\u5220\u9664\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046854190\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>consumer\u7edf\u4e00\u7531Coordinator\u7ba1\u7406\uff0c\u7531\u4e8eFindCoordinator\u63a5\u53e3\u4e0d\u4e00\u5b9a\u662fCoordinator\u5904\u7406\u7684\uff0c\u5982\u679c\u6362\u6210\u7531Coordinator\u5904\u7406\u7684JoinGroup\u63a5\u53e3\u662f\u5426\u5c31\u53ef\u4ee5\u4e86\uff0c\u8fd9\u6837consumer\u65ad\u5f00\u4e0eCoordinator\u7684\u8fde\u63a5\u5c31\u5e94\u8be5\u5220\u9664\u6570\u636e\u3002\u4f46\u5b9e\u73b0\u9a8c\u8bc1\u65f6\u5374\u53d1\u73b0\uff0c\u5ba2\u6237\u7aef\u5728\u65ad\u8fde\u540e\u4e5f\u4e0d\u4f1a\u518d\u91cd\u8fde\uff0c\u6240\u4ee5\u6ca1\u6cd5\u91cd\u65b0\u5199\u5165zk\uff0c\u4e0d\u7b26\u5408\u9884\u671f\u3002<\/p>\n<p><strong>\u65b9\u6848\u4e8c<\/strong><\/p>\n<p>\u8fd8\u662f\u7531FindCoordinator\u5199\u5165zk\u8282\u70b9\uff0c\u4f46\u5220\u9664\u6539\u4e3aGroupCoordinator\u76d1\u542cconsumer\u65ad\u5f00\u89e6\u53d1\u3002<\/p>\n<p>\u56e0\u4e3aconsumer\u7edf\u4e00\u7531Coordinator\u7ba1\u7406\uff0c\u5b83\u80fd\u76d1\u542c\u5230consumer\u52a0\u5165\u6216\u8005\u79bb\u5f00\u3002GroupCoordinator\u7684removeMemberAndUpdateGroup\u65b9\u6cd5\u662fcoordinator\u5bf9consumer\u6210\u5458\u7ba1\u7406\u3002<\/p>\n<pre><code>private void removeMemberAndUpdateGroup(GroupMetadata group,\n                                        MemberMetadata member) {\n    group.remove(member.memberId());\n    switch (group.currentState()) {\n        case Dead:\n        case Empty:\n            return;\n        case Stable:\n        case CompletingRebalance:\n            maybePrepareRebalance(group);\n            break;\n        case PreparingRebalance:\n            joinPurgatory.checkAndComplete(new GroupKey(group.groupId()));\n            break;\n        default:\n            break;\n    }\n    \/\/ \u5220\u9664 \/client_group_id\/xxx \u8282\u70b9\n    deleteClientIdGroupMapping(group, member.clientHost(), member.clientId());\n}\n<\/code><\/pre>\n<p>\u8c03\u7528\u5165\u53e3\u6709\u4e24\u4e2a\uff0c\u5176\u4e2dhandleLeaveGroup\u662f\u4e3b\u52a8\u79bb\u5f00\uff0conExpireHeartbeat\u662f\u8d85\u65f6\u88ab\u52a8\u79bb\u5f00\uff0c\u5ba2\u6237\u7aef\u6b63\u5e38\u9000\u51fa\u6216\u8005\u5b95\u673a\u90fd\u53ef\u4ee5\u8c03\u7528removeMemberAndUpdateGroup\u65b9\u6cd5\u89e6\u53d1\u5220\u9664\u3002<\/p>\n<pre><code>public CompletableFuture&lt;Errors&gt; handleLeaveGroup(\n    String groupId,\n    String memberId\n) {\n    return validateGroupStatus(groupId, ApiKeys.LEAVE_GROUP).map(error -&gt;\n        CompletableFuture.completedFuture(error)\n    ).orElseGet(() -&gt; {\n        return groupManager.getGroup(groupId).map(group -&gt; {\n            return group.inLock(() -&gt; {\n                if (group.is(Dead) || !group.has(memberId)) {\n                    return CompletableFuture.completedFuture(Errors.UNKNOWN_MEMBER_ID);\n                } else {\n                    ...\n                \n                    \/\/ \u89e6\u53d1\u5220\u9664\u6d88\u8d39\u8005consumer\n                    removeMemberAndUpdateGroup(group, member);\n                    return CompletableFuture.completedFuture(Errors.NONE);\n                }\n            });\n        })\n        ....\n    });\n}\n\nvoid onExpireHeartbeat(GroupMetadata group,\n                       MemberMetadata member,\n                       long heartbeatDeadline) {\n    group.inLock(() -&gt; {\n        if (!shouldKeepMemberAlive(member, heartbeatDeadline)) {\n            log.info(&quot;Member {} in group {} has failed, removing it from the group&quot;,\n                member.memberId(), group.groupId());\n            \/\/ \u89e6\u53d1\u5220\u9664\u6d88\u8d39\u8005consumer\n            removeMemberAndUpdateGroup(group, member);\n        }\n        return null;\n    });\n}\n<\/code><\/pre>\n<p>\u4f46\u8fd9\u4e2a\u65b9\u6848\u6709\u4e2a\u95ee\u9898\u662f\uff0c\u65e5\u5fd7\u8fd0\u7ef4\u5173\u95edbroker\u4e5f\u4f1a\u89e6\u53d1\u4e00\u4e2aonExpireHeartbeat\u4e8b\u4ef6\u5220\u9664zk\u8282\u70b9\uff0c\u4e0e\u6b64\u540c\u65f6\u5ba2\u6237\u7aef\u53d1\u73b0Coordinator\u65ad\u5f00\u4e86\u4f1a\u9a6c\u4e0a\u89e6\u53d1FindCoordinator\u5199\u5165\u65b0\u7684zk\u8282\u70b9\uff0c\u4f46\u5982\u679c\u5220\u9664\u665a\u4e8e\u5199\u5165\u7684\u8bdd\uff0c\u4f1a\u5bfc\u81f4\u8bef\u5220\u9664\u65b0\u5199\u5165\u7684\u8282\u70b9\u3002\u6211\u4eec\u5e72\u8106\u5728\u5173\u95edbroker\u65f6\uff0c\u4f7f\u7528ShutdownHook\u52a0\u4e0ashuttingdown\u72b6\u6001\u9632\u6b62\u5173\u95edbroker\u65f6\u5220\u9664zk\u8282\u70b9\uff0c\u53ea\u6709\u5ba2\u6237\u7aef\u65ad\u5f00\u65f6\u624d\u5220\u9664\u3002<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6848\u4fee\u6539\u4e0a\u7ebf\u534a\u4e2a\u6708\u540e\uff0c\u8fd8\u662f\u51fa\u73b0\u4e86\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u6d88\u8d39\u6307\u6807\u65e0\u6cd5\u4e0a\u62a5\u7684\u60c5\u51b5\u3002\u540e\u6765\u5b9a\u4f4d\u53d1\u73b0\uff0c\u5982\u679c\u5ba2\u6237\u7aef\u56e0FullGC\u51fa\u73b0\u5361\u987f\u60c5\u51b5\uff0c\u5ba2\u6237\u7aef\u53ef\u80fd\u4f1a\u5148\u4e8ebroker\u89e6\u53d1\u8d85\u65f6\uff0c\u4e5f\u5c31\u662f\u5148\u8d85\u65f6\u7684\u5ba2\u6237\u7aef\u65b0\u5199\u5165\u7684\u6570\u636e\u88ab\u540e\u76d1\u542c\u5230\u8d85\u65f6\u7684broker\u8bef\u5220\u9664\u4e86\u3002\u56e0\u4e3a\u5199\u5165\u4e0e\u5220\u9664\u5e76\u4e0d\u662f\u7531\u540c\u4e00\u4e2a\u8282\u70b9\u5904\u7406\uff0c\u6240\u4ee5\u65e0\u6cd5\u5728\u8fdb\u7a0b\u7ea7\u522b\u505a\u5e76\u53d1\u63a7\u5236\uff0c\u800c\u4e14\u4e5f\u65e0\u6cd5\u5224\u65ad\u54ea\u6b21\u5220\u9664\u5bf9\u5e94\u54ea\u6b21\u7684\u5199\u5165\uff0c\u6240\u4ee5\u7528zk\u4e5f\u662f\u5f88\u96be\u5b9e\u73b0\u5e76\u53d1\u63a7\u5236\u3002<\/p>\n<p><strong>\u65b9\u6848\u4e09<\/strong><\/p>\n<p>\u5176\u5b9e\u8fd9\u5e76\u4e0d\u662f\u65b0\u7684\u65b9\u6848\uff0c\u53ea\u662f\u5728\u65b9\u6848\u4e8c\u57fa\u7840\u4e0a\u4f18\u5316\uff1a\u6570\u636e\u4e00\u81f4\u6027\u68c0\u67e5\u3002<\/p>\n<p>\u65e2\u7136\u6211\u4eec\u5f88\u96be\u63a7\u5236\u597d\u5199\u5165\u4e0e\u5220\u9664\u7684\u5148\u540e\u987a\u5e8f\uff0c\u6211\u4eec\u53ef\u4ee5\u505a\u6570\u636e\u4e00\u81f4\u6027\u68c0\u67e5\uff0c\u7c7b\u4f3c\u4e8e\u4ea4\u6613\u7cfb\u7edf\u91cc\u7684\u5bf9\u8d26\u3002\u56e0\u4e3aGroupCoordinator\u662f\u8d1f\u8d23\u7ba1\u7406consumer\u6210\u5458\u7684\uff0c\u7ef4\u62a4\u7740consumer\u7684\u5b9e\u65f6\u72b6\u6001\uff0c\u5c31\u7b97zk\u8282\u70b9\u88ab\u8bef\u5220\u9664\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4ececonsumer\u6210\u5458\u4fe1\u606f\u4e2d\u6062\u590d\uff0c\u91cd\u65b0\u5199\u5165zk\u8282\u70b9\u3002<\/p>\n<pre><code>private void checkZkGroupMapping(){  \n    for (GroupMetadata group : groupManager.currentGroups()) {  \n        for (MemberMetadata memberMetadata : group.allMemberMetadata()) {  \n            String clientPath = GroupIdUtils.groupIdPathFormat(memberMetadata.clientHost(), memberMetadata.clientId());  \n            String zkGroupClientPath = kafkaConfig.getGroupIdZooKeeperPath() + clientPath;  \n            \/\/ \u67e5\u627ezk\u4e2d\u662f\u5426\u5b58\u5728\u8282\u70b9\n            metadataStore.get(zkGroupClientPath).thenAccept(resultOpt -&gt; {  \n                if (!resultOpt.isPresent()) {  \n                    \/\/ \u4e0d\u5b58\u5728\u5219\u8fdb\u884c\u8865\u507f\u4fee\u590d\n                    metadataStore.put(zkGroupClientPath, memberMetadata.groupId().getBytes(UTF\/_8), Optional.empty())  \n                            .thenAccept(stat -&gt; {  \n                                log.info(&quot;repaired clientId and group mapping: {}({})&quot;,  \n                                        zkGroupClientPath, memberMetadata.groupId());  \n                            })  \n                            .exceptionally(ex -&gt; {  \n                                log.warn(&quot;repaired clientId and group mapping failed: {}({})&quot;,  \n                                        zkGroupClientPath, memberMetadata.groupId());  \n                                return null;  \n                            });  \n                }  \n            }).exceptionally(ex -&gt; {  \n                log.warn(&quot;repaired clientId and group mapping failed: {} &quot;, zkGroupClientPath, ex);  \n                return null;  \n            });  \n        }  \n    }  \n}\n<\/code><\/pre>\n<p>\u7ecf\u8fc7\u65b9\u6848\u4e09\u7684\u4f18\u5316\u4e0a\u7ebf\uff0c\u5373\u4f7f\u662f\u5386\u53f2\u5b58\u5728\u95ee\u9898\u7684\u6d88\u8d39\u7ec4\uff0c\u4e2a\u522b\u5206\u533a\u6d88\u8d39\u6d41\u91cf\u6307\u6807\u7f3a\u5c11group\u5b57\u6bb5\u7684\u95ee\u9898\u4e5f\u5f97\u5230\u4e86\u4fee\u590d\u3002\u5177\u4f53\u6548\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046854191\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<p>\u7ecf\u8fc7\u591a\u4e2a\u7248\u672c\u7684\u4f18\u5316\u548c\u7ebf\u4e0a\u9a8c\u8bc1\uff0c\u6700\u7ec8\u901a\u8fc7\u65b9\u6848\u4e09\u6bd4\u8f83\u5b8c\u7f8e\u7684\u89e3\u51b3\u4e86\u8fd9\u4e2a\u6d88\u8d39\u6307\u6807\u95ee\u9898\u3002\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5e76\u53d1\u95ee\u9898\u5f80\u5f80\u96be\u4ee5\u6a21\u62df\u548c\u590d\u73b0\uff0c\u6211\u4eec\u4e5f\u5728\u5c1d\u8bd5\u591a\u4e2a\u7248\u672c\u540e\u624d\u627e\u5230\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5982\u679c\u60a8\u5728\u8fd9\u65b9\u9762\u6709\u66f4\u597d\u7684\u7ecf\u9a8c\u6216\u60f3\u6cd5\uff0c\u6b22\u8fce\u63d0\u51fa\uff0c\u6211\u4eec\u5171\u540c\u63a2\u8ba8\u548c\u4ea4\u6d41\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046854183\">https:\/\/segmentfault.com\/a\/1190000046854183<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1avivo \u4e92\u8054\u7f51\u5927\u6570\u636e\u56e2\u961f- Chen Jianbo \u672c\u6587\u662f\u300avivo Pulsar\u4e07\u4ebf\u7ea7\u6d88\u606f\u5904\u7406\u5b9e\u8df5\u300b [&hellip;]<\/p>\n","protected":false},"author":3,"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-10020896","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\/10020896","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020896"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020896\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}