一个jar包启动多个jar服务


在公司开发中,遇到一个很有意思的需求。
我们需要使用一个jar包,启动jar包后读取包内jar资源,释放出来并启动其他服务。
期间涉及到jar包内读取资源,并且释放出来,很简单。

1.将需要捆绑的包放入程序目录

  1. 新建一个与src目录同级目录
  2. 将捆绑的包和启动脚本放入工程目录

启动脚本

这里只是一个demo,以自己的项目为准
#!/bin/sh
source /etc/profile
active=$1
jar_path=$2
echo "start other app"
nohup java -jar -Dspring.profiles.active=${active} -Dfile.encoding=utf-8 ${jar_path} >otherApp.log 2>&1 &
echo "app start is ok"

2.pom文件增加配置

增加配置是用来打包的时候把第三方jar打包进去
    <build>
        <resources>
           <resource>
                <directory>此处填写 第一个步骤创建的目录名称</directory>
                <filtering>false</filtering>
                <includes>
                    <include>*.jar</include>
                    <include>*.sh</include>
                    <include>*.bat</include>
                </includes>
            </resource>
        </resources>
    </build>

3.添加示例代码

该代码支持linux和windows启动jar包后再启动其他的jar服务,主要就是使用了hutool现成的工具类,很简单
package cn.com.xxx.xxx.xxx.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.net.URL;
import java.util.List;

/**
 * 启动后启动其他捆绑服务
 *
 * @author yuaxx
 * @Date 2022/2/27 0027 9:13
 */
@Component
@Slf4j
public class SatrtOtherApp {

    /**
     * windows启动脚本
     */
    private static final String WIN_BAT = "cmd /c start \"{}\" cmd /k \"java -jar -Dfile.encoding=utf-8 -Dspring.profiles.active={} {} \"";

    /**
     * linux启动脚本
     */
    private static final String LINUX_SHELL_START = "start-other.sh";

    /**
     * 描述:是否开启多jar启动
     * 时间:2022/2/27 0027 9:13
     */
    @Value("${hyit.openOther:true}")
    private Boolean openOtherServer;

    /**
     * 当前激活的环境
     * 时间:2022/2/27 0027 9:13
     */
    @Value("${spring.profiles.active:dev}")
    private String active;

    /**
     * 需要绑定的其他服务的名称
     * 时间:2022/2/27 0027 9:13
     */
    @Value("${hyit.otherName:fin-mp-auth-exec.jar}")
    private String otherName;


    @PostConstruct
    public void init() {

        if (!openOtherServer) {
            log.error("其他服务不允许被启动");
            return;
        }
        ThreadUtil.execute(() -> {
            log.info("开始启动{}服务,激活环境:{}", otherName, active);
            List<URL> jarFiles = ResourceUtil.getResources(otherName);
            List<URL> shellFiles = ResourceUtil.getResources(LINUX_SHELL_START);
            if (jarFiles.isEmpty() || shellFiles.isEmpty()) {
                log.error("当前目录未找到需要启动的服务包");
                return;
            }
            String rootPath = SystemUtil.getUserInfo().getCurrentDir();
            String newPath = rootPath + "/" + otherName;
            log.info("当前目录为 :{},jar部署路径为:{}", rootPath, newPath);
            FileUtil.writeFromStream(ResourceUtil.getStream(otherName), new File(newPath));

            if (SystemUtil.getOsInfo().isWindows()) {
                run(newPath);
            } else {
                String newShellPath = rootPath + "/" + LINUX_SHELL_START;
                run(newShellPath, newPath);
            }
        });


    }

    private void run(String newPath) {
        String cmd = StrUtil.format(WIN_BAT, otherName, active, newPath);
        log.info("实际执行启动脚本为:{}", cmd);
        RuntimeUtil.exec(cmd);
    }

    private void run(String newShellPath, String newPath) {
        FileUtil.writeFromStream(ResourceUtil.getStream(LINUX_SHELL_START), new File(newShellPath));
        String auth = "chmod 777 " + newShellPath;
        RuntimeUtil.exec(auth);
        RuntimeUtil.exec(newShellPath, active, newPath);
        log.info("实际执行启动脚本路径为:{}", newShellPath);
    }
}

声明:DungCry.|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - 一个jar包启动多个jar服务


以此热爱,以此谋生。
Le vent se lève, il faut tenter de vivre