环境搭建与快速入门

Kafka 搭建

在开始前,首先准备好自己的 docker 环境,可以安装Docker Desktop.

为了更方便的搭建学习环境,推荐使用 confluent 公司社区版 docker 镜像。

对于 confluent 版本和 apache 版本对照表如下:

Confluent Platform and Apache Kafka Compatibility

Confluent PlatformApache Kafka®Release DateStandard End of SupportPlatinum End of Support
7.0.x3.0.xOctober 27, 2021October 27, 2023October 27, 2024
6.2.x2.8.xJune 8, 2021June 8, 2023June 8, 2024
6.1.x2.7.xFebruary 9, 2021February 9, 2023February 9, 2024
6.0.x2.6.xSeptember 24, 2020September 24, 2022September 24, 2023
5.5.x2.5.xApril 24, 2020April 24, 2022April 24, 2023
5.4.x2.4.xJanuary 10, 2020January 10, 2022January 10, 2023
5.3.x2.3.xJuly 19, 2019July 19, 2021July 19, 2022
5.2.x2.2.xMarch 28, 2019March 28, 2021March 28, 2022
5.1.x2.1.xDecember 14, 2018December 14, 2020December 14, 2021
5.0.x2.0.xJuly 31, 2018July 31, 2020July 31, 2021
4.1.x1.1.xApril 16, 2018April 16, 2020April 16, 2021
4.0.x1.0.xNovember 28, 2017November 28, 2019November 28, 2020
3.3.x0.11.0.xAugust 1, 2017August 1, 2019August 1, 2020
3.2.x0.10.2.xMarch 2, 2017March 2, 2019March 2, 2020
3.1.x0.10.1.xNovember 15, 2016November 15, 2018November 15, 2019
3.0.x0.10.0.xMay 24, 2016May 24, 2018May 24, 2019
2.0.x0.9.0.xDecember 7, 2015December 7, 2017December 7, 2018
1.0.0February 25, 2015February 25, 2017February 25, 2018

首先新建文件docker-compose.yml,并写入如下内容:

---
version: '3'
services:
zookeeper:
image: confluentinc/cp-zookeeper:7.0.1
container_name: zookeeper
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
broker:
image: confluentinc/cp-kafka:7.0.1
container_name: broker
ports:
# To learn about configuring Kafka for access across networks see
# https://www.confluent.io/blog/kafka-client-cannot-connect-to-broker-on-aws-on-docker-etc/
- "9092:9092"
depends_on:
- zookeeper
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_INTERNAL:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_INTERNAL://broker:29092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1

然后在该文件目录下执行docker-compose up -d,既可启动 zookeeper 容器和 kafka broker 容器,docker-compose down可以关闭创建好的集群。

PS F:\docker-workspace\kafka> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aac9044963e5 confluentinc/cp-kafka:7.0.1 "/etc/confluent/dock…" 11 seconds ago Up 10 seconds 0.0.0.0:9092->9092/tcp broker
debcc3a0235d confluentinc/cp-zookeeper:7.0.1 "/etc/confluent/dock…" 13 seconds ago Up 12 seconds 2181/tcp, 2888/tcp, 3888/tcp zookeeper

如上情况,即为创建成功。

Quick Start

创建 Topic

默认自动创建 Topic 配置是启用的,也就是说第一步也可以省略,只要有 Producer 或者 Consumer,就会自动创建 Topic 的。

docker exec broker kafka-topics --bootstrap-server broker:9092 --create --topic trumandu

查看 Topic 信息

PS F:\docker-workspace\kafka> docker exec broker kafka-topics --describe --bootstrap-server broker:9092 --topic trumandu
Topic: trumandu TopicId: 8peszWGBTgulbSZPYHiMzQ PartitionCount: 1 ReplicationFactor: 1 Configs:
Topic: trumandu Partition: 0 Leader: 1 Replicas: 1 Isr: 1

生产消息

执行

docker exec --interactive --tty broker kafka-console-producer --bootstrap-server broker:9092 --topic trumandu

然后输入测试信息,完成后执行Ctrl-D可以退出命令行。

消费消息

PS F:\docker-workspace\kafka> docker exec --interactive --tty broker kafka-console-consumer --bootstrap-server broker:9092 --topic trumandu --from-beginning
hello
truman

执行Ctrl-C可以退出命令行

以上例子中kafka-topics kafka-console-producerkafka-console-consumer具体参数和用法详见文档

参考

  1. Apache Kafka® Quick Start