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

SpringBoot自动重启

目录

  • 方法一:使用SpringCloud RestartEndpoint
  • 方法二:重新创建ApplicationContext上下文

方法一:使用SpringCloud RestartEndpoint

  • 使用的jar
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-config:3.0.7'
  • 在application.properties添加配置
management.endpoint.restart.enabled=true
spring.cloud.config.enabled=false
  • 重启方法的Controller
import javax.annotation.Resource;

import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/restart")
public class RestartController {

    @Resource
    private RestartEndpoint restartEndpoint;

    @GetMapping("/restartApplication")
    public void restartApplication() {
        restartEndpoint.restart();
    }

}

方法二:重新创建ApplicationContext上下文

  • 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

    public static ConfigurableApplicationContext context;

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

}

  • 重启方法的Controller
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/restart")
public class RestartController {

    private static void restart() {
        ApplicationArguments args = Application.context.getBean(ApplicationArguments.class);
        Thread thread = new Thread(() -> {
            log.info("springboot restart...");
            Application.context.close();
            Application.context = SpringApplication.run(Application.class, args.getSourceArgs());
        });
        // 设置为用户线程,不是守护线程
        thread.setDaemon(false);
        thread.start();
    }
}


http://www.kler.cn/news/161352.html

相关文章:

  • solidity案例详解(六)服务评价合约
  • 集成开发环境 PyCharm 的安装【侯小啾python基础领航计划 系列(二)】
  • ARM虚拟化与车联网安全应用
  • 第二十一章网络通信总结博客
  • 最全的DEM数据,我给你们处理好了
  • SpringBoot中MyBatis-Flex的集成和使用
  • 关于高校电子邮件系统开通双因素认证的经验分享-以清华大学为例
  • 2个月拿下信息系统项目管理师攻略(攻略超级全)
  • 德国进口高速主轴电机在机器人上的应用及选型方案
  • 龙迅LT2611UX 四端口LVDS转HDMI(2.0)
  • 软件工程-(可行性分析、需求分析)
  • 安装you-get(mac)
  • 说说React jsx转换成真实DOM的过程?
  • 【链表Linked List】力扣-109 有序链表转换二叉搜索树
  • [node] Node.js的Web 模块
  • Ubuntu之Sim2Real环境配置(坑居多)
  • 好用免费的AI换脸5个工具
  • <软考>软件设计师-3程序设计语言基础(总结)
  • ESP32-Web-Server编程-通过 Base64 编码在网页中插入图片
  • Windows+WSL开发环境下微服务注册(Consul)指定IP
  • 第5节:Vue3 JavaScript 表达式
  • 道可云会展元宇宙平台全新升级,打造3D沉浸式展会新模式
  • React笔记
  • 软件多开助手的创新使用:在同一设备上玩转多个游戏
  • 软件工程期末复习(2)
  • 【Linux】公网远程访问AMH服务器管理面板
  • 外包干了4年,技术退步明显...
  • python-比较Excel两列数据,并分别显示差异
  • 处理器中的TrustZone之安全状态
  • Git常用命令#merge分支合并