我是 Kafka 的新手,正在运行一个简单的 Kafka 消费者/生产者示例,如 KafkaConsumer 上给出的那样和 KafkaProducer .当我从终端运行消费者时,消费者正在接收消息,但我无法使用 Java 代码收听。
我也在 StackoverFlow 上搜索了类似的问题(链接:Link1、Link2)并尝试了这些解决方案,但似乎对我没有任何作用。
Kafka版本:kafka_2.10-0.10.2.1
,pom中使用了对应的maven依赖。
生产者和消费者的 Java 代码:
public class SimpleProducer {
public static void main(String[] args) throws InterruptedException {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9094");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
for(int i = 0; i < 10; i++)
producer.send(new ProducerRecord<String, String>("topic3", Integer.toString(i), Integer.toString(i)));
producer.close();
}}
public class SimpleConsumer {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9094");
props.put("group.id", "test");
props.put("zookeeper.connect", "localhost:2181");
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("topic3", "topic2"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records)
System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
}
}}
启动卡夫卡:
bin/kafka-server-start.sh config/server.properties
(我已经在属性文件中设置了端口,brokerid)
最佳答案
首先检查所有可用的组:
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
然后使用以下命令检查您的主题属于哪个组:
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group <your group name> --describe
一旦你找到你的主题和相关的组名(如果它不属于默认组,只需将 group.id 替换为你的组)然后尝试使用下面的 prop 并让我知道它是否有效:
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-consumer-group"); // default topic name
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
props.put("session.timeout.ms", "30000");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
//Kafka Consumer subscribes list of topics here.
consumer.subscribe(Arrays.asList(topicName)); // replace you topic name
//print the topic name
java.util.Map<String,java.util.List<PartitionInfo>> listTopics = consumer.listTopics();
System.out.println("list of topic size :" + listTopics.size());
for(String topic : listTopics.keySet()){
System.out.println("topic name :"+topic);
}
关于java - 简单的 Kafka 消费者没有收到消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44760866/