Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · บทเรียน

การจัดการกลุ่มผู้บริโภค

ทำความเข้าใจว่ากลุ่มผู้บริโภคช่วยให้ประมวลผลแบบขนานและรองรับความขัดข้องได้อย่างไร เพื่อให้มั่นใจว่าข้อความจะถูกประมวลผลเพียงครั้งเดียวต่อกลุ่ม

บทเรียน 2 จาก 411 ขั้นตอน

การจัดการกลุ่มผู้บริโภค เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What are Consumer Groups?

Welcome! Today, we'll explore Consumer Groups in Kafka. These are a core concept for scaling message consumption and ensuring fault tolerance.

Think of a consumer group as a team of consumers working together to process messages from one or more topics. Each message from a topic's partitions is delivered to only one consumer within that specific group.

Scaling & Fault Tolerance

Consumer groups solve two major challenges:

  • Scaling: By distributing partitions across multiple consumers, you can process messages much faster, increasing your application's throughput.
  • Fault Tolerance: If a consumer fails or leaves the group, Kafka automatically reassigns its partitions to other active consumers in the same group. This ensures continuous message processing.

Ultimately, a message is processed exactly once per consumer group, even with multiple consumers.

Partitions & Consumers

To understand groups, remember that Kafka topics are divided into partitions. These partitions are the unit of parallelism.

  • Within a consumer group, each partition is assigned to at most one consumer.
  • If you have more consumers than partitions in a group, some consumers will be idle.
  • If you have fewer consumers than partitions, some consumers will handle multiple partitions.

This assignment strategy ensures ordered processing within each partition while allowing parallel processing across partitions.

Dynamic Partition Rebalancing

Kafka is smart! When a consumer joins or leaves a group (e.g., an application starts, stops, or crashes), Kafka automatically triggers a rebalance.

During a rebalance, partitions are dynamically re-assigned among the active consumers in the group. This ensures all partitions continue to be consumed and adapts to changes in your application's scaling needs.

Rebalancing is an automatic process that guarantees high availability and adapts to fluctuating workloads.

`group.id` in Spring Kafka

In Spring Kafka, you define which consumer group your listener belongs to using the group.id property.

This unique string identifies your consumer group to Kafka. All consumers (whether in the same application instance or different instances) that share the exact same group.id are considered part of the same group.

You typically configure this in your application.properties or directly in the @KafkaListener annotation.

Spring Listener Example

Here's a basic Spring Boot Kafka listener configured with a specific group.id. This tells Kafka that this listener is part of the 'my-first-group' consumer group.

Try running this example and observe the output!

package com.coddykit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class ConsumerGroupApp {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerGroupApp.class, args);
    }

    @Component
    public static class MyKafkaListener {

        @KafkaListener(topics = "my-topic", groupId = "my-first-group")
        public void listen(String message) {
            System.out.println("Received in Group 1: " + message);
        }
    }
}

Scaling with Multiple Consumers

To achieve parallel processing and higher throughput for a topic, you can run multiple instances of your application, all configured with the same group.id.

Kafka will then distribute the topic's partitions across these running instances. Each instance will process a subset of the partitions independently, effectively scaling out your message consumption.

This is the primary way to handle high-volume topics efficiently.

Multiple Listeners, Same Group

You can also define multiple @KafkaListener methods within the same Spring Boot application instance, all belonging to the same consumer group.

Spring Kafka manages these as separate consumers within that group. If the topic has enough partitions, these listeners can process messages from different partitions in parallel within a single application.

The id attribute helps differentiate listener beans, especially when multiple listeners are defined for the same topic and group.

package com.coddykit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class MultiListenerApp {

    public static void main(String[] args) {
        SpringApplication.run(MultiListenerApp.class, args);
    }

    @Component
    public static class MyMultiKafkaListeners {

        // Consumer 1 for "my-topic" in "my-app-group"
        @KafkaListener(topics = "my-topic", groupId = "my-app-group", id = "listener1")
        public void listen1(String message) {
            System.out.println("Listener 1 received: " + message);
        }

        // Consumer 2 for "my-topic" in "my-app-group"
        @KafkaListener(topics = "my-topic", groupId = "my-app-group", id = "listener2")
        public void listen2(String message) {
            System.out.println("Listener 2 received: " + message);
        }
    }
}

Independent Consumption

What if different applications need to process the same messages from a topic, but for different purposes?

This is where multiple consumer groups come in handy. You can have several distinct consumer groups, each with its own unique group.id, consuming from the same Kafka topic.

Each group will receive a full copy of all messages published to that topic. This allows for independent processing without affecting other groups.

Group Management Check

Consider a Kafka topic with 3 partitions. You start an application with one consumer group and two active consumers. How will messages be distributed?

Consumer Group Summary

You've learned about the power of Kafka Consumer Groups!

  • They are crucial for scaling message consumption and building fault-tolerant applications.
  • Messages from a topic's partitions are distributed among consumers in a group, processed exactly once per group.
  • Kafka handles partition assignment and rebalancing automatically as consumers join or leave.
  • Using different group.id values enables multiple applications to independently consume the same stream of messages.

Mastering consumer groups is key to building robust Kafka applications.

เริ่มต้นได้ฟรี

เรียนรู้ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การจัดการกลุ่มผู้บริโภค” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการกลุ่มผู้บริโภค” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการกลุ่มผู้บริโภค”

ทำความเข้าใจว่ากลุ่มผู้บริโภคช่วยให้ประมวลผลแบบขนานและรองรับความขัดข้องได้อย่างไร เพื่อให้มั่นใจว่าข้อความจะถูกประมวลผลเพียงครั้งเดียวต่อกลุ่ม คุณปฏิบัติ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการกลุ่มผู้บริโภค” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม

ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสร้างคอนเทนเนอร์ตัวรับฟัง Kafka
  2. การจัดการกลุ่มผู้บริโภค
  3. การแปลงข้อมูลกลับและการแปลงข้อความ
  4. การรับข้อมูลเป็นชุดและโหมดการยืนยัน
← กลับไปที่ Advanced Spring Boot 4: Event-Driven Architecture (Kafka)