1. 安装Kafka和ZooKeeper
-
安装ZooKeeper:
-
下载并解压ZooKeeper:
bash
wget http://apache.opencas.org/zookeeper/zookeeper-3.6.4/apache-zookeeper-3.6.4-bin.tar.gz tar -xvf apache-zookeeper-3.6.4-bin.tar.gz -
配置ZooKeeper:
-
编辑
conf/zoo.cfg文件,设置数据存储目录:dataDir=/var/lib/zookeeper -
启动ZooKeeper:
bash
bin/zkServer.sh start
-
-
-
安装Kafka:
-
下载并解压Kafka:
bash
wget https://downloads.apache.org/kafka/3.5.1/kafka_2.13-3.5.1.tgz tar -xvf kafka_2.13-3.5.1.tgz
-
2. 配置Kafka
-
修改Kafka配置文件:
-
进入Kafka的
config目录:bash
cd kafka_2.13-3.5.1/config -
编辑
server.properties文件,设置以下参数:broker.id=0 listeners=PLAINTEXT://:9092 log.dirs=/var/lib/kafka/logs zookeeper.connect=localhost:2181
-
-
启动Kafka服务:
bash
bin/kafka-server-start.sh config/server.properties
3. 创建Topic
-
创建一个Topic:
bash
bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 -
查看所有Topic:
bash
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
4. 测试消息传递
-
启动生产者:
-
在一个终端中运行生产者:
bash
bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092 -
输入消息内容并按Enter发送。
-
-
启动消费者:
-
在另一个终端中运行消费者:
bash
bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092 -
查看生产者发送的消息。
-
5. 配置Kafka为后台运行
-
使用
nohup命令启动Kafka和ZooKeeper:bash
nohup bin/zookeeper-server-start.sh config/zookeeper.properties > zookeeper.log 2>&1 & nohup bin/kafka-server-start.sh config/server.properties > kafka.log 2>&1 &