{"id":918,"date":"2023-03-11T17:11:22","date_gmt":"2023-03-11T09:11:22","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=918"},"modified":"2023-04-29T15:48:53","modified_gmt":"2023-04-29T07:48:53","slug":"several-methods-for-gracefully-stopping-services-in-spring-boot","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/several-methods-for-gracefully-stopping-services-in-spring-boot\/","title":{"rendered":"Spring Boot\u4f18\u96c5\u505c\u6b62\u670d\u52a1\u7684\u51e0\u79cd\u65b9\u6cd5"},"content":{"rendered":"<p>\u5728\u4f7f\u7528Spring Boot\u7684\u65f6\u5019\uff0c\u90fd\u8981\u6d89\u53ca\u5230\u670d\u52a1\u7684\u505c\u6b62\u548c\u542f\u52a8\uff0c\u5f53\u6211\u4eec\u505c\u6b62\u670d\u52a1\u7684\u65f6\u5019\uff0c\u5f88\u591a\u65f6\u5019\u5927\u5bb6\u90fd\u662f<code>kill -9<\/code>\u76f4\u63a5\u628a\u7a0b\u5e8f\u8fdb\u7a0b\u6740\u6389\uff0c\u8fd9\u6837\u7a0b\u5e8f\u4e0d\u4f1a\u6267\u884c\u4f18\u96c5\u7684\u5173\u95ed\u3002\u800c\u4e14\u4e00\u4e9b\u6ca1\u6709\u6267\u884c\u5b8c\u7684\u7a0b\u5e8f\u5c31\u4f1a\u76f4\u63a5\u9000\u51fa\u3002<\/p>\n<p>\u6211\u4eec\u5f88\u591a\u65f6\u5019\u90fd\u9700\u8981\u5b89\u5168\u7684\u5c06\u670d\u52a1\u505c\u6b62\uff0c\u4e5f\u5c31\u662f\u628a\u6ca1\u6709\u5904\u7406\u5b8c\u7684\u5de5\u4f5c\u7ee7\u7eed\u5904\u7406\u5b8c\u6210\u3002\u6bd4\u5982\u505c\u6b62\u4e00\u4e9b\u4f9d\u8d56\u7684\u670d\u52a1\uff0c\u8f93\u51fa\u4e00\u4e9b\u65e5\u5fd7\uff0c\u53d1\u4e00\u4e9b\u4fe1\u53f7\u7ed9\u5176\u4ed6\u7684\u5e94\u7528\u7cfb\u7edf\uff0c\u8fd9\u4e2a\u5728\u4fdd\u8bc1\u7cfb\u7edf\u7684\u9ad8\u53ef\u7528\u662f\u975e\u5e38\u6709\u5fc5\u8981\u7684\u3002\u90a3\u4e48\u5c31\u6765\u770b\u4e00\u4e0b\u51e0\u79cd\u505c\u6b62Spring Boot\u7684\u65b9\u6cd5\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>Spring Boot\u63d0\u4f9b\u7684actuator\u529f\u80fd<\/h2>\n<p><code>actuator<\/code>\u53ef\u4ee5\u6267\u884c<code>shutdown<\/code>, <code>health<\/code>, <code>info<\/code>\u7b49\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>actuator<\/code>\u7684<code>shutdown<\/code>\u662f<code>disable<\/code>\u7684\uff0c\u9700\u8981\u624b\u52a8\u6253\u5f00\u3002\u9996\u5148\u5f15\u5165<code>acturator<\/code>\u7684maven\u4f9d\u8d56<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-actuator&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u7136\u540e\u5c06<code>shutdown<\/code>\u8282\u70b9\u6253\u5f00\uff0c\u5c06<code>\/actuator\/shutdown<\/code>\u66b4\u9732web\u8bbf\u95ee\u4e5f\u8bbe\u7f6e\u4e0a\uff0c\u9664\u4e86<code>shutdown<\/code>\u4e4b\u5916\u8fd8\u6709<code>health<\/code>, <code>info<\/code>\u7684web\u8bbf\u95ee\u90fd\u6253\u5f00\u7684\u8bdd\u5c06<code>management.endpoints.web.exposure.include=*<\/code>\u5373\u53ef\u3002\u5c06\u5982\u4e0b\u914d\u7f6e\u8bbe\u7f6e\u5230<code>application.properties<\/code>\uff0c\u8bbe\u7f6e\u4e00\u4e0b\u670d\u52a1\u7684\u7aef\u53e3\u53f7\u4e3a3333<\/p>\n<pre><code class=\"language-yml\">server.port=3333\nmanagement.endpoint.shutdown.enabled=true\nmanagement.endpoints.web.exposure.include=shutdown<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2aSpring Boot\u5de5\u7a0b\uff0c\u7136\u540e\u8bbe\u7f6e\u4e00\u4e2a<code>bean<\/code>\u5bf9\u8c61\uff0c\u914d\u7f6e\u4e0a<code>PreDestroy<\/code>\u65b9\u6cd5\u3002\u8fd9\u6837\u5728\u505c\u6b62\u7684\u65f6\u5019\u4f1a\u6253\u5370\u8bed\u53e5\u3002<code>bean<\/code>\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u5206\u4e3a\u521b\u5efa\u3001\u521d\u59cb\u5316\u3001\u9500\u6bc1\uff0c\u5f53\u6700\u540e\u5173\u95ed\u7684\u65f6\u5019\u4f1a\u6267\u884c\u9500\u6bc1\u64cd\u4f5c\u3002\u5728\u9500\u6bc1\u7684\u65b9\u6cd5\u4e2d\u6267\u884c\u4e00\u6761\u8f93\u51fa\u65e5\u5fd7\u3002<\/p>\n<pre><code class=\"language-java\">package cn.appblog.springboot.shutdowndemo.bean;\n\nimport javax.annotation.PreDestroy;\n\npublic class TerminateBean {\n\n    @PreDestroy\n    public void preDestroy() {\n        System.out.println(&quot;TerminalBean is destroyed&quot;);\n    }\n}<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a<code>configuration<\/code>\uff0c\u7136\u540e\u63d0\u4f9b\u4e00\u4e2a\u83b7\u53d6<code>bean<\/code>\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837\u8be5<code>bean<\/code>\u5bf9\u8c61\u4f1a\u88ab\u521d\u59cb\u5316\u3002<\/p>\n<pre><code class=\"language-java\">package cn.appblog.springboot.shutdowndemo.config;\n\nimport cn.appblog.springboot.shutdowndemo.bean.TerminateBean;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration\npublic class ShutDownConfig {\n\n    @Bean\n    public TerminateBean getTerminateBean() {\n        return new TerminateBean();\n    }\n}<\/code><\/pre>\n<p>\u5728\u542f\u52a8\u7c7b\u91cc\u8fb9\u8f93\u51fa\u4e00\u4e2a\u542f\u52a8\u65e5\u5fd7\uff0c\u5f53\u5de5\u7a0b\u542f\u52a8\u7684\u65f6\u5019\uff0c\u4f1a\u770b\u5230\u542f\u52a8\u7684\u8f93\u51fa\uff0c\u63a5\u4e0b\u6765\u6267\u884c\u505c\u6b62\u547d\u4ee4\u3002<\/p>\n<pre><code>curl -X POST http:\/\/localhost:3333\/actuator\/shutdown<\/code><\/pre>\n<p>\u53ef\u4ee5\u8f93\u51fa\u542f\u52a8\u65f6\u7684\u65e5\u5fd7\u6253\u5370\u548c\u505c\u6b62\u65f6\u7684\u65e5\u5fd7\u6253\u5370\uff0c\u540c\u65f6\u7a0b\u5e8f\u5df2\u7ecf\u505c\u6b62\u3002<\/p>\n<h2>\u5173\u95edcontext<\/h2>\n<p>\u83b7\u53d6\u7a0b\u5e8f\u542f\u52a8\u65f6\u5019\u7684<code>context<\/code>\uff0c\u7136\u540e\u5173\u95ed\u4e3b\u7a0b\u5e8f\u542f\u52a8\u65f6\u7684<code>context<\/code>\u3002\u8fd9\u6837\u7a0b\u5e8f\u5728\u5173\u95ed\u7684\u65f6\u5019\u4e5f\u4f1a\u8c03\u7528<code>PreDestroy<\/code>\u6ce8\u89e3\u3002\u5982\u4e0b\u65b9\u6cd5\u5728\u7a0b\u5e8f\u542f\u52a8\u5341\u79d2\u540e\u8fdb\u884c\u5173\u95ed\u3002<\/p>\n<pre><code class=\"language-java\">\/* use ctx.close to shutdown all application context *\/\nConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args);\n\ntry {\n    TimeUnit.SECONDS.sleep(10);\n\n} catch (InterruptedException e) {\n    e.printStackTrace();\n}\n\nctx.close();<\/code><\/pre>\n<h2>\u5173\u95edapp.pid\u6587\u4ef6<\/h2>\n<p>\u5728Spring Boot\u542f\u52a8\u7684\u65f6\u5019\u5c06\u8fdb\u7a0b\u53f7\u5199\u5165\u4e00\u4e2a<code>app.pid<\/code>\u6587\u4ef6\uff0c\u751f\u6210\u7684\u8def\u5f84\u662f\u53ef\u4ee5\u6307\u5b9a\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>cat \/Users\/huangqingshi\/app.id | xargs kill<\/code>\u547d\u4ee4\u76f4\u63a5\u505c\u6b62\u670d\u52a1\uff0c\u6b64\u65f6<code>bean<\/code>\u5bf9\u8c61\u7684<code>PreDestroy<\/code>\u65b9\u6cd5\u4e5f\u4f1a\u8c03\u7528\u7684\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u6bd4\u8f83\u666e\u904d\uff0c\u5199\u4e00\u4e2a<code>start.sh<\/code>\u7528\u4e8e\u542f\u52a8Spring Boot\u7a0b\u5e8f\uff0c\u7136\u540e\u5199\u4e00\u4e2a\u505c\u6b62\u7a0b\u5e8f\u5c06\u670d\u52a1\u505c\u6b62\u3002<\/p>\n<pre><code class=\"language-java\">\/* generate a pid in a specified path, while use command to shutdown pid :\n    &#039;cat \/Users\/huangqingshi\/app.pid | xargs kill&#039; *\/\nSpringApplication application = new SpringApplication(ShutdowndemoApplication.class);\napplication.addListeners(new ApplicationPidFileWriter(&quot;\/Users\/huangqingshi\/app.pid&quot;));\napplication.run();<\/code><\/pre>\n<h2>SpringApplication.exit()<\/h2>\n<p>\u901a\u8fc7\u8c03\u7528\u4e00\u4e2a<code>SpringApplication.exit()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u9000\u51fa\u7a0b\u5e8f\uff0c\u540c\u65f6\u5c06\u751f\u6210\u4e00\u4e2a\u9000\u51fa\u7801\uff0c\u8fd9\u4e2a\u9000\u51fa\u7801\u53ef\u4ee5\u4f20\u9012\u7ed9\u6240\u6709\u7684<code>context<\/code>\u3002\u8fd9\u4e2a\u5c31\u662f\u4e00\u4e2aJVM\u7684\u94a9\u5b50\uff0c\u901a\u8fc7\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u628a\u6240\u6709<code>PreDestroy<\/code>\u7684\u65b9\u6cd5\u6267\u884c\u5e76\u505c\u6b62\uff0c\u5e76\u4e14\u4f20\u9012\u7ed9\u5177\u4f53\u7684\u9000\u51fa\u7801\u7ed9\u6240\u6709<code>context<\/code>\u3002\u901a\u8fc7\u8c03\u7528<code>System.exit(exitCode)<\/code>\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u9519\u8bef\u7801\u4e5f\u4f20\u7ed9JVM\u3002\u7a0b\u5e8f\u6267\u884c\u5b8c\u540e\u6700\u540e\u4f1a\u8f93\u51fa\uff1a<code>Process finished with exit code 0<\/code>\uff0c\u7ed9JVM\u4e00\u4e2aSIGNAL\u3002<\/p>\n<pre><code class=\"language-java\">\/* exit this application using static method *\/\nConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args);\nexitApplication(ctx);<\/code><\/pre>\n<pre><code class=\"language-java\">public static void exitApplication(ConfigurableApplicationContext context) {\n    int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -&gt; 0);\n\n    System.exit(exitCode);\n}<\/code><\/pre>\n<h2>Controller\u4e2d\u505c\u6b62<\/h2>\n<p>\u81ea\u5df1\u5199\u4e00\u4e2aController\uff0c\u5728Controller\u4e2d\u83b7\u53d6\u5230\u7a0b\u5e8f\u7684<code>context<\/code>\uff0c\u7136\u540e\u8c03\u7528\u81ea\u5df1\u914d\u7f6e\u7684Controller\u65b9\u6cd5\u9000\u51fa\u7a0b\u5e8f\u3002\u901a\u8fc7\u8c03\u7528\u81ea\u5df1\u5199\u7684<code>\/shutDownContext<\/code>\u65b9\u6cd5\u5173\u95ed\u7a0b\u5e8f\uff1a<code>curl -X POST http:\/\/localhost:3333\/shutDownContext<\/code><\/p>\n<pre><code class=\"language-java\">package cn.appblog.springboot.shutdowndemo.controller;\n\nimport org.springframework.beans.BeansException;\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.ApplicationContextAware;\nimport org.springframework.context.ConfigurableApplicationContext;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.PostMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\npublic class ShutDownController implements ApplicationContextAware {\n\n    private ApplicationContext context;\n\n    @PostMapping(&quot;\/shutDownContext&quot;)\n    public String shutDownContext() {\n        ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context;\n        ctx.close();\n        return &quot;context is shutdown&quot;;\n    }\n\n    @GetMapping(&quot;\/&quot;)\n    public String getIndex() {\n        return &quot;OK&quot;;\n    }\n\n    @Override\n    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {\n        context = applicationContext;\n    }\n}<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4ee5\u4e0a\u8fd9\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u662f\u771f\u5b9e\u5de5\u4f5c\u4e2d\u8fd8\u9700\u8981\u8003\u8651\u7684\u70b9\u8fd8\u5f88\u591a\uff0c\u6bd4\u5982\u9700\u8981\u4fdd\u62a4\u66b4\u9732\u7684\u70b9\u4e0d\u88ab\u522b\u4eba\u5229\u7528\uff0c\u4e00\u822c\u8981\u52a0\u4e00\u4e9b\u9632\u706b\u5899\uff0c\u6216\u8005\u53ea\u5728\u5185\u7f51\u4f7f\u7528\uff0c\u4fdd\u8bc1\u7a0b\u5e8f\u5b89\u5168\u3002<\/p>\n<p>\u5728\u771f\u5b9e\u7684\u5de5\u4f5c\u4e2d\u7b2c\u4e09\u79cd\u6bd4\u8f83\u5e38\u7528\uff0c\u7a0b\u5e8f\u4e2d\u4e00\u822c\u4f7f\u7528\u5185\u5b58\u961f\u5217\u6216\u7ebf\u7a0b\u6c60\u7684\u65f6\u5019\u6700\u597d\u8981\u4f18\u96c5\u7684\u5173\u673a\uff0c\u5c06\u5185\u5b58\u961f\u5217\u6ca1\u6709\u5904\u7406\u7684\u4fdd\u5b58\u8d77\u6765\u6216\u7ebf\u7a0b\u6c60\u4e2d\u6ca1\u5904\u7406\u5b8c\u7684\u7a0b\u5e8f\u5904\u7406\u5b8c\u3002\u4f46\u662f\u56e0\u4e3a\u505c\u673a\u6bd4\u8f83\u5feb\uff0c\u6240\u4ee5\u505c\u670d\u52a1\u7684\u65f6\u5019\u6700\u597d\u4e0d\u8981\u5904\u7406\u5927\u91cf\u7684\u6570\u636e\u64cd\u4f5c\uff0c\u8fd9\u6837\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u505c\u6b62\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f7f\u7528Spring Boot\u7684\u65f6\u5019\uff0c\u90fd\u8981\u6d89\u53ca\u5230\u670d\u52a1\u7684\u505c\u6b62\u548c\u542f\u52a8\uff0c\u5f53\u6211\u4eec\u505c\u6b62\u670d\u52a1\u7684\u65f6\u5019\uff0c\u5f88\u591a\u65f6\u5019\u5927\u5bb6\u90fd\u662fkill [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-918","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=918"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/918\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}