我是Kafka的新手,并在KafkaConsumer和KafkaProducer上运行了一个简单的kafka消费者/制作人示例。当我从终端运行消费者时,消费者正在接收消息,但我无法使用Java代码进行监听。我也在StackoverFlow上搜索了类似的问题(链接:Link1,Link2)并尝试了解决方案,但似乎没有什么对我有用。 Kafka版本:kafka_2.10-0.10.2.1
和相应的maven依赖在pom中使用。
生产者和消费者的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());
}
}}
启动kafka:bin/kafka-server-start.sh config/server.properties
(我已经在属性文件中设置了port,brokerid)
首先使用以下方法检查所有组的可用内容:
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
然后使用以下cmd检查您的主题所属的组:
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group <your group name> --describe
找到您的主题和关联的组名后(如果它不属于默认组,只需将group.id替换为您的组),然后尝试使用以下道具并让我知道它是否有效:
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);
}
在运行生产者之前运行消费者,以便消费者首先向组协调员注册。当你运行生产者时,消费者会消费消息。第一次你运行消费者注册组协调员。为了找到直到使用此kafka-consumer-offset-checker.bat --group group-1 --topic testing-1 --zookeeper localhost:2181
消费者已消耗消息的消息这表明消费者已经消耗了该主题的最后偏移量。
清除您访问kafka的驱动器中的'tmp'
文件夹。然后打开新的'cmd'命令窗口!重新启动服务器,并在命令窗口中发布“.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic H1 --from-beginning
”此代码以运行使用者而不会出现任何错误
尝试将enable.partition.eof
参数设置为false
:
props.put("enable.partition.eof", "false");
这对我有用。