SpringBoot:通过web控制当前的SpringBoot程序重新启动

当前版本:SpringBoot2.3.12.RELEASEJDK1.8

1. 声明

当前内容主要为本人学习和使用SpringBoot,实现通过web方式控制SpringBoot的程序重启操作,主要参考SpringBoot官方文档

  1. 通过观察发现重启是与RemoteSpringApplication有关
  2. 查看源码发现与Restarter有关
  3. 所以可以手动实现

2.源码寻找

RemoteSpringApplication源码

private void run(String[] args) {
	Restarter.initialize(args, RestartInitializer.NONE);
	SpringApplication application = new SpringApplication(RemoteClientConfiguration.class);
	application.setWebApplicationType(WebApplicationType.NONE);
	application.setBanner(getBanner());
	application.setInitializers(getInitializers());
	application.setListeners(getListeners());
	application.run(args);
	waitIndefinitely();
}

看到主要实现的为Restarter,然后找到Restarter源码
在这里插入图片描述

/**
 * Restart the running application.
 * @param failureHandler a failure handler to deal with application that doesn't start
 */
public void restart(FailureHandler failureHandler) {
	if (!this.enabled) {
		this.logger.debug("Application restart is disabled");
		return;
	}
	this.logger.debug("Restarting application");
	getLeakSafeThread().call(() -> {
		Restarter.this.stop();
		Restarter.this.start(failureHandler);
		return null;
	});
}

所以找到了重启的方式

3.重启实现Demo

首先导入devtools依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<optional>false</optional>
</dependency>

注意先导入springboot-parent依赖

为了方便比较是否重启,定义一个随机数用于查看

AppConfig代码

@Configuration
public class AppConfig implements WebMvcConfigurer{

	public AppConfig() {
		int randomNum = (int)(Math.random()*1000)+1;
		System.out.println("AppConfig 实例化成功........."+(randomNum));
	}	
}

写一个重启controller

@RestController
public class RestartController {
	@Autowired
	ApplicationContext applicationContext;
	
	@RequestMapping("/restart")
	public String restart() {
		Restarter restarter = Restarter.getInstance();
		restarter.restart(new FailureHandler() {

			public Outcome handle(Throwable failure) {
				System.out.println("当前系统出现问题,无法重启项目...........");
				return Outcome.ABORT;
			}
			
		});
		return "重启服务成功!";
	}
}

启动类

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplicationBuilder(Application.class).build(args);
		application.run();
	}
}

4. 测试

在这里插入图片描述
调用接口后
在这里插入图片描述
在这里插入图片描述
重新启动了当前的SpringBoot程序了,测试成功

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中的异常信息表明,在启动Spring Boot应用程序时出现了java.lang.NoClassDefFoundError: org/apache/catalina/Context错误。这个错误通常表示应用程序无法找到org/apache/catalina/Context类。该类是Apache Tomcat服务器的一部分,它提供了与Web应用程序的上下文相关的功能。因此,它的缺失可能导致Spring Boot应用程序无法正确启动。 引用和提供了一些可能导致此错误的原因。一种可能性是由于缺少正确的jar包或依赖项。在引用中,jar包被清空了,可能是由于maven的规则导致的。而引用中的pom文件中指定了本地引用的jar包路径。这些问题可能导致应用程序无法加载所需的类。 要解决这个问题,你可以尝试以下步骤: 1. 检查你的项目依赖项和构建配置是否正确。确保所有的jar包和依赖项都正确引入并存在于应用程序的类路径中。 2. 如果你使用maven进行构建,可以尝试清理和重新构建项目。这将确保所有的依赖项被正确下载和配置。 3. 确保你的应用程序的类路径中包含了Apache Tomcat相关的jar包。你可以检查你的构建配置文件(如pom.xml)中是否有相关依赖项,并且这些依赖项是否正确下载和配置。 4. 如果你使用的是本地引用的jar包,确保路径和配置正确无误,并检查jar包是否存在于指定的位置。 记住,这只是一些可能的解决方案之一。具体的解决方法可能因为你的项目配置和环境而有所不同。如果这些方法都没有解决问题,你可能需要进一步调查和排除其他可能的原因。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringMVC4.3.6配置json所需要的jar包](https://download.csdn.net/download/renxingwu2008/9765096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [IDEA启动tomcat报错:java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext、...](https://blog.csdn.net/wuxun1997/article/details/115525915)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值