和黑猫之家聊聊?
项目需求:将kafka启动注册为windows系统服务
开发环境中使用批处理一键启动,但是生产环境中需要注册为系统服务方便维护

原始批处理代码:
@echo off
start cmd /k "bin\windows\zookeeper-server-start.bat config\zookeeper.properties"
start cmd /k "bin\windows\kafka-server-start.bat config\server.properties"
exit1234567
使用winSW工具将批处理bat注册为windows系统服务步骤
1.下载winSW,地址:http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/
2.将.exe和.xml放到kafka根目录

配置winSW.xml
<service>
<!-- 指定在Windows系统内部使用的识别服务的ID。在系统中安装的所有服务中,这必须是唯一的,它应该完全由字母数字字符组成 -->
<id>kafka</id>
<!-- 服务的简短名称,它可以包含空格和其他字符。尽量简短,就像“id”一样,在系统的所有服务名称中,也要保持唯一 -->
<name>Kafka Service</name>
<!-- 该服务可读描述。当选中该服务时,它将显示在Windows服务管理器中 -->
<description>A Service For Kafka</description>
<!-- 该元素指定要启动的可执行文件 -->
<executable>%BASE%\startup.bat</executable>
<!-- 日志输出位置 -->
<logpath>%BASE%\serviceLogs</logpath>
</service>123456789101112
3.修改startup.bat启动文件,choice目的暂停5s让服务保持运行状态
@echo off
start cmd /k "bin\windows\zookeeper-server-start.bat config\zookeeper.properties"
start cmd /k "bin\windows\kafka-server-start.bat config\server.properties"
:start
echo pause 5s
choice /t 5 /d y /n >nul
goto start1234567891011
4.安装服务脚本install.bat
@echo off
start cmd /k "winSW.exe install"
exit12345
5.卸载服务脚本uninstall.bat
@echo off
start cmd /k "winSW.exe uninstall"
exit12345
6.运行安装服务脚本install.bat

7.使用kafkatool工具测试是否启动成功

黑猫之家原创文章
来黑猫之家看看呗