栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot 如何优雅关闭服务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot 如何优雅关闭服务

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

   org.springframework.boot
   spring-boot-starter-actuator
  -->

配置
在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 {
		@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();
 }

到此这篇关于spring boot 如何优雅关闭服务的文章就介绍到这了,更多相关spring boot 关闭服务 内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130595.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号