使用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服务已启动,代理已启动,主题已创建。消费者也准备好了。
有人能帮我解决这个问题吗?
2条答案
按热度按时间wwwo4jvm1#
请检查您的kafka服务器是否在producer api使用的同一端口上运行?
Kafka集群通常在端口9092上运行。如果您的设置是这种情况,请在producer配置中使用相同的端口。您的生产商正在使用端口2181。可能这是个错误。
56lgkhnf2#
您可以尝试使用Kafka文档中简要描述的新Kafka制作人。
便笺导入
org.apache.kafka.clients.producer.*
而不是kafka.javaapi.producer.Producer
比如: