{"id":14565,"date":"2023-12-04T15:52:26","date_gmt":"2023-12-04T07:52:26","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=14565"},"modified":"2023-12-12T16:19:27","modified_gmt":"2023-12-12T08:19:27","slug":"kafka-zhong-xiao-fei-zheapi-de-zuo-yong-shi-shen-m","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/04\/kafka-zhong-xiao-fei-zheapi-de-zuo-yong-shi-shen-m\/","title":{"rendered":"Kafka\u4e2d\u6d88\u8d39\u8005API\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p>Kafka \u6d88\u8d39\u8005 API \u662f Kafka \u63d0\u4f9b\u7ed9\u5f00\u53d1\u8005\u7684\u4e00\u7ec4\u63a5\u53e3\u548c\u5de5\u5177\uff0c\u7528\u4e8e\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u4ee5\u6d88\u8d39 Kafka \u4e3b\u9898\u4e2d\u7684\u6d88\u606f\u3002\u6d88\u8d39\u8005 API \u5177\u6709\u4ee5\u4e0b\u4e3b\u8981\u4f5c\u7528\uff1a<\/p>\n<ol>\n<li>\n<strong>\u6d88\u606f\u8ba2\u9605\uff1a<\/strong> \u6d88\u8d39\u8005 API \u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u8ba2\u9605\u4e00\u4e2a\u6216\u591a\u4e2a Kafka \u4e3b\u9898\u3002\u901a\u8fc7\u6307\u5b9a\u4e3b\u9898\u540d\u79f0\uff0c\u6d88\u8d39\u8005\u53ef\u4ee5\u4ece\u76f8\u5e94\u7684\u4e3b\u9898\u4e2d\u62c9\u53d6\u6d88\u606f\u3002<\/p>\n<pre><code class=\"language-java\">consumer.subscribe(Arrays.asList(&quot;topic1&quot;, &quot;topic2&quot;));\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6d88\u606f\u62c9\u53d6\uff1a<\/strong> \u6d88\u8d39\u8005 API \u5141\u8bb8\u6d88\u8d39\u8005\u901a\u8fc7\u8f6e\u8be2\uff08polling\uff09\u7684\u65b9\u5f0f\u4ece Kafka \u670d\u52a1\u5668\u62c9\u53d6\u6d88\u606f\u3002\u6d88\u8d39\u8005\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8c03\u6574\u62c9\u53d6\u7684\u9891\u7387\u3002<\/p>\n<pre><code class=\"language-java\">ConsumerRecords&lt;String, String&gt; records = consumer.poll(Duration.ofMillis(100));\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6d88\u606f\u5904\u7406\uff1a<\/strong> \u6d88\u8d39\u8005 API \u5141\u8bb8\u5f00\u53d1\u8005\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u6d88\u606f\u5904\u7406\u903b\u8f91\u3002\u62c9\u53d6\u5230\u7684\u6d88\u606f\u5c06\u88ab\u5b58\u50a8\u5728 <code>ConsumerRecords<\/code> \u5bf9\u8c61\u4e2d\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u904d\u5386\u8fd9\u4e9b\u6d88\u606f\u5e76\u6267\u884c\u76f8\u5e94\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<pre><code class=\"language-java\">for (ConsumerRecord&lt;String, String&gt; record : records) {\n    \/\/ \u5904\u7406\u6d88\u606f\u903b\u8f91\n    System.out.printf(&quot;Consumed record with key %s and value %s%n&quot;, record.key(), record.value());\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u504f\u79fb\u63d0\u4ea4\uff1a<\/strong> \u6d88\u8d39\u8005 API \u63d0\u4f9b\u4e86\u624b\u52a8\u63d0\u4ea4\u548c\u81ea\u52a8\u63d0\u4ea4\u504f\u79fb\u7684\u65b9\u5f0f\u3002\u504f\u79fb\uff08offset\uff09\u8868\u793a\u6d88\u8d39\u8005\u5728\u5206\u533a\u4e2d\u7684\u8bfb\u53d6\u4f4d\u7f6e\u3002\u901a\u8fc7\u63d0\u4ea4\u504f\u79fb\uff0c\u6d88\u8d39\u8005\u53ef\u4ee5\u8bb0\u5f55\u81ea\u5df1\u8bfb\u53d6\u5230\u7684\u6d88\u606f\u4f4d\u7f6e\uff0c\u4ee5\u4fbf\u5728\u4e0b\u6b21\u62c9\u53d6\u6d88\u606f\u65f6\u4ece\u6b63\u786e\u7684\u4f4d\u7f6e\u5f00\u59cb\u3002<\/p>\n<ul>\n<li>\n\u624b\u52a8\u63d0\u4ea4\u504f\u79fb\uff1a<\/p>\n<pre><code class=\"language-java\">consumer.commitSync();\n<\/code><\/pre>\n<\/li>\n<li>\n\u81ea\u52a8\u63d0\u4ea4\u504f\u79fb\uff1a<\/p>\n<pre><code class=\"language-java\">properties.put(&quot;enable.auto.commit&quot;, &quot;true&quot;);\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u591a\u7ebf\u7a0b\u5904\u7406\uff1a<\/strong> \u6d88\u8d39\u8005 API \u5141\u8bb8\u521b\u5efa\u591a\u4e2a\u6d88\u8d39\u8005\u5b9e\u4f8b\uff0c\u6bcf\u4e2a\u5b9e\u4f8b\u8fd0\u884c\u5728\u72ec\u7acb\u7684\u7ebf\u7a0b\u4e2d\uff0c\u4ee5\u63d0\u9ad8\u6d88\u606f\u5904\u7406\u7684\u5e76\u53d1\u6027\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5927\u91cf\u6d88\u606f\u6216\u5b9e\u73b0\u5e76\u884c\u5904\u7406\u903b\u8f91\u5f88\u6709\u7528\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u521b\u5efa\u591a\u4e2a\u6d88\u8d39\u8005\u5b9e\u4f8b\nConsumer&lt;String, String&gt; consumer1 = new KafkaConsumer&lt;&gt;(properties);\nConsumer&lt;String, String&gt; consumer2 = new KafkaConsumer&lt;&gt;(properties);\n\n\/\/ \u5728\u72ec\u7acb\u7684\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u6bcf\u4e2a\u6d88\u8d39\u8005\u5b9e\u4f8b\nnew Thread(() -&gt; {\n    while (true) {\n        ConsumerRecords&lt;String, String&gt; records = consumer1.poll(Duration.ofMillis(100));\n        \/\/ \u5904\u7406\u6d88\u606f\u903b\u8f91\n    }\n}).start();\n\nnew Thread(() -&gt; {\n    while (true) {\n        ConsumerRecords&lt;String, String&gt; records = consumer2.poll(Duration.ofMillis(100));\n        \/\/ \u5904\u7406\u6d88\u606f\u903b\u8f91\n    }\n}).start();\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u603b\u4f53\u800c\u8a00\uff0cKafka \u6d88\u8d39\u8005 API \u63d0\u4f9b\u4e86\u7075\u6d3b\u4e14\u529f\u80fd\u4e30\u5bcc\u7684\u5de5\u5177\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u5730\u6784\u5efa\u6d88\u8d39 Kafka \u4e3b\u9898\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5904\u7406\u5b9e\u65f6\u6d41\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kafka \u6d88\u8d39\u8005 API \u662f Kafka \u63d0\u4f9b\u7ed9\u5f00\u53d1\u8005\u7684\u4e00\u7ec4\u63a5\u53e3\u548c\u5de5\u5177\uff0c\u7528\u4e8e\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u4ee5\u6d88\u8d39 Kafka  [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[122],"tags":[],"class_list":["post-14565","post","type-post","status-publish","format-standard","hentry","category-kafka"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=14565"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14565\/revisions"}],"predecessor-version":[{"id":47971,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14565\/revisions\/47971"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=14565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=14565"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=14565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}