欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

spring boot 如何优雅关闭服务

时间:2021-05-21 08:25:43|栏目:JAVA代码|点击:

spring boot 优雅的关闭服务

实现ContextClosedEvent 监听器,监听到关闭事件后,关闭springboot进程

**
网上有很多例子 使用spring boot 插件做关闭经测试此插件只能是关闭spring boot服务,不能杀死服务进程。还是需要实现关闭监听,去杀死进程。
网上有很多例子 使用spring boot 插件做关闭经测试此插件只能是关闭spring boot服务,不能杀死服务进程。还是需要实现关闭监听,去杀死进程。
网上有很多例子 使用spring boot 插件做关闭经测试此插件只能是关闭spring boot服务,不能杀死服务进程。还是需要实现关闭监听,去杀死进程。
重要的事说三遍

**

actuator 关闭spring boot 实现方式
引入actuator 配置 shutdown
调用http://127.0.0.1/xxx/

引入actuator
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>-->

配置
在application.properties中开启关闭
management.endpoints.web.exposure.include=*
#management.endpoint.shutdown.enabled = true

1.调用

1.主入口
	public static ConfigurableApplicationContext configurableApplicationContext;
	public static void main(String[] args) throws InterruptedException {
		 configurableApplicationContext = SpringApplication.run(GatewayApplication.class, args);
	}
	
2.关闭监听
@Controller
	public static class ShutdownAction implements ApplicationListener<ContextClosedEvent> {
		@Override
		public void onApplicationEvent(ContextClosedEvent event) {
			System.exit(SpringApplication.exit(configurableApplicationContext));

		}
	}

3.关闭服务命令

 /**
  * 关闭服务
  */
 @RequestMapping(value = "/stop", method = RequestMethod.GET)
 @ResponseBody
 public void stopServer() {
  MySpringApplication.configurableApplicationContext.close();
 }

上一篇:Idea工具中创建 SpringBoot工程及入门详解

栏    目:JAVA代码

下一篇:java简单实现用语音读txt文档方法总结

本文标题:spring boot 如何优雅关闭服务

本文地址:http://www.codeinn.net/misctech/125730.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有