{"id":3755,"date":"2024-01-04T17:52:31","date_gmt":"2024-01-04T09:52:31","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/java\/kafka%e9%ab%98%e7%ba%a7%e5%ba%94%e7%94%a8%ef%bc%9a%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae%e5%a4%84%e7%90%86mq%e7%99%be%e4%b8%87%e7%ba%a7%e6%b6%88%e6%81%af%e9%98%9f%e5%88%97%ef%bc%9f\/"},"modified":"2024-01-10T14:39:06","modified_gmt":"2024-01-10T06:39:06","slug":"kafka%e9%ab%98%e7%ba%a7%e5%ba%94%e7%94%a8%ef%bc%9a%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae%e5%a4%84%e7%90%86mq%e7%99%be%e4%b8%87%e7%ba%a7%e6%b6%88%e6%81%af%e9%98%9f%e5%88%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/java\/kafka%e9%ab%98%e7%ba%a7%e5%ba%94%e7%94%a8%ef%bc%9a%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae%e5%a4%84%e7%90%86mq%e7%99%be%e4%b8%87%e7%ba%a7%e6%b6%88%e6%81%af%e9%98%9f%e5%88%97%ef%bc%9f\/","title":{"rendered":"Kafka\u9ad8\u7ea7\u5e94\u7528\uff1a\u5982\u4f55\u914d\u7f6e\u5904\u7406MQ\u767e\u4e07\u7ea7\u6d88\u606f\u961f\u5217\uff1f"},"content":{"rendered":"<div class='wxsyncmain'>\n<section data-tool=\"mdnice\u7f16\u8f91\u5668\" data-website=\"https:\/\/www.mdnice.com\" style=\"font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Roboto, Oxygen, Ubuntu, Cantarell, PingFangSC-regular, PingFangTC-regular, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif;\" data-mpa-powered-by=\"yiban.io\">\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\"><span style=\"color: rgb(0, 0, 0);\"><strong><span style=\"color: rgb(0, 0, 0);font-size: 17px;\">\u5927\u5bb6\u597d\uff0c\u6211\u662f\u9e4f\u78ca<\/span><\/strong><\/span><\/p>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u5728\u5927\u6570\u636e\u65f6\u4ee3\uff0cApache Kafka\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684\u5206\u5e03\u5f0f\u6d88\u606f\u961f\u5217\u7cfb\u7edf\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6d41\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u5728Kafka\u73af\u5883\u4e2d\u5904\u7406\u767e\u4e07\u7ea7\u6d88\u606f\u961f\u5217\u7684\u9ad8\u7ea7\u5e94\u7528\u6280\u5de7\u3002<\/p>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u672c\u6587\uff0c\u5df2\u6536\u5f55\u4e8e\uff0c\u6211\u7684\u6280\u672f\u7f51\u7ad9 ddkk.com\uff0c\u6709\u5927\u5382\u5b8c\u6574\u9762\u7ecf\uff0c\u5de5\u4f5c\u6280\u672f\uff0c\u67b6\u6784\u5e08\u6210\u957f\u4e4b\u8def\uff0c\u7b49\u7ecf\u9a8c\u5206\u4eab<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">1\u3001\u5408\u7406\u914d\u7f6e\u5206\u533a<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">\/\/\u81ea\u5b9a\u4e49\u5206\u533a\u7b56\u7565publicclassCustomPartitionerimplementsPartitioner{@Overridepublicintpartition(Stringtopic,Objectkey,byte[]keyBytes,Objectvalue,byte[]valueBytes,Clustercluster){\/\/\u6839\u636ekey\u5206\u914d\u5206\u533aintpartitionCount=cluster.partitionCountForTopic(topic);<span style=\"color: #a6e22e;line-height: 26px;\">return<\/span>(key.hashCode()&amp;Integer.MAX_VALUE)%partitionCount;}\/\/\u5176\u4ed6\u5fc5\u8981\u7684\u65b9\u6cd5\u5b9e\u73b0...}<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5206\u533a\u5668\u3002\u5b83\u6839\u636e\u6d88\u606f\u952e\u503c\u7684\u54c8\u5e0c\u503c\u5c06\u6d88\u606f\u5206\u914d\u5230\u4e0d\u540c\u7684\u5206\u533a\uff0c\u6709\u52a9\u4e8e\u5747\u8861\u8d1f\u8f7d\u548c\u63d0\u9ad8\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">2\u3001\u6d88\u606f\u6279\u91cf\u5904\u7406<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">Propertiesprops=newProperties();props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"bootstrap.servers\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"kafka-server1:9092,kafka-server2:9092\"<\/span>);props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"linger.ms\"<\/span>,10);\/\/\u6d88\u606f\u5ef6\u8fdf\u65f6\u95f4props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"batch.size\"<\/span>,16384);\/\/\u6279\u91cf\u5927\u5c0f\/\/\u521b\u5efa\u751f\u4ea7\u8005\u5b9e\u4f8bKafkaProducer&lt;String,String&gt;producer=newKafkaProducer&lt;&gt;(props);<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u901a\u8fc7<strong style=\"color: black;\">linger.ms<\/strong>\u548c<strong style=\"color: black;\">batch.size<\/strong>\u7684\u8bbe\u7f6e\uff0c\u751f\u4ea7\u8005\u53ef\u4ee5\u79ef\u7d2f\u4e00\u5b9a\u6570\u91cf\u7684\u6d88\u606f\u540e\u518d\u53d1\u9001\uff0c\u51cf\u5c11\u7f51\u7edc\u8bf7\u6c42\uff0c\u63d0\u9ad8\u541e\u5410\u91cf\u3002<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">3\u3001\u6d88\u606f\u538b\u7f29\u7b56\u7565<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"compression.type\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"snappy\"<\/span>);\/\/\u542f\u7528Snappy\u538b\u7f29\u7b97\u6cd5\/\/\u521b\u5efa\u751f\u4ea7\u8005\u5b9e\u4f8bKafkaProducer&lt;String,String&gt;producer=newKafkaProducer&lt;&gt;(props);<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u8fd9\u6bb5\u4ee3\u7801\u542f\u7528\u4e86Snappy\u538b\u7f29\u7b97\u6cd5\u3002\u6570\u636e\u538b\u7f29\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u6d88\u606f\u7684\u5927\u5c0f\uff0c\u63d0\u9ad8\u7f51\u7edc\u4f20\u8f93\u6548\u7387\u3002<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">4\u3001\u6d88\u8d39\u8005\u7fa4\u7ec4\u548c\u8d1f\u8f7d\u5747\u8861<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">PropertiesconsumerProps=newProperties();consumerProps.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"bootstrap.servers\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"kafka-server1:9092,kafka-server2:9092\"<\/span>);consumerProps.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"group.id\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"consumer-group-1\"<\/span>);\/\/\u6d88\u8d39\u8005\u7fa4\u7ec4consumerProps.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"key.deserializer\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"org.apache.kafka.common.serialization.StringDeserializer\"<\/span>);consumerProps.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"value.deserializer\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"org.apache.kafka.common.serialization.StringDeserializer\"<\/span>);\/\/\u521b\u5efa\u6d88\u8d39\u8005\u5b9e\u4f8bKafkaConsumer&lt;String,String&gt;consumer=newKafkaConsumer&lt;&gt;(consumerProps);<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u914d\u7f6e\u4e0d\u540c\u7684\u6d88\u8d39\u8005\u7fa4\u7ec4\uff08<strong style=\"color: black;\">group.id<\/strong>\uff09\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u548c\u9ad8\u6548\u7684\u6d88\u606f\u6d88\u8d39\u3002<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">5\u3001Kafka\u6d41\u5904\u7406<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">StreamsBuilderbuilder=newStreamsBuilder();KStream&lt;String,String&gt;kstream=builder.stream(<span style=\"color: #a6e22e;line-height: 26px;\">\"source-topic\"<\/span>);kstream.mapValues(value-&gt;<span style=\"color: #a6e22e;line-height: 26px;\">\"Processed:\"<\/span>+value).to(<span style=\"color: #a6e22e;line-height: 26px;\">\"destination-topic\"<\/span>);\/\/\u521b\u5efa\u5e76\u542f\u52a8KafkaStreams\u5e94\u7528KafkaStreamsstreams=newKafkaStreams(builder.build(),props);streams.start();<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528Kafka Streams API\u5b9e\u73b0\u4e86\u7b80\u5355\u7684\u6d41\u5904\u7406\u3002\u8fd9\u5141\u8bb8\u5bf9\u6570\u636e\u6d41\u8fdb\u884c\u5b9e\u65f6\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">6\u3001\u5e42\u7b49\u6027\u751f\u4ea7\u8005\u914d\u7f6e<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">Propertiesprops=newProperties();props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"bootstrap.servers\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"kafka-server1:9092,kafka-server2:9092\"<\/span>);props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"key.serializer\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"org.apache.kafka.common.serialization.StringSerializer\"<\/span>);props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"value.serializer\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"org.apache.kafka.common.serialization.StringSerializer\"<\/span>);props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"enable.idempotence\"<\/span>,<span style=\"color: #f92672;font-weight: bold;line-height: 26px;\">true<\/span>);\/\/\u542f\u7528\u5e42\u7b49\u6027\/\/\u521b\u5efa\u751f\u4ea7\u8005\u5b9e\u4f8bKafkaProducer&lt;String,String&gt;producer=newKafkaProducer&lt;&gt;(props);<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u901a\u8fc7\u8bbe\u7f6e<strong style=\"color: black;\">enable.idempotence<\/strong>\u4e3a<strong style=\"color: black;\">true<\/strong>\uff0c\u53ef\u4ee5\u786e\u4fdd\u751f\u4ea7\u8005\u5373\u4f7f\u5728\u7f51\u7edc\u6ce2\u52a8\u7b49\u60c5\u51b5\u4e0b\u4e5f\u4e0d\u4f1a\u4ea7\u751f\u91cd\u590d\u6570\u636e\u3002<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">7\u3001\u6d88\u8d39\u8005\u504f\u79fb\u91cf\u7ba1\u7406<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">consumerProps.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"enable.auto.commit\"<\/span>,<span style=\"color: #f92672;font-weight: bold;line-height: 26px;\">false<\/span>);\/\/\u5173\u95ed\u81ea\u52a8\u63d0\u4ea4\u504f\u79fb\u91cfKafkaConsumer&lt;String,String&gt;consumer=newKafkaConsumer&lt;&gt;(consumerProps);\/\/\u5728\u5e94\u7528\u903b\u8f91\u4e2d\u624b\u52a8\u63d0\u4ea4\u504f\u79fb\u91cf<span style=\"color: #f92672;font-weight: bold;line-height: 26px;\">while<\/span>(<span style=\"color: #f92672;font-weight: bold;line-height: 26px;\">true<\/span>){ConsumerRecords&lt;String,String&gt;records=consumer.poll(Duration.ofMillis(100));<span style=\"color: #f92672;font-weight: bold;line-height: 26px;\">for<\/span>(ConsumerRecord&lt;String,String&gt;record:records){\/\/\u5904\u7406\u6d88\u606f\/\/...\/\/\u624b\u52a8\u63d0\u4ea4\u504f\u79fb\u91cfconsumer.commitSync();}}<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u5173\u95ed\u81ea\u52a8\u63d0\u4ea4\u5e76\u624b\u52a8\u63a7\u5236\u504f\u79fb\u91cf\u7684\u63d0\u4ea4\uff0c\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u63a7\u5236\u6d88\u606f\u7684\u6d88\u8d39\u72b6\u6001\uff0c\u907f\u514d\u6d88\u606f\u4e22\u5931\u6216\u91cd\u590d\u6d88\u8d39\u3002<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">8\u3001\u4f7f\u7528Kafka Connect\u96c6\u6210\u5916\u90e8\u7cfb\u7edf<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">\/\/KafkaConnect\u914d\u7f6e\u793a\u4f8b\uff08\u901a\u5e38\u4e3aJSON\u683c\u5f0f\uff09{<span style=\"color: #a6e22e;line-height: 26px;\">\"name\"<\/span>:<span style=\"color: #a6e22e;line-height: 26px;\">\"my-connector\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"config\"<\/span>:{<span style=\"color: #a6e22e;line-height: 26px;\">\"connector.class\"<\/span>:<span style=\"color: #a6e22e;line-height: 26px;\">\"io.confluent.connect.jdbc.JdbcSinkConnector\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"tasks.max\"<\/span>:<span style=\"color: #a6e22e;line-height: 26px;\">\"1\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"topics\"<\/span>:<span style=\"color: #a6e22e;line-height: 26px;\">\"my-topic\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"connection.url\"<\/span>:<span style=\"color: #a6e22e;line-height: 26px;\">\"jdbc:mysql:\/\/localhost:3306\/mydb\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"key.converter\"<\/span>:<span style=\"color: #a6e22e;line-height: 26px;\">\"org.apache.kafka.connect.json.JsonConverter\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"value.converter\"<\/span>:<span style=\"color: #a6e22e;line-height: 26px;\">\"org.apache.kafka.connect.json.JsonConverter\"<\/span>,\/\/\u66f4\u591a\u914d\u7f6e...}}<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u914d\u7f6eKafka Connect\u6765\u8fde\u63a5\u5916\u90e8\u7cfb\u7edf\uff08\u5982\u6570\u636e\u5e93\uff09\u3002Kafka Connect\u662f\u4e00\u79cd\u6d41\u884c\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u5728Kafka\u548c\u5176\u4ed6\u7cfb\u7edf\u4e4b\u95f4\u9ad8\u6548\u5730\u4f20\u8f93\u6570\u636e\u3002<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">9\u3001Kafka\u5b89\u5168\u914d\u7f6e<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"security.protocol\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"SSL\"<\/span>);props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"ssl.truststore.location\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"\/var\/private\/ssl\/kafka.client.truststore.jks\"<\/span>);props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"ssl.truststore.password\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"test1234\"<\/span>);props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"ssl.keystore.location\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"\/var\/private\/ssl\/kafka.client.keystore.jks\"<\/span>);props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"ssl.keystore.password\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"test1234\"<\/span>);props.put(<span style=\"color: #a6e22e;line-height: 26px;\">\"ssl.key.password\"<\/span>,<span style=\"color: #a6e22e;line-height: 26px;\">\"test1234\"<\/span>);\/\/\u521b\u5efa\u5b89\u5168\u7684\u751f\u4ea7\u8005\u6216\u6d88\u8d39\u8005\u5b9e\u4f8bKafkaProducer&lt;String,String&gt;producer=newKafkaProducer&lt;&gt;(props);<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u914d\u7f6eSSL\/TLS\u53ef\u4ee5\u4e3aKafka\u901a\u4fe1\u589e\u52a0\u52a0\u5bc6\u5c42\uff0c\u63d0\u9ad8\u6570\u636e\u4f20\u8f93\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 18px;\"><span style=\"display: none;\"><\/span><strong style=\"color: black;\">10\u3001Kafka\u76d1\u63a7\u4e0e\u8fd0\u7ef4<\/strong><\/h2>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;padding: 16px;background: #272822;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;border-radius: 0px;-webkit-overflow-scrolling: touch;font-size: 12px;\">\/\/Kafka\u76d1\u63a7\u7684\u4f2a\u4ee3\u7801\u793a\u4f8bMonitormonitor=newKafkaMonitor(kafkaServers);monitor.on(<span style=\"color: #a6e22e;line-height: 26px;\">\"event\"<\/span>,event-&gt;{<span style=\"color: #f92672;font-weight: bold;line-height: 26px;\">if<\/span>(event.type==EventType.BROKER_DOWN){alert(<span style=\"color: #a6e22e;line-height: 26px;\">\"Brokerdown:\"<\/span>+event.brokerId);}\/\/\u5176\u4ed6\u4e8b\u4ef6\u5904\u7406...});monitor.start();<\/code><\/pre>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u867d\u7136\u8fd9\u662f\u4e00\u4e2a\u4f2a\u4ee3\u7801\u793a\u4f8b\uff0c\u4f46\u5b83\u5c55\u793a\u4e86\u5982\u4f55\u76d1\u63a7Kafka\u96c6\u7fa4\u7684\u5173\u952e\u4e8b\u4ef6\uff08\u5982Broker\u5b95\u673a\uff09\uff0c\u5e76\u6839\u636e\u9700\u8981\u91c7\u53d6\u76f8\u5e94\u7684\u54cd\u5e94\u63aa\u65bd\u3002\u5728\u5b9e\u9645\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u76d1\u63a7\u5de5\u5177\u548c\u670d\u52a1\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 17px;\"><span style=\"display: none;\"><\/span>\u672c\u6587\u603b\u7ed3<span style=\"display: none;\"><\/span><\/h3>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">Kafka\u5728\u5904\u7406\u5927\u89c4\u6a21\u3001\u9ad8\u541e\u5410\u91cf\u7684\u6d88\u606f\u961f\u5217\u65b9\u9762\u6709\u7740\u7a81\u51fa\u7684\u6027\u80fd\u3002\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u5206\u533a\u3001\u4f18\u5316\u6279\u91cf\u5904\u7406\u3001\u5e94\u7528\u6d88\u606f\u538b\u7f29\u3001\u8bbe\u7f6e\u6d88\u8d39\u8005\u7fa4\u7ec4\u548c\u5229\u7528\u6d41\u5904\u7406\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8Kafka\u5904\u7406\u767e\u4e07\u7ea7\u6d88\u606f\u961f\u5217\u7684\u80fd\u529b\u3002\u5f53\u7136\uff0c\u8fd9\u4e9b\u6280\u5de7\u7684\u5e94\u7528\u9700\u8981\u7ed3\u5408\u5177\u4f53\u7684\u4e1a\u52a1\u573a\u666f\u548c\u73af\u5883\u6765\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: rgb(255, 169, 0);font-size: 17px;\"><span style=\"display: none;\"><\/span>\u6c42\u4e00\u952e\u4e09\u8fde\uff1a\u70b9\u8d5e\u3001\u5206\u4eab\u3001\u6536\u85cf<span style=\"display: none;\"><\/span><\/h3>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u70b9\u8d5e\u5bf9\u6211\u771f\u7684\u975e\u5e38\u91cd\u8981\uff01\u5728\u7ebf\u6c42\u8d5e\uff0c\u52a0\u4e2a\u5173\u6ce8\u6211\u4f1a\u975e\u5e38\u611f\u6fc0\uff01<\/p>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px;font-size: 15px;color: rgb(74, 74, 74);font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;\">\u672c\u6587\uff0c\u5df2\u6536\u5f55\u4e8e\uff0c\u6211\u7684\u6280\u672f\u7f51\u7ad9 ddkk.com\uff0c\u6709\u5927\u5382\u5b8c\u6574\u9762\u7ecf\uff0c\u5de5\u4f5c\u6280\u672f\uff0c\u67b6\u6784\u5e08\u6210\u957f\u4e4b\u8def\uff0c\u7b49\u7ecf\u9a8c\u5206\u4eab<\/p>\n<\/section>\n<p style=\"display: none;\"><mp-style-type data-value=\"3\"><\/mp-style-type><\/p>\n<blockquote>\n<p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7:                 \u7a0b\u5e8f\u5458\u9e4f\u78ca              <\/p>\n<\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u9e4f\u78ca \u5728\u5927\u6570\u636e\u65f6\u4ee3\uff0cApache Kafka\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684\u5206\u5e03\u5f0f\u6d88\u606f\u961f\u5217\u7cfb\u7edf\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6d41\u3002\u672c\u6587\u5c06\u6df1\u5165 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":3753,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[2,16,7],"tags":[],"class_list":["post-3755","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-kafka","category-spring-boot"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/01\/20240110143636576-150x150.png","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/01\/20240110143636576-300x167.png","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/01\/20240110143636576-1024x569.png","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/01\/20240110143636576-300x167.png","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/01\/20240110143636576-1024x569.png","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/01\/20240110143636576.png","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Java","post_date":"2024-01-04","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":1151,"next_post_id":3736,"next_post_title":"\u4e8b\u52a1+\u9501\uff0c\u9501+\u4e8b\u52a1\uff0c\u89e3\u6790\u4e8b\u52a1\u4e0e\u9501\u7684\u9519\u7efcBug","previous_post_id":3724,"previous_post_title":"Java + lua = \u738b\u70b8\uff01\uff01","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3755"}],"version-history":[{"count":1,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3755\/revisions"}],"predecessor-version":[{"id":3759,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3755\/revisions\/3759"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/3753"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}