以下是在Ubuntu上搭建Kafka集群的步骤:
安装Java环境
使用OpenJDK 8/11,执行命令:
sudo apt update
sudo apt install openjdk-11-jdk -y
java -version # 验证安装
安装Zookeeper(传统模式需配置)
wget https://downloads.apache.org/zookeeper/zookeeper-3.8.0/apache-zookeeper-3.8.0-bin.tar.gz
tar -xzf apache-zookeeper-3.8.0-bin.tar.gz
sudo mv apache-zookeeper-3.8.0 /opt/zookeeper
zoo.cfg(需在所有Zookeeper节点修改):dataDir=/var/lib/zookeeper
clientPort=2181
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
/opt/zookeeper/bin/zkServer.sh start
安装Kafka
wget https://downloads.apache.org/kafka/3.6.2/kafka_2.13-3.6.2.tgz
tar -xzf kafka_2.13-3.6.2.tgz
sudo mv kafka_2.13-3.6.2 /usr/local/kafka
server.properties(每节点需唯一broker.id,修改listeners和zookeeper.connect):broker.id=1 # 每个节点唯一
listeners=PLAINTEXT://节点IP:9092
zookeeper.connect=zoo1:2181,zoo2:2181,zoo3:2181 # 传统模式需配置
log.dirs=/tmp/kafka-logs
启动Kafka集群
在每个节点执行:
/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties
验证集群
/usr/local/kafka/bin/kafka-topics.sh --create --bootstrap-server 节点IP:9092 --replication-factor 3 --partitions 1 --topic test
/usr/local/kafka/bin/kafka-topics.sh --list --bootstrap-server 节点IP:9092
# 生产者
/usr/local/kafka/bin/kafka-console-producer.sh --broker-list 节点IP:9092 --topic test
# 消费者
/usr/local/kafka/bin/kafka-console-consumer.sh --bootstrap-server 节点IP:9092 --topic test --from-beginning
注意事项:
server.properties中设置process.roles=broker等参数)。systemd配置开机自启(参考)。