Backend

Integrating Spring Boot with Apache Kafka

SSite Admin
25 tháng 06, 2026 1 phút đọc 748 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 Spring — Ngày 14: Bean Validation — @Valid & bộ constraint chuẩn

Ngày 14 của 99 Ngày Spring: Bean Validation khai báo luật ngay trên DTO — @Valid kích hoạt, bộ constraint chuẩn (@NotBlank vs @NotEmpty vs @NotNull, @Size, @Email, @Min/@Max), bẫy int vs Integer, validate object lồng nhau không tự lan, và lỗi 400 gom một lượt.

10 thg 8, 20265 phút0
99 Ngày Java — Ngày 14: Kế thừa (Inheritance)

Ngày 14 của 99 Ngày Java: extends trao gia tài từ lớp cha, override với @Override và super., thứ tự khai sinh cha trước con sau qua super(...), protected trả nợ Ngày 13, và phép thử is-a vs has-a — khi nào nên composition thay vì kế thừa.

10 thg 8, 20266 phút0
99 Ngày Spring — Ngày 13: @RequestBody & DTO — đừng công khai entity ra API

Ngày 13 của 99 Ngày Spring: @RequestBody + Jackson deserialize JSON thành object, DTO là hợp đồng API — chặn lỗ hổng mass assignment chiều vào và rò rỉ dữ liệu chiều ra, request/response DTO khác nhau là bình thường, và Jackson annotations tinh chỉnh hợp đồng JSON.

9 thg 8, 20266 phút9