一、环境准备
sudo apt update && sudo apt install openjdk-11-jdk -ysudo yum install java-11-openjdk -yjava -version(需显示Java版本信息)。二、下载并解压Kafka
wget https://downloads.apache.org/kafka/3.6.1/kafka_2.13-3.6.1.tgz(2.13为Scala版本,可根据需求调整)。/opt/kafka):tar -xzf kafka_2.13-3.6.1.tgz && mv kafka_2.13-3.6.1 /opt/kafka。三、配置Kafka
cd /opt/kafka/config。server.properties(核心配置文件),修改以下关键参数:
broker.id=0:Broker唯一标识(集群中需唯一);listeners=PLAINTEXT://:9092:监听端口(客户端通信端口);log.dirs=/data/kafka/logs:日志存储目录(需提前创建并授权:sudo mkdir -p /data/kafka/logs && sudo chown -R kafka:kafka /data/kafka);zookeeper.connect=localhost:2181:ZooKeeper连接地址(若使用KRaft模式,需修改为controller.quorum.voters=0@localhost:9093,并参考KRaft配置)。四、启动ZooKeeper(传统模式)
bin子目录:cd /opt/kafka/bin。./zookeeper-server-start.sh ../../config/zookeeper.propertiesnohup或systemd后台运行。五、启动Kafka
bin子目录:cd /opt/kafka/bin。./kafka-server-start.sh ../../config/server.properties-daemon参数后台运行:./kafka-server-start.sh -daemon ../../config/server.properties。六、验证Kafka功能
test-topic,1个分区,1个副本):./kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1。./kafka-topics.sh --list --bootstrap-server localhost:9092。test-topic发送消息:./kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092Hello Kafka)后按回车键发送。test-topic接收消息:./kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092七、可选:配置KRaft模式(无ZooKeeper) 若使用Kafka 3.4+版本,可通过KRaft模式摆脱ZooKeeper依赖:
config/kraft/server.properties,设置process.roles=broker,controller、controller.quorum.voters=0@localhost:9093(0为Broker ID);./kafka-storage.sh format -t $(./kafka-storage.sh random-uuid) -c config/kraft/server.properties;./kafka-server-start.sh -daemon config/kraft/server.properties。