初期段階で私が踏み込んだ落とし穴(スプリングブート1.x)
1. mavne依存関係を追加します
<! - スプリングブート監視 - > <依存関係> <groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-actuator </artifactid> </dependency>
2.シャットダウンを有効にします
次の構成ファイルを追加します
#ENABLE HTTPシャットダウンエンドポイントエンドポイントエンドポイントエンドポイントエンドポイントエンドポイントエンドポイントエンドポイントエンドポイント。ShutDown.ENABLED= true#endpoints.shutdown.sensitive = falseを検証する必要はありません
開始時に次のログを見ることができます。つまり、成功していることを意味します
3。エレガントなシャットダウン
投稿リクエストを送信http:// localhost:8080/shutdown
応答コードが404の場合、http:// localhost:8080/actuator/shutdownを投稿することができます
スプリングブート2.0
使用しているスプリングブートバージョンが2.xの場合、これらの投稿リクエストの結果は404の結果であることがわかります。
Spring Boot 2.0エレガントなダウンタイムがどのように実装されているかは次のとおりです。
1.アプリケーションスタートアップクラスを変更します
トムキャットコンテナ
@SpringBootApplicationPublic Class ShutDownApplication {public static void main(string [] args){springApplication.run(shutdownApplication.class、args); } / ***シャットダウンイベントを受け入れるために使用* / @bean public gracefulshutdown gracefulshutdown(){return new Gracefulshutdown(); } / ** * configure tomcat * * @return * / @bean public servletwebserverfactory servletcontainer(){tomcatservletwebserverfactory tomcat = new tomcatservletwebserverfactory(); tomcat.addconnectorcustomizers(gracefulshutdown()); Tomcatを返します。 } /***スプリングブーツを優雅に閉じます。コンテナはtomcat */ private class gracefulshutdownを実装する必要があります。Tomcatconnectorcustomizer、applicationlistener <contextclosedevent> {private final logger log = loggerfactory.getlogger(gracefulshutdown.class);プライベート揮発性コネクタコネクタ。プライベートファイナルint waittime = 10; @Override public voidカスタマイズ(コネクタコネクタ){this.connector = connector; } @Override public void onapplicationEvent(contextclosedEvent contextclosedevent){this.connector.pause(); executor executor = this.connector.getProtocolhandler()。getExecutor(); if(threadpoolexecutorのexecutor Instance){try {shoodPoolexecutor threadPoolexecutor =(threadPoolexecutor)executor; threadpoolexecutor.shutdown(); if(!threadPoolexecutor.awaittermination(waittime、timeunit.seconds)){log.warn( "tomcatプロセスは" + waittime + "秒で終了できません。 }} catch(interruptedexception ex){thread.currentthread()。arturn(); }}}}}}}}}}}}}}アンダートーコンテナ(使用されておらず、利用可能であることが保証されていません)
@springBootApplicationPublic class Application {public static void main(string [] args){springApplication.run(application.class、args); } / ** *スプリングブーツをエレガントに閉じる * / @Component Public Class GracefulShutDown Explments ApplicationListener <ContetuntClosedEvent> {@autowired private gracefulshutdownwrapper gracefulshutdownwrapper; @autowired private servletwebserverapplicationcontextコンテキスト。 @Override public void onApplicationEvent(contextClosedEvent ContextClosedClosedEvent){gracefulshutdownwrapper.getGraceFulShutDownhandler()。shutdown(); try {UnderTowservletWebserver webServer =(undertowservletwebserver)context.getWebserver();フィールドフィールド= webserver.getClass()。getDeclaredField( "Undertow"); field.setAccessible(true); undertow undertow =(undertow)field.get(webserver); List <UnderTow.ListenerInfo> ristenerInfo = undertow.getListenerInfo(); undertow.listenerinfoリスナー= listenerinfo.get(0); connectorStatistics connectorStatistics = listener.getConnectorStatistics(); while(connectorStatistics.getActiveConnections()> 0){}} catch(Exception E){// Application Shutdown}}} @Component Class Class GracefulShutDownWrapperはハンドラーを実装しています{Private Gracefulshutdownhandler Gracefulshutdownhandler; @Override public httphandler wrap(httphandler Handler){if(gracefulshutdownhandler == null){this.gracefulshutdownhandler = new gracefulshutdownhandler(handler); } gracefulshutdownhandlerを返します。 } public Gracefulshutdownhandler getGraceFulShutDownhandler(){return gracefulshutdownhandler; }} @component @AllargSconstructor public class undertowextraconfiguration {private final final gracefulshutdownwrapper gracefulshutdownwrapper; @bean public undertowletwebserverfactory servletwebserverfactory(){undertowservletwebserfactory factory = new underTowservletWebserverFactory(); Factory.adddeploymentInfocustomizers(deploymentinfo-> deploymentinfo.addouterhandlerchainwrapper(gracefulshutdownwrapper)); Factory.AddBuilderCustomizers(builder-> builder.setserveroption(undertowoptions.enable_statistics、true));ファクトリーを返します。 }}}2。キルコマンドを使用してプロセスを殺します
次のコマンドを使用して、プロセスを殺します。このコマンドは、プロセスに終了信号を送信します。
キル-15 [PID]
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。