kafka运行就像上篇提到的那样执行一下脚本,但是一关机一重启就又需要重新启动。

  • zookeeper-server-start.sh
#开一个终端 zookeeper
./zookeeper-server-start.sh ./../config/zookeeper.properties 
  • kafka-server-start.sh
#再开一个终端 kafka
./kafka-server-start.sh ./../config/server.properties 

所以需要配置一下kafka的开机自启。

1.zookeeper.service

vi /lib/systemd/system/zookeeper.service
[Unit]
Description=Zookeeper service
After=network.target

[Service]
Type=simple
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/jdk-16.0.1/bin"
User=root
Group=root
ExecStart=/home/randyfield/kafka-release/kafka_2.12-2.8.0/bin/zookeeper-server-start.sh /home/randyfield/kafka-release/kafka_2.12-2.8.0/config/zookeeper.properties
ExecStop=/home/randyfield/kafka-release/kafka_2.12-2.8.0/bin/zookeeper-server-stop.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

2.kafka.service

vi /lib/systemd/system/kafka.service
[Unit]
Description=Apache Kafka server (broker)
After=network.target  zookeeper.service

[Service]
Type=simple
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/jdk-16.0.1/bin"
User=root
Group=root
ExecStart=/home/randyfield/kafka-release/kafka_2.12-2.8.0/bin/kafka-server-start.sh /home/randyfield/kafka-release/kafka_2.12-2.8.0/config/server.properties
ExecStop=/home/randyfield/kafka-release/kafka_2.12-2.8.0/bin/kafka-server-stop.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

3.启动服务

#刷新配置
systemctl daemon-reload

#服务加入开机自启
systemctl enable zookeeper
systemctl enable kafka

#启动/关闭/重启 启动kafka前必须先启动zookeeper
systemctl start zookeeper
systemctl start kafka

#查看状态
systemctl status zookeeper