用户在apache kafka中未接收消息

f0brbegy  于 2021-06-07  发布在  Kafka
关注(0)|答案(2)|浏览(395)

我正在构建一个apachekafka消费者,以订阅另一个已经运行的kafka。现在,我的问题是,当我的生产者将消息推送到服务器时,我的消费者并没有收到它们。这里我给出了制片人代码,

Properties properties = new Properties();
        properties.put("metadata.broker.list","Running kafka ip addr:9092");
        properties.put("serializer.class","kafka.serializer.StringEncoder");
        ProducerConfig producerConfig = new ProducerConfig(properties);
        kafka.javaapi.producer.Producer<String,String> producer = new kafka.javaapi.producer.Producer<String, String>(producerConfig);
        String filePath="filepath";
        File rootFile= new File(filePath);
        Collection<File> allFiles = FileUtils.listFiles(rootFile, CanReadFileFilter.CAN_READ, TrueFileFilter.INSTANCE);
        for(File file : allFiles) {
            StringBuilder sb = new StringBuilder();
            sb.append(file);
            KeyedMessage<String, String> message =new KeyedMessage<String, String>(TOPIC,sb.toString());
            System.out.println("sending msg from producer.."+sb.toString());
            producer.send(message);
        }
           producer.close();

这里是消费者代码,

properties.put("bootstrap.servers","Running zookeaper ip addr:2181");
         properties.put("group.id","test-group");
         properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
         properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
         properties.put("enable.auto.commit", "false");

         KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
         consumer.subscribe(Collections.singletonList(topicName)); 
         while (true) {
                ConsumerRecords<String, String> records = consumer.poll(100);
                for (ConsumerRecord<String, String> record : records)
                {
                    System.out.println("topic = "+record.topic());
                    System.out.println("topic = "+record.partition());
                    System.out.println("topic = "+record.offset());
                }
                try {
                  consumer.commitSync(); 
                } catch (CommitFailedException e) {
                    System.out.printf("commit failed", e) ;
                }
            }

我使用这种依赖关系:

<dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.11</artifactId>
        <version>0.10.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>0.10.1.0</version>
    </dependency>

我从该链接获得所有信息:
https://kafka.apache.org/0100/javadoc/index.html?org/apache/kafka/clients/consumer/kafkaconsumer.html
当我们运行consumer时,我们没有收到来自consumer方面的任何通知。请告诉我任何想法。

kcwpcxri

kcwpcxri1#

我是kafka和java的新手,但我想建议以下方法
使用以下命令验证生产者是否确实在向主题写入内容 /usr/bin/kafka-avro-console-consumer --new-consumer --bootstrap-server localhost:9092 --topic KumarTopic --from-beginning .
如果是这样,您可能需要关注您的消费代码。confluent的指南非常有用。

0s7z1bwu

0s7z1bwu2#

生产商:

properties.put("metadata.broker.list","Running kafka ip addr:9092");

我想,这应该是“bootstrap.servers”。
对于消费者:

properties.put("bootstrap.servers","Running zookeaper ip addr:2181");
``` `bootstrap.servers` 必须指向经纪人,而不是zk。
“问题”是,如果指定的主机/端口上没有代理,使用者只会等待代理,而不会失败。

相关问题