0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · 강의

간단한 스트림 애플리케이션 구축

Kafka Streams를 활용해 이벤트를 실시간으로 처리하고 변환하는 기본 Spring Boot 애플리케이션을 개발합니다.

간단한 스트림 애플리케이션 구축은(는) CoddyKit의 무료 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Your First Stream App

Welcome! In this lesson, we'll build a basic Spring Boot application that uses Kafka Streams to process events in real-time.

Our goal is simple: read messages from one Kafka topic, transform them, and write the results to another topic.

Spring Boot Project Setup

To begin, create a new Spring Boot project using Spring Initializr (start.spring.io).

Make sure to include these dependencies:

  • Spring Web (for a web context, though not strictly needed for streams)
  • Spring for Apache Kafka
  • Kafka Streams

Essential Stream Properties

Kafka Streams applications need some core properties to function. These are typically set in your application.properties or as a @Bean.

Key properties include:

  • application.id: A unique ID for your stream application.
  • bootstrap.servers: The Kafka broker addresses.
  • default.key.serde: Serializer/Deserializer for message keys.
  • default.value.serde: Serializer/Deserializer for message values.

Activating Stream Processing

For Spring Boot to recognize and manage your Kafka Streams application, you need to annotate your main application class with @EnableKafkaStreams.

This annotation tells Spring to look for stream topology definitions and manage their lifecycle.

package com.coddykit.kafka.streams;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.EnableKafkaStreams;

@SpringBootApplication
@EnableKafkaStreams // This enables Kafka Streams
public class SimpleStreamApplication {
    public static void main(String[] args) {
        SpringApplication.run(SimpleStreamApplication.class, args);
    }
}

Kafka Streams Configuration Bean

You can define a @Bean of type KafkaStreamsConfiguration to configure your stream application. This is often preferred over application.properties for more complex setups.

Here, we set essential properties like the application ID and Kafka broker address:

package com.coddykit.kafka.streams;

import org.apache.kafka.common.serialization.Serdes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaStreamsDefaultConfiguration;
import org.springframework.kafka.config.KafkaStreamsConfiguration;

import java.util.HashMap;
import java.util.Map;

import static org.apache.kafka.streams.StreamsConfig.*;

@Configuration
public class KafkaStreamsConfig {

    @Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
    public KafkaStreamsConfiguration kStreamsConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(APPLICATION_ID_CONFIG, "my-uppercase-app");
        props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
        props.put(DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
        return new KafkaStreamsConfiguration(props);
    }
}

Building Your Stream Topology

The StreamsBuilder is your primary tool for defining the processing logic, or 'topology', of your Kafka Streams application.

Spring automatically injects an instance of StreamsBuilder into any @Bean method that defines your stream topology.

Defining the Stream Source

To start processing, you need to define where your stream gets its data. This is done by creating a KStream from an input topic.

The stream() method of StreamsBuilder does exactly this:

KStream<String, String> stream = kStreamBuilder.stream("input-topic");

Here, we're reading messages with String keys and String values from input-topic.

Transforming and Sending Data

Once you have a KStream, you can apply various transformations. For our simple app, we'll convert message values to uppercase using mapValues().

Finally, we'll send the transformed messages to an output-topic using the to() method. Try running this example!

package com.coddykit.kafka.streams;

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.kstream.KStream;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.annotation.EnableKafkaStreams;
import org.springframework.kafka.annotation.KafkaStreamsDefaultConfiguration;
import org.springframework.kafka.config.KafkaStreamsConfiguration;

import java.util.HashMap;
import java.util.Map;

import static org.apache.kafka.streams.StreamsConfig.*;

@SpringBootApplication
@EnableKafkaStreams
public class SimpleStreamApplication {

    public static void main(String[] args) {
        System.out.println("Starting SimpleStreamApplication...");
        SpringApplication.run(SimpleStreamApplication.class, args);
    }

    @Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
    public KafkaStreamsConfiguration kStreamsConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(APPLICATION_ID_CONFIG, "uppercase-stream-app");
        props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
        props.put(DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
        return new KafkaStreamsConfiguration(props);
    }

    @Bean
    public KStream<String, String> kStream(StreamsBuilder kStreamBuilder) {
        KStream<String, String> stream = kStreamBuilder.stream("input-topic");

        stream.mapValues(String::toUpperCase)
              .to("output-topic");

        System.out.println("Kafka Stream 'uppercase-stream-app' topology built!");
        return stream;
    }
}

Testing Your Stream App

To see your application in action:

  1. Ensure a Kafka broker is running (e.g., via Docker).
  2. Run this Spring Boot application.
  3. Use a Kafka console producer to send messages to input-topic.
  4. Use a Kafka console consumer to read messages from output-topic and observe the uppercase transformation.

Stream Concepts Quick Check

Which of the following is the primary purpose of the application.id configuration in a Kafka Streams application?

Recap: Building Stream Apps

Great job! You've learned how to build a basic Kafka Streams application with Spring Boot:

  • Configured essential Kafka Streams properties.
  • Used @EnableKafkaStreams to activate stream processing.
  • Defined a stream topology using StreamsBuilder, including reading from a source topic, applying transformations, and writing to a sink topic.

This foundation will help you build more complex real-time data processing pipelines!

자주 묻는 질문

“간단한 스트림 애플리케이션 구축” 강의는 무료인가요?

네 — “간단한 스트림 애플리케이션 구축” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 강의 전체를 잠금 해제할 수 있습니다. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 강의에는 총 4개의 강의가 포함되어 있습니다.

“간단한 스트림 애플리케이션 구축”에서 뭘 배우나요?

Kafka Streams를 활용해 이벤트를 실시간으로 처리하고 변환하는 기본 Spring Boot 애플리케이션을 개발합니다. 브라우저에서 직접 실행하는 실습 코드로 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Advanced Spring Boot 4: Event-Driven Architecture (Kafka)을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“간단한 스트림 애플리케이션 구축” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Kafka Streams 소개
  2. KStream 및 KTable을 활용한 스트림 처리
  3. 간단한 스트림 애플리케이션 구축
  4. Kafka Streams의 윈도잉과 상태 저장 집계
← Advanced Spring Boot 4: Event-Driven Architecture (Kafka)(으)로 돌아가기