smile_geek_sq
极客宋
采纳率0%
2019-03-29 10:30 阅读 643

kafka 虚拟机内 shell 可以正常生产消费 远程连接创建消费者消费就没反应

有遇到过同样问题的吗?网上找了半天也没找到解决办法 能 ping 通 且防火墙都已关闭

代码如下

package com.yn.kafkatoredis.demo;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;


public class MyKafkaConsumer {
public static void main(String[] args) {

Properties props = new Properties();
props.put("bootstrap.servers", "192.168.100.100:9092");
props.put("group.id", "wtf");//消费者的组 id
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);
//订阅主题列表 topic
consumer.subscribe(Arrays.asList("test"));

while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records)
System.out.printf("offset = %d, key = %s, value = %s", record.offset(), record.key(), record.value()+"\n");
}
}
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • vane11 Vane11 2019-03-29 14:59

    如果可以的话,可以再提供一下provider的配置,但看consumer确定不了问题

    点赞 评论 复制链接分享

相关推荐