Kafka 컨슈머 만들기
Kafka 토픽의 메시지를 구독하고 처리하는 Spring Kafka 컨슈머를 개발합니다.
Kafka 컨슈머 만들기은(는) CoddyKit의 무료 Spring Boot 4 Microservices & REST APIs 강의입니다. 이것은 3개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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:9092Meet @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.StringDeserializerThe 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
@KafkaListenerto 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 컨슈머 만들기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Spring Boot 4 Microservices & REST APIs 강의 전체를 잠금 해제할 수 있습니다. Spring Boot 4 Microservices & REST APIs 강의에는 총 3개의 강의가 포함되어 있습니다.
“Kafka 컨슈머 만들기”에서 뭘 배우나요?
Kafka 토픽의 메시지를 구독하고 처리하는 Spring Kafka 컨슈머를 개발합니다. 브라우저에서 직접 실행하는 실습 코드로 Spring Boot 4 Microservices & REST APIs을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Spring Boot 4 Microservices & REST APIs을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Spring Boot 4 Microservices & REST APIs은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 3개 중 2번째 강의입니다.
“Kafka 컨슈머 만들기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Spring Boot 4 Microservices & REST APIs 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Spring Boot 4 Microservices & REST APIs 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Kafka 프로듀서 입문
- Kafka 컨슈머 만들기
- 이벤트 기반 마이크로서비스 통합