kafka(9092)
1.上传文件
tar -xvf kafka_2.11-1.0.2.tgz -C /opt
cd /opt
mv kafka_2.11-1.0.2 kafka
2.创建logs文件
cd /opt/kafka
mkdir logs
3.修改配置文件
vim /opt/kafka/config/server.properties
...
broker.id=1
delete.topic.enable=true
listeners=PLAINTEXT://192.168.94.10:9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/opt/module/kafka/logs
num.partitions=1
num.recovery.threads.per.data.dir=1
log.retention.hours=168
zookeeper.connect=192.168.94.10:2181,192.168.94.11:2181,192.168.94.12:2181
4.配置环境变量
vi /etc/profile
...
export KAFKA_HOME=/opt/kafka
export PATH=$PATH:$KAFKA_HOME/bin
:wq
source /etc/profile
scp -r /etc/profile 192.168.94.11:/etc
scp -r /etc/profile 192.168.94.12:/etc
5.把修改好的安装包分发到各个节点
scp -r /opt/kafka root@192.168.94.11:/opt
scp -r /opt/kafka root@192.168.94.12:/opt
6.修改各个节点的配置文件
vim /opt/kafka/config/server.properties
broker.id=2
delete.topic.enable=true
listeners=PLAINTEXT://192.168.94.11:9092
vim /opt/kafka/config/server.properties
broker.id=3
delete.topic.enable=true
listeners=PLAINTEXT://192.168.94.12:9092
7.编写批量启动脚本
vim /opt/kafka-all.sh
#!/bin/bash
case $1 in
"start"){
for i in 192.168.94.10 192.168.94.11 192.168.94.12
do
echo --------------------------kafka $i 启动 -------------------
ssh $i "source /etc/profile && /opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server.properties"
if [ $? -eq 0 ]; then
echo -e "\033[32m 启动成功 \033[0m"
else
echo -e "\033[31m 启动失败 \033[0m"
fi
done
}
;;
"stop"){
for i in 192.168.94.10 192.168.94.11 192.168.94.12
do
echo --------------------------kafka $i 关闭 -------------------
ssh $i "/opt/kafka/bin/kafka-server-stop.sh"
if [ $? -eq 0 ]; then
echo -e "\033[32m 关闭成功 \033[0m"
else
echo -e "\033[31m 关闭失败 \033[0m"
fi
done
}
;;
""){
echo "参数选项 start|stop"
}
;;
esac
:wq
chmod +x kafka-all.sh
/opt/kafka-all.sh start
/opt/kafka-all.sh stop
8.kafka命令
kafka-topics.sh --zookeeper 192.168.94.10:2181 --create --replication-factor 1 --partitions 1 --topic first
选项说明:
--topic 定义topic名
--replication-factor 定义副本数
--partitions 定义分区数
kafka-topics.sh --zookeeper 192.168.94.10:2181 --list
kafka-console-producer.sh --broker-list 192.168.94.10:9092 --topic first
kafka-console-consumer.sh --bootstrap-server 192.168.94.11:9092 --from-beginning --topic first