我正在尝试一个简单的kafka producer-consumer客户机api,我的producer类工作得很好,因为我可以从控制台看到consumer中的消息,但是当我运行消费代码时,什么都没有显示,我不知道是什么问题或者我在哪里出错
这是生产商代码-
package com.app.test;
import java.util.Properties;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
public class SimpleProducer {
private static Producer<Integer, String> producer;
private final Properties props = new Properties();public SimpleProducer()
{
props.put("metadata.broker.list", "localhost:9092");
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("request.required.acks", "1");
producer = new Producer<Integer, String>(new ProducerConfig(props));
}
public static void main(String[] args) {
SimpleProducer sp = new SimpleProducer();
String topic = "test4";
String messageStr = "hello";
KeyedMessage<Integer, String> data = new KeyedMessage<Integer, String>(topic, messageStr);
System.out.println("producer : "+producer);
producer.send(data);
producer.close();
}
}
消费者类别是-
package com.app.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
public class SimpleHLConsumer {
private final ConsumerConnector consumer;
private final String topic;
public SimpleHLConsumer(String zookeeper, String groupId, String topic) {
Properties props = new Properties();
props.put("zookeeper.connect", zookeeper);
props.put("group.id", groupId);
props.put("zookeeper.session.timeout.ms", "500");
props.put("zookeeper.sync.time.ms", "250");
props.put("auto.commit.interval.ms", "1000");
consumer = Consumer.createJavaConsumerConnector(
new ConsumerConfig(props));
this.topic = topic;
}
public void testConsumer() {
Map<String, Integer> topicCount = new HashMap<String, Integer>();
// Define single thread for topic
topicCount.put(topic, new Integer(1));
System.out.println("check1");
Map<String, List<KafkaStream<byte[], byte[]>>> consumerStreams = consumer.createMessageStreams(topicCount);
System.out.println("check2");
List<KafkaStream<byte[], byte[]>> streams = consumerStreams.get(topic);
for (KafkaStream stream : streams) {
System.out.println("test----");
System.out.println("test----"+stream.toString());
ConsumerIterator<byte[], byte[]> consumerIte = stream.iterator();
while (consumerIte.hasNext()) {
try {
System.out.println("Message from Single Topic: " + new String(consumerIte.next().message(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if (consumer != null)
consumer.shutdown();
}
public static void main(String[] args) {
String topic = "test4";
SimpleHLConsumer simpleHLConsumer = new SimpleHLConsumer("localhost:2181", "testgroup", topic);
simpleHLConsumer.testConsumer();
}
}
对于检查,我已经在testconsumer()方法中使用sysout应用了2个检查,所以当运行时只显示check1,即代码没有到达check2,我认为有一些问题 consumer.createMessageStreams(topicCount);
,那么原因是什么,我该如何解决呢?
1条答案
按热度按时间nlejzf6q1#
代码中没有问题。
只需在jar中构建包含kafka版本(您正在使用的kafka版本)的jar并运行它,然后尝试从producer控制台发送消息。
希望这能帮到你。