SpringBoot 如何优雅的停机
这里写目录标题
- 1 介绍
- 2 使用
- 2.1 开启 hook
- 2.2 禁用 hook
- 3 手动指定 hook
1 介绍
SpringBoot 如果需要使用hook则需要开启spring.main.register-shutdown-hook=true(默认为true)
如果使用kill -9则不会出发JVM的hook,kill可以正常触发hook
server:port: 8080shutdown: IMMEDIATE #GRACEFUL/IMMEDIATE (默认IMMEDIATE)spring:main:register-shutdown-hook: true #默认ture
2 使用
package com.example.java.test;import jakarta.annotation.PreDestroy;
import org.springframework.stereotype.Component;/*** @author hyacinth* @date 2023/10/13 23:07* @desc: 测试* @title: Test* @package com.example.java.test*/
@Component
public class Test {@PreDestroypublic void destroy() {System.out.println("销毁");}public Test() {System.out.println("创建");}}
2.1 开启 hook
如果spring.main.register-shutdown-hook=true
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v3.1.4)2023-10-13T23:11:26.801+08:00 INFO 18244 --- [ restartedMain] com.example.java.JavaApplication : Starting JavaApplication using Java 21 with PID 18244 (D:\SourceCode\Intellij\java\target\classes started by xuhya in D:\SourceCode\Intellij\java)
2023-10-13T23:11:26.805+08:00 INFO 18244 --- [ restartedMain] com.example.java.JavaApplication : No active profile set, falling back to 1 default profile: "default"
2023-10-13T23:11:26.916+08:00 INFO 18244 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-10-13T23:11:26.916+08:00 INFO 18244 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-10-13T23:11:28.594+08:00 INFO 18244 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-10-13T23:11:28.622+08:00 INFO 18244 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-10-13T23:11:28.623+08:00 INFO 18244 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.13]
2023-10-13T23:11:28.696+08:00 INFO 18244 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-10-13T23:11:28.697+08:00 INFO 18244 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1779 ms
创建
2023-10-13T23:11:29.308+08:00 INFO 18244 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2023-10-13T23:11:29.386+08:00 INFO 18244 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-10-13T23:11:29.403+08:00 INFO 18244 --- [ restartedMain] com.example.java.JavaApplication : Started JavaApplication in 3.274 seconds (process running for 3.915)
销毁Process finished with exit code 130
2.2 禁用 hook
如果spring.main.register-shutdown-hook=false
如果spring.main.register-shutdown-hook=true```java. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v3.1.4)2023-10-13T23:11:26.801+08:00 INFO 18244 --- [ restartedMain] com.example.java.JavaApplication : Starting JavaApplication using Java 21 with PID 18244 (D:\SourceCode\Intellij\java\target\classes started by xuhya in D:\SourceCode\Intellij\java)
2023-10-13T23:11:26.805+08:00 INFO 18244 --- [ restartedMain] com.example.java.JavaApplication : No active profile set, falling back to 1 default profile: "default"
2023-10-13T23:11:26.916+08:00 INFO 18244 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-10-13T23:11:26.916+08:00 INFO 18244 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-10-13T23:11:28.594+08:00 INFO 18244 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-10-13T23:11:28.622+08:00 INFO 18244 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-10-13T23:11:28.623+08:00 INFO 18244 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.13]
2023-10-13T23:11:28.696+08:00 INFO 18244 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-10-13T23:11:28.697+08:00 INFO 18244 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1779 ms
创建
2023-10-13T23:11:29.308+08:00 INFO 18244 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2023-10-13T23:11:29.386+08:00 INFO 18244 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-10-13T23:11:29.403+08:00 INFO 18244 --- [ restartedMain] com.example.java.JavaApplication : Started JavaApplication in 3.274 seconds (process running for 3.915)Process finished with exit code 130
3 手动指定 hook
package com.example.java;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class JavaApplication {public static void main(String[] args) {SpringApplication.run(JavaApplication.class, args);Runtime.getRuntime().addShutdownHook(new Thread(() -> {System.out.println("服务已停止");}));}}
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v3.1.4)2023-10-13T23:15:08.667+08:00 INFO 16108 --- [ restartedMain] com.example.java.JavaApplication : Starting JavaApplication using Java 21 with PID 16108 (D:\SourceCode\Intellij\java\target\classes started by xuhya in D:\SourceCode\Intellij\java)
2023-10-13T23:15:08.671+08:00 INFO 16108 --- [ restartedMain] com.example.java.JavaApplication : No active profile set, falling back to 1 default profile: "default"
2023-10-13T23:15:08.752+08:00 INFO 16108 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-10-13T23:15:08.752+08:00 INFO 16108 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-10-13T23:15:10.314+08:00 INFO 16108 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-10-13T23:15:10.335+08:00 INFO 16108 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-10-13T23:15:10.335+08:00 INFO 16108 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.13]
2023-10-13T23:15:10.402+08:00 INFO 16108 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-10-13T23:15:10.403+08:00 INFO 16108 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1649 ms
创建
2023-10-13T23:15:10.914+08:00 INFO 16108 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2023-10-13T23:15:10.996+08:00 INFO 16108 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-10-13T23:15:11.015+08:00 INFO 16108 --- [ restartedMain] com.example.java.JavaApplication : Started JavaApplication in 3.038 seconds (process running for 3.626)
服务已停止相关文章:
SpringBoot 如何优雅的停机
这里写目录标题 1 介绍2 使用2.1 开启 hook2.2 禁用 hook 3 手动指定 hook 1 介绍 SpringBoot 如果需要使用hook则需要开启spring.main.register-shutdown-hooktrue(默认为true) 如果使用kill -9则不会出发JVM的hook,kill可以正常触发hook server:port: 8080shutd…...
详细教程:Postman 怎么调试 WebSocket
WebSocket 是一个支持双向通信的网络协议,它在实时性和效率方面具有很大的优势。Postman 是一个流行的 API 开发工具,它提供了许多功能来测试和调试 RESTful API 接口,最新的版本也支持 WebSocket 接口的调试。想要学习更多关于 Postman 的知…...
互联网Java工程师面试题·Java 并发编程篇·第五弹
目录 52、什么是线程池? 为什么要使用它? 53、怎么检测一个线程是否拥有锁? 54、你如何在 Java 中获取线程堆栈? 55、JVM 中哪个参数是用来控制线程的栈堆栈小的? 56、Thread 类中的 yield 方法有什么作用? 57、…...
mysql与oracle分页的有什么区别
Java面试:mysql与oracle分页的有什么区别 相信许多人在日常工作中都会用到分页,比如日常查询数据量太大,而我们只需要其中的几条即可,所以这时就会去使用分页去查询,今天主要就mysql与oracle的分页进行分析。 MySQL 分…...
华为云云耀云服务器L实例评测|华为云耀云服务器L实例docker部署及应用(七)
八、华为云耀云服务器L实例docker、docker-compose安装及部署MySQL、Redis应用: 随着云原生、容器化、微服务、K8S等技术的发展,容器 docker 也逐渐在企业团队实践中大量的使用。它可以提供了一套标准化的解决方案,极大地提升了部署、发布、运…...
实体解析实施的复杂性
实体的艺术表现斯特凡伯克纳 一、说明 实体解析是确定数据集中的两条或多条记录是否引用同一现实世界实体(通常是个人或公司)的过程。乍一看,实体分辨率可能看起来像一个相对简单的任务:例如,给定一张人物的两张照片&a…...
MAKEFLAGS += -rR --include-dir=$(CURDIR)的含义
一、目的 在看uboot顶层Makefile文件时遇到这个代码不甚明白,故查找了一下资料以供大家学习 二、介绍 MAKEFLAGS -rR 表示禁止使用内置的隐含规则和变量定义;这个选项用于启用recursive make,使得Makefile目标可以调用其他Makefile目标&…...
maven问题与解决方案、部署
问题一、was cached in the local repository, resolution will not be reattempted until the update interval of idea中 Maven中Lifecycle时,能正常clean 和 install,但在idea的Terminal中mvn install出现: was cached in the local repo…...
【大数据】Hadoop MapReduce与Hadoop YARN(学习笔记)
一、Hadoop MapReduce介绍 1、设计构思 1)如何对付大数据处理场景 对相互间不具有计算依赖关系的大数据计算任务,实现并行最自然的办法就是采取MapReduce分而治之的策略。 不可拆分的计算任务或相互间有依赖关系的数据无法进行并行计算! …...
接口测试文档
接口测试的总结文档 第一部分:主要从问题出发,引入接口测试的相关内容并与前端测试进行简单对比,总结两者之前的区别与联系。但该部分只交代了怎么做和如何做?并没有解释为什么要做? 第二部分:主要介绍为什…...
Ubuntu中不能使用ifconfig命令
问题 打开终端使用如下命令不能运行: ifconfig显示如下错误: 解决方法 在VMware中的虚拟机下面打开“编辑虚拟机设置”,或者在已经打开的虚拟机面板上面打开“虚拟机—设置” 选择网络适配器,选择“NAT模式”,没开机的就…...
BAT020:将文本文档中多行文本拼接为;分隔的单行文本
引言:编写批处理程序,实现将文本文档中多行文本拼接为;分隔的单行文本。 一、新建Windows批处理文件 参考博客: CSDNhttps://mp.csdn.net/mp_blog/creation/editor/132137544 二、写入批处理代码 1.右键新建的批处理文件,点击【…...
安防初识命令【学习笔记】
1、美杜莎爆破 medusa -M ssh -h 192.168.42.135 -u guest -P top1000.txt -t 8 2、passwd文件与shadow文件 root:x:0:0:root:/root:/usr/bin/zsh 用户名:密码:uid:gid:描述性信息:主目录:默认…...
idea 启动出现 Failed to create JVM JVM Path
错误 idea 启动出现如下图情况 Error launching IDEA If you already a 64-bit JDK installed, define a JAVA_HOME variable in Computer > System Properties> System Settings > Environment Vanables. Failed to create JVM. JVM Path: D:\Program Files\JetB…...
凉鞋的 Unity 笔记 108. 第二个通识:增删改查
在这一篇,我们来学习此教程的第二个通识,即:增删改查。 增删改查我们不只是一次接触到了。 在最先接触的场景层次窗口中,我们是对 GameObject 进行增删改查。 在 Project 文件窗口中,我们是对文件&文件夹进行增删…...
angular项目指定端口,实现局域网内ip访问
直接修改package.json文件 "dev": "ng serve --host 0.0.0.0 --port 8080"终端运行npm run dev启动项目。 这里就指定了使用8080端口运行项目,同时局域网内的其他电脑可以通过访问运行项目主机的ip来访问项目 例如项目运行在ip地址为192.168.2…...
解决uniapp里scroll-view横向滚动的问题
一、前言 本以为是一件很简单的事,结果浪费了整整一个上午,并且问题并没有全部解决....后来没办法,用了touchmove模拟的滑动,如果有好的解决方法麻烦告诉我...非常感谢~ 一、问题 其实我想要实现的功能很简单,就是一…...
LeetCode——动态规划(五)
刷题顺序及思路来源于代码随想录,网站地址:https://programmercarl.com 目录 121. 买卖股票的最佳时机 - 力扣(LeetCode) 122. 买卖股票的最佳时机 II - 力扣(LeetCode) 123. 买卖股票的最佳时机 III …...
与HTTP相关的各种概念
网络世界 网络世界中最重要的一个名词就是互联网(Internet),它以TCP/IP协议族为基础,构建成了一望无际的信息传输网络。而我们通常所说的“上网”,主要就是访问互联网的一个子集——万维网(World Wide Web)…...
CentOS 7 编译安装Boost
1、前提条件 linux平台/CentOS 7 下要编译安装Boost除gcc和gcc-c之外,还需要两个开发库:bzip2-devel 和python-devel ,因此在安装前应该先保证这两个库已经安装。 安装指令: yum install bzip2 bzip2-devel bzip2-libs python-devel Cent…...
[2025CVPR]DeepVideo-R1:基于难度感知回归GRPO的视频强化微调框架详解
突破视频大语言模型推理瓶颈,在多个视频基准上实现SOTA性能 一、核心问题与创新亮点 1.1 GRPO在视频任务中的两大挑战 安全措施依赖问题 GRPO使用min和clip函数限制策略更新幅度,导致: 梯度抑制:当新旧策略差异过大时梯度消失收敛困难:策略无法充分优化# 传统GRPO的梯…...
利用ngx_stream_return_module构建简易 TCP/UDP 响应网关
一、模块概述 ngx_stream_return_module 提供了一个极简的指令: return <value>;在收到客户端连接后,立即将 <value> 写回并关闭连接。<value> 支持内嵌文本和内置变量(如 $time_iso8601、$remote_addr 等)&a…...
Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动
一、前言说明 在2011版本的gb28181协议中,拉取视频流只要求udp方式,从2016开始要求新增支持tcp被动和tcp主动两种方式,udp理论上会丢包的,所以实际使用过程可能会出现画面花屏的情况,而tcp肯定不丢包,起码…...
STM32F4基本定时器使用和原理详解
STM32F4基本定时器使用和原理详解 前言如何确定定时器挂载在哪条时钟线上配置及使用方法参数配置PrescalerCounter ModeCounter Periodauto-reload preloadTrigger Event Selection 中断配置生成的代码及使用方法初始化代码基本定时器触发DCA或者ADC的代码讲解中断代码定时启动…...
Neo4j 集群管理:原理、技术与最佳实践深度解析
Neo4j 的集群技术是其企业级高可用性、可扩展性和容错能力的核心。通过深入分析官方文档,本文将系统阐述其集群管理的核心原理、关键技术、实用技巧和行业最佳实践。 Neo4j 的 Causal Clustering 架构提供了一个强大而灵活的基石,用于构建高可用、可扩展且一致的图数据库服务…...
Rust 异步编程
Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...
是否存在路径(FIFOBB算法)
题目描述 一个具有 n 个顶点e条边的无向图,该图顶点的编号依次为0到n-1且不存在顶点与自身相连的边。请使用FIFOBB算法编写程序,确定是否存在从顶点 source到顶点 destination的路径。 输入 第一行两个整数,分别表示n 和 e 的值(1…...
Element Plus 表单(el-form)中关于正整数输入的校验规则
目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入(联动)2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...
IP如何挑?2025年海外专线IP如何购买?
你花了时间和预算买了IP,结果IP质量不佳,项目效率低下不说,还可能带来莫名的网络问题,是不是太闹心了?尤其是在面对海外专线IP时,到底怎么才能买到适合自己的呢?所以,挑IP绝对是个技…...
现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?
现有的 Redis 分布式锁库(如 Redisson)相比于开发者自己基于 Redis 命令(如 SETNX, EXPIRE, DEL)手动实现分布式锁,提供了巨大的便利性和健壮性。主要体现在以下几个方面: 原子性保证 (Atomicity)ÿ…...
