<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Boot 優(yōu)雅停止服務(wù)

          共 7589字,需瀏覽 16分鐘

           ·

          2021-05-20 23:04

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

          76套java從入門到精通實(shí)戰(zhàn)課程分享

          1、基礎(chǔ)知識(shí)

          kill 與 kill -9 的區(qū)別

          內(nèi)容概述 :kill 優(yōu)雅退出, kill -9 暴力退出。

          2、測(cè)試

          2.1、新建測(cè)試項(xiàng)目

          2.1.1、環(huán)境信息

          操作系統(tǒng) :Windows 10

          IDE :IntelliJ IDEA 2020.1 (Ultimate Edition)

          JDK : 11.0.8

          Spring Boot :2.3.7.RELEASE

          2.1.2、新建 SpringBoot 項(xiàng)目

          2.2、打包

          2.3、部署

          上傳 demo-0.0.1-SNAPSHOT.jar 包到服務(wù)器。

          2.3.1、環(huán)境信息

          操作系統(tǒng) :CentOS Linux release 7.6.1810 (Core)

          JDK : 11.0.8

          2.3.2、手動(dòng)優(yōu)雅停止服務(wù)

          啟動(dòng)服務(wù)

          [root@test test]# nohup java -jar ./demo-0.0.1-SNAPSHOT.jar > ./logs/stdout.log 2>&1 &
          [1] 1679

          查看服務(wù)是否正常啟動(dòng)

          [root@test test]# ps -ef|grep demo-0.0.1-SNAPSHOT.jar 
          root      1679 20331 99 23:36 pts/0    00:00:12 java -jar ./demo-0.0.1-SNAPSHOT.jar
          root      1729 20331  0 23:36 pts/0    00:00:00 grep --color=auto demo-0.0.1-SNAPSHOT.jar

          手動(dòng)優(yōu)雅停止服務(wù)

          [root@test test]# kill 1679

          查看日志

          [root@test test]# more logs/stdout.log 
          nohup: ignoring input

            .   ____          _            __ _ _
           /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
          ( ( )\___ | '
          _ | '_| | '_ \/ _` | \ \ \ \
           \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
            '  |____| .__|_| |_|_| |_\__, | / / / /
           =========|_|==============|___/=/_/_/_/
           :: Spring Boot ::        (v2.3.7.RELEASE)

          2021-05-13 23:36:31.536  INFO 1679 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on 196-Pure-CentOS7 with PID 1679 (/root/test/demo-0.0.1-SNAPSHOT.jar started by root in /root/test)
          2021-05-13 23:36:31.541  INFO 1679 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
          2021-05-13 23:36:32.807  INFO 1679 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
          2021-05-13 23:36:32.825  INFO 1679 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
          2021-05-13 23:36:32.825  INFO 1679 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
          2021-05-13 23:36:32.917  INFO 1679 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
          2021-05-13 23:36:32.918  INFO 1679 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1285 ms
          2021-05-13 23:36:33.171  INFO 1679 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService '
          applicationTaskExecutor'
          2021-05-13 23:36:33.388  INFO 1679 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '
          '
          2021-05-13 23:36:33.404  INFO 1679 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.59 seconds (JVM running for 3.289)
          2021-05-13 23:40:40.481  INFO 1679 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService '
          applicationTaskExecutor'

          最后一行日志可以看出,程序優(yōu)雅退出了。

          如果使用 kill -9 1679 , 則不會(huì)有最后一行日志輸出。

          2.3.3、腳本退出

          不想每次都寫那么長(zhǎng)的命令,所以使用腳本啟動(dòng)&退出。

          修改類 DemoApplication.java

          package com.example.demo;

          import org.springframework.boot.SpringApplication;
          import org.springframework.boot.autoconfigure.SpringBootApplication;
          import org.springframework.boot.context.ApplicationPidFileWriter;

          @SpringBootApplication
          public class DemoApplication {

              public static void main(String[] args) {
                  // generate a pid in a specified path, while use command to shutdown pid : cat ./app.pid | xargs kill
                  SpringApplication application = new SpringApplication(DemoApplication.class);
                  application.addListeners(new ApplicationPidFileWriter("./app.pid"));
                  application.run();
              }

          }

          同樣的方式進(jìn)行打包、上傳。

          啟動(dòng)腳本 start.sh

          #!/bin/bash
          # Program:
          # This program for start java services.
          # History:
          # 2021/05/12 First release

          # PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
          # export PATH

          # start javar services
          nohup java -jar ./demo-0.0.1-SNAPSHOT.jar > ./logs/stdout.log 2>&1 &

          exit 0

          停止腳本 stop.sh

          #!/bin/bash
          # Program:
          # This program for stop java services.
          # History:
          # 2021/05/12 First release

          # PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
          # export PATH

          # service : java
          cat ./app.pid | xargs kill

          exit 0

          利用啟動(dòng)&停止腳本,同樣可以達(dá)到優(yōu)雅停止服務(wù)的效果。(推薦使用)

          3、總結(jié)

          事實(shí)上,有很多種方法可以優(yōu)雅的停止服務(wù)。本文僅記錄了真實(shí)的工作中比較常用的一種方法。程序中一般使用內(nèi)存隊(duì)列或線程池的時(shí)候最好要優(yōu)雅的停止服務(wù),將內(nèi)存隊(duì)列沒有處理的數(shù)據(jù)保存起來(lái)或線程池中沒處理完的程序處理完。但是因?yàn)橥C(jī)的時(shí)候比較快,所以停服務(wù)的時(shí)候最好不要處理大量的數(shù)據(jù)操作,這樣會(huì)影響程序停止。



          版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。

          本文鏈接:

          https://blog.csdn.net/achi010/article/details/116770240





          粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

          ??????

          ??長(zhǎng)按上方微信二維碼 2 秒


          感謝點(diǎn)贊支持下哈 


          瀏覽 53
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产一级婬片A片AA片 | 亚洲欧洲日韩在线蜜桃 | 一级免费看精品 | 无码多人| 亚洲AV影院 |