日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 程序停止打印日志_停止 Spring Boot 服务的几种优雅姿势

發布時間:2023/12/10 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 程序停止打印日志_停止 Spring Boot 服务的几种优雅姿势 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在使用 Spring Boot 的時候,都要涉及到服務的停止和啟動,當我們停止服務的時候,很多時候大家都是 kill -9 直接把程序進程殺掉,這樣程序不會執行優雅的關閉。而且一些沒有執行完的程序就會直接退出。

我們很多時候都需要安全的將服務停止,也就是把沒有處理完的工作繼續處理完成。比如停止一些依賴的服務,輸出一些日志,發一些信號給其他的應用系統,這個在保證系統的高可用是非常有必要的。那么咱么就來看一下幾種停止 Spring Boot 的方法。

第一種就是 Spring Boot 提供的 actuator 的功能,它可以執行 shutdown, health, info 等,默認情況下,actuator 的 shutdown 是 disable 的,我們需要打開它,首先引入 acturator 的 maven 依賴。

  <dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-actuatorartifactId>

dependency>

然后將 shutdown 節點打開,也將 /actuator/shutdown 暴露 web 訪問也設置上,除了 shutdown 之外還有 health, info 的 web 訪問都打開的話將 management.endpoints.web.exposure.include=* 就可以。將如下配置設置到 application.properties 里邊,設置一下服務的端口號為 3333。

server.port=3333

management.endpoint.shutdown.enabled=true

management.endpoints.web.exposure.include=shutdown

接下來,咱們創建一個 Spring Boot 工程,然后設置一個 bean 對象,配置上 PreDestroy 方法。這樣在停止的時候會打印語句。bean 的整個生命周期分為創建、初始化、銷毀,當最后關閉的時候會執行銷毀操作,在銷毀的方法中執行一條輸出日志。

package com.hqs.springboot.shutdowndemo.bean;

import javax.annotation.PreDestroy;

/**

* @author huangqingshi

* @Date 2019-08-17

*/

public class TerminateBean {

@PreDestroy

public void preDestroy() {

System.out.println("TerminalBean is destroyed");

}

}

做一個 configuration,然后提供一個獲取 bean 的方法,這樣該 bean 對象會被初始化。

package com.hqs.springboot.shutdowndemo.config;

import com.hqs.springboot.shutdowndemo.bean.TerminateBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* @author huangqingshi

* @Date 2019-08-17

*/

@Configuration

public class ShutDownConfig {

@Bean

public TerminateBean getTerminateBean() {

return new TerminateBean();

}

}

在啟動類里邊輸出一個啟動日志,當工程啟動的時候,會看到啟動的輸出,接下來咱們執行停止命令。

curl -X POST http://localhost:3333/actuator/shutdown

以下日志可以輸出啟動時的日志打印和停止時的日志打印,同時程序已經停止,是不是比較神奇。

第二種方法也比較簡單,獲取程序啟動時候的 context,然后關閉主程序啟動時的 context。這樣程序在關閉的時候也會調用 PreDestroy 注解,如下方法在程序啟動十秒后進行關閉。

/* method 2: use ctx.close to shutdown all application context */

ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args);

try {

TimeUnit.SECONDS.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

ctx.close();

第三種方法,在 Spring Boot 啟動的時候將進程號寫入一個 app.pid 文件,生成的路徑是可以指定的,可以通過命令 cat /Users/huangqingshi/app.id | xargs kill 命令直接停止服務,這個時候 bean 對象的 PreDestroy 方法也會調用的。這種方法大家使用的比較普遍。寫一個 start.sh 用于啟動 Spring Boot 程序,然后寫一個停止程序將服務停止。

/* method 3 : generate a pid in a specified path, while use command to shutdown pid :

'cat /Users/huangqingshi/app.pid | xargs kill' */

SpringApplication application = new SpringApplication(ShutdowndemoApplication.class);

application.addListeners(new ApplicationPidFileWriter("/Users/huangqingshi/app.pid"));

application.run();

第四種方法,通過調用一個 SpringApplication.exit() 方法也可以退出程序,同時將生成一個退出碼,這個退出碼可以傳遞給所有的 context。

這個就是一個 JVM 的鉤子,通過調用這個方法的話會把所有 PreDestroy 的方法執行并停止,并且傳遞給具體的退出碼給所有 Context。通過調用 System.exit(exitCode) 可以將這個錯誤碼也傳給 JVM。程序執行完后最后會輸出:Process finished with exit code 0,給 JVM 一個 SIGNAL。

/* method 4: exit this application using static method */

ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args);

exitApplication(ctx);

public static void exitApplication(ConfigurableApplicationContext context) {

int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);

System.exit(exitCode);

}

第五種方法,自己寫一個 Controller,然后將自己寫好的 Controller 獲取到程序的 context,然后調用自己配置的 Controller 方法退出程序。

通過調用自己寫的 /shutDownContext 方法關閉程序:curl -X POST http://localhost:3333/shutDownContext。

package com.hqs.springboot.shutdowndemo.controller;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @author huangqingshi

* @Date 2019-08-17

*/

@RestController

public class ShutDownController implements ApplicationContextAware {

private ApplicationContext context;

@PostMapping("/shutDownContext")

public String shutDownContext() {

ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context;

ctx.close();

return "context is shutdown";

}

@GetMapping("/")

public String getIndex() {

return "OK";

}

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

context = applicationContext;

}

}

好了,Spring Boot 的優雅關閉方法也都實現好了,也有同學問,如何暴力停止呢,簡單,直接 kill -9 相應的 PID 即可。

總結

以上這幾種方法實現的話比較簡單,但是真實工作中還需要考慮的點還很多,比如需要保護暴露的點不被別人利用,一般要加一些防火墻,或者只在內網使用,保證程序安全。

在真實的工作中的時候第三種比較常用,程序中一般使用內存隊列或線程池的時候最好要優雅的關機,將內存隊列沒有處理的保存起來或線程池中沒處理完的程序處理完。但是因為停機的時候比較快,所以停服務的時候最好不要處理大量的數據操作,這樣會影響程序停止。

所謂技多不壓身,我們所讀過的每一本書,所學過的每一門語言,在未來指不定都能給我們意想不到的回饋呢。其實做為一個開發者,有一個學習的氛圍跟一個交流圈子特別重要這里我推薦一個Java學習交流群私信小編回復JAVA獲得進群資料,不管你是小白還是大牛歡迎入駐,大家一起交流成長。

總結

以上是生活随笔為你收集整理的python 程序停止打印日志_停止 Spring Boot 服务的几种优雅姿势的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。