设为首页 加入收藏

TOP

从零构建可视化jar包部署平台JarManage(一)
2023-07-25 21:34:17 】 浏览:56
Tags:从零构 jar 包部署 平台 JarManage

项目背景

java项目部署过程中,由于内外部各种因素,可能会遇到一些感觉操作不便捷的场景,例如

  • jar包未随系统自动启动需要每次手动重启
  • 系统vpn堡垒机多重防御更新繁琐
  • 系统无图形化界面命令行操作复杂
  • 等等......

在工作中之前也总结了windows的Jar包部署工具linux下的jar包自动化部署脚本,这次就想着否能将二者统一结合,本着简单/高效/功能专一的原则,做出一

个可视化jar包部署平台,JarManage应运而生

功能介绍

项目地址:https://gitee.com/code2roc/jar-manage

支持在线创建项目,上传Jar包,自动备份,配置启动参数,注册系统服务,查看启动日志等功能,具有以下优点

  • 基于servlet开发,依赖简洁,部署包10MB左右
  • 结合嵌入式tomcat一键部署,无外部容器依赖
  • 使用h2db存储数据,无外部数据库依赖
  • 适配windows/linux平台,满足多种环境
  • 具体项目经平台部署后自动注册系统服务,无需担心服务器重启

系统架构图如下

系统截图展示

技术分析

平台识别

首先通过系统os识别是windows平台还是linux平台

String os = System.getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
   platform = DepolyPlatform.Windows;
}

通过system-release文件识别部分基于CentOS开发的Linux系统

String command = "cat /etc/system-release";
String result = CMDUtil.executeLinuxCommand(command);
if (result.startsWith("Red Hat")) {
   platform = DepolyPlatform.LinuxRedHat;
} else if (result.startsWith("CentOS")) {
   platform = DepolyPlatform.LinuxCentOS;
} else if (result.startsWith("openEuler")) {
   platform = DepolyPlatform.LinuxOpenEuler;
}

通过issue文件识别部分基于Ubuntu/Debian开发的Linux系统

command = "cat /etc/issue";
result = CMDUtil.executeLinuxCommand(command);
if (!StringUtil.isEmpty(result)) {
  if (result.startsWith("Ubuntu")) {
     platform = DepolyPlatform.LinuxUbuntu;
} else if (result.startsWith("Debian")) {
      platform = DepolyPlatform.LinuxDebian;
   }
}

windows注册服务

通过sc query命令判断服务状态

    public String getStatus(String serviceName) {
        String status = DepolyStatus.UnInstall;
        try {
            String command = "sc query " + serviceName;
            String commandResultFilePath = CMDUtil.executeWindowCommandStoreFile(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(commandResultFilePath)));
            String line = reader.readLine();
            while (line != null) {
                if (line.trim().startsWith("STATE")) {
                    if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("1"))
                        status = DepolyStatus.Stopped;
                    else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("2"))
                        status = DepolyStatus.Startting;
                    else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("3"))
                        status = DepolyStatus.Stopping;
                    else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("4"))
                        status = DepolyStatus.Running;
                }
                line = reader.readLine();
            }
        } catch (IOException e) {
            LogUtil.error(e);
        }
        return status;
    }

通过winsw这个开源项目配置exe和xml文件将jar包注册为windows服务,项目地址:https://github.com/winsw/winsw/

linux注册服务

通过systemctl status命令判断服务状态

    public String getStatus(String serviceName) {
        String status = DepolyStatus.UnInstall;
        try {
            String command = "systemctl status " + serviceName;
            String commandResultFilePath = CMDUtil.executeLinuxCommandWithStore(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(commandResultFilePath)));
            String line = reader.rea
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Spring Boot 多数据源配置 下一篇IDEA项目下out与target目录的区别..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目