最终效果:

先回顾命令行的启动方式:

kafka的启动

进入kafka的安装目录

1、首先启动zookeeper服务:

bin/zookeeper-server-start.sh config/zookeeper.properties

2、再启动kafka

bin/kafka-server-start.sh config/server.properties &

以上这种方式其他太繁琐,以系统服务启动更加简便如:

systemctl start zookeeper && systemctl start kafka

下面介绍实现方式,注意将(/usr/local/kafka_2.13-3.5.0)替换成自己的kafka安装路径

Step-01:创建服务文件

创建 /usr/lib/systemd/system/zookeeper.service 文件,并写入以下内容

[Unit]Requires=network.targetAfter=network.target[Service]Type=simpleLimitNOFILE=1048576ExecStart=/usr/local/kafka_2.13-3.5.0/bin/zookeeper-server-start.sh /usr/local/kafka_2.13-3.5.0/config/zookeeper.propertiesExecStop=/usr/local/kafka_2.13-3.5.0/bin/zookeeper-server-stop.shRestart=Always[Install]WantedBy=multi-user.target

创建 /usr/lib/systemd/system/kafka.service 文件,并写入以下内容

[Unit]Requires=zookeeper.serviceAfter=zookeeper.service[Service]Type=simpleLimitNOFILE=1048576ExecStart=/usr/local/kafka_2.13-3.5.0/bin/kafka-server-start.sh /usr/local/kafka_2.13-3.5.0/config/server.properties ExecStop=/usr/local/kafka_2.13-3.5.0/bin/kafka-server-stop.shRestart=Always[Install]WantedBy=multi-user.target

Step-02:完成后重启系统服务:

systemctl daemon-reload

Step-03:使用指令

下面就可以正常使用了,相关命令如下:

systemctl enable zookeeper && systemctl enable kafka#自启动systemctl start zookeeper && systemctl start kafka#启动服务systemctl status zookeeper && systemctl status kafka#查看服务状态