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

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

重启springboot的更多相关文章

  1. Linux编辑启动停止重启springboot jar包脚本

    springboot的配置文件中,配置文件的名字都有各自的意义跟用途 dev 开发环境 prod 生产环境(默认) test 测试环境 加载指定配置文件 --spring.profiles.activ ...

  2. 从源码研究如何不重启Springboot项目实现redis配置动态切换

    上一篇Websocket的续篇暂时还没有动手写,这篇算是插播吧.今天讲讲不重启项目动态切换redis服务. 背景 多个项目或微服务场景下,各个项目都需要配置redis数据源.但是,每当运维搞事时(修改 ...

  3. Springboot项目修改html后不需要重启---springboot项目的热部署

    一.spring-boot-devtools 在pom中直接引入依赖 <dependency> <groupId>org.springframework.boot</gr ...

  4. SpringBoot(六):springboot热部署

    在j2ee项目开发中,热部署插件是JRebel.JRebel的使用为开发人员带来了极大的帮助,且挺高了开发便捷.而在SpringBoot开发生态环境中,SpringBoot热部署常用插件是:sprin ...

  5. SpringBoot热部署-解决方案

    在SpringBoot中启用热部署是非常简单的一件事,因为SpringBoot为我们提供了一个非常方便的工具spring-boot-devtools,我们只需要把这个工具引入到工程里就OK了,下面我就 ...

  6. SpringBoot之解决云服务器VPS在所处云端集群的内网不能解析域名的问题:java.net.UnknownHostException:abc.cn: Temporary failure in name resolution

    一.起因与原因分析过程 前端小伙伴儿告诉我,说服务器崩了. 请求数据接口,接口有响应,但报的json提示指向:数据库异常错误. 遂登陆云主机查看日志,核心记录显示如下: 2018-11-09 22:1 ...

  7. SpringBoot中动态加载(热部署)

    在常规的Java Web开发过程中,在修改完代码后,往往需要重启Tomcat来使得我们的修改生效,在SpringBoot中也需要从新启动SpringBoot来将修改部署.如果我们不希望重启tomcat ...

  8. IntelliJ IDEA 2017版 SpringBoot的关闭自动配置和自定义Banner

    一.关闭自动配置 在jar包下找下边的名字    设置关闭自动配置jar    多个的时候配置       二.自定义Banner   (1)网站搜索一个图案.网址:http://patorjk.co ...

  9. springboot整合mybatis增删改查(二):springboot热部署

    SpringBoot整合热部署 传统情况下, 我们用idea运行springboot程序时, 如果我们需要修改类里的方法,或者其他信息 我们需要修改完保存,并且重启springboot,有时候会很浪费 ...

随机推荐

  1. 面经手册 · 第8篇《LinkedList插入速度比ArrayList快?你确定吗?》

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你以为考你个数据结构是要造火箭? 汽车75马力就够奔跑了,那你怎么还想要2.0涡轮+ ...

  2. element UI dialog 固定高度 且关闭时清空数据

    解决方法:在dialog里写一个div ,div的大小设置为相对视窗的大小就行 <el-dialog title="xxx" :visible.sync="dial ...

  3. Xutils 的框架问题retry error, curr request is null Android开发之网络请问问题

    没有网络权限也能导致这个问题 也可能是因为模拟机没联网的问题

  4. ract-native常用命令

    1.新建项目:react-native init AwesomeProject 2.运动项目 cd AwesomeProject react-native run-ios 3.添加第三方插件: yar ...

  5. Educational Codeforces Round 65 (Rated for Div. 2)(ACD)B是交互题,不怎么会

    A. Telephone Number A telephone number is a sequence of exactly 11 digits, where the first digit is  ...

  6. 发送信息到邮箱的第三方扩展库PHPMailer使用方法

    一.下载 使用composer下载PHPMailer :composer require phpmailer/phpmailer 二.使用实例 use PHPMailer\PHPMailer\PHPM ...

  7. vue 在使用数组的时候,数组内部数据发生变化,视图却没有改变

    data(){ return{ todos: [ {name: 'aa', age: 22}, {name: 'bb', age: 23} ] } } methods:{ changeTodos(){ ...

  8. netty如何进行单元测试

    一种特殊的Channel 实现——EmbeddedChannel,它是Netty 专门为改进针对ChannelHandler 的单元测试而提供的. 将入站数据或者出站数据写入到EmbeddedChan ...

  9. 白嫖党看番神器 AnimeSeacher

    - Anime Searcher - 简介 通过整合第三方网站的视频和弹幕资源, 给白嫖党提供最舒适的看番体验~ 目前有 4 个资源搜索引擎和 2 个弹幕搜索引擎, 资源丰富, 更新超快, 不用下载, ...

  10. PHP木马免杀的一些总结

    前言 这篇文章写一些php木马免杀的一些技巧,希望对大家有点帮助.这里解释一下什么是php木马,这里大体分为三种: 能完成写入文件.列目录.查看文件.执行一些系统命令等少量功能的,这种的是" ...