Sub topic 3
Setting up Spring Boot with Spring for Apache Kafka
This chapter assumes you know Spring Boot basics (beans, dependency injection, application.yml), can add a Maven or Gradle dependency, and can run a local Kafka broker. It covers the minimum wiring to connect an application to Kafka. Later chapters build on it for topics, producing and consuming.
What Spring Boot gives you
Spring for Apache Kafka is the library that wraps the plain Kafka client. Spring Boot adds auto-configuration on top of it. When spring-kafka is on the classpath, Boot reads the spring.kafka.* properties and creates these beans for you:
KafkaTemplate, used to send records.ConsumerFactoryandProducerFactory, which build the underlying Kafka clients.- A listener container factory, which runs your
@KafkaListenermethods. KafkaAdmin, which creates topics declared as beans.
Add the dependency
Boot manages the version of spring-kafka, so you declare it without one.
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>Start a local broker
Any broker reachable at localhost:9092 works. One option is the official Docker image:
docker run -p 9092:9092 apache/kafka:latestConfigure the connection
Put the producer and consumer settings in application.yml.
spring:
kafka:
bootstrap-servers: localhost:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
acks: all
properties:
enable.idempotence: true
consumer:
group-id: order-service
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
properties:
spring.json.trusted.packages: com.example.orders
spring.json.value.default.type: com.example.orders.OrderEventEach group of settings does a specific job:
| Property | Purpose |
|---|---|
| `bootstrap-servers` | Initial broker addresses. The client discovers the rest of the cluster from them. |
| `key-serializer`, `value-serializer` | Convert the key and value objects to bytes when sending. |
| `acks: all` | The leader waits for all in-sync replicas before confirming a write. |
| `enable.idempotence` | Prevents duplicate records when the producer retries. |
| `group-id` | The consumer group this application joins. |
| `auto-offset-reset` | Where to start when the group has no committed offset. |
| `trusted.packages` | The only packages the JSON deserializer may instantiate. |
Entries under properties are passed straight to the Kafka client. Use them for any setting that Boot has no dedicated property for.
Common errors
- Deserialization fails on the first message. The type is not in
trusted.packages, or no default type is set and the message lacks type headers. Set both properties as shown above. @KafkaListenerfails at startup with a missing group ID. Setspring.kafka.consumer.group-id, or passgroupIdon the annotation.- A new consumer ignores existing messages.
auto-offset-resetonly applies when the group has no committed offset. Once offsets exist, the group resumes from them, whatever this setting says. - The client connects, then times out. The broker's advertised listener address is not reachable from your application. This is typical when the broker runs in Docker with a hostname the host cannot resolve.
- Class-cast or serializer errors on send. The
KafkaTemplategeneric types must match the configured serializers.KafkaTemplate<String, OrderEvent>pairs with a string key serializer and the JSON value serializer.
Check that it works
Start the application and read the startup log. The Kafka client prints its full configuration, including bootstrap.servers, acks and enable.idempotence. If your YAML values appear there, the setup is wired correctly.
Exercise
A new consumer group with auto-offset-reset set to earliest starts, and the topic already holds 500 messages. Later you restart the same application with the setting changed to latest. Does the group skip the messages it has already processed, and why?
Show answer
No. The group has committed offsets from the first run, so auto-offset-reset no longer applies. The consumer resumes from the committed offsets. The setting only matters when the group has no committed offset, or when the committed offset no longer exists on the broker.