再談為了提醒明知故犯(在一坑裡迭倒兩次不是不多見),由於業務系統中大量使用了spring Boot embedded tomcat的模式運行,在一些運維腳本中經常看到Linux 中 kill 指令,然而它的使用也有些講究,要思考如何能做到優雅停機。 何為優雅關機 就是為確保應用關閉時,通知應用進程釋 ...
再談為了提醒明知故犯(在一坑裡迭倒兩次不是不多見),由於業務系統中大量使用了spring Boot embedded tomcat
的模式運行,在一些運維腳本中經常看到Linux 中 kill
指令,然而它的使用也有些講究,要思考如何能做到優雅停機。
何為優雅關機
就是為確保應用關閉時,通知應用進程釋放所占用的資源
- 線程池,shutdown(不接受新任務等待處理完)還是shutdownNow(調用
Thread.interrupt
進行中斷) - socket 鏈接,比如:netty、mq
- 告知註冊中心快速下線(靠心跳機制客服早都跳起來了),比如:eureka
- 清理臨時文件,比如:poi
- 各種堆內堆外記憶體釋放
總之,進程強行終止會帶來數據丟失或者終端無法恢復到正常狀態,在分散式環境下還可能導致數據不一致的情況。
kill指令
kill -9 pid
可以模擬了一次系統宕機,系統斷電等極端情況,而kill -15 pid
則是等待應用關閉,執行阻塞操作,有時候也會出現無法關閉應用的情況(線上理想情況下,是bug就該尋根溯源)
#查看jvm進程pid
jps
#列出所有信號名稱
kill -l
# Windows下信號常量值
# 簡稱 全稱 數值
# INT SIGINT 2 Ctrl+C中斷
# ILL SIGILL 4 非法指令
# FPE SIGFPE 8 floating point exception(浮點異常)
# SEGV SIGSEGV 11 segment violation(段錯誤)
# TERM SIGTERM 5 Software termination signal from kill(Kill發出的軟體終止)
# BREAK SIGBREAK 21 Ctrl-Break sequence(Ctrl+Break中斷)
# ABRT SIGABRT 22 abnormal termination triggered by abort call(Abort)
#linux信號常量值
# 簡稱 全稱 數值
# HUP SIGHUP 1 終端斷線
# INT SIGINT 2 中斷(同 Ctrl + C)
# QUIT SIGQUIT 3 退出(同 Ctrl + \)
# KILL SIGKILL 9 強制終止
# TERM SIGTERM 15 終止
# CONT SIGCONT 18 繼續(與STOP相反, fg/bg命令)
# STOP SIGSTOP 19 暫停(同 Ctrl + Z)
#....
#可以理解為操作系統從內核級別強行殺死某個進程
kill -9 pid
#理解為發送一個通知,等待應用主動關閉
kill -15 pid
#也支持信號常量值全稱或簡寫(就是去掉SIG後)
kill -l KILL
思考:jvm是如何接受處理linux信號量的?
當然是在jvm啟動時就載入了自定義SignalHandler
,關閉jvm時觸發對應的handle。
public interface SignalHandler {
SignalHandler SIG_DFL = new NativeSignalHandler(0L);
SignalHandler SIG_IGN = new NativeSignalHandler(1L);
void handle(Signal var1);
}
class Terminator {
private static SignalHandler handler = null;
Terminator() {
}
//jvm設置SignalHandler,在System.initializeSystemClass中觸發
static void setup() {
if (handler == null) {
SignalHandler var0 = new SignalHandler() {
public void handle(Signal var1) {
Shutdown.exit(var1.getNumber() + 128);//調用Shutdown.exit
}
};
handler = var0;
try {
Signal.handle(new Signal("INT"), var0);//中斷時
} catch (IllegalArgumentException var3) {
;
}
try {
Signal.handle(new Signal("TERM"), var0);//終止時
} catch (IllegalArgumentException var2) {
;
}
}
}
}
Runtime.addShutdownHook
在瞭解Shutdown.exit
之前,先看Runtime.getRuntime().addShutdownHook(shutdownHook);
則是為jvm中增加一個關閉的鉤子,當jvm關閉的時候調用。
public class Runtime {
public void addShutdownHook(Thread hook) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("shutdownHooks"));
}
ApplicationShutdownHooks.add(hook);
}
}
class ApplicationShutdownHooks {
/* The set of registered hooks */
private static IdentityHashMap<Thread, Thread> hooks;
static synchronized void add(Thread hook) {
if(hooks == null)
throw new IllegalStateException("Shutdown in progress");
if (hook.isAlive())
throw new IllegalArgumentException("Hook already running");
if (hooks.containsKey(hook))
throw new IllegalArgumentException("Hook previously registered");
hooks.put(hook, hook);
}
}
//它含數據結構和邏輯管理虛擬機關閉序列
class Shutdown {
/* Shutdown 系列狀態*/
private static final int RUNNING = 0;
private static final int HOOKS = 1;
private static final int FINALIZERS = 2;
private static int state = RUNNING;
/* 是否應該運行所以finalizers來exit? */
private static boolean runFinalizersOnExit = false;
// 系統關閉鉤子註冊一個預定義的插槽.
// 關閉鉤子的列表如下:
// (0) Console restore hook
// (1) Application hooks
// (2) DeleteOnExit hook
private static final int MAX_SYSTEM_HOOKS = 10;
private static final Runnable[] hooks = new Runnable[MAX_SYSTEM_HOOKS];
// 當前運行關閉鉤子的鉤子的索引
private static int currentRunningHook = 0;
/* 前面的靜態欄位由這個鎖保護 */
private static class Lock { };
private static Object lock = new Lock();
/* 為native halt方法提供鎖對象 */
private static Object haltLock = new Lock();
static void add(int slot, boolean registerShutdownInProgress, Runnable hook) {
synchronized (lock) {
if (hooks[slot] != null)
throw new InternalError("Shutdown hook at slot " + slot + " already registered");
if (!registerShutdownInProgress) {//執行shutdown過程中不添加hook
if (state > RUNNING)//如果已經在執行shutdown操作不能添加hook
throw new IllegalStateException("Shutdown in progress");
} else {//如果hooks已經執行完畢不能再添加hook。如果正在執行hooks時,添加的槽點小於當前執行的槽點位置也不能添加
if (state > HOOKS || (state == HOOKS && slot <= currentRunningHook))
throw new IllegalStateException("Shutdown in progress");
}
hooks[slot] = hook;
}
}
/* 執行所有註冊的hooks
*/
private static void runHooks() {
for (int i=0; i < MAX_SYSTEM_HOOKS; i++) {
try {
Runnable hook;
synchronized (lock) {
// acquire the lock to make sure the hook registered during
// shutdown is visible here.
currentRunningHook = i;
hook = hooks[i];
}
if (hook != null) hook.run();
} catch(Throwable t) {
if (t instanceof ThreadDeath) {
ThreadDeath td = (ThreadDeath)t;
throw td;
}
}
}
}
/* 關閉JVM的操作
*/
static void halt(int status) {
synchronized (haltLock) {
halt0(status);
}
}
//JNI方法
static native void halt0(int status);
// shutdown的執行順序:runHooks > runFinalizersOnExit
private static void sequence() {
synchronized (lock) {
/* Guard against the possibility of a daemon thread invoking exit
* after DestroyJavaVM initiates the shutdown sequence
*/
if (state != HOOKS) return;
}
runHooks();
boolean rfoe;
synchronized (lock) {
state = FINALIZERS;
rfoe = runFinalizersOnExit;
}
if (rfoe) runAllFinalizers();
}
//Runtime.exit時執行,runHooks > runFinalizersOnExit > halt
static void exit(int status) {
boolean runMoreFinalizers = false;
synchronized (lock) {
if (status != 0) runFinalizersOnExit = false;
switch (state) {
case RUNNING: /* Initiate shutdown */
state = HOOKS;
break;
case HOOKS: /* Stall and halt */
break;
case FINALIZERS:
if (status != 0) {
/* Halt immediately on nonzero status */
halt(status);
} else {
/* Compatibility with old behavior:
* Run more finalizers and then halt
*/
runMoreFinalizers = runFinalizersOnExit;
}
break;
}
}
if (runMoreFinalizers) {
runAllFinalizers();
halt(status);
}
synchronized (Shutdown.class) {
/* Synchronize on the class object, causing any other thread
* that attempts to initiate shutdown to stall indefinitely
*/
sequence();
halt(status);
}
}
//shutdown操作,與exit不同的是不做halt操作(關閉JVM)
static void shutdown() {
synchronized (lock) {
switch (state) {
case RUNNING: /* Initiate shutdown */
state = HOOKS;
break;
case HOOKS: /* Stall and then return */
case FINALIZERS:
break;
}
}
synchronized (Shutdown.class) {
sequence();
}
}
}
spring 3.2.12
在spring中通過ContextClosedEvent
事件來觸發一些動作(可以拓展),主要通過LifecycleProcessor.onClose
來做stopBeans
。由此可見spring也基於jvm做了拓展。
推薦一個開源免費的 Spring Boot 最全教程:
public abstract class AbstractApplicationContext extends DefaultResourceLoader {
public void registerShutdownHook() {
if (this.shutdownHook == null) {
// No shutdown hook registered yet.
this.shutdownHook = new Thread() {
@Override
public void run() {
doClose();
}
};
Runtime.getRuntime().addShutdownHook(this.shutdownHook);
}
}
protected void doClose() {
boolean actuallyClose;
synchronized (this.activeMonitor) {
actuallyClose = this.active && !this.closed;
this.closed = true;
}
if (actuallyClose) {
if (logger.isInfoEnabled()) {
logger.info("Closing " + this);
}
LiveBeansView.unregisterApplicationContext(this);
try {
//發佈應用內的關閉事件
publishEvent(new ContextClosedEvent(this));
}
catch (Throwable ex) {
logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex);
}
// 停止所有的Lifecycle beans.
try {
getLifecycleProcessor().onClose();
}
catch (Throwable ex) {
logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
}
// 銷毀spring 的 BeanFactory可能會緩存單例的 Bean.
destroyBeans();
// 關閉當前應用上下文(BeanFactory)
closeBeanFactory();
// 執行子類的關閉邏輯
onClose();
synchronized (this.activeMonitor) {
this.active = false;
}
}
}
}
public interface LifecycleProcessor extends Lifecycle {
/**
* Notification of context refresh, e.g. for auto-starting components.
*/
void onRefresh();
/**
* Notification of context close phase, e.g. for auto-stopping components.
*/
void onClose();
}
spring boot
到這裡就進入重點了,spring boot中有spring-boot-starter-actuator
模塊提供了一個 restful 介面,用於優雅停機。執行請求 curl -X POST http://127.0.0.1:8088/shutdown
,待關閉成功則返回提示。
Spring Boot 基礎就不介紹了,推薦看這個免費教程:
註:線上環境該url需要設置許可權,可配合 spring-security使用或在nginx中限制內網訪問
#啟用shutdown
endpoints.shutdown.enabled=true
#禁用密碼驗證
endpoints.shutdown.sensitive=false
#可統一指定所有endpoints的路徑
management.context-path=/manage
#指定管理埠和IP
management.port=8088
management.address=127.0.0.1
#開啟shutdown的安全驗證(spring-security)
endpoints.shutdown.sensitive=true
#驗證用戶名
security.user.name=admin
#驗證密碼
security.user.password=secret
#角色
management.security.role=SUPERUSER
spring boot的shutdown
原理也不複雜,其實還是通過調用AbstractApplicationContext.close
實現的。
@ConfigurationProperties(
prefix = "endpoints.shutdown"
)
public class ShutdownMvcEndpoint extends EndpointMvcAdapter {
public ShutdownMvcEndpoint(ShutdownEndpoint delegate) {
super(delegate);
}
//post請求
@PostMapping(
produces = {"application/vnd.spring-boot.actuator.v1+json", "application/json"}
)
@ResponseBody
public Object invoke() {
return !this.getDelegate().isEnabled() ? new ResponseEntity(Collections.singletonMap("message", "This endpoint is disabled"), HttpStatus.NOT_FOUND) : super.invoke();
}
}
@ConfigurationProperties(
prefix = "endpoints.shutdown"
)
public class ShutdownEndpoint extends AbstractEndpoint<Map<String, Object>> implements ApplicationContextAware {
private static final Map<String, Object> NO_CONTEXT_MESSAGE = Collections.unmodifiableMap(Collections.singletonMap("message", "No context to shutdown."));
private static final Map<String, Object> SHUTDOWN_MESSAGE = Collections.unmodifiableMap(Collections.singletonMap("message", "Shutting down, bye..."));
private ConfigurableApplicationContext context;
public ShutdownEndpoint() {
super("shutdown", true, false);
}
//執行關閉
public Map<String, Object> invoke() {
if (this.context == null) {
return NO_CONTEXT_MESSAGE;
} else {
boolean var6 = false;
Map var1;
class NamelessClass_1 implements Runnable {
NamelessClass_1() {
}
public void run() {
try {
Thread.sleep(500L);
} catch (InterruptedException var2) {
Thread.currentThread().interrupt();
}
//這個調用的就是AbstractApplicationContext.close
ShutdownEndpoint.this.context.close();
}
}
try {
var6 = true;
var1 = SHUTDOWN_MESSAGE;
var6 = false;
} finally {
if (var6) {
Thread thread = new Thread(new NamelessClass_1());
thread.setContextClassLoader(this.getClass().getClassLoader());
thread.start();
}
}
Thread thread = new Thread(new NamelessClass_1());
thread.setContextClassLoader(this.getClass().getClassLoader());
thread.start();
return var1;
}
}
}
版權聲明:本文為CSDN博主「佈道」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。原文鏈接:https://blog.csdn.net/alex_xfboy/article/details/90404691
近期熱文推薦:
1.1,000+ 道 Java面試題及答案整理(2022最新版)
4.別再寫滿屏的爆爆爆炸類了,試試裝飾器模式,這才是優雅的方式!!
覺得不錯,別忘了隨手點贊+轉發哦!