本文介绍了KafkaProducer 未成功将消息发送到队列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Windows PC 上搭建了一个小型测试环境,并写下以下代码来测试 kafka(使用来自 org.apache.kafka 的 kafka_2.10:0.9.0.1).

I have built a small testing environment on my Windows PC and write down the following code for testing kafka (using kafka_2.10:0.9.0.1 from org.apache.kafka).

package iii.functiontesting;

import java.text.ParseException;
import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;


/**
 * Hello world!
 *
 */
public class test4
{
    public static void main( String[] args ) throws ParseException
    {
        Properties producerProps=new Properties();
        producerProps.put("bootstrap.servers", "localhost:9092");
        producerProps.put("serializer.class",org.apache.kafka.common.serialization.StringSerializer.class.getName());
        producerProps.put("key.serializer",org.apache.kafka.common.serialization.StringSerializer.class.getName());
        producerProps.put("value.serializer",org.apache.kafka.common.serialization.StringSerializer.class.getName());
        producerProps.put("request.required.acks","1");
        KafkaProducer<String,String> kafkawriter= new KafkaProducer<String,String>(producerProps);
        ProducerRecord<String,String> msg=new ProducerRecord<>("TEST3","ImKey","teststring1");
        kafkawriter.send(msg);
    }
}

我用下面的命令检查消息是否正确写入队列

I use the following command to check whether the message is correctly written into the queue

D:\Work\kafkaenv\kafka_2.10-0.9.0.1\bin\windows>.\kafka-console-consumer.bat--zookeeper localhost:2181 --topic TEST3 --from-beginning

然而,我发现 kafka-console-consumer 什么也没显示.

However, I found that the kafka-console-consumer shows nothing.

我怀疑我的kafka服务器运行不正常,所以我使用console-producer来测试.

I have doubted that my kafka server doesn't run properly, so I use console-producer to test.

D:\Work\kafkaenv\kafka_2.10-0.9.0.1\bin\windows>.\kafka-console-producer.bat--broker-list 本地主机:9092 --topic TEST3

啊啊啊

这次我可以看到控制台消费者下清楚地显示了aaaaa.我无法弄清楚会发生什么.有人可以帮我吗?

This time I can see the aaaaa is clearly shown under the console-consumer.I cannot figure out what happens.Can anyone help me?

推荐答案

您必须在终止程序之前调用 KafkaProducer#flush [或] KafkaProducer#close 方法.

You have to call either KafkaProducer#flush [or] KafkaProducer#close method before terminating the program.

实际上,生产者在将记录发送给代理之前会对其进行缓冲.请参阅 buffer.memory 和 batch.size>Kafka Producer 配置

Actually, the producer buffers the records before sending it to the broker. See buffer.memory and batch.size in the Kafka Producer configuration

kafkawriter.send(msg);
kafkawriter.close();

这篇关于KafkaProducer 未成功将消息发送到队列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 06:43