kafka生产者代码失败EndMessageException

0ejtzxu1  于 2021-06-08  发布在  Kafka
关注(0)|答案(2)|浏览(401)

使用Kafka生成消息

import java.util.Date;
import java.util.Properties;

import kafka.common.FailedToSendMessageException;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

public class KafkaProducer {

    private static Producer<String, String> producer;
    public KafkaProducer()
    {
        Properties props = new Properties();
        props.put("metadata.broker.list","localhost:2181");
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        props.put("request.required.acks","1");

        ProducerConfig config = new ProducerConfig(props);
        producer = new Producer<String,String>(config);

    }

    public static void main(String[] args)
    {
        if(args.length<2)
        {
            System.err.println("Usage: KafkaProducer TopicName MessageCount");
            System.exit(0);
        }
        String topic = args[0];
        int messageCount = Integer.parseInt(args[1]);

        KafkaProducer kafka = new KafkaProducer();
        kafka.publishMessage(topic,messageCount);

    }

    private void publishMessage(String topic, int messageCount)
    {
        for(int mcount=0;mcount<messageCount;mcount++)
        {
            String runtime = new Date().toString();
            String msg = "Message Published Time -" + runtime;
            System.out.println(msg);

            KeyedMessage<String,String> data = new KeyedMessage<String,String>(topic,msg);
            producer.send(data);

        }
        producer.close();
    }

}

使用eclipse运行此程序时,出现以下异常:

log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Message Published Time -Fri Jul 10 13:05:20 IST 2015
Exception in thread "main" kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
    at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:90)
    at kafka.producer.Producer.send(Producer.scala:77)
    at kafka.javaapi.producer.Producer.send(Producer.scala:33)
    at KafkaProducer.publishMessage(KafkaProducer.java:52)
    at KafkaProducer.main(KafkaProducer.java:39)

zookeeper服务已启动,代理已启动,主题已创建。消费者也准备好了。
有人能帮我解决这个问题吗?

wwwo4jvm

wwwo4jvm1#

请检查您的kafka服务器是否在producer api使用的同一端口上运行?
Kafka集群通常在端口9092上运行。如果您的设置是这种情况,请在producer配置中使用相同的端口。您的生产商正在使用端口2181。可能这是个错误。

56lgkhnf

56lgkhnf2#

您可以尝试使用Kafka文档中简要描述的新Kafka制作人。
便笺导入 org.apache.kafka.clients.producer.* 而不是 kafka.javaapi.producer.Producer 比如:

import java.util.Properties;
import java.util.concurrent.ExecutionException;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

public class KafkaProducerTest {

    public static void main(String args[]) throws InterruptedException, ExecutionException {
        // set up Kafka producer
        KafkaProducer<String,String> kafkaProducer;
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        // instantiate the producer
        kafkaProducer = new KafkaProducer<String,String>(props);

        // add data to kafka
        ProducerRecord<String,String> producerRecord = new ProducerRecord<String,String>("topic", "test key", "test value");
        kafkaProducer.send(producerRecord);

        // close producer
        kafkaProducer.close();
    }
}

相关问题