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

使用 Docker 设置本地 Kafka

学习使用 Docker Compose 快速设置本地 Kafka 集群,为开发和测试提供沙盒环境。

使用 Docker 设置本地 Kafka 是 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 节课。

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

Local Kafka: Your Dev Sandbox

When building applications that use Apache Kafka, you'll need a Kafka cluster to connect to. For development and testing, setting up a local cluster is ideal.

It provides a safe, isolated environment where you can experiment without affecting production systems. This lesson will guide you through setting it up easily with Docker.

Docker and Docker Compose Basics

Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, portable, and self-sufficient units.

Docker Compose is a tool for defining and running multi-container Docker applications. You use a YAML file to configure your application's services, then create and start them all with a single command.

Essential Kafka Local Components

A minimal Kafka cluster requires two main components to function:

  • Zookeeper: This service manages Kafka brokers, handling leader election, configuration, and coordination. Kafka relies on Zookeeper for its operational state.
  • Kafka Broker: This is the core server that stores topics, handles message production and consumption, and replicates data. You'll typically run at least one broker.

Defining Your Services with YAML

We'll define our Kafka setup using a docker-compose.yml file. This file tells Docker Compose which services to run, which images to use, and how they should be configured and networked.

Each service in the file represents a container that will be launched. Let's start building it step-by-step.

Zookeeper in Docker Compose

First, let's define the Zookeeper service. We'll use a standard Zookeeper image and expose its default client port.

version: '3.8'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:7.5.0
    container_name: zookeeper
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

Here, ports: "2181:2181" maps the container's port 2181 to your host's port 2181.

Kafka Broker in Docker Compose

Next, we add the Kafka broker service. It needs to know how to connect to Zookeeper and also exposes its own ports for clients.

  kafka:
    image: confluentinc/cp-kafka:7.5.0
    container_name: kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
    depends_on:
      - zookeeper

depends_on: zookeeper ensures Zookeeper starts before Kafka.

Your Complete Kafka Setup File

Combining both services, your docker-compose.yml file looks like this:

version: '3.8'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:7.5.0
    container_name: zookeeper
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  kafka:
    image: confluentinc/cp-kafka:7.5.0
    container_name: kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
    depends_on:
      - zookeeper

Save this content into a file named docker-compose.yml in an empty directory.

Launching Your Kafka Cluster

Once your docker-compose.yml file is ready, navigate to its directory in your terminal. Then, use the following command to start your Kafka cluster:

docker-compose up -d
  • up: Builds, (re)creates, starts, and attaches to containers for a service.
  • -d: Runs containers in detached mode (in the background).

Docker will download images (if not present) and start Zookeeper, then Kafka.

Confirming Kafka is Running

After running docker-compose up -d, you can check if your services are running correctly:

  • docker ps: Lists all running Docker containers. You should see zookeeper and kafka.
  • docker logs kafka: Shows the logs for the Kafka container. Look for messages indicating it started successfully, like "started (kafka.server.KafkaServer)".

To stop and remove the containers, use docker-compose down.

Docker Compose Command Check

You've just created a docker-compose.yml file and want to start your Kafka cluster in the background. Which command should you use?

Recap: Local Kafka with Docker

Congratulations! You've learned how to set up a local Kafka cluster using Docker Compose. Here's a quick summary:

  • Local Kafka provides an isolated dev environment.
  • Docker Compose uses a docker-compose.yml file to define multi-container apps.
  • A minimal Kafka setup requires Zookeeper and a Kafka Broker.
  • The docker-compose.yml specifies images, ports, and environment variables.
  • Use docker-compose up -d to start and docker-compose down to stop your cluster.

Now you have a sandbox to build your Spring Boot Kafka applications!

常见问题解答

「使用 Docker 设置本地 Kafka」课时是免费的吗?

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

「使用 Docker 设置本地 Kafka」这节课中我会学到什么?

学习使用 Docker Compose 快速设置本地 Kafka 集群,为开发和测试提供沙盒环境。 你通过在浏览器中直接运行的动手代码来练习 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 节。

「使用 Docker 设置本地 Kafka」课时需要多长时间?

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

我能在这节 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课中编写并运行代码吗?

能。每节 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Kafka 架构概览
  2. 主题、分区与偏移量
  3. 使用 Docker 设置本地 Kafka
  4. 消费者组与再平衡
← 返回 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)