{"id":3916,"date":"2022-08-30T21:39:02","date_gmt":"2022-08-30T13:39:02","guid":{"rendered":"\/?p=3916"},"modified":"2022-09-04T20:09:13","modified_gmt":"2022-09-04T12:09:13","slug":"18-kafka%e8%84%9a%e6%9c%ac%e5%b7%a5%e5%85%b7%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=3916","title":{"rendered":"18.kafka\u811a\u672c\u5de5\u5177\u4f7f\u7528"},"content":{"rendered":"<p>\u672c\u7ae0\u6211\u4eec\u8bb2\u89e3\u4e00\u4e0b\u57fa\u672c\u7684\u811a\u672c\u5de5\u5177\u4f7f\u7528\uff0c\u6309\u7167\u4e0d\u540c\u7684\u89d2\u5ea6\u51fa\u53d1<\/p>\n<p>1.\u00a0broker\u7684\u7ba1\u7406<\/p>\n<p>\u57fa\u672c\u7684\u542f\u52a8\u65f6\u5229\u7528 kafka-server-start.sh<\/p>\n<p>bin\/kafka-server-start.sh -daemon &lt;path&gt;\/server.properties<\/p>\n<p>\u9700\u8981\u6ce8\u610f\uff0c\u4e0d\u52a0\u5165-daemon\uff0c\u5176\u662f\u4e0d\u4f1a\u8fdb\u5165\u540e\u5929\u8fd0\u884c\u7684<\/p>\n<p>\u5982\u679c\u4e0d\u60f3\u52a0\u5165-daemon,\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 nohup \u2026 &amp;\u6765\u542f\u52a8\u3002<\/p>\n<p>\u5bf9\u4e8ebroker\u7684\u5173\u95ed\uff0c\u5219\u662f\u4e00\u4e2a\u5927\u95ee\u9898<\/p>\n<p>\u6b63\u786e\u7684\u5173\u95edbroker\u8fdb\u7a0b\uff0c\u5982\u679c\u4e0d\u662f\u4ee5daemon\u8fd9\u6837\u7684\u540e\u53f0\u65b9\u5f0f\u542f\u52a8\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u76f4\u63a5 CTRL+C\u5373\u53ef<\/p>\n<p>\u5982\u679c\u662f\u540e\u53f0\u8fd0\u884c\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u4f7f\u7528 bin\/kafka-server-stop.sh\uff0c\u8fd9\u4e2a\u811a\u672c\u4f1a\u5229\u7528grep\u6765\u8fdb\u884c\u67e5\u627e\u6240\u6709\u7684kakfa\u63a5\u53e3\uff0c\u5e76\u8fdb\u884c\u5173\u95ed\u3002<\/p>\n<p>\u5982\u679c\u9700\u8981\u5173\u95ed\u6307\u5b9a\u7684Kafka\uff0c\u53ef\u4ee5\u81ea\u884c\u67e5\u627eKafka\u5bf9\u5e94\u7684PID\uff0c\u7136\u540e\u5229\u7528kill -s TERM $PID\u6765\u5173\u95ed<\/p>\n<p>\u66b4\u9732broker\u7684JMX\u7aef\u53e3<\/p>\n<p>\u53ef\u4ee5\u5229\u7528<\/p>\n<p>Export\u00a0JMX_PORT=9997 bin\/kafka-server-start.sh -daemon\u00a0&lt;path&gt;\/server.properties<\/p>\n<p>\u5bf9\u4e8ebroker\u7684\u589e\u52a0<\/p>\n<p>\u76f4\u63a5\u4e3abroker\u65b0\u589e\u4e00\u4e2abroker.id\uff0c\u7136\u540e\u8fdb\u884c\u542f\u52a8\uff0c\u4e0d\u8fc7\u7f3a\u70b9\u662f\u65b0\u589e\u7684broker\u4e0d\u4f1a\u88ab\u5206\u914d\u4efb\u4f55\u8001\u7684topic\u5206\u533a\uff0c\u9700\u8981\u624b\u52a8\u8fdb\u884c\u91cd\u5206\u914d\u3002<\/p>\n<p>Broker\u7248\u672c\u7684\u5347\u7ea7<\/p>\n<p>\u5176\u5b9e\u5347\u7ea7\u5f88\u7b80\u5355<\/p>\n<p>\u9996\u5148\u662f\u786e\u5b9abroker\u4e4b\u95f4\u7684\u901a\u4fe1\u7248\u672c\u548c\u6d88\u606f\u7248\u672c<\/p>\n<p>\u9700\u8981\u786e\u5b9abroker\u7684server.properties\u4e2d\u662f\u5426\u5305\u542bkey<\/p>\n<p>Inter.broker.protocol.version<\/p>\n<p>Log.message.format.version<\/p>\n<p>\u7136\u540e\u66f4\u65b0\u4ee3\u7801\uff0c\u8fdb\u884cbroker\u7684\u91cd\u542f<\/p>\n<p>\u4e0b\u8f7d\u65b0\u7684broker\uff0c\u8fdb\u884c\u4f9d\u6b21\u91cd\u542f<\/p>\n<p>\u66f4\u65b0\u901a\u4fe1\u7248\u672c\u548c\u6d88\u606f\u7248\u672c<\/p>\n<p>Inter.broker.protocol.version=0.10.2<\/p>\n<p>Log.message.format.version=0.10.2<\/p>\n<p>\u8fd9\u6837\u5c31\u5347\u7ea7\u5b8c\u6210\u4e86\u3002<\/p>\n<p>2.\u00a0topic\u7684\u7ba1\u7406<\/p>\n<p>\u5bf9\u4e8etopic\u521b\u5efa\u7684\u65b9\u5f0f\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u56db\u79cd<\/p>\n<p>\u6267\u884ckafka-topics.sh\u547d\u4ee4\u884c\u5de5\u5177\u521b\u5efa<\/p>\n<p>\u663e\u5f0f\u53d1\u9001CreateTopicsRequest\u6765\u521b\u5efaTopic<\/p>\n<p>\u53d1\u9001MetadataRequest\u8bf7\u6c42\uff0c\u4e14\u8bbe\u7f6e\u4e86auto.create.topics.enable\u4e3atrue<\/p>\n<p>\u901a\u8fc7Zookeeper\u6765\u5411\/brokers\/topics\u8def\u5f84\u4e0b\u5199\u5165\u4ee5topic\u540d\u79f0\u547d\u540d\u7684\u5b50\u8282\u70b9<\/p>\n<p>\u4e0a\u9762\u56db\u79cd\u65b9\u6cd5\u4e2d\uff0c\u5b98\u65b9\u793e\u533a\u63a8\u8350\u4f7f\u7528\u524d\u4e24\u79cd\u65b9\u6cd5\u521b\u5efatopic\uff0c<\/p>\n<p>\u5982\u679c\u4f7f\u7528\u524d\u4e00\u79cd\u65b9\u6cd5\u6765\u521b\u5efatopic\uff0c\u811a\u672c\u7684\u53ef\u9009\u53c2\u6570\u5305\u542b\u4e0b\u9762\u7684\u5217\u8868<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"589\" height=\"464\" class=\"wp-image-3917\" src=\"\/wp-content\/uploads\/2022\/08\/unnamed-file-29.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-29.png 589w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-29-300x236.png 300w\" sizes=\"(max-width: 589px) 100vw, 589px\" \/><\/p>\n<p>\u6bd4\u5982\u6211\u4eec\u521b\u5efa\u4e00\u4e2atopic\uff0c\u540d\u79f0\u4e3a test1,\u914d\u7f6e\u4e3a6\u4e2a\u5206\u533a\uff0c\u6bcf\u4e2a\u5206\u533a\u4e09\u4e2a\u526f\u672c\uff0c\u7559\u5b58\u65f6\u95f4\u4e3a3\u5929\uff0c<\/p>\n<p>\/bin\/kafka-topics.sh \u2013create \u2013zookeeper localhost:2181 \u2013partition 6 \u2013replication-factor 3 \u2013topic test1 \u2013config delete.retention.ms=259200000<\/p>\n<p>\u6216\u8005\u6211\u4eec\u624b\u52a8\u6307\u5b9a\u5206\u533a\u5728\u96c6\u7fa4\u4e0a\u7684\u5206\u914d<\/p>\n<p>\/bin\/kafka-topics.sh \u2013create \u2013zookeeper localhost:2181 \u2013partition 6 \u2013replication-factor 3 \u2013topic test2<\/p>\n<p>&#8211;replica-assignment 0:1,1:2,0:2,1:2<\/p>\n<p>\u5220\u9664topic<\/p>\n<p>\u5220\u9664topic\u5f53\u524d\u67093\u4e2a\u65b9\u5f0f\uff0c<\/p>\n<p>\u4f7f\u7528kafka-topics\u811a\u672c<\/p>\n<p>\u6784\u5efaDeleteTopicsRequest\u8bf7\u6c42<\/p>\n<p>\u76f4\u63a5\u5411ZooKeeper\u7684\/admin\/delete_topics\u4e0b\u5199\u5165\u5b50\u8282\u70b9<\/p>\n<p>\u4e0d\u8fc7\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u76f4\u63a5\u5199\u5165ZooKeeper\u4e2d<\/p>\n<p>\u800c\u4f7f\u7528\u5176\u4ed6\u65b9\u5f0f\uff0c\u6267\u884c\u547d\u4ee4\u4e00\u822c\u662f\u7acb\u523b\u8fd4\u56de\uff0c\u8fd9\u65f6\u5019\u5e76\u6ca1\u6709\u76f4\u63a5\u5220\u9664topic\u6570\u636e\uff0c\u800c\u662f\u9700\u8981\u7528\u6237\u591a\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u624d\u80fd\u89c2\u5bdf\u5230topic\u6570\u636e\u88ab\u5220\u9664\u3002<\/p>\n<p>\u800c\u4e14\u5220\u9664topic\uff0c\u4e5f\u9700\u8981\u8bbe\u7f6edelete.topic.enable\u53c2\u6570\u4e3atrue.<\/p>\n<p>\u83b7\u53d6topic\u7684\u5217\u8868<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528Kafka\u7684kafka-topics\u811a\u672c\u6765\u83b7\u53d6\u96c6\u7fa4\u7684topic\u5217\u8868\u3002<\/p>\n<p>bin\/kafka-topics.sh \u2013zookeeper\u00a0localhost:2181 \u2013list<\/p>\n<p>\u67e5\u8be2topic\u7684\u8be6\u60c5<\/p>\n<p>\u5229\u7528kafka-topics\u811a\u672c\u6765\u770btopic\u7684\u5177\u4f53\u8be6\u60c5<\/p>\n<p>bin\/kafka-topics.sh \u2013zookeeper\u00a0localhost:2181 \u2013topic test1 \u2013describe<\/p>\n<p>\u8f93\u51fa\u4e86\u5185\u90e8\u5305\u542b\u4e86\u54ea\u4e9b\u5206\u533a\uff0c\u54ea\u4e9b\u540c\u6b65\u7684\u526f\u672c\uff0c\u54ea\u4e9bISR<\/p>\n<p>\u4fee\u6539topic\u672c\u8eab\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u589e\u52a0\u5206\u533a\uff0c\u4f7f\u7528bin\/kafka-topics.sh<\/p>\n<p>bin\/kafka-topics.sh \u2013alter \u2013zookeeper\u00a0localhost:2181 \u2013partition 10 \u2013topic\u00a0test1<\/p>\n<p>\u8fd9\u6837\u5c31\u4f1a\u8fdb\u884c\u5206\u533a\u7684\u589e\u52a0\uff0c\u800c\u4e14\u73b0\u5728Kafka\u5e76\u4e0d\u652f\u6301\u51cf\u5c11\u5206\u533a\u6570\uff0c\u5982\u679c\u8bbe\u7f6e\u4e00\u4e2a\u5c0f\u4e8e\u5f53\u524d\u5206\u533a\u6570\u7684\u6570\u91cf\uff0c\u4f1a\u88ab\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p>\u800c\u5982\u679c\u5e0c\u671b\u4fee\u6539\u67d0\u4e9btopic\u7ea7\u522b\u7684\u53c2\u6570\u5462\uff0c\u53ef\u4ee5\u4f7f\u7528kafka-configs\u811a\u672c\u6765\u8bbe\u7f6e\uff0c<\/p>\n<p>\u5176\u53ef\u4ee5\u6dfb\u52a0\uff0c\u4fee\u6539\uff0c\u5220\u9664\u4e0d\u540c\u5b9e\u4f53\u7684\u53c2\u6570<\/p>\n<p>\u5728kafak-configs\u811a\u672c\u4e2d\uff0c\u5305\u542b\u4e86\u56db\u79cd\u5b9e\u4f53\uff0c\u5206\u522b\u662ftopic\u00a0user\u00a0clients\u00a0broker<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u5982\u4f55\u4e3a\u5df2\u7ecf\u6709\u7684topic\u6dfb\u52a0\u4e00\u4e2atopic\u7684\u53c2\u6570\uff0c\u5047\u8bbe\u8981\u4e3a\u4e0a\u9762\u7684test2\u6765\u6dfb\u52a0\u914d\u7f6e<\/p>\n<p>cleanup.policy<\/p>\n<p>bin\/kafka-config.sh \u2013zookeeper\u00a0localhost:2181 -alter \u2013entity-type topics \u00a0\u2013entity-name test-topics2 \u2013add-config\u00a0cleanup.policy=compact<\/p>\n<p>\u800c\u4e14\u7531\u4e8etopics\u662fkafka\u4e2d\u7684\u865a\u62df\u6982\u5ff5\uff0c\u6240\u4ee5\u5bf9\u4e8e\u5176\u7684\u8bbe\u7f6e\u66f4\u65b0\uff0c\u662f\u4e0d\u9700\u8981\u91cd\u542fbroker\u7684\u3002<\/p>\n<p>\u67e5\u770btopic\u7684\u53c2\u6570\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528kafak-topics \u6216\u8005 kafak-configs\u811a\u672c<\/p>\n<p>\u5982\u679c\u662f\u4f7f\u7528kafka-topics\u811a\u672c\u6765\u67e5\u770b\u914d\u7f6e\u7684\u8bdd<\/p>\n<p>\u57fa\u672c\u4f7f\u7528\u5982\u4e0b\u7684\u547d\u4ee4<\/p>\n<p>bin\/kafka-topics.sh -describe\u00a0\u2013zookeeper localhost:2181 \u2013topic\u00a0test<\/p>\n<p>\u4f7f\u7528kafka-configs\u811a\u672c\u6765\u770b<\/p>\n<p>bin\/kafka-configs.sh \u2013describe\u00a0\u2013zookeeper\u00a0localhost:2181 \u2013-entity-type topics\u00a0\u2013entity-name test<\/p>\n<p>\u5220\u9664\u914d\u7f6e\u5219\u662f\u4f7f\u7528kafka-configs\u811a\u672c\u8fdb\u884c\u64cd\u4f5c\u7684<\/p>\n<p>bin\/kafka-configs.sh \u2013zookeeper localhost:2181 \u2013alter \u2013entity-type topics \u2013entity-name test \u2013delete-config preallocate<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"561\" height=\"113\" class=\"wp-image-3918\" src=\"\/wp-content\/uploads\/2022\/08\/unnamed-file-30.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-30.png 561w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-30-300x60.png 300w\" sizes=\"(max-width: 561px) 100vw, 561px\" \/><\/p>\n<p>consumer\u7ba1\u7406\u76f8\u5173\u811a\u672c<\/p>\n<p>\u67e5\u770b\u6d88\u8d39\u8005\u7ec4\u7684\u811a\u672c\u4e3akafka-consumer-groups.sh<\/p>\n<p>\u8fd9\u4e2a\u811a\u672c\u7684\u4e3b\u8981\u529f\u80fd\u53c2\u6570\u4e3a<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"602\" height=\"142\" class=\"wp-image-3919\" src=\"\/wp-content\/uploads\/2022\/08\/unnamed-file-31.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-31.png 602w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-31-300x71.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/p>\n<p>\u4e0a\u9762bootstrap-server\u548czookeeper\u00a0\u4e24\u8005\u662f\u4e0d\u53ef\u540c\u65f6\u6307\u5b9a\u7684\uff0c\u5206\u522b\u5bf9\u5e94\u7740\u65b0\u65e7\u7684consumer\u7248\u672c<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u6d4b\u8bd5list\u529f\u80fd\uff0c\u67e5\u770b\u6240\u6709\u7684\u6d88\u8d39\u8005\u7ec4\u3002<\/p>\n<p>bin\/kafka-consumer-groups.sh -\u2013bootstrap-server\u00a0localhost:9092 \u2013-list<\/p>\n<p>\u7136\u540e\u5206\u522b\u67e5\u770b\u4e0d\u540c\u6d88\u8d39\u7ec4\u7684\u8be6\u60c5<\/p>\n<p>bin\/kafka-consumer-groups.sh -\u2013bootstrap-server\u00a0localhost:9092 \u2013describe\u00a0-group test1<\/p>\n<p>\u5176\u4e2d\u5c55\u793a\u7684\u5185\u5bb9\u6709\uff0cTOPIC-\u6d88\u8d39\u8005\u7ec4\u6d88\u8d39\u7684topic<\/p>\n<p>PARTITION \u6d88\u8d39\u8005\u7ec4\u6d88\u8d39\u4e86\u90a3\u4e2a\u5206\u533a<\/p>\n<p>CURRENT-OFFSET \u6d88\u8d39\u8005\u7ec4\u6700\u65b0\u6d88\u8d39\u7684\u4f4d\u79fb\u503c<\/p>\n<p>LOG-END-OFFSET topic\u6240\u6709\u5206\u533a\u5f53\u524d\u65e5\u5fd7\u7ec8\u7aef\u4f4d\u79fb\u503c<\/p>\n<p>LAG \u6d88\u8d39\u6ede\u540e\u8fdb\u5ea6\uff0c\u5c31\u662fLOG-END-OFFSET \u51cf\u53bb CURRENT-OFFSET\u7684\u503c<\/p>\n<p>\u4e00\u822c\u662f\u5927\u4e8e\u7b49\u4e8e0\u7684\u6b63\u6570\uff0c\u5982\u679c\u662f\u8d1f\u6570\uff0c\u8bf4\u660e\u6d88\u606f\u8fd8\u6ca1\u88ab\u6d88\u8d39\u5c31\u88abconsumer\u8df3\u8fc7\u4e86\u3002<\/p>\n<p>CONSUMER-ID consumer\u7684id\uff0c\u4e00\u822c\u4e3a\u81ea\u52a8\u751f\u6210\u7684\uff0c\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u503c\uff0c\u8bf4\u660econsumer\u8fd8\u6ca1\u5728\u8fd0\u884c\u4e2d<\/p>\n<p>HOST: consumer\u6240\u5728\u7684\u4e3b\u673a\u4fe1\u606f\uff0c\u5982\u679c\u6ca1\u6709\u503c\uff0c\u8bf4\u660e\u8fd8\u6ca1\u5728\u8fd0\u884c<\/p>\n<p>CLIENT-ID \u81ea\u52a8\u751f\u6210\u7684\u4e00\u4e2a\u5ba2\u6237\u7aefID<\/p>\n<p>\u7136\u540e\u662f\u91cd\u8bbe\u6d88\u8d39\u8005\u7ec4\u7684\u4f4d\u79fb<\/p>\n<p>\u5229\u7528\u811a\u672c\u6765\u4e3aconsumer\u00a0group\u91cd\u65b0\u8bbe\u7f6e\u4f4d\u79fb\uff0c\u4f46\u662f\u8981\u6c42consumer\u00a0group\u4e0d\u80fd\u5904\u4e8e\u8fd0\u884c\u72b6\u6001<\/p>\n<p>\u76f8\u5173\u811a\u672c\u662fkafka-consumer-groups<\/p>\n<p>\u5e38\u89c1\u53c2\u6570\u53ef\u4ee5\u5206\u4e3a\u4e09\u7c7b<\/p>\n<p>\u4f5c\u7528\u57df\u76f8\u5173\uff0c\u660e\u786e\u4fee\u6539\u6d88\u8d39\u8005\u7ec4\u4e2d\u54ea\u4e9btopic<\/p>\n<p>&#8211;all-topics \u6240\u6709topics<\/p>\n<p>&#8211;topic\u00a0t1\uff0c&#8211;topic\u00a0t2 \u82e5\u5e72\u4e2atopic<\/p>\n<p>&#8211;topic\u00a0t1:0,1,2 \u6307\u5b9a\u5206\u533a<\/p>\n<p>\u91cd\u8bbe\u89c4\u5219<\/p>\n<p>&#8211;to-earliest \u8bbe\u7f6e\u5230\u6700\u65e9\u4f4d\u79fb\u5904<\/p>\n<p>&#8211;to-latest\u8bbe\u7f6e\u5230\u6700\u665a\u4f4d\u79fb\u5904<\/p>\n<p>&#8211;to-current \u8bbe\u7f6e\u5230\u5f53\u524d\u4f4d\u79fb\u5904<\/p>\n<p>&#8211;to-offset&lt;offset&gt; \u8bbe\u7f6e\u5230\u6307\u5b9a\u4f4d\u79fb\u5904<\/p>\n<p>&#8211;shift-by N\u4f4d\u79fb\u8c03\u6574\u5230\u5f53\u524d\u4f4d\u79fb\u9664+N\u7684\u4f4d\u7f6e<\/p>\n<p>&#8211;to-datetime &lt;datetime&gt;\u8bbe\u7f6e\u5230\u7ed9\u5b9a\u65f6\u95f4\u7684\u6700\u65e9\u4f4d\u79fb\u5904<\/p>\n<p>&#8211;by-duration\u00a0\u8bbe\u5b9a\u5230\u8ddd\u79bb\u5f53\u524d\u65f6\u95f4\u5236\u5b9a\u95f4\u9694\u7684\u4f4d\u79fb\u5904<\/p>\n<p>&#8211;from-file \u4ececsv\u4e2d\u8bfb\u53d6\u8c03\u6574\u7b56\u7565<\/p>\n<p>\u6700\u540e\u53ef\u4ee5\u786e\u5b9a\u662f\u5426\u6267\u884c<\/p>\n<p>&#8211;execute \u6267\u884c\uff0c\u4e0d\u52a0\u5c31\u662f\u6253\u5370\u8c03\u6574\u65b9\u6848<\/p>\n<p>&#8211;export\u00a0\u4fdd\u5b58\u4e3aCSV\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0<\/p>\n<p>\u6bd4\u5982\u4e0b\u9762\u7684\u547d\u4ee4<\/p>\n<p>bin\/kafka-consumer-groups.sh\u00a0\u2013bootstrap-server localhost:9092 \u2013group test1 \u2013reset-offsets \u2013all-topics \u2013to-earliest &#8211;execute<\/p>\n<p>\u5220\u9664\u6d88\u8d39\u8005\u7ec4\uff0c\u540c\u6837\u4e5f\u662f\u4f7f\u7528kafka-consumer-groups\u811a\u672c\uff0c\u6765\u5220\u9664\u5904\u4e8einactive\u7248\u672c\u7684\u6d88\u8d39\u8005\u7ec4<\/p>\n<p>bin\/kafka-consumer-groups.sh \u2013zookeeper\u00a0localhost:2181 \u2013delete\u00a0\u2013group test1<\/p>\n<p>\u4f46\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u56e0\u4e3a\u6709offset.retention.minutes\u7684\u53c2\u6570\uff0c\u6240\u4ee5\u4f1a\u81ea\u52a8\u79fb\u9664inactive\u7684\u4f4d\u79fb\u4fe1\u606f\uff0c\u6bd5\u7adf\u4f4d\u79fb\u4fe1\u606f\u4fdd\u5b58\u5728\u5185\u90e8topic\u4e2d<\/p>\n<p>\u7531\u4e8e\u4e00\u4e2atopic\u4e0b\uff0c\u5f80\u5f80\u5177\u6709\u591a\u4e2a\u5206\u533a\uff0c\u6bcf\u4e2a\u5206\u533a\u8fd8\u6709\u540c\u6b65\u7684\u6982\u5ff5\uff0c\u540c\u6b65\u5c31\u5e26\u6765\u4e86leader\u548cfollower\u7684\u526f\u672c\u5dee\u5f02\uff0c\u4e3a\u4e86\u907f\u514dKafka\u4e2d\u5c0f\u90e8\u5206\u7684broker\u627f\u8f7d\u4e86\u5927\u91cf\u7684\u5206\u533aleader\u526f\u672c\uff0c\u6240\u4ee5\u5f15\u5165\u4e86\u9996\u9009\u526f\u672cpreferred\u00a0replica.<\/p>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u7b2c\u4e00\u6b21\u5206\u914d\u526f\u672c\u7684\u65f6\u5019\uff0c\u5f53\u9009\u7684leader\u526f\u672c\u5c31\u662fpreferred\u00a0replica\uff0c<\/p>\n<p>kafka\u00a0preferred replica\u7684\u53d8\u66f4\u811a\u672c\u4f7f\u7528\u7684\u58eb kafka-preferred-replica-election\u811a\u672c<\/p>\n<p>\u8fd9\u4e2a\u811a\u672c\u9700\u8981\u4e00\u4e2ajson\u6587\u4ef6\uff0c\u6765\u786e\u5b9a\u5982\u4f55\u8c03\u6574\u5206\u533a\uff0c\u6bd4\u5982\u4e0b\u9762\u7684json\u6587\u4ef6<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"417\" height=\"23\" class=\"wp-image-3920\" src=\"\/wp-content\/uploads\/2022\/08\/unnamed-file-32.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-32.png 417w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-32-300x17.png 300w\" sizes=\"(max-width: 417px) 100vw, 417px\" \/><\/p>\n<p>\u4e4b\u540e\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u5373\u53ef<\/p>\n<p>bin\/kafka-preferred-replica-election.sh \u2013zookeeper\u00a0localhost:2181 \u2013path-to-json-file &lt;path&gt;\/preferred-leader-plan.json<\/p>\n<p>\u4e4b\u540e\u7684preferred\u00a0replica\u5c31\u4f1a\u88ab\u8c03\u6574\u4e86\u3002\u5f53\u7136\u9664\u4e86\u624b\u52a8\u8c03\u6574\u5916,\u8fd8\u6709kafka\u63d0\u4f9b\u4e86broker\u7aef\u53c2\u6570\u6765\u5e2e\u52a9\u7528\u6237\u81ea\u52a8\u6267\u884cpreferred leader\uff0c\u5229\u7528\u7684\u53c2\u6570\u662f leader.imbalance.per.broker.percentage<\/p>\n<p>\u6839\u636e\u767e\u5206\u6bd4\u6765\u8fdb\u884cperferred\u00a0leader\u9009\u4e3e\u3002\u8d85\u8fc710%\uff0c\u5c31\u8fdb\u884cpreferred leader\u9009\u4e3e\u3002<\/p>\n<p>\u5206\u533a\u7684\u91cd\u5206\u914d\uff0c\u662f\u4e3a\u4e86\u5728\u65b0\u7684broker\u52a0\u5165\uff0c\u5c06topic\u91cd\u65b0\u5206\u914d\u66f4\u52a0\u5747\u5300\u7684\u3002<\/p>\n<p>\u6211\u4eec\u6765\u6f14\u793a\u5982\u4f55\u8fdb\u884c\u91cd\u5206\u914d\uff0c<\/p>\n<p>bin\/kafka-reassign-partitions.sh \u2013zookeeper localhost:2181 \u2013topics-to-move-json-file test.json \u2013broker-list \u201c5,6\u201d \u2013generate<\/p>\n<p>\u8fd9\u6837\u5c31\u4f1a\u7ed9\u4e0e\u4e00\u4e2a\u65b0\u7684\u5206\u914d\u65b9\u6848\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u6267\u884c\uff0c\u800c\u662f\u5e0c\u671b\u4fdd\u5b58\u4e3a\u4e00\u4e2a\u65b0\u7684json\u6587\u4ef6\uff0c\u6765\u8fdb\u884c\u540e\u7eed\u771f\u6b63\u7684\u91cd\u5206\u914d\u64cd\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"349\" height=\"103\" class=\"wp-image-3921\" src=\"\/wp-content\/uploads\/2022\/08\/unnamed-file-33.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-33.png 349w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-33-300x89.png 300w\" sizes=\"(max-width: 349px) 100vw, 349px\" \/><\/p>\n<p>\u8bf4\u5b9e\u8bdd,\u00a0\u4e00\u5b9a\u8981\u8c28\u614e\u7684\u53d1\u8d77\u5206\u533a\u91cd\u5206\u914d\u64cd\u4f5c,\u56e0\u4e3a\u4e0d\u540cbroker\u4e4b\u95f4\u6570\u636e\u8fc1\u79fb\u4f1a\u5360\u7528\u5f88\u5927\u7684broker\u673a\u5668\u5e26\u5bbd.<\/p>\n<p>\u5bf9\u4e8eclient\u7aef\u7684\u811a\u672c\u6709kafka-console-producer\u811a\u672c<\/p>\n<p>kafka-console-producer\u811a\u672c\u662f\u6a21\u62dfproducer\u8fdb\u884c\u6d88\u606f\u7684\u751f\u4ea7,\u652f\u6301\u7684\u53c2\u6570\u6709<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"448\" height=\"180\" class=\"wp-image-3922\" src=\"\/wp-content\/uploads\/2022\/08\/unnamed-file-34.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-34.png 448w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-34-300x121.png 300w\" sizes=\"(max-width: 448px) 100vw, 448px\" \/><\/p>\n<p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u53d1\u9001\u6d88\u606f,\u53ef\u4ee5\u6307\u5b9aproducer\u7684ack\u4e3aall,\u4f7f\u7528LZ4\u6d88\u606f\u538b\u7f29,\u5931\u8d25\u91cd\u8bd5\u6b21\u6570\u4e3a10\u6b21,linger.ms\u8bbe\u7f6e\u4e3a3\u79d2,\u547d\u4ee4\u4e3a<\/p>\n<p>bin\/kafka-console-producer.sh \u2013broker-list \u00a0localhost:9092 \u2013topic test \u2013compression-codec lz4 \u2013request-required-acks all \u2013timeout 3000 \u2013message-send-max-retries 10<\/p>\n<p>\u9700\u8981\u6ce8\u610f,\u4f7f\u7528\u811a\u672c\u5c31\u4e0d\u7528\u6307\u5b9a\u81ea\u5b9a\u4e49\u5e8f\u5217\u5316\u7c7b,\u4e0d\u9700\u8981\u624b\u52a8\u6307\u5b9aserializer.<\/p>\n<p>\u540c\u6837kafka-console-consumer\u811a\u672c\u662f\u7528\u6765\u6a21\u62df\u6d88\u8d39\u8005\u7684\u6d88\u8d39.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"447\" height=\"136\" class=\"wp-image-3923\" src=\"\/wp-content\/uploads\/2022\/08\/unnamed-file-35.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-35.png 447w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-35-300x91.png 300w\" sizes=\"(max-width: 447px) 100vw, 447px\" \/><\/p>\n<p>kafka-console-consumer\u811a\u672c\u7684\u4f7f\u7528\u57fa\u672c\u5982\u4e0b.<\/p>\n<p>bin\/kafka-console-consumer.sh \u2013bootstrap-server localhost:9092 \u2013topic test \u2013from-beginning<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u4e0d\u9700\u8981\u5236\u5b9agroup\u00a0id,\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2agroup\u00a0ID.<\/p>\n<p>kafka-run-class\u662f\u5f88\u591akafka\u811a\u672c\u5b9e\u73b0\u7684\u57fa\u77f3.\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u5f88\u6709\u7528\u7684\u811a\u672c\u5de5\u5177.<\/p>\n<p>\u6bd4\u5982\u67e5\u770b\u5143\u6570\u636e,\u800c\u5143\u6570\u636e\u4fe1\u606f\u662f\u5305\u542b\u6d88\u606f\u4f4d\u79fb,\u521b\u5efa\u65f6\u95f4\u6233,\u538b\u7f29\u7c7b\u578b,\u5b57\u8282\u6570\u7b49<\/p>\n<p>kafka\u63d0\u4f9b\u4e86\u8fd9\u6837\u7684\u529f\u80fd,\u4e3akafka.tools.DumpLogSegments<\/p>\n<p>\u57fa\u672c\u7684\u4f7f\u7528\u5982\u4e0b<\/p>\n<p>bin\/kafka-run-class.sh kafak.tools.DumpLogSegments \u2013files ..\/datalogs\/kafka_1\/t1-0\/0000000000000000000.log<\/p>\n<p>\u5176\u4e2d\u4f1a\u663e\u793a\u65e5\u5fd7\u4e2d\u4f4d\u79fb\u4fe1\u606f,\u7269\u7406\u6587\u4ef6\u4f4d\u7f6e,\u6d88\u606f\u957f\u5ea6,\u538b\u7f29\u7c7b\u578b ,CRC\u7801\u7b49\u5143\u6570\u636e\u4fe1\u606f.<\/p>\n<p>\u83b7\u53d6topic\u5f53\u524d\u6d88\u606f\u6570<\/p>\n<p>\u7528\u6237\u5e0c\u671b\u80fd\u591f\u5b9e\u65f6\u7684\u4e86\u89e3topic\u751f\u4ea7\u4e86\u591a\u5c11\u6761\u6d88\u606f,Kafka\u63d0\u4f9b\u4e86GetOffsetShell\u6765\u7edf\u8ba1\u6d88\u606f\u603b\u6570.<\/p>\n<p>\u57fa\u672c\u7684\u4f7f\u7528\u5982\u4e0b<\/p>\n<p>bin\/kafka-run-class.sh kafka.tools.GetOffsetShell \u2013broker-list localhost:9092 \u2013topic test \u2013time -1<\/p>\n<p>\u5176\u4e2dtime\u00a0-1\u4f1a\u8868\u793a\u83b7\u53d6topic\u7684\u6240\u6709\u5206\u533a\u6700\u5927\u4f4d\u79fb,\u00a0-2\u8868\u793a\u6700\u65e9\u4f4d\u79fb.<\/p>\n<p>\u83b7\u53d6consumer_offset<\/p>\n<p>\u5373\u4f7fconsumer\u7684\u4f4d\u79fb\u4fdd\u5b58\u5728Kafka\u7684\u5185\u90e8topic\u4e2d,\u4e5f\u662f\u53ef\u4ee5\u901a\u8fc7 kafka-simple-consumer-shell.sh\u6765\u67e5\u770b<\/p>\n<p>\u4e0d\u8fc7\u56e0\u4e3a__consumer_offsets\u7684topic\u4e2d,\u4f4d\u79fb\u4f1a\u88ab\u5728\u67d0\u4e00\u4e2a\u5b50\u5206\u533a\u4e0a,\u9996\u5148\u786e\u5b9a\u4e86\u5b50\u5206\u533a,\u7136\u540e\u8fdb\u884c\u67e5\u8be2topic\u4f4d\u79fb\u4fe1\u606f.<\/p>\n<p>bin\/kafka-simple-consumer-shell.sh \u2013topic\u00a0__consumer_offset \u2013partition\u00a012 \u2013broker-list localhost 9092 \u2013formatter \u201ckafka.coordinator.GroupMetadataManager $OffsetMessageFormatter\u201d<\/p>\n<p>\u5f97\u5230\u7684\u662f\u7ed3\u679c\u5982\u4e0b<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"416\" height=\"186\" class=\"wp-image-3924\" src=\"\/wp-content\/uploads\/2022\/08\/unnamed-file-36.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-36.png 416w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/08\/unnamed-file-36-300x134.png 300w\" sizes=\"(max-width: 416px) 100vw, 416px\" \/><\/p>\n<p>\u6bcf\u4e00\u884c\u5c55\u793a\u4e86\u67d0\u4e00\u4e2a\u5206\u533a\u7684\u5f53\u524d\u7684\u5df2\u63d0\u4ea4\u4f4d\u79fb,\u63d0\u4ea4\u65f6\u95f4\u7b49\u4fe1\u606f.<\/p>\n<p>\u6700\u540e\u6211\u4eec\u7ed9\u51fa\u4e0a\u9762\u811a\u672c\u548c\u5bf9\u5e94\u529f\u80fd\u7684\u4e00\u4e2a\u8868\u683c:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\u811a\u672c<\/td>\n<td>\u529f\u80fd<\/td>\n<\/tr>\n<tr>\n<td>Kafka-server-start.sh<\/td>\n<td>\u542f\u52a8broker<\/td>\n<\/tr>\n<tr>\n<td>Kafka-server-start.sh<\/td>\n<td>\u8bbe\u7f6eJMX\u7aef\u53e3<\/td>\n<\/tr>\n<tr>\n<td>Kafka-server-stop.sh<\/td>\n<td>\u5173\u95edbroker<\/td>\n<\/tr>\n<tr>\n<td>Kafka-topics.sh<\/td>\n<td>\u521b\u5efatopic<\/td>\n<\/tr>\n<tr>\n<td>Kafka-topics.sh<\/td>\n<td>\u4fee\u6539topic\u53c2\u6570<\/td>\n<\/tr>\n<tr>\n<td>Kafka-topics.sh<\/td>\n<td>\u5220\u9664topic<\/td>\n<\/tr>\n<tr>\n<td>Kafka-topics.sh<\/td>\n<td>\u67e5\u8be2topic\u5217\u8868<\/td>\n<\/tr>\n<tr>\n<td>Kafka-topics.sh<\/td>\n<td>\u67e5\u8be2topic\u8be6\u60c5<\/td>\n<\/tr>\n<tr>\n<td>Kafka-configs.sh<\/td>\n<td>\u67e5\u770b topic user clients,broker\u7b49\u914d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>Kafka-configs.sh<\/td>\n<td>\u4fee\u6539 topic user clients,broker\u7b49\u914d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>Kafka-configs.sh<\/td>\n<td>\u5220\u9664 topic user clients,broker\u7b49\u914d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>Kafka-consumer-groups.sh<\/td>\n<td>\u67e5\u8be2consumer\u6d88\u8d39\u8005\u7ec4\u76f8\u5173\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td>Kafka-consumer-groups.sh<\/td>\n<td>\u91cd\u8bbe\u6d88\u8d39\u8005\u7ec4\u4f4d\u79fb<\/td>\n<\/tr>\n<tr>\n<td>Kafka-consumer-groups.sh<\/td>\n<td>\u5220\u9664\u6d88\u8d39\u8005\u7ec4<\/td>\n<\/tr>\n<tr>\n<td>Kafka-preferred-replica-election.sh<\/td>\n<td>\u8fdb\u884cpreferred leader\u9009\u4e3e<\/td>\n<\/tr>\n<tr>\n<td>Kafka-reassign-partitions.sh<\/td>\n<td>\u91cd\u5206\u533a<\/td>\n<\/tr>\n<tr>\n<td>Kafka-console-producer<\/td>\n<td>\u6a21\u62dfproducer<\/td>\n<\/tr>\n<tr>\n<td>Kafka-console-consumer<\/td>\n<td>\u6a21\u62dfconsumer<\/td>\n<\/tr>\n<tr>\n<td>Kakfa-run-class<\/td>\n<td>\u67e5\u770b\u6d88\u606f\u5143\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>Kakfa-run-class<\/td>\n<td>\u67e5\u770btopic\u5f53\u524d\u6d88\u606f\u6570<\/td>\n<\/tr>\n<tr>\n<td>Kakfa-run-class<\/td>\n<td>\u67e5\u8be2__consumer_offsets<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u6211\u4eec\u8bb2\u89e3\u4e00\u4e0b\u57fa\u672c\u7684\u811a\u672c\u5de5\u5177 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/3916"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3916"}],"version-history":[{"count":0,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/3916\/revisions"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3916"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}