由于kafka版本的关系(详见坑二),在获取kafka元数据时新旧版本也产生了差别。
0.10.0版本中,ZkConsumerGroupService、KafkaConsumerGroupService是ConsumerGroupService的子类,kafka源码在执行时能够根据ConsumerGroupCommandOptions(程序配置)自动区分要实例化哪个类。
然后执行describeGroup()方法,
根据实例化的类调用对应的collectGroupAssignment():
通过adminClient.describeConsumerGroup()、adminClient.listGroupOffsets()、describePartition()获取基本信息、各个分区的消费位移、水位线(HW,但此时被称为LOG-END-OFFSET,详见坑一)
根据groupid获取主题等基本信息
获取分区消费位移
获取LOG-END-OFFSET
最后通过collectConsumerAssignment()计算合成List<PartitionAssignmentState>,而describeGroup()直接返回一个val (state, assignments),最后由printOffsets格式化输出。
ZkConsumerGroupService和KafkaConsumerGroupService的不同之处在于发送请求时的request不同,本质流程是一样的。
因此在0.10.0版本中,使用ZkConsumerGroupService或KafkaConsumerGroupService的describeGroup()即可获得元数据。
但在2.1.0及以后的计算版本中,kafka源码只使用Kafka的describeGroup()方法,且describeGroup()不再有返回值。由于不再区分是Zk还是Kafka,也就没有了collectGroupAssignment()方法,取而代之的是collectGroupOffsets():
获取基本信息
获取分区的消费位移
通过collectConsumerAssignment()合成List<PartitionAssignmentState>
最后由printOffsets格式化输出。
因此在2.1.0版本中,需要使用consumerGroupService.collectGroupOffsets()获取元数据。