根据ip和端口获取kafka列表、判断kafka中是否存在某个topic、获取kafka消息队列上有哪些topic
public static Boolean topicExistence(String topic) {
Boolean ok = false;
if (topic == null || topic.length() <=0){
return ok;
}
Properties properties = new Properties();
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getCanonicalName());
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getCanonicalName());
properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"127.0.0.1:9092,127.0.0.1:9093");
properties.put(ConsumerConfig.GROUP_ID_CONFIG, "TEST");
KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
for (String e :consumer.listTopics().keySet()){
System.out.println(e);
if (topic.equals(e)){
ok = true;
}
}
return ok;
}
其他步骤
- kafka服务自带zookeeper下载与启动
- Spring boot配置kafka服务
- kafka生产者发送消息成功回调
- kafka根据ip端口获取消息队列上的topic
- kafka动态设置监听哪些topic
- 动态启动关闭kafka监听、设置默认不监听kafka
- kafka设置:1只接受消息、不发送消息;2只发送消息不接受消息;3既接受消息也发送消息;4既不接收消息也不发送消息
- kafka会把历史数据都获取下来
- Spring boot kafka执行多次多次消费