Kafka with Spring Boot

Harry · 13 Sep 2026 · 1 views

Dependencies

implementation 'org.springframework.kafka:spring-kafka'

Spring Boot auto-configures producers and consumers from application properties.

Configuration

spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=orders-svc
spring.kafka.consumer.auto-offset-reset=earliest

Produce a Record

@Service
public class OrderPublisher {
    private final KafkaTemplate<String, Order> kafka;

    public void publish(Order order) {
        kafka.send("orders", order.id(), order);
    }
}

KafkaTemplate.send publishes with a key for ordering; JSON serialization needs the JsonSerializer and a trust-all config for payload packages.

Consume with @KafkaListener

@KafkaListener(topics = "orders", groupId = "orders-svc")
public void onOrder(Order order,
                    @Header(KafkaHeaders.RECEIVED_PARTITION) int partition) {
    orderService.apply(order);
}

Listeners auto-commit by default. For exactly-once workflows, use transactions via @KafkaListener with a TransactionManager or manual commit and read_committed.

Error Handling

Set a Dead Letter Topic (DLT) via listener error handlers or a ErrorHandler bean so poison records move to a retry/DLT topic instead of blocking the group.

Key Points

  • Spring Kafka wires producers and consumers automatically.
  • KafkaTemplate.send handles the producer side.
  • @KafkaListener declares consumption declaratively.
  • Error handlers and DLT keep groups moving.
Share this post:

Comments (0)

Please login or register to comment.