I'm getting started with Apache Kafka using Spring Boot. And I want to achieve the fact that two instances of the same service consumes messages of the same topic in the round robin style.
So instance 1 receive the first message of the topic, instance 2 gets the second, instance 1 gets the third and on.
That's my current configuration:
spring:
kafka:
consumer:
bootstrap-servers: rommel.local:9092
group-id: core
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
enable-auto-commit: false
producer:
bootstrap-servers: rommel.local:9092
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
listener:
missing-topics-fatal: false
ack-mode: record
This is my listener:
@KafkaListener(topics = "new-topic", groupId = "core")
public void consume(String payload) {
System.out.println("Data received: " + payload);
}
The problem is, when I run two instances of my application, only one receives the messages of the topic.
I realized this in the logs:
Instance 1:
core: partitions assigned: [new-topic-0]
Instance 2:
core: partitions assigned: []
When I kill instance 1, then. the new-topic-0 is assigned to instance 2.
So how can I achieve the scenario of both instances getting messages of the same topic in round robin?
Thanks!!