spring boot 关闭服务 spring boot 怎样优雅关闭服务

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring boot 关闭服务 spring boot 怎样优雅关闭服务

zkxiaowu   2021-03-15 我要评论
想了解spring boot 怎样优雅关闭服务的相关内容吗,zkxiaowu在本文为您仔细讲解spring boot 关闭服务 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,boot,关闭服务,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
<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();
 }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们