简体   繁体   English

Kafka服务器包含消息,但使用者无法接收任何消息

[英]Kafka server contains messages but consumer cannot receive any

Using a producer I entered some messages inside Kafka server which is running on localhost. 我使用生产者在运行于localhost的Kafka服务器内部输入了一些消息。 Zookeeper is also on localhost. Zookeeper也在本地主机上。 I have used ConsumerGroupExample as given here . 我使用了此处给出的ConsumerGroupExample

However, the consumer does not seem to receive any message! 但是,消费者似乎没有收到任何消息! the kafka-console-consumer.sh script can pull out all those messages, but the code cannot. kafka-console-consumer.sh脚本可以提取所有这些消息,但是代码不能。 What is wrong? 怎么了? The Consumer code is exactly as it is given on that page. 使用者代码与该页面上给出的完全相同。

zookeeper="localhost:2181", group id = "test-a", topic = "test". 

This is the same topic on which I published messages. 这是我发布消息的同一主题。 Here's code for the producer: 这是生产者的代码:

package test;

import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
import java.util.Properties;
import kafka.javaapi.producer.Producer;

public class KakfaProducer {
    public static void main(String[] args) {
        Properties prop = new Properties();
        prop.put("zookeeper.connect", "localhost:2181");
        prop.put("metadata.broker.list", "localhost:9092");
        prop.put("serializer.class", "kafka.serializer.StringEncoder");
        ProducerConfig producerConfig = new ProducerConfig(prop);
        Producer<String, String> producer = new<String, String> Producer(producerConfig);
        Task t = new Task(producer);
        Thread newThread = new Thread(t);
        newThread.start();
    }
}

class Task implements Runnable {
    Producer<String, String> producer;

    public Task(Producer producer) {
        this.producer = producer;
    }

    public void run() {
        long num = 1;
        while (true) {
            String topic = "test";
            KeyedMessage<String, String> message = new<String, String> KeyedMessage(topic, "Hello Test message " + num);
            producer.send(message);
            synchronized (this) {
                num++;
            }
            if (num % 1000 == 0) {
                System.out.println("Total messages sent by Thread-" + Thread.currentThread().getId() + num);
            }
        }
    }
}

Try changing your group.id to something else and test. 尝试将group.id更改为其他内容并进行测试。

When you used kafka-console-conumser.sh to test your consumer, it consumed data from the topic and changed an offset. 当您使用kafka-console-conumser.sh测试您的使用者时,它消耗了主题中的数据并更改了偏移量。 That's why your Consumer code couldn't consume with the same group.id . 这就是为什么您的使用者代码无法与同一group.id一起使用的group.id

Also, when you change your group.id , add to your Consumer : 另外,当您更改group.id ,将其添加到使用者:

props.put("auto.offset.reset", "earliest");

This will make sure that your new consumer group consumes data from the beginning. 这将确保您的新使用者组从一开始就使用数据。

Your consumer didn't receive any data : 您的消费者未收到任何数据:

  1. You already used the same group.id to consume before, and it has a new offset. 您之前已经使用了相同的group.id进行消费,并且具有新的偏移量。 It will consume only when there is new data in your Broker. 仅当您的Broker中有新数据时,它才会消耗。 (Change the group.id and test again.) (更改group.id并再次测试。)

  2. Try auto.offset.reset=earliest . 尝试使用auto.offset.reset=earliest The consumer will read messages from the beginning, and you can use this for testing your consumer. 使用者将从头开始阅读消息,您可以使用它来测试您的使用者。 There are also ways to read from a certain offset. 也有从特定偏移量读取的方法。 seek() , seekToBeginning() , and seekToEnd() . seek()seekToBeginning()seekToEnd()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kafka Spark Streaming Consumer是否不会从Kafka Console Producer接收任何消息? - Kafka Spark Streaming Consumer will not receive any messages from Kafka Console Producer? Spring Kafka Consumer有时会停止接收消息 - Spring Kafka Consumer Sometime stop receive messages 为什么这个使用者(在servlet上运行)为什么没有收到来自kafka主题的消息? - Why does this consumer (running on a servlet) not receive any messages from the kafka topic? 重启Kafka生产者/消费者后,消费者未收到消息 - Consumer does not receive messages after kafka producer/consumer restart Kafka Java使用者永远不会收到任何消息 - Kafka Java consumer never receives any messages 运行在不同计算机上的Kafka用户组可以接收唯一消息吗? - Can Kafka consumer group running on different machines receive unique messages? Spring-Kafka消费者未收到消息 - Spring-Kafka consumer doesn't receive messages Spring-Kafka消费者不会自动接收消息 - Spring-Kafka consumer doesn't receive messages automatically Java Kafka Consumer 使用新的 Consumer Group ID 可以正常工作,但是在关闭并重新启动时它不会消耗任何消息 - Java Kafka Consumer works fine with new Consumer Group ID but when shutdown and restarted it does not consume any messages 卡夫卡消费者没有收到旧消息 - Kafka consumer not receiving old messages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM