0Pricing
Spring Boot 4 Microservices & REST APIs · 课时

构建 Kafka 消费者

开发 Spring Kafka 消费者,以订阅并处理来自 Kafka 主题的消息。

构建 Kafka 消费者 是 CoddyKit 上的免费 Spring Boot 4 Microservices & REST APIs 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Microservices & REST APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Microservices & REST APIs 课程共包含 3 节课。

本课时的部分内容尚未翻译,以英文显示。

Kafka Consumers: The Listeners

In event-driven architectures, Kafka Consumers are the components responsible for reading messages (records) from Kafka topics. Think of them as listeners waiting for new events!

They subscribe to one or more topics and process the incoming data, enabling different parts of your application or other services to react to events.

Spring Boot & Kafka Config

To build a Kafka consumer in Spring Boot, first, you need the spring-kafka dependency. Add it to your pom.xml:

<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>

Next, configure your Kafka broker details in application.properties. This tells your Spring Boot app where to find the Kafka server.

spring.kafka.bootstrap-servers=localhost:9092

Meet @KafkaListener

Spring for Apache Kafka provides the powerful @KafkaListener annotation. This annotation marks a method to be a Kafka listener, meaning it will automatically consume messages from specified topics.

  • topics: The Kafka topic(s) to listen to.
  • groupId: Identifies the consumer group. Essential for scaling.

It handles all the low-level Kafka API details for you!

Your First Kafka Listener

Let's create a simple consumer that listens to a topic named my-first-topic and prints any incoming string messages to the console.

Notice the groupId. All consumers with the same groupId are part of a consumer group.

package com.coddykit.kafka;

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

@SpringBootApplication
@EnableKafka
public class KafkaConsumerApp {
  public static void main(String[] args) {
    SpringApplication.run(KafkaConsumerApp.class, args);
  }
}

@Component
class SimpleKafkaListener {

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

Understanding Deserialization

Kafka messages are stored as byte arrays. When a consumer reads a message, it needs to convert these bytes back into a usable object (like a String or a custom Java object).

This process is called deserialization. You configure deserializers in application.properties:

spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer

The choice of deserializer depends on how the producer serialized the message.

Consumer Groups for Scale

Consumer groups are key to Kafka's scalability. Multiple consumer instances can belong to the same group, sharing the workload of consuming messages from a topic.

  • Each message in a topic partition is delivered to only one consumer instance within a group.
  • If you have more consumers than partitions, some consumers will be idle.
  • If a consumer fails, another consumer in the same group automatically takes over its partitions.

This allows for both high availability and horizontal scaling.

Listening for JSON Data

Often, you'll send complex data as JSON. To consume JSON, you'll need to define a Java class (POJO) that matches the JSON structure and use Spring Kafka's JsonDeserializer.

Add spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer to your config.

package com.coddykit.kafka;

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

// Define a simple DTO matching the JSON structure
class MyEvent {
  private String name;
  private int value;

  // Default constructor required for deserialization
  public MyEvent() {}

  public MyEvent(String name, int value) {
    this.name = name;
    this.value = value;
  }

  public String getName() { return name; }
  public void setName(String name) { this.name = name; }
  public int getValue() { return value; }
  public void setValue(int value) { this.value = value; }

  @Override
  public String toString() {
    return "MyEvent{" +
           "name='" + name + '\'' +
           ", value=" + value +
           '}';
  }
}

@SpringBootApplication
@EnableKafka
public class KafkaJsonConsumerApp {
  public static void main(String[] args) {
    SpringApplication.run(KafkaJsonConsumerApp.class, args);
  }
}

@Component
class JsonKafkaListener {

  @KafkaListener(topics = "my-json-topic", groupId = "json-group")
  public void listenJson(MyEvent event) {
    System.out.println("Received JSON Event: " + event);
  }
}

Graceful Error Handling

What happens if a message is malformed or your processing logic throws an error? Consumers need robust error handling.

For simple errors, a try-catch block within your listener method is effective. For more advanced scenarios, Spring Kafka offers error handlers:

  • DefaultErrorHandler: Retries messages with backoff.
  • DeadLetterPublishingRecoverer: Sends failed messages to a dead-letter topic.

These prevent a single bad message from stopping your entire consumer.

Peeking at Message Metadata

Sometimes, you need more than just the message payload. Kafka messages come with useful metadata, such as the topic name, partition, and offset.

You can access this metadata directly in your @KafkaListener method using annotations like @Header or by accepting a ConsumerRecord object.

@KafkaListener(topics = "my-topic", groupId = "my-group") public void listenWithInfo( String message, @Header(org.springframework.kafka.support.KafkaHeaders.RECEIVED_TOPIC) String topic, @Header(org.springframework.kafka.support.KafkaHeaders.RECEIVED_PARTITION_ID) int partition ) { System.out.println("From topic " + topic + ", partition " + partition + ": " + message); }

Quick Check: Kafka Consumers

Which of the following is the primary annotation used in Spring Kafka to mark a method as a message listener for a specific topic?

Recap: Building Kafka Consumers

Great job! You've learned how to build Spring Kafka consumers to process messages from topics.

  • We set up Spring Kafka and used @KafkaListener to create message-consuming methods.
  • We explored deserialization and how to consume both simple strings and complex JSON objects.
  • You also understand the importance of consumer groups for scaling and handling errors.

Next up, we'll dive deeper into integrating producers and consumers to build full event-driven microservices!

常见问题解答

「构建 Kafka 消费者」课时是免费的吗?

是的 — 「构建 Kafka 消费者」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Microservices & REST APIs 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Microservices & REST APIs 课程共包含 3 节课。

「构建 Kafka 消费者」这节课中我会学到什么?

开发 Spring Kafka 消费者,以订阅并处理来自 Kafka 主题的消息。 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Microservices & REST APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Spring Boot 4 Microservices & REST APIs 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Spring Boot 4 Microservices & REST APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。

「构建 Kafka 消费者」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Spring Boot 4 Microservices & REST APIs 课中编写并运行代码吗?

能。每节 Spring Boot 4 Microservices & REST APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Kafka 生产者入门
  2. 构建 Kafka 消费者
  3. 事件驱动的微服务集成
← 返回 Spring Boot 4 Microservices & REST APIs