您好,登錄后才能下訂單哦!
Springboot項目平滑關閉及自動化關閉腳本,供大家參考,具體內容如下
核心代碼
操作步驟
核心代碼
GracefulShutdown.java
package cnkj.site.utils; import org.apache.catalina.LifecycleException; import org.apache.catalina.connector.Connector; import org.apache.catalina.util.LifecycleBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.event.ContextClosedEvent; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /* * @version 1.0 created by Carol on 2019/4/25 16:22 */ public class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> { private static final Logger LOGGER = LoggerFactory.getLogger(GracefulShutdown.class); private volatile Connector connector; @Override public void customize(Connector connector) { this.connector = connector; } @Override public void onApplicationEvent(ContextClosedEvent event) { try { // 指定執行的方法 shutdown(); //手動清理內存 System.gc(); LOGGER.warn("清理內存完畢,正在退出服務......"); if (this.connector == null){ return; } this.connector.pause(); LOGGER.warn("關閉全部連接......"); Executor executor = this.connector.getProtocolHandler().getExecutor(); if (executor instanceof ThreadPoolExecutor) { try { ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; threadPoolExecutor.shutdown(); LOGGER.warn("當前服務線程池被關閉"); if (!threadPoolExecutor.awaitTermination(30, TimeUnit.SECONDS)) { LOGGER.warn("Tomcat thread pool did not shut down gracefully within 30 seconds. Proceeding with forceful shutdown"); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } this.connector.stop(); } catch (LifecycleException e) { e.printStackTrace(); } } @Bean public GracefulShutdown gracefulShutdown() { return new GracefulShutdown(); } @Bean public ConfigurableServletWebServerFactory webServerFactory(final GracefulShutdown gracefulShutdown) { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.addConnectorCustomizers(gracefulShutdown); return factory; } /** * 執行服務關閉前的一些定制化操作 * 通常需要確認以下步驟 * 1.關閉kafka等數據連接 * 2.flush內存中全部的未處理數據 * 3.清理服務中全部待處理的數據 */ public void shutdown(){} }
Shutdown.java
import cnkj.site.utils.GracefulShutdown; import org.springframework.stereotype.Component; /* * @version 1.0 created by Carol on 2019/4/25 16:39 */ @Component public class Shutdown extends GracefulShutdown { @Override public void shutdown() { // TODO 定制化關閉操作流程 // 關閉 kafka 消費 // flush全部讀寫流 // 清空隊列 // 關閉全部文件流讀寫 } }
ApplicationStarterRunner.java
package cn.migu.log.component; import cnkj.site.utils.HttpCommonUtil; import cnkj.site.CommonInfo; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /* * @version 1.0 created by LXW on 2019/3/14 17:05 */ @Component public class ApplicationStarterRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // 設置服務名 commonInfo.setSERVICE_NAME("Service-Name"); // 自動設置服務啟動后的進程號 commonInfo.setSERVICE_PID(HttpCommonUtil.getCurrentPid()); } }
CommonInfo.java
package cnkj.site.utils; import lombok.Builder; import lombok.Data; import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; @Data @Component public class CommonInfo implements InfoContributor { //當前服務名 private String SERVICE_NAME="SERVICE_NAME"; //服務當前狀態 private int SERVICE_PID; @Override public void contribute(Info.Builder builder) { builder.withDetail("SERVICE_NAME",SERVICE_NAME); builder.withDetail("SERVICE_PID", SERVICE_PID); } public void clearAll(){ this.SERVICE_NAME=""; this.SERVICE_PID=-1; } public Map getAll(){ Map map = new HashMap(); map.put("SERVICE_NAME", getSERVICE_NAME()); map.put("SERVICE_PID", getSERVICE_PID()); return map; } }
HttpCommonUtil.java
package cnkj.site.utils; import javax.servlet.http.HttpServletRequest; import java.lang.management.ManagementFactory; import java.net.InetAddress; import java.net.UnknownHostException; /* * @version 1.0 created by Carol on 2018/10/25 10:04 */ public class HttpCommonUtil { /** * 獲取當前服務的PID * @return PID */ public static Integer getCurrentPid(){ String name = ManagementFactory.getRuntimeMXBean().getName(); String pid = name.split("@")[0]; return Integer.valueOf(pid); } }
application.properties
#服務關閉 management.endpoint.shutdown.enabled=true #監控相關 management.endpoint.prometheus.enabled=true management.endpoints.web.exposure.include=info
操作步驟
項目使用步驟:
1.拷貝上面的 Shutdown.java 代碼到自己的項目中
2.在 Shutdown.java 文件中的shutdown 方法中寫定制化的關閉操作流程
腳本使用步驟:
1.從git獲取最新的項目關閉腳本 地址
2.壓縮server_close 為server_close.zip
3.上傳 server_close.zip 到你服務所在服務器上的 /data/shell 路徑下
4.配置環境變量 vim /etc/profile
5.在profile文件的最下面新增 export PATH=/data/shell/server_close:$PATH
6.保存并退出 :wq
7.如果提示 /bin/bash^M: bad interpreter: No such file or directory,請vim serviceControll.sh,然后 :set fileformat=unix ,然后 :wq 保存并退出即可
8.cd /data/shell/server_close & ./serviceControll.sh 運行即可使用服務關閉腳本
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。