部署jar包windows服务工具

打印 上一主题 下一主题

主题 878|帖子 878|积分 2634

背景

某个周末一个线上项目因为服务器自动重启导致了系统挂了,我们是通过jenkins部署的jar包所以需要手动重启项目,解决问题后准备调换部署方式让项目随系统自动启动,试用tomcat后发现启动慢,并且日常开发springboot都是使用内置tomcat启动,如果要保持和部署方式保持一致(避免本地代码执行和部署方式不一致导致的bug),需要配置外部tomcat比较麻烦,所以决定还是以java -jar命令方式启动并注册为window服务
项目地址:https://gitee.com/code2roc/deploy-jar-util
环境依赖


  • windows系统
  • 安装framework4.0
  • 安装jdk配置环境变量
    jdk可以使用免安装版本(1.8)点击bat文件快速一键配置,下载地址如下
    https://yunpan.360.cn/surl_y83kPfrK6n7 (提取码:c4f2)
功能介绍

工具包含【服务名称】【jar包路径】【部署端口】【执行结果】【操作按钮】五个部分

  • 服务名称
对应的就是安装后windows服务的名字

  • jar包路径
部署项目的jar文件物理路径

  • 部署端口
默认为空不指定使用配置文件中端口,指定后使用自定义端口

  • 执行结果
显示安装/卸载/启动/关闭服务适输出的操作日志

  • 操作按钮
在进行服务操作前必须将所有配置确定输入后点击保存配置按钮
安装/卸载/启动/停止四个按钮对应相关windows服务的操作
服务安装后默认停止状态,需要手动启动,服务启动方式为自动
点击启动服务后会自动弹出启动日志界面动态刷新日志内容,若关闭了日志窗口,则进入deploylog文件夹查看deploy.out.log文件,每次启动项目该文件内容自动重置清除



实现介绍

window服务安装

使用开源组件winsw(https://github.com/winsw/winsw/),获取编译好的exe运行文件和xml配置文件,调用cmd进行相关命令操作,例如安装操作如下所示,页面相关配置保存读取直接操作xml文件即可
  1.   private void btn_InstallService_Click(object sender, EventArgs e)
  2.         {
  3.             string command = "deploy.exe install";
  4.             StartCmd(AppDomain.CurrentDomain.BaseDirectory, command, FinishCommand);
  5.         }
  6.   public void StartCmd(String workingDirectory, String command, EventHandler FinsishEvent)
  7.         {
  8.             Process p = new Process();
  9.             p.StartInfo.FileName = "cmd.exe";
  10.             p.StartInfo.WorkingDirectory = workingDirectory;
  11.             p.StartInfo.UseShellExecute = false;
  12.             p.StartInfo.RedirectStandardInput = true;
  13.             p.StartInfo.RedirectStandardOutput = true;
  14.             p.StartInfo.RedirectStandardError = true;
  15.             p.StartInfo.CreateNoWindow = true;
  16.             p.EnableRaisingEvents = true;  // 启用Exited事件  
  17.             p.Exited += FinsishEvent;   // 注册进程结束事件  
  18.             p.Start();
  19.             p.StandardInput.WriteLine(command);
  20.             p.StandardInput.WriteLine("exit");
  21.             p.StandardInput.AutoFlush = true;
  22.             string strOuput = p.StandardOutput.ReadToEnd();
  23.             txt_Result.Text = strOuput;
  24.             //等待程序执行完退出进程
  25.             p.WaitForExit();
  26.             p.Close();
  27.         }
复制代码
服务状态监控

通过引入System.ServiceProcess程序集调用服务相关api
  1.   public void InitOpStatus()
  2.         {
  3.             btn_InstallService.Enabled = false;
  4.             btn_StartService.Enabled = false;
  5.             btn_UnstallService.Enabled = false;
  6.             btn_StopService.Enabled = false;
  7.             var serviceControllers = ServiceController.GetServices();
  8.             bool existservice = false;
  9.             foreach (var service in serviceControllers)
  10.             {
  11.                 if (service.ServiceName == txt_ServerName.Text)
  12.                 {
  13.                     existservice = true;
  14.                     break;
  15.                 }
  16.             }
  17.             if (existservice)
  18.             {
  19.                 var server = serviceControllers.FirstOrDefault(service => service.ServiceName == txt_ServerName.Text);
  20.                 if (server.Status == ServiceControllerStatus.Running)
  21.                 {
  22.                     //服务运行中允许停止
  23.                     btn_StopService.Enabled = true;
  24.                 }
  25.                 else
  26.                 {
  27.                     //服务未运行允许卸载和启动
  28.                     btn_UnstallService.Enabled = true;
  29.                     btn_StartService.Enabled = true;
  30.                 }
  31.             }
  32.             else
  33.             {
  34.                 //无此服务允许安装
  35.                 btn_InstallService.Enabled = true;
  36.             }
  37.         }
复制代码
启动日志显示

使用定时器,不断刷新deploylog\deploy.out.log日志文件
  1.    System.Windows.Forms.Timer timer;
  2.         public LogForm()
  3.         {
  4.             InitializeComponent();
  5.         }
  6.         private void LogForm_Load(object sender, EventArgs e)
  7.         {
  8.             timer = new System.Windows.Forms.Timer();
  9.             //1秒间隔
  10.             timer.Interval = 1000;
  11.             //执行事件
  12.             timer.Tick += (s, e1) =>
  13.             {
  14.                 RefreshLogContent();
  15.             };
  16.             //开始执行
  17.             timer.Start();
  18.         }
  19.         public void RefreshLogContent()
  20.         {
  21.             string logPath = AppDomain.CurrentDomain.BaseDirectory + "deploylog\\deploy.out.log";
  22.             string logContent = ReadFileContent(logPath);
  23.             SetTextCallback d = new SetTextCallback(SetText);
  24.             this.txt_Log.Invoke(d, new object[] { logContent });
  25.         }
  26.         public string ReadFileContent(string FileFullName)
  27.         {
  28.             if (File.Exists(FileFullName))
  29.             {
  30.                 System.IO.FileStream fs = new System.IO.FileStream(FileFullName, System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  31.                 string FileContent = "";
  32.                 try
  33.                 {
  34.                     int fsLen = Convert.ToInt32(fs.Length);
  35.                     byte[] heByte = new byte[fsLen];
  36.                     int r = fs.Read(heByte, 0, heByte.Length);
  37.                     FileContent = System.Text.Encoding.Default.GetString(heByte);
  38.                 }
  39.                 catch (Exception e)
  40.                 {
  41.                     throw;
  42.                 }
  43.                 finally
  44.                 {
  45.                     fs.Close();
  46.                     fs.Dispose();
  47.                 }
  48.                 return FileContent;
  49.             }
  50.             else
  51.             {
  52.                 return "";
  53.             }
  54.         }
  55.         delegate void SetTextCallback(string text);
  56.         private void SetText(string text)
  57.         {
  58.             txt_Log.Text = "";
  59.             txt_Log.AppendText(text);
  60.         }
  61.         private void LogForm_FormClosed(object sender, FormClosedEventArgs e)
  62.         {
  63.             timer.Stop();
  64.         }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

耶耶耶耶耶

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表