如果我的生产者在生产,那为什么消费者不能消费?卡住了@ poll() [英] If my producer producing, then why the consumer couldn't consume? it stuck @ poll()
问题描述
我正在发布到远程kafka服务器,并尝试使用来自该远程服务器的消息.(卡夫卡v 0.90.1)发布工作正常,但也不费劲.
发布者
package org.test;导入java.io.IOException;导入java.util.Properties;导入org.apache.kafka.clients.producer.KafkaProducer;导入org.apache.kafka.clients.producer.ProducerRecord;公共类生产者{私人void generateMessgaes()引发IOException {字符串主题="MY_TOPIC";属性props = new Properties();props.put("bootstrap.servers","kafka.xx.com:9092");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");props.put("serializer.class","org.apache.kafka.common.serialization.StringSerializer");KafkaProducer< String,String>生产者= null;尝试 {生产者=新的KafkaProducer(props);对于(int i = 0; i< 10; i ++){producer.send(new ProducerRecord< String,String>(topic,"test msg")));System.out.println("production ---");}} catch(Throwable e){e.printStackTrace();System.out.println(将消息发布到主题时出错:" + topic);} 最后 {producer.close();}}公共静态void main(String [] args)引发IOException {生产者生产者= new Producer();producer.generateMessgaes();System.out.println("$$$$$");}}
我可以看到正在生产---和$$$$打印."但是当我尝试使用时,看不到正在轮询"打印消息.它卡在poll(timeout).
有任何线索吗?
消费者
package org.test;导入java.util.Arrays;导入java.util.List;导入java.util.Properties;导入org.apache.kafka.clients.consumer.ConsumerRecord;导入org.apache.kafka.clients.consumer.ConsumerRecords;导入org.apache.kafka.clients.consumer.KafkaConsumer;公共类Listener {公共无效start()引发CoreException {字符串主题="MY_TOPIC";List< String>topic = Arrays.asList(topic);属性props = new Properties();props.put("bootstrap.servers","kafka.xx.com:9092");props.put("enable.auto.commit",true);props.put("receive.buffer.bytes",262144);props.put("consumer.timeout.ms",10000);props.put("session.timeout.ms",7000);props.put("heartbeat.interval.ms",1000);props.put("auto.offset.reset",最早");props.put("group.id","test");props.put("fetch.min.bytes",1);props.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");props.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");props.put("serializer.class","org.apache.kafka.common.serialization.StringDeserializer");KafkaConsumer< String,String>消费者=新的KafkaConsumer< String,String>(道具);消费者.订阅(主题);尝试 {而(true){ConsumerRecords< String,String>记录= Consumer.poll(100);System.out.println("polling msges:" + records.count());对于(ConsumerRecord< String,String>记录:记录){System.out.println("kafka record:" + record.value());}}} catch(Throwable e){e.printStackTrace();System.out.println("eror in polling");} 最后 {Consumer.close();}}公共静态void main(String args [])引发CoreException {侦听器=新的Listener();listener.start();}}
免责声明:我不知道Kafka,但我知道消息传递.
关于主题的第一件事:默认情况下,订阅不是持久性的,因此,如果生产者将消息发送到主题,并且没有人在听,则消息将被丢弃.
第二件事是轮询:您正在轮询100毫秒,然后如果什么都没有,则会引发异常,从而使您退出循环.
如果使用者启动时没有消息-如我所描述的那样,因为生产者的消息进入了位桶-然后使用者失败了,因为没有消耗.所以我会说一切都按预期运行.
两个选项:-添加更大的初始轮询,使消费者有机会看到一条消息(假设您知道生产者将在该时间范围内生产)-更改逻辑,使异常导致您停留在while循环中并继续消耗,并找出另一种方法来停止消耗器.
Im publishing to the remote kafka server and try to consume messages from that remote server. (Kafka v 0.90.1) Publishing works fine but nor the consuming.
Publisher
package org.test;
import java.io.IOException;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class Producer {
private void generateMessgaes() throws IOException {
String topic = "MY_TOPIC";
Properties props = new Properties();
props.put("bootstrap.servers", "kafka.xx.com:9092");
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");
props.put("serializer.class", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = null;
try {
producer = new KafkaProducer<>(props);
for (int i = 0; i < 10; i++) {
producer.send(new ProducerRecord<String, String>(topic, "test msg"));
System.out.println("producing---");
}
} catch (Throwable e) {
e.printStackTrace();
System.out.println("Error in publishing messages to the topic : " + topic);
} finally {
producer.close();
}
}
public static void main(String[] args) throws IOException {
Producer producer = new Producer();
producer.generateMessgaes();
System.out.println("$$$$$");
}
}
I can see "producing--- and $$$$ prints. But when i try to consume, i do not see "polling " print messages.. It got stuck at poll(timeout).
Any clue?
Consumer
package org.test;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class Listener {
public void start() throws CoreException {
String topic = "MY_TOPIC";
List<String> topics = Arrays.asList(topic);
Properties props = new Properties();
props.put("bootstrap.servers", "kafka.xx.com:9092");
props.put("enable.auto.commit", true);
props.put("receive.buffer.bytes", 262144);
props.put("consumer.timeout.ms", 10000);
props.put("session.timeout.ms", 7000);
props.put("heartbeat.interval.ms", 1000);
props.put("auto.offset.reset", "earliest");
props.put("group.id", "test");
props.put("fetch.min.bytes", 1);
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("serializer.class", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
consumer.subscribe(topics);
try {
while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
System.out.println("polling msges : " + records.count());
for (ConsumerRecord<String, String> record : records) {
System.out.println("kafka record : " + record.value());
}
}
} catch (Throwable e) {
e.printStackTrace();
System.out.println("eror in polling");
} finally {
consumer.close();
}
}
public static void main(String args[]) throws CoreException {
Listener listener = new Listener();
listener.start();
}
}
Disclaimer: I don't know Kafka but I do know messaging.
First thing about topics: by default, subscriptions are not persistent, so if a producers sends messages to the topic and there's no one listening, the messages are dropped.
Second thing is the polling: you're polling for 100ms and then if there's nothing there an exception is thrown, dropping you out of the loop.
If there's no messages when the consumer starts - because, as I described, the producer's messages went to the bit bucket - and then the consumer failed because there was nothing to consume. So I would say everything is working as you should expect.
Two options: -add a much larger initial polling to give the consumer a chance to see a message (assuming you know the producer is going to produce in the time frame) -change your logic such that an exception causes you to stay in the while loop and continue to consume, and figure out a different way to stop the consumer.
这篇关于如果我的生产者在生产,那为什么消费者不能消费?卡住了@ poll()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!