该脚本可以自行下载kafka_2.13-3.1.0.tgz并安装服务启动,离线使用请将kafka安装包放到脚本同目录。
笔者喜欢把程序安装成服务的形式。好处有以下几点:
1、方便地设置开机自启
2、启停便捷:不必cd到程序路径下启动,在任何路径下可查看进程状态、启动与停止
3、使用Restart=always等参数可控制进程意外停止后自行启动
systemctl简介
Linux 服务管理两种方式service和systemctl,systemd对应的进程管理命令是systemctl,systemctl命令兼容了service。
systemctl 动作在前,名称在后。service 名称在前,动作在后。例如systemctl start kafka 和 service kafka start。
systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
注意:系统中先安装Java 8+,笔者已经把JAVA_HOME写到/etc/profile中,系统已经自动应用到环境变量里去了,所以服务配置文件中注释掉了Environment=JAVA_HOME=/usr/java/jdk1.8.0_192
监听器地址取的系统第一个IP地址 listeners=PLAINTEXT://${ip}:9092,默认监听配置只能通过本机使用。
安装完默认已启动,使用systemctl status zookeeper、systemctl status kafka查看状态。
#!/bin/bash # by zerlong #安装使用说明 usage() { echo "Usage example: $0 -p /data " echo " -p the install path of this programs." exit 1 } #自定义参数 kfk_filename=kafka_2.13-3.1.0.tgz url=https://dlcdn.apache.org/kafka/3.1.0/$kfk_filename #获取系统第一个IP地址 ip=`ifconfig|awk 'NR==2 {print $2}'` #字体蓝绿变色 cyan() { echo -e "\033[36m\033[1m$1\033[0m" } #安装包检查,本地不存在则在线下载 package_down() { if [[ ! -f `ls $(dirname $0)/$1` ]] ;then echo "未发现本地$1安装包,下载中..." curl -o $(dirname $0)/$1 $url fi } #kafka安装 kafka_install() { if [ -d "${DATA_PATH}/kafka" ] ;then cyan "已存在kafka,跳过安装" else echo 'kafka安装...' package_down $kfk_filename tar -xf $kfk_filename -C ${DATA_PATH} mv ${DATA_PATH}/kafka_* ${DATA_PATH}/kafka sed -i "s#^\#listeners=PLAINTEXT.*#listeners=PLAINTEXT://${ip}:9092#g" ${DATA_PATH}/kafka/config/server.properties cat > /etc/systemd/system/zookeeper.service <<EOF [Unit] Description=Apache Zookeeper server (Kafka) Documentation=http://zookeeper.apache.org Requires=network.target remote-fs.target After=network.target remote-fs.target [Service] Type=simple
#jdk路径
#Environment=JAVA_HOME=/usr/java/jdk1.8.0_192 ExecStart=${DATA_PATH}/kafka/bin/zookeeper-server-start.sh ${DATA_PATH}/kafka/config/zookeeper.properties ExecStop=${DATA_PATH}/kafka/bin/zookeeper-server-stop.sh Restart=always [Install] WantedBy=multi-user.target EOF cat > /etc/systemd/system/kafka.service <<EOF [Unit] Description=Apache Kafka server (broker) Documentation=http://kafka.apache.org/documentation.html Requires=network.target remote-fs.target After=network.target remote-fs.target zookeeper.service [Service] Type=simple
#jdk路径
#Environment=JAVA_HOME=/usr/java/jdk1.8.0_192 ExecStart=${DATA_PATH}/kafka/bin/kafka-server-start.sh ${DATA_PATH}/kafka/config/server.properties ExecStop=${DATA_PATH}/kafka/bin/kafka-server-stop.sh Restart=always RestartSec=3 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl --now enable zookeeper.service sleep 1 systemctl --now enable kafka.service firewall-cmd --zone=public --add-port=9092/tcp --permanent firewall-cmd --reload fi } #检查参数 if [ "$1" = "-p" ];then DATA_PATH=$2 else usage fi #去掉最后的/ SE=${DATA_PATH##*/} if [ "${SE}" = "" ];then DATA_PATH=${DATA_PATH%*/} fi kafka_install
延伸参考:
https://kafka.apache.org/
https://gist.github.com/vipmax/9ceeaa02932ba276fa810c923dbcbd4f
https://blog.csdn.net/skh2015java/article/details/94012643