Backend

Integrating Spring Boot with Apache Kafka

SSite Admin
25 tháng 06, 2026 1 phút đọc 807 lượt xem
Integrating Spring Boot with Apache Kafka

Event-driven architectures let services communicate asynchronously and scale independently. In this article we wire Spring Boot to Apache Kafka end to end.

Introduction

Kafka is a distributed commit log. Spring for Apache Kafka gives us idiomatic producers and consumers backed by templates and listener containers.

Configuring the Producer

Define a ProducerFactory and a KafkaTemplate:

@Bean
public KafkaTemplate<String, OrderEvent> kafkaTemplate(
    ProducerFactory<String, OrderEvent> pf) {
  return new KafkaTemplate<>(pf);
}

Consuming Messages

Annotate a method with @KafkaListener:

@KafkaListener(topics = "orders", groupId = "billing")
public void onOrder(OrderEvent event) {
  log.info("Received {}", event.id());
}

Error Handling and Retries

Use a DefaultErrorHandler with a dead-letter topic so poison messages don't block the partition.

S

Site Admin

Engineer and writer. Building things with TypeScript and distributed systems.

Bình luận (3)

Bạn cần đăng nhập bằng Google để bình luận.

  • SA
    Site Admin27 thg 6, 2026

    Tại sao lại như thế này

  • SA
    Site Admin26 thg 6, 2026

    Great walkthrough — the dead-letter topic tip saved me!

  • SA
    Site Admin26 thg 6, 2026

    Could you cover exactly-once semantics next?

Bài viết liên quan

99 Ngày Java — Ngày 58: Optional

Optional là công cụ cho kiểu trả về — không field, không tham số, không bọc collection; chuỗi map/flatMap/filter thay kim tự tháp if; orElse luôn tính tham số còn orElseGet lười, orElseThrow cho “không có là lỗi” — và các anti-pattern isPresent + get, orElse(null).

23 thg 9, 20269 phút9
99 Ngày Spring — Ngày 58: Spring Boot Actuator

Boot 3 mặc định chỉ lộ health; bản đồ endpoint an toàn và nguy hiểm; HealthIndicator tự viết với liveness tách khỏi readiness cho Kubernetes, info từ build và git, metrics Micrometer với tag hữu hạn — và ba lớp khóa: cổng riêng, SecurityFilterChain với EndpointRequest, exclude env/heapdump/threaddump.

23 thg 9, 202610 phút5
So sánh EasyExcel và Apache Fesod: khác biệt thật nằm ở đâu?

Fesod là hậu duệ của EasyExcel, cùng engine SAX/SXSSF. Khác biệt kiểm chứng được: vòng đời dự án, POI 5.2.5 vs 5.5.1, các API mới (numRows, csv(), headerMergeStrategy) và độ bền với file xấu — không phải hiệu năng gấp nhiều lần.

23 thg 9, 20268 phút1