How to pass multiple bootstrap servers for listener using spring-kafka
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Spring Kafka, a listener does not usually receive bootstrap servers directly through the @KafkaListener annotation. Instead, the listener container uses a consumer factory, and that consumer factory gets its bootstrap servers from application properties or from explicit bean configuration.
Where the Listener Gets Bootstrap Servers
Kafka clients expect bootstrap servers as a comma-separated host and port list. The list does not need to contain every broker in the cluster, but it should contain more than one reachable broker so startup is resilient.
In a typical Spring Boot application, the easiest configuration is:
That property is used by Spring Boot when it creates the default consumer factory and listener container factory. Any @KafkaListener that uses the default container factory will inherit those bootstrap servers automatically.
YAML Configuration Works Too
If you prefer YAML, the same value can be expressed as a list or as a comma-separated string. Both are common in Spring Boot projects.
The key idea is unchanged: the listener itself is not the place where you pass the brokers. The listener uses the consumer infrastructure that Spring builds around it.
Example Listener
Once the bootstrap servers are configured, the listener stays focused on messages:
Notice that there is no bootstrap-server list in the annotation. That is normal.
Custom Consumer Factory
If you need a listener to use a different cluster or a different set of properties, define a custom consumer factory and listener container factory.
This is the right approach when one application listens to multiple clusters or needs listener-specific infrastructure.
What Multiple Bootstrap Servers Actually Do
A common misconception is that the client keeps publishing or consuming through every bootstrap server in the list. It does not. The list is only the initial contact point. Once the client gets cluster metadata, it connects to the appropriate broker leaders for the partitions it needs.
That is why the list should be thought of as a discovery seed list, not as a load-balancing list.
Common Pitfalls
- Trying to put bootstrap servers on
@KafkaListenerinstead of on the consumer configuration. - Using only one broker in production, which creates an unnecessary startup dependency.
- Assuming the bootstrap list must contain every broker in the cluster.
- Forgetting that a custom listener container factory must actually be used by the listener if you define more than one.
- Mixing producer and consumer configuration mentally. Listeners use consumer settings, not producer settings.
Summary
- In Spring Kafka, listeners get bootstrap servers from the consumer factory, not from the annotation.
- The simplest setup is
spring.kafka.bootstrap-serversin properties or YAML. - Multiple bootstrap servers are passed as a comma-separated list or YAML list.
- Use a custom consumer factory only when the default Boot configuration is not enough.
- Bootstrap servers are for initial discovery, not for round-robin message handling.
Related reading
- How to pass parameters for a specific Schema registry when using Kafka Avro Console Consumer?
- How to pass topics dynamically to a kafka listener?
- How to pause a kafka consumer?
- how to pause and resume @KafkaListener using spring-kafka
- How to pass system property to Gradle task
- How to pass TTL in Cassandra Java Driver QueryBuilder?
- How to peek at messages in the queue
- How to pick a Kafka transaction.id

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.