{"id":14661,"date":"2023-12-04T15:59:51","date_gmt":"2023-12-04T07:59:51","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=14661"},"modified":"2023-12-13T14:27:16","modified_gmt":"2023-12-13T06:27:16","slug":"kafkaproducer-ru-he-you-hua-xie-ru-su-du","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/04\/kafkaproducer-ru-he-you-hua-xie-ru-su-du\/","title":{"rendered":"KafkaProducer\u5982\u4f55\u4f18\u5316\u5199\u5165\u901f\u5ea6\uff1f"},"content":{"rendered":"<p>KafkaProducer \u7684\u5199\u5165\u901f\u5ea6\uff08\u541e\u5410\u91cf\uff09\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u4f18\u5316\u7b56\u7565\u548c\u914d\u7f6e\u6765\u63d0\u9ad8\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316 KafkaProducer \u5199\u5165\u901f\u5ea6\u7684\u5e38\u89c1\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<strong>\u6279\u91cf\u53d1\u9001\uff1a<\/strong> \u4f7f\u7528 <code>producer.send()<\/code> \u65b9\u6cd5\u53d1\u9001\u6d88\u606f\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u6279\u91cf\u53d1\u9001\u6d88\u606f\u800c\u4e0d\u662f\u9010\u4e2a\u53d1\u9001\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5c06\u6d88\u606f\u653e\u5165 ProducerRecord \u5bf9\u8c61\u7684\u5217\u8868\uff0c\u7136\u540e\u4e00\u6b21\u6027\u8c03\u7528 <code>producer.send(records)<\/code> \u6765\u5b9e\u73b0\u3002\u6279\u91cf\u53d1\u9001\u53ef\u4ee5\u51cf\u5c11\u7f51\u7edc\u5f00\u9500\uff0c\u63d0\u9ad8\u541e\u5410\u91cf\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u6279\u91cf\u53d1\u9001\u6d88\u606f\nList&lt;ProducerRecord&lt;String, String&gt;&gt; records = new ArrayList&lt;&gt;();\nfor (int i = 0; i &lt; 1000; i++) {\n    ProducerRecord&lt;String, String&gt; record = new ProducerRecord&lt;&gt;(&quot;my-topic&quot;, &quot;key&quot;, &quot;value-&quot; + i);\n    records.add(record);\n}\nproducer.send(records);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5f02\u6b65\u53d1\u9001\uff1a<\/strong> \u4f7f\u7528 <code>producer.send()<\/code> \u65b9\u6cd5\u53d1\u9001\u6d88\u606f\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5f02\u6b65\u53d1\u9001\uff0c\u5373\u4e0d\u7b49\u5f85\u6d88\u606f\u53d1\u9001\u7684\u786e\u8ba4\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <code>producer.send(...).get()<\/code> \u800c\u4e0d\u662f <code>producer.send(...)<\/code> \u6765\u5b9e\u73b0\u3002\u5f02\u6b65\u53d1\u9001\u53ef\u4ee5\u63d0\u9ad8\u751f\u4ea7\u8005\u7684\u5e76\u53d1\u6027\u548c\u6027\u80fd\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u5f02\u6b65\u53d1\u9001\u6d88\u606f\nFuture&lt;RecordMetadata&gt; future = producer.send(new ProducerRecord&lt;&gt;(&quot;my-topic&quot;, &quot;key&quot;, &quot;value&quot;));\nRecordMetadata metadata = future.get(); \/\/ \u963b\u585e\u7b49\u5f85\u786e\u8ba4\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u538b\u7f29\u6d88\u606f\uff1a<\/strong> \u5982\u679c\u7f51\u7edc\u5e26\u5bbd\u6709\u9650\uff0c\u53ef\u4ee5\u8003\u8651\u542f\u7528\u6d88\u606f\u538b\u7f29\uff0c\u5c06\u6d88\u606f\u5728\u751f\u4ea7\u8005\u7aef\u538b\u7f29\u540e\u518d\u53d1\u9001\u3002Kafka \u652f\u6301\u591a\u79cd\u538b\u7f29\u7b97\u6cd5\uff0c\u5305\u62ecGzip\u548cSnappy\u3002\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e <code>compression.type<\/code> \u6765\u542f\u7528\u6d88\u606f\u538b\u7f29\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u542f\u7528\u6d88\u606f\u538b\u7f29\nProperties props = new Properties();\nprops.put(&quot;bootstrap.servers&quot;, &quot;localhost:9092&quot;);\nprops.put(&quot;key.serializer&quot;, &quot;org.apache.kafka.common.serialization.StringSerializer&quot;);\nprops.put(&quot;value.serializer&quot;, &quot;org.apache.kafka.common.serialization.StringSerializer&quot;);\nprops.put(&quot;compression.type&quot;, &quot;gzip&quot;); \/\/ \u6216 &quot;snappy&quot;\nKafkaProducer&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u8c03\u6574 <code>batch.size<\/code> \u548c <code>linger.ms<\/code>\uff1a<\/strong> \u901a\u8fc7\u8c03\u6574 <code>batch.size<\/code> \u548c <code>linger.ms<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u6d88\u606f\u7684\u6279\u91cf\u53d1\u9001\u548c\u7b49\u5f85\u65f6\u95f4\u3002\u8f83\u5927\u7684 <code>batch.size<\/code> \u548c\u8f83\u957f\u7684 <code>linger.ms<\/code> \u53ef\u4ee5\u589e\u52a0\u6bcf\u6b21\u53d1\u9001\u7684\u6d88\u606f\u91cf\uff0c\u63d0\u9ad8\u541e\u5410\u91cf\uff0c\u4f46\u540c\u65f6\u4e5f\u4f1a\u589e\u52a0\u5ef6\u8fdf\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u8c03\u6574 batch.size \u548c linger.ms\nprops.put(&quot;batch.size&quot;, 16384); \/\/ \u9ed8\u8ba4\u503c\u4e3a 16384 \u5b57\u8282\nprops.put(&quot;linger.ms&quot;, 1); \/\/ \u9ed8\u8ba4\u503c\u4e3a 0 \u6beb\u79d2\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u9002\u5f53\u8bbe\u7f6e <code>acks<\/code> \u53c2\u6570\uff1a<\/strong> <code>acks<\/code> \u53c2\u6570\u63a7\u5236\u751f\u4ea7\u8005\u5728\u53d1\u9001\u6d88\u606f\u540e\u7b49\u5f85\u591a\u5c11\u4e2a\u526f\u672c\u6536\u5230\u786e\u8ba4\u3002\u8f83\u5c0f\u7684 <code>acks<\/code> \u53ef\u4ee5\u63d0\u9ad8\u541e\u5410\u91cf\uff0c\u4f46\u53ef\u80fd\u4f1a\u964d\u4f4e\u53ef\u9760\u6027\u3002\u9002\u5f53\u7684\u914d\u7f6e\u53d6\u51b3\u4e8e\u5bf9\u6570\u636e\u53ef\u9760\u6027\u7684\u8981\u6c42\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u8bbe\u7f6e acks \u53c2\u6570\nprops.put(&quot;acks&quot;, &quot;1&quot;); \/\/ \u53ef\u9009\u503c\uff1a&quot;0&quot;\u3001&quot;1&quot;\u3001&quot;all&quot;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528\u5f02\u6b65\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570\uff1a<\/strong> \u5f53\u53d1\u9001\u6d88\u606f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <code>producer.send(..., callback)<\/code> \u4e2d\u7684\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u53d1\u9001\u7ed3\u679c\u3002\u8fd9\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u5904\u7406\u53d1\u9001\u7ed3\u679c\uff0c\u4ece\u800c\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u5f02\u6b65\u53d1\u9001\u6d88\u606f\u5e76\u5904\u7406\u56de\u8c03\nproducer.send(new ProducerRecord&lt;&gt;(&quot;my-topic&quot;, &quot;key&quot;, &quot;value&quot;), (metadata, exception) -&gt; {\n    if (exception != null) {\n        System.err.println(&quot;Error sending message: &quot; + exception.getMessage());\n    } else {\n        System.out.println(&quot;Message sent to partition &quot; + metadata.partition() +\n                           &quot; with offset &quot; + metadata.offset());\n    }\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u8fdb\u884c\u7075\u6d3b\u8c03\u6574\uff0c\u4ee5\u4f18\u5316 KafkaProducer \u7684\u5199\u5165\u901f\u5ea6\u3002\u5728\u8fdb\u884c\u4f18\u5316\u65f6\uff0c\u5efa\u8bae\u901a\u8fc7\u76d1\u63a7\u548c\u6027\u80fd\u6d4b\u8bd5\u6765\u9a8c\u8bc1\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>KafkaProducer \u7684\u5199\u5165\u901f\u5ea6\uff08\u541e\u5410\u91cf\uff09\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u4f18\u5316\u7b56\u7565\u548c\u914d\u7f6e\u6765\u63d0\u9ad8\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316 KafkaP [&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-14661","post","type-post","status-publish","format-standard","hentry","category-kafka"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14661","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=14661"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14661\/revisions"}],"predecessor-version":[{"id":48023,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14661\/revisions\/48023"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=14661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=14661"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=14661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}