单节点kafka部署笔记

1 背景

因为工作中需要对接kafka,准备在测试环境中自己部署一套,考虑方便决定部署一台单点。

2 部署

2.1 scala

2.1.1 java环境

openjdk即可,我使用的是openjdk1.8

2.1.2 下载软件

下载scala-2.12.17.tgz并解压,例如解压到/home/scala/scala-2.12.17

2.1.3 环境变量

export SCALA_HOME=/home/scala/scala-2.12.17
export PATH=$SCALA_HOME/bin:$PATH

2.1.4 测试

scala -version,如能正常输出版本则一切正常

2.2 kafka

以下为官网教程

2.2.1 下载

下载 kafka_2.12-3.3.1.tgz 并解压,进入目录下。

2.2.2 生成Cluster UUID

KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"

2.2.3 日志记录

bin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c config/kraft/server.properties

2.2.4 启动

首先修改kafka目录下 /config/kraft/server.properties中的

listeners=PLAINTEXT://:9092

修改为对外地址,否则只能localhost使用

bin/kafka-server-start.sh config/kraft/server.properties &

如果正常,则tcp 9092、9093端口处于listening状态

3 测试

3.1 命令行调用

3.1.1 操作topic

创建topic

bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092

删除topic

bin/kafka-topics.sh --delete --topic quickstart-events --bootstrap-server localhost:9092

3.1.2 显示topic信息

显示所有topic

bin/kafka-topics.sh --bootstrap-server localhost:9092 --list

显示具体topic信息

bin/kafka-topics.sh --describe --topic quickstart-events --bootstrap-server localhost:9092

查看topic是否已成功创建

3.1.3 写入信息

bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server localhost:9092

3.1.4 读取信息

新开一个终端执行

bin/kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server localhost:9092

完成后,在原先终端输入信息,在本终端则可以查看到

3.2 python调用

3.2.1 模块安装

pip3 install kafka-python -i https://pypi.tuna.tsinghua.edu.cn/simple

3.2.2 demo代码

from kafka import KafkaProducer
kafka_producer= KafkaProducer(bootstrap_servers = "localhost:9092")
kafka_producer.send("topic-01", "this is message".encode('utf-8'))

3.3 旧版本命令

bin/kafka-topics.sh --zookeeper localhost:2181 --list
bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic alarm-01 --partitions 1 --replication-factor 1
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic alarm-01
bin/kafka-console-consumer.sh --topic alarm-01 --from-beginning --bootstrap-server localhost:9092
bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic alarm-01
posted @ 2023-06-04 18:27  virtualzzf  阅读(148)  评论(0编辑  收藏  举报