栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

前端控制后台springboot项目停止,启动,重启

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

前端控制后台springboot项目停止,启动,重启

1. windows 端口号被占用 强制删除
netstat -ano|findstr "8080"
taskkill /F /PID 708
2. 思路

模拟容器平台前端控制后台springboot项目停止,启动,重启。
后台需要两个,一个专门负责管理脚本 vue-back-bat.jar,通过脚本控制另一个后台,一个是具体服务 vue-back-manage.jar 都解压后放在D盘
启动
cmd /c start /b javaw -Dfile.encoding=utf-8 -cp . org.springframework.boot.loader.JarLauncher

3. vue-back-bat后台 设置端口9002

使用commons-exec执行bat脚本


     org.apache.commons
     commons-exec
     1.3
 

yml 配置异步请求接口超时时间

spring:
  mvc:
    async:
      # 超时设置15s
      request-timeout: 15000

封装CommandUtil

@Slf4j
@Component
public class CommandUtil {
    
    private static final long TIMEOUT = 10 * 1000L;

    
    public String execCommand(String command) {
        CommandLine cmdLine = CommandLine.parse(command);
        DefaultExecutor executor = new DefaultExecutor();

        // 设置命令执行退出值为0,如果命令成功执行并且没有错误,则返回0
        executor.setExitValue(0);
        // 输出结果
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(baos, baos);
        executor.setStreamHandler(streamHandler);

        ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
        executor.setWatchdog(watchdog);

        try {
            int exitValue = executor.execute(cmdLine);
            if (exitValue == 0) {
                log.info(baos.toString("GBK"));
                return exitValue + "";
            }
        } catch (IOException e) {
            return null;
        }
        return null;
    }

    
    public String execCommandAsync(String command) {
        CommandLine commandLine = CommandLine.parse(command);
        DefaultExecutor executor = new DefaultExecutor();

        // 输出结果
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //接收异常结果流
        ByteArrayOutputStream errStream = new ByteArrayOutputStream();

        //10s超时
        ExecuteWatchdog watchdog = new ExecuteWatchdog(10 * 1000);
        executor.setWatchdog(watchdog);

        PumpStreamHandler streamHandler = new PumpStreamHandler(baos, baos);
        executor.setStreamHandler(streamHandler);

        try {
            int code = executor.execute(commandLine);
            System.out.println("result code: " + code);
            // 不同操作系统注意编码,否则结果乱码
            String suc = baos.toString("GBK");
            String err = errStream.toString("GBK");
            log.info(err);
            return suc;
        } catch (Exception e) {
            watchdog.killedProcess();
        }
        return "";
    }
}

Controller

@Slf4j
@RestController
@RequestMapping("/cmd")
public class CmdController {
    @Autowired
    private SyConfigFileUtil syConfigFileUtil;
    @Autowired
    private CommandUtil commandUtil;

    private static final String RESTART_CMD = "restart.cmd";
    private static final String STOP_CMD = "stop.cmd";

    
    @PostMapping("/restart")
    public Callable restart() {
      return () -> {
          try {
              String command = "cmd /c start /b " + syConfigFileUtil.getClassPath() + "/" + RESTART_CMD;
              log.info(command);
              return commandUtil.execCommandAsync(command);
          } catch (Exception exception) {
              log.info("超时返回");
              return "0";
          }
      };
    }

    
    @PostMapping("/stop")
    public String stop() {
        String command = "cmd /c start /b " + syConfigFileUtil.getClassPath() + "/" + STOP_CMD;
        log.info(command);
        String code = commandUtil.execCommand(command);
        log.info("退出码:{}", code);
        return code;
    }
}

stop.bat

set num=9001
for /f "tokens=5" %%a in ('netstat /ano ^| findstr %num%') do (
	if %%a neq 0 (
		echo 端口号9001的pid为%%a
        taskkill /F /pid %%a
        goto _break
	) else (
		echo 端口号9001未被占用
	)
)

rem 结束for循环
:_break

```
restart.bat
```bat
set num=9001
for /f "tokens=5" %%a in ('netstat /ano ^| findstr %num%') do (
	if %%a neq 0 (
		echo 端口号9001的pid为%%a
        taskkill /F /pid %%a
        goto _break
	) else (
		echo 端口号9001未被占用
	)
)

rem 结束for循环
:_break

rem 切换盘符
D:
rem 这里切换到项目目录内,否则会导致application.pid无法生成
cd D:/vue-back-manage

rem 后台执行 启动springboot项目 cmd /c start /b
java -Dfile.encoding=utf-8 -cp . org.springframework.boot.loader.JarLauncher
```
#### 4. vue-back-manage后台 设置端口9001
获取项目路径工具类SyConfigFileUtil
```java

@Slf4j
@Component
public class SyConfigFileUtil {
    @Value("classpath:restart.cmd")
    private Resource resource;

    
    public String getClassPath() {
        try {
            return resource.getFile().getParentFile().getAbsolutePath();
        } catch (IOException e) {
            return "";
        }
    }

    
    public String getFileContent(String filePath) {
        File file = new File(getClassPath(), filePath);
        log.info("路径:{}", file.getPath());
        String content = null;
        try (InputStream inputStream = new FileInputStream(file)) {
            content = IOUtils.toString(inputStream);
        } catch (IOException e) {
            throw new BizException("读取文件失败");
        }
        return content;
    }

    
    public boolean updateFile(String filePath, String content) {
        File file = new File(getClassPath(), filePath);
        log.info("路径:{}", file.getPath());
        try (OutputStream out = new FileOutputStream(file)) {
            IOUtils.write(content, out, StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new BizException("写入文件失败");
        }
        return true;
    }
}
```
读取pidController
```java
 @Autowired
 private SyConfigFileUtil syConfigFileUtil;
 private static final String APPLICATION_PID = "../../application.pid";
    @PostMapping("/getPID")
    public String getPID() {
        return syConfigFileUtil.getFileContent(APPLICATION_PID);
    }
```

读取ymlController
```java
@Slf4j
@Validated
@RestController
@RequestMapping("/yml")
public class YmlController {
    @Autowired
    private SyConfigFileUtil syConfigFileUtil;

    private static final String CONFIG_FILE = "application.yml";

    
    @PostMapping("/getApplication")
    public String getApplication() {
        return syConfigFileUtil.getFileContent(CONFIG_FILE);
    }

    
    @PostMapping("/updateApplication")
    public boolean updateApplication(@RequestSingleBody @NotBlank(message = "内容不能为空") String content) {
        return syConfigFileUtil.updateFile(CONFIG_FILE, content);
    }

}
```
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/927163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号