Ubuntu24.04 安装 Kafka

399 阅读1分钟

Kafka 官网 kafka.apache.org/

下载地址: kafka.apache.org/downloads

下载

wget https://archive.apache.org/dist/kafka/3.8.1/kafka_2.13-3.8.1.tgz

解压

tar -xzf kafka_2.13-3.8.1.tgz

mv kafka_2.13-3.8.1 /opt/app/

配置

cd /opt/app/kafka_2.13-3.8.1
vim config/server.properties
# 每个节点单独配置
broker.id=0

# broker对外暴露的IP和端口 每个节点单独配置
listeners=PLAINTEXT://localhost:9092

# 数据路径配置 log就是指kafaka数据
log.dirs=/opt/app/kafka_2.13-3.8.1/kafka-logs

# zookeeper地址
zookeeper.connect=localhost:2181

可使用外置或内置Zookeeper,这里使用内置Zookeeper

修改Zookeeper配置文件

cd /opt/app/kafka_2.13-3.8.1
vim config/zookeeper.properties
dataDir=/opt/app/kafka_2.13-3.8.1/zkData
dataLogDir=/opt/app/kafka_2.13-3.8.1/zkLogs

启动内置 Zookeeper

这里启动内置Zookeeper,也可使用外置Zookeeper

cd /opt/app/kafka_2.13-3.8.1/

# 前台启动(临时查看)
./bin/zookeeper-server-start.sh config/zookeeper.properties

# 后台启动(一般使用)
nohup ./bin/zookeeper-server-start.sh config/zookeeper.properties > zookeeper.log 2>1 &

启动Kafka

cd /opt/app/kafka_2.13-3.8.1/

# 前台启动(临时查看)
./bin/kafka-server-start.sh config/server.properties

# 后台启动(一般使用)
nohup ./bin/kafka-server-start.sh config/server.properties > kafka.log 2>1 &

测试

测试topic创建与删除

创建 topic

./bin/kafka-topics.sh  --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test-topic1

Created topic test-topic1.

查看 topic

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

test-topic1

删除 topic

./bin/kafka-topics.sh  --delete --bootstrap-server localhost:9092 --topic test-topic1

再次查看 topic

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

test-topic1

创建 topic

./bin/kafka-topics.sh  --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test-topic1

Created topic test-topic1.

生产者

新开一个命令端作为生产者,然后另外再开一个命令端作为消费者。 在生产者命令窗口,给消费方发送消息。

cd /opt/app/kafka_2.13-3.8.1/
./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test-topic1

image.png

消费者

cd /opt/app/kafka_2.13-3.8.1/

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

image.png