当前位置: 首页 > 后端技术 > Java

部署jar包windows服务工具

时间:2023-04-01 15:29:45 Java

背景某周末,一个在线项目,由于服务器自动重启导致系统挂掉。我们通过jenkins部署了jar包,所以需要手动重启项目。解决问题后,我们打算更改部署方式,让项目随系统自动启动。试过tomcat后,发现启动慢,springboot的日常开发都是用自带的tomcat启动的。如果要和部署方式保持一致(避免本地代码执行和部署方式不一致导致的bug),配置外部tomcat很麻烦,所以决定使用java-jar命令方式启动并注册为一个window服务项目地址:https://gitee.com/code2roc/de...环境依赖windows系统安装framework4.0安装jdk配置环境变量。jdk可以使用免安装版(1.8)点击bat文件快速一键配置,下载地址如下https://yunpan.360.cn/surl_y8...(提取码:c4f2)功能介绍工具包括【服务名称】【jar包路径】【部署端口】【执行结果】】【操作按钮】服务名称五部分对应安装后的windows服务名称。jar包路径为部署项目的jar文件的物理路径。部署端口默认为空。未指定配置文件中的端口。指定后使用自定义端口执行结果显示安装/卸载/启动/关闭服务输出的操作日志操作按钮。在执行服务操作之前,必须确认并输入所有配置。点击保存配置按钮后,安装/卸载/启动/停止四个按钮对应相关windows服务的操作。服务安装后默认停止状态,需要手动启动。服务启动方式是自动点击启动服务,会自动弹出启动日志界面动态刷新日志内容。如果日志窗口关闭,进入deploylog文件夹查看deploy.out.log文件。启动项目。自动重置并清除文件内容。window服务安装介绍使用开源组件winsw(https://github.com/winsw/winsw/)获取编译好的exe运行文件和xml配置文件,调用cmd执行相关命令操作,例如安装操作如下,页面相关配置保存并直接读取操作xml文件privatevoidbtn_InstallService_Click(objectsender,EventArgse){stringcommand="deploy.exeinstall";StartCmd(AppDomain.CurrentDomain.BaseDirectory,命令,FinishCommand);}publicvoidStartCmd(StringworkingDirectory,Stringcommand,EventHandlerFinsishEvent){Processp=newProcess();p.StartInfo.FileName="cmd.exe";p.StartInfo.WorkingDirectory=workingDirectory;p.StartInfo.UseShellExecute=false;p.StartInfo.RedirectStandardInput=true;p.StartInfo.RedirectStandardOutput=true;p.StartInfo.RedirectStandardError=true;p.StartInfo.CreateNoWindow=true;p.EnableRaisingEvents=true;//启动Exited事件p.Exited+=FinsishEvent;//注册程序结束程序p.Start();p.StandardInput.WriteLine(命令);p.StandardInput.WriteLine("退出");p.StandardInput.AutoFlush=true;字符串strOuput=p.StandardOutput.ReadToEnd();txt_Resu文件lt.Text=strOuput;//等待程序完成并退出进程p.WaitForExit();p.关闭();}服务状态监控通过引入System.ServiceProcess程序集调用服务相关APIpublicvoidInitOpStatus(){btn_InstallService.Enabled=false;btn_StartService.Enabled=false;btn_UnstallService.Enabled=false;btn_StopService.Enabled=false;varserviceControllers=ServiceController.GetServices();布尔存在服务=假;foreach(varserviceinserviceControllers){if(service.ServiceName==txt_ServerName.Text){existservice=true;休息;}}if(existservice){varserver=serviceControllers.FirstOrDefault(service=>service.ServiceName==txt_ServerName.Text);if(server.Status==ServiceControllerStatus.Running){//服务正在运行,允许停止btn_StopService.Enabled=true;}else{//服务没有运行,允许卸载并启动btn_UnstallService.Enabled=true;btn_StartService.Enabled=true;}}else{//不允许这样的服务安装btn_InstallService.Enabled=true;}}启动日志显示使用定时器刷新deploylog\deploy.out.log日志文件System.Windows.Forms.Timer定时器;公共LogForm(){InitializeComponent();}privatevoidLogForm_Load(objectsender,EventArgse){timer=newSystem.Windows.Forms.Timer();//1秒间隔timer.Interval=1000;//执行事件timer.Tick+=(s,e1)=>{RefreshLogContent();};//开始执行timer.Start();}公共电话idRefreshLogContent(){stringlogPath=AppDomain.CurrentDomain.BaseDirectory+"deploylog\\deploy.out.log";字符串logContent=ReadFileContent(logPath);SetTextCallbackd=newSetTextCallback(SetText);this.txt_Log.Invoke(d,newobject[]{logContent});}publicstringReadFileContent(stringFileFullName){if(File.Exists(FileFullName)){System.IO.FileStreamfs=newSystem.IO.FileStream(FileFullName,System.IO.FileMode.Open,FileAccess.Read,FileShare.ReadWrite);字符串文件内容="";尝试{intfsLen=Convert.ToInt32(fs.Length);byte[]heByte=newbyte[fsLen];intr=fs.Read(heByte,0,heByte.Length);FileContent=System.Text.Encoding.Default.GetString(heByte);}赶上(异常e){抛出;}最后{fs.Close();fs.Dispose();}返回文件内容;}else{返回“”;}}委托voidSetTextCallback(stringtext);privatevoidSetText(stringtext){txt_Log.Text="";txt_Log.AppendText(文本);}privatevoidLogForm_FormClosed(objectsender,FormClosedEventArgse){timer.Stop();}