监控Kafka消费进度
监控某个groupId下所有topic的消费进度
InputStream inputStream = ClasspathUtils.streamFromClasspath("consumer.properties");
Properties properties = new Properties();
properties.load(inputStream);
try (AdminClient client = AdminClient.create(properties);
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
) {
ListConsumerGroupOffsetsResult listConsumerGroupOffsetsResult = client.listConsumerGroupOffsets(properties.getProperty("group.id"));
Map<String, Map<TopicPartition, OffsetAndMetadata>> groupOffsetResult = listConsumerGroupOffsetsResult.all().get();
for (Map.Entry<String, Map<TopicPartition, OffsetAndMetadata>> entry : groupOffsetResult.entrySet()) {
String groupId = entry.getKey();
Map<TopicPartition, OffsetAndMetadata> topicPartitionOffsetAndMetadataMap = entry.getValue();
Set<TopicPartition> topicPartitions = topicPartitionOffsetAndMetadataMap.keySet();
Map<TopicPartition, Long> topicPartitionLongMap = consumer.endOffsets(topicPartitions);
for (Map.Entry<TopicPartition, OffsetAndMetadata> topicPartitionOffsetAndMetadataEntry : topicPartitionOffsetAndMetadataMap.entrySet()) {
TopicPartition key = topicPartitionOffsetAndMetadataEntry.getKey();
OffsetAndMetadata offsetAndMetadata = topicPartitionOffsetAndMetadataEntry.getValue();
long offset = offsetAndMetadata.offset();
Long total = topicPartitionLongMap.get(key);
long lag = total - offset;
System.out.println(key + ",offset:" + offset + ",total:" + total + ",lag:" + lag);
}
}
}

浙公网安备 33010602011771号