关于java:部署jar包windows服务工具

3次阅读

共计 3589 个字符,预计需要花费 9 分钟才能阅读完成。

背景

某个周末一个线上我的项目因为服务器主动重启导致了零碎挂了,咱们是通过 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 文件即可

  private void btn_InstallService_Click(object sender, EventArgs e)
        {
            string command = "deploy.exe install";
            StartCmd(AppDomain.CurrentDomain.BaseDirectory, command, FinishCommand);
        }
  public void StartCmd(String workingDirectory, String command, EventHandler FinsishEvent)
        {Process p = new Process();
            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(command);
            p.StandardInput.WriteLine("exit");
            p.StandardInput.AutoFlush = true;
            string strOuput = p.StandardOutput.ReadToEnd();
            txt_Result.Text = strOuput;
            // 期待程序执行完退出过程
            p.WaitForExit();
            p.Close();}

服务状态监控

通过引入 System.ServiceProcess 程序集调用服务相干 api

  public void InitOpStatus()
        {
            btn_InstallService.Enabled = false;
            btn_StartService.Enabled = false;
            btn_UnstallService.Enabled = false;
            btn_StopService.Enabled = false;
            var serviceControllers = ServiceController.GetServices();
            bool existservice = false;
            foreach (var service in serviceControllers)
            {if (service.ServiceName == txt_ServerName.Text)
                {
                    existservice = true;
                    break;
                }
            }
            if (existservice)
            {var server = 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 timer;
        public LogForm()
        {InitializeComponent();
        }

        private void LogForm_Load(object sender, EventArgs e)
        {timer = new System.Windows.Forms.Timer();
            // 1 秒距离
            timer.Interval = 1000;
            // 执行事件
            timer.Tick += (s, e1) =>
            {RefreshLogContent();
            };
            // 开始执行
            timer.Start();}


        public void RefreshLogContent()
        {
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "deploylog\\deploy.out.log";
            string logContent = ReadFileContent(logPath);
            SetTextCallback d = new SetTextCallback(SetText);
            this.txt_Log.Invoke(d, new object[] {logContent});
        }

        public string ReadFileContent(string FileFullName)
        {if (File.Exists(FileFullName))
            {System.IO.FileStream fs = new System.IO.FileStream(FileFullName, System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                string FileContent = "";
                try
                {int fsLen = Convert.ToInt32(fs.Length);
                    byte[] heByte = new byte[fsLen];
                    int r = fs.Read(heByte, 0, heByte.Length);
                    FileContent = System.Text.Encoding.Default.GetString(heByte);
                }
                catch (Exception e)
                {throw;}
                finally
                {fs.Close();
                    fs.Dispose();}
                return FileContent;
            }
            else
            {return "";}

        }

        delegate void SetTextCallback(string text);

        private void SetText(string text)
        {
            txt_Log.Text = "";
            txt_Log.AppendText(text);
        }

        private void LogForm_FormClosed(object sender, FormClosedEventArgs e)
        {timer.Stop();
        }
正文完
 0