0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · 课时

构建简单的流应用

开发一个基础的 Spring Boot 应用,利用 Kafka Streams 实时处理和转换事件

构建简单的流应用 是 CoddyKit 上的免费 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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!

常见问题解答

「构建简单的流应用」课时是免费的吗?

是的 — 「构建简单的流应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程的其余内容,请升级到 CoddyKit PRO。 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程共包含 4 节课。

「构建简单的流应用」这节课中我会学到什么?

开发一个基础的 Spring Boot 应用,利用 Kafka Streams 实时处理和转换事件 你通过在浏览器中直接运行的动手代码来练习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「构建简单的流应用」课时需要多长时间?

大多数 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)