For the latest stable version, please use Spring for Apache Kafka 3.3.0!

Obtaining the Consumer group.id

When running the same listener code in multiple containers, it may be useful to be able to determine which container (identified by its group.id consumer property) that a record came from.

You can call KafkaUtils.getConsumerGroupId() on the listener thread to do this. Alternatively, you can access the group id in a method parameter.

@KafkaListener(id = "id", topicPattern = "someTopic")
public void listener(@Payload String payload, @Header(KafkaHeaders.GROUP_ID) String groupId) {
    ...
}
This is available in record listeners and batch listeners that receive a List<?> of records. It is not available in a batch listener that receives a ConsumerRecords<?, ?> argument. Use the KafkaUtils mechanism in that case.