#!/bin/bash
echo "安装KAFKA2.12-2.4.1,按任意键继续"
read -n1
mkdir -p /data/software
cd /data/software
curl -O http://down.yadeaiot.net:8000/kafka_2.12-2.4.1.tgz
echo '#kafka
export KAFKA_HOME=/usr/local/kafka
export PATH=$PATH:$KAFKA_HOME/bin' >> /etc/profile
source /etc/profile
tar -zxvf /data/software/kafka_2.12-2.4.1.tgz
cd  /data/software/kafka_2.12-2.4.1/ 
mkdir -p /usr/local/kafka
cp -r * /usr/local/kafka
mkdir -p /usr/local/kafka/log/kafka
cd /usr/local/kafka/config
sed -i '35ilisteners=PLAINTEXT://:9092' /usr/local/kafka/config/server.properties
sed -i "s@log.dirs=/tmp/kafka-logs@log.dirs=/usr/local/kafka/log/kafka@g" /usr/local/kafka/config/server.properties

echo 配置zookeeper
sleep 3
mkdir -p /usr/local/kafka/zookeeper
mkdir -p /usr/local/kafka/log/zookeeper
cd /usr/local/kafka/config
echo 'dataDir=/usr/local/kafka/zookeeper 
dataLogDir=/usr/local/kafka/log/zookeeper 
clientPort=2181
maxClientCnxns=100
tickTime=2000
initLimit=10
syncLimit=5' >> /usr/local/kafka/config/zookeeper.properties
echo 创建启动、关闭kafka脚本
sleep 2
cd /usr/local/kafka
echo '#!/bin/sh
#启动zookeeper
/usr/local/kafka/bin/zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties &
sleep 3 #等3秒后执行
#启动kafka
/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties &' >> /usr/local/kafka/kafkastart.sh
chmod 777 /usr/local/kafka/kafkastart.sh
echo '#!/bin/sh
#关闭zookeeper
nohup /usr/local/kafka/bin/zookeeper-server-stop.sh /usr/local/kafka/config/zookeeper.properties &
sleep 10 #等3秒后执行
#关闭kafka
nohup /usr/local/kafka/bin/kafka-server-stop.sh /usr/local/kafka/config/server.properties &' >> /usr/local/kafka/kafkastop.sh
chmod 777 /usr/local/kafka/kafkastop.sh

echo 设置脚本开机自动执行
echo 'sh /usr/local/kafka/kafkastart.sh & ' >> /etc/rc.d/rc.local
echo '以上，kafka安装配置完成，安装目录/usr/local/kafka,端口9092,sh /usr/local/kafka/kafkastart.sh #启动kafka,sh /usr/local/kafka/kafkastop.sh #关闭kafka'
sh /usr/local/kafka/kafkastart.sh &
