im发布到远程kafka服务器并尝试使用来自该远程服务器的消息(Kafkav 0.90.1)出版作品很好,但也不是消费品。
出版商
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("$$$$$");
}
}
我能看到“生产——和$$$印刷品”。但当我尝试消费时,我看不到“轮询”打印消息。。它在轮询时卡住了(超时)。
有什么线索吗?
消费者
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();
}
}
1条答案
按热度按时间nnvyjq4y1#
免责声明:我不知道Kafka,但我知道。
关于主题的第一件事:默认情况下,订阅是不持久的,因此如果生产者向主题发送消息,而没有人侦听,则消息将被丢弃。
第二件事是轮询:您正在轮询100ms,然后如果没有异常,就会抛出一个异常,将您从循环中除去。
如果在消费者启动时没有消息-因为,正如我所描述的,生产者的消息进入了位桶-然后消费者失败了,因为没有什么可消费的。所以我想说一切都如你所料。
两个选项:-添加一个更大的初始轮询,让消费者有机会看到一条消息(假设您知道生产者将在时间范围内生产)-更改您的逻辑,使异常导致您停留在while循环中继续消费,并找出一种不同的方法来停止消费者。