首页 > 分享 > springboot中如何graceful关闭服务器

springboot中如何graceful关闭服务器

在生产环境中遇到一些奇葩的问题,一部署服务,就有一些异常的数据,经过分析是部署shell中直接用kill -9进行关闭服务,一些请求还在处理中就被kill了,导致数据存脏数据。开始提出了一些方案,比如后台增加开始部署按钮,点击后,关键的请求(如交易)被拒绝,没有完成的请求继续,等待一段时间后部署。后来看到一篇文章中提到了如何优雅关闭服务器,便详细研究研究,总结了一下优雅关闭服务器大概包括一下几种:
1、设置拦截器,需要部署时点击后台按钮激活拦截关键的请求。过一段时间再部署。缺点是部署麻烦,需要人工去点击。这也是解决问题的初始想法,后来放弃了。

2、通过actuator/shutdown,该功能是集成spring-actuator的功能,操作步骤是:
a、引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>;
b、在配置文件中添加
management.endpoint.shutdown.enabled=true management.endpoints.web.exposure.include=shutdown
c、关闭发送请求:http://localhost/actuator/shutdown
测试过程:先求/leader**,请求在处理中,然后发送http://localhost/actuator/shutdown请求,接着请求/leader**。第一次的/leader**有返回,第二次则显示Could not get any response。
actuator源码:

@Endpoint(id = "shutdown", enableByDefault = false) public class ShutdownEndpoint implements ApplicationContextAware { @WriteOperation public Map<String, String> shutdown() { Thread thread = new Thread(this::performShutdown); thread.setContextClassLoader(getClass().getClassLoader()); thread.start(); } private void performShutdown() { try { Thread.sleep(500L); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } //和3中的原理一样 this.context.close(); } }

123456789101112131415161718192021

3、自己实现关闭请求接口,通过ConfigurableApplicationContext 的close方法。

@GetMapping("/shutdown") @ApiOperation(value = "关机") public void shutDown() { ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context; log.info("关机"); ctx.close(); } 1234567

测试:先求/leader**,请求在处理中,然后发送/shutdown请求,接着请求/leader**。
第一次的/leader**有返回,第二次则显示Could not get any response。

@GetMapping("/leader**") @ApiOperation(value = "***排行榜") public Result getLeaderBoard(PageVo pageVo,LeaderBoardTypeEnum leaderBoardTypeEnum) throws InterruptedException { ThreadLocalUtil.setCacheValue(pageVo); log.info("+++++++++++++++++++++++++++++++++++++++++++准备睡觉"); Thread.sleep(20 * 1000L); log.info("--------------------------------------------睡醒了"); log.info(leaderBoardTypeEnum.getValue()); LeaderBoardService bean = (LeaderBoardService) context.getBean(leaderBoardTypeEnum.className()); return bean.handle(); } 1234567891011

测试结果如下:

在这里插入图片描述
4、在最新的 spring boot 2.3 版本,内置此功能,不需要再自行扩展容器线程池来处理,
目前 spring boot 嵌入式支持的 web 服务器(Jetty、Reactor Netty、Tomcat 和 Undertow)以及反应式和基于 Servlet 的 web 应用程序都支持优雅停机功能。我们来看下如何使用:
当使用server.shutdown=graceful启用时,在 web 容器关闭时,web 服务器将不再接收新请求,并将等待活动请求完成的缓冲期。
在这里插入图片描述
然后在shell部署脚本中停止服务用kill -2。这种方法在服务中尝试了一下,没有成功,在努力中。。。

相关知识

SpringBoot 统一时区的方案
【2024最新】springboot网上商城购物系统lw+ppt
java计算机毕业设计基于SpringBoot的疫苗接种管理系统(开题+程序+论文)
Springboot+vue的客户关系管理系统(有报告),Javaee项目,springboot vue前后端分离项目
springboot“花开富贵”花园管理系统
springboot中@PropertySource(value = {“classpath:FoundBean.properties”})读取不出内容
基于springboot智能鲜花商店销售系统
关于32g服务器内存价格(32GB服务器内存成本分析)
【SpringBoot项目
重生推迟部分区域比赛《Apex英雄》要花时间修复服务器问题

网址: springboot中如何graceful关闭服务器 https://m.huajiangbk.com/newsview606774.html

所属分类:花卉
上一篇: 春暖花开皮肤过敏怎么办
下一篇: Tomcat配置多个不同JDK版