当前位置: 首页 > news >正文

口重启Spring Boot项目中,通过接口实现应用重启是运维场景中的常见需求。以下是三种主流实现方案及其详细步骤和注意事项:

接口重启Spring Boot项目中,通过接口实现应用重启是运维场景中的常见需求。以下是三种主流实现方案及其详细步骤和注意事项:

⚙️ ​​1. 手动重启上下文(无需额外依赖)​​

​​核心原理​​
通过关闭当前ConfigurableApplicationContext并重新初始化Spring上下文实现重启。

​​实现步骤​​:

​​主类改造​​
保存上下文与启动参数,提供静态restart()方法

@SpringBootApplication
public class App {private static ConfigurableApplicationContext context;private static String[] args;public static void main(String[] args) {App.args = args;App.context = SpringApplication.run(App.class, args);}public static void restart() {// 使用独立线程避免JVM退出Thread thread = new Thread(() -> {context.close(); // 关闭当前上下文context = SpringApplication.run(App.class, args); // 重建上下文});thread.setDaemon(false); // 必须为非守护线程thread.start();}
}

​​添加重启接口​​
在Controller中调用主类的重启方法

@RestController
public class RestartController {@PostMapping("/restart")public String restart() {App.restart();return "重启中...";}
}

在Spring Boot项目中,通过接口实现应用重启是运维场景中的常见需求。以下是三种主流实现方案及其详细步骤和注意事项:

⚙️ ​​1. 手动重启上下文(无需额外依赖)​​
​​核心原理​​
通过关闭当前ConfigurableApplicationContext并重新初始化Spring上下文实现重启。

​​实现步骤​​:

​​主类改造​​
保存上下文与启动参数,提供静态restart()方法:
@SpringBootApplication
public class App {
private static ConfigurableApplicationContext context;
private static String[] args;

public static void main(String[] args) {App.args = args;App.context = SpringApplication.run(App.class, args);
}public static void restart() {// 使用独立线程避免JVM退出Thread thread = new Thread(() -> {context.close(); // 关闭当前上下文context = SpringApplication.run(App.class, args); // 重建上下文});thread.setDaemon(false); // 必须为非守护线程thread.start();
}

}
​​添加重启接口​​
在Controller中调用主类的重启方法:
@RestController
public class RestartController {
@PostMapping(“/restart”)
public String restart() {
App.restart();
return “重启中…”;
}
}
​​注意事项​​⚠️:

​​线程安全​​:确保重启操作在新线程中执行,避免阻塞主线程导致JVM退出。
​​接口保护​​:生产环境需通过Spring Security限制IP白名单或鉴权。

方案二:手动重建 ApplicationContext(无需 Spring Cloud)

适用场景:避免引入额外依赖,需完全控制重启逻辑
实现原理:保存主上下文,通过新线程关闭旧上下文后重新初始化
步骤:

启动类保存上下文引用:

@SpringBootApplication
public class Application {public static ConfigurableApplicationContext context;public static void main(String[] args) {context = SpringApplication.run(Application.class, args);}
}

编写重启接口:

import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationArguments;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class RestartController {@PostMapping("/restart")public String restart() {new Thread(() -> {// 获取启动参数ApplicationArguments args = Application.context.getBean(ApplicationArguments.class);// 关闭旧上下文Application.context.close();// 创建新上下文Application.context = SpringApplication.run(Application.class, args.getSourceArgs());}).start();return "重启中...";}
}

注意:需设置线程为非守护线程(thread.setDaemon(false)),避免进程被意外终止
1

http://www.lqws.cn/news/597439.html

相关文章:

  • 图像处理专业书籍以及网络资源总结
  • 讯飞大模型实时语音识别
  • Kubernetes 之Ingress 从基础到实战全解析
  • Async和Await关键字
  • 电力交易的实现路径
  • CppCon 2018 学习:A New Take on Polymorphism
  • (JAVA)自建应用调用企业微信API接口,实现消息推送
  • 【网工|知识升华版|理论】ARQ机制|CSMA/CD协议
  • Rust征服字节跳动:高并发服务器实战
  • 记一次使用sa-token导致的预检请求跨域问题
  • 前端常用构建工具介绍及对比
  • 人才交流的价值创造模型与合作演化方程
  • Kubernetes Pod 调度基础
  • 华为设备 QoS 流分类与流标记深度解析及实验脚本
  • 【UniApp picker-view 多列对齐问题深度剖析与完美解决】
  • 4.Stable Diffusion WebUI 模型训练
  • OpenCV CUDA模块设备层-----“大于阈值设为零” 的图像处理函数 thresh_to_zero_inv_func()
  • torch.nn
  • Postman - API 调试与开发工具 - 标准使用流程
  • Mac 部署 Dify小红书种草工作流
  • 新手向:从零开始MySQL超详细安装、配置与使用指南
  • stm32l4系列启用看门狗后,调用HAL_IWDG_Refreh()就复位
  • HakcMyVM-Arroutada
  • java生成word文档
  • 飞算JavaAI:重构软件开发范式的智能引擎
  • ABB驱动系列SCYC51213 63911607C驱动板
  • java微服务-linux单机CPU接近100%优化
  • Python应用指南:利用高德地图API获取公交+地铁可达圈(二)
  • 再见 RAG?Gemini 2.0 Flash 刚刚 “杀死” 了它!
  • 学习面向对象