重启springboot

前言:springboot项目开发时,会遇到项目重新启动的情况。在百度上资料比较零碎需要整理,实践时需要踩坑,自己在项目中已经实现的功能拿出来与大家分享。希望每一位coder能在编程的路上少走一些弯路。

重启springboot

1. 开发环境(development)

  • 在application.properties文件中加入如下的配置信息,为动态修改配置信息提供数据:
spring.application.name= SPRING-BOOT-APPLICATION
  • 在springboot主类中定义两个私有变量,用于存放main()方法的参数和SpringApplication.run()方法返回的值:
public class SpringbootwebApplication {
    @Value( "${spring.application.name}" )
    String appName;
    private static String[] args;
    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        SpringbootwebApplication.args = args;
        SpringbootwebApplication.context = SpringApplication.run(SpringbootwebApplication.class, args);
    }
}
  • RESTful API
@GetMapping("/api/refresh")
public Object restart(){
    try {
        ExecutorService threadPool = new ThreadPoolExecutor(1,1,0, TimeUnit.SECONDS,new ArrayBlockingQueue<>( 1 ),new ThreadPoolExecutor.DiscardOldestPolicy ());
        threadPool.execute (()->{
            context.close();
            context = SpringApplication.run(SpringbootwebApplication.class,args);
            } );
            threadPool.shutdown();
            return ResultBody.success("重启网站成功");
        } catch (Exception e) {
            throw new Exception(CommonEnum.INTERNAL_SERVER_ERROR);
        }
}

2. 生产环境(production)

项目打成jar包完成部署以后,生产环境重启失效项目缓存并未清理未达到预计效果,此时需要用脚本重启。

  • idea>settings>plugins search BashSupport

  • create bash file

  • bash linux restart. use: sh restart.sh restart
#!/bin/bash
#脚本与jar包放在同一目录下
#可替换为你自己的执行程序,其他代码无需更改
APP_NAME=springbootweb-0.0.1-SNAPSHOT.jar
#使用说明,用来提示输入参数
usage() {
    echo "Usage: sh restart.sh [start|stop|restart|status]"
    exit 1
}

#检查程序是否在运行
is_exist(){
  pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
  #如果不存在返回1,存在返回0
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}

#启动方法
start(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is already running. pid=${pid} ."
  else
    nohup java -jar $APP_NAME > nohup.out 2>&1 &
    echo "${APP_NAME} running successfully";
  fi
}

#停止方法
stop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
    echo "${APP_NAME} Closing successfully"
  else
    echo "${APP_NAME} is not running"
  fi
}

#输出运行状态
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. Pid is ${pid}"
  else
    echo "${APP_NAME} is NOT running."
  fi
}

#重启
restart(){
  stop
  start
}

#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  *)
    usage
    ;;
esac

到了这一步其实还未结束,当你在网上一步步走到这里的时候以为linux敲个命令 sh restart.sh start 就万事大吉了。
其实这个时候bash很可能还不能用,因为开发环境是windows的bash,此时使用会异常。
解决方案:
使用dos2unix命令,转译bash > dos2unix restart.sh(需要安装插件)
1.下载插件
   Dos2Unix
2.上传taz。此时是压缩包格式用命令解压安装

tar -zxvf hd2u-1.0.0.tgz
cd hd2u-1.0.0.tgz
./configure && make && make install

3.此时如果安装不上dos2unix。此时需要另外一个插件popt
   Popt-1.18

tar -zxvf popt-1.18.tar.gz
cd popt-1.18
./configure && make && make install

4.验证一下Dos2Unix安装成功与否。

dos2unix -v

5.大功告成 转译。然后就可以愉快的使用bash了

dos2unix restart.sh
原文地址:https://www.cnblogs.com/wufanJY/p/13754743.html