spring boot2.0实现优雅停机的方法

时间:2021-08-12 18:23:48

前期踩的坑 (spring boot 1.x)

1. 添加mavne依赖

?
1
2
3
4
5
<!-- springboot监控 -->
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-actuator</artifactid>
</dependency>

2. 启用shutdown

在配置文件里添加下面的配置

?
1
2
3
4
#启用shutdown endpoint的http访问
endpoints.shutdown.enabled=true
#不需要验证
endpoints.shutdown.sensitive=false

启动的时候可以看到下面的日志,就说明成功了

spring boot2.0实现优雅停机的方法

3. 优雅停机

发送post请求 http://localhost:8080/shutdown
如果响应码是404 可以尝试post http://localhost:8080/actuator/shutdown

spring boot 2.0

如果你使用的spring boot版本是2.x的就会发现,这些post请求都会出现404的结果。

下面是spring boot 2.0 优雅停机的实现方式。

1.修改application启动类

tomcat容器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@springbootapplication
public class shutdownapplication {
 
  public static void main(string[] args) {
    springapplication.run(shutdownapplication.class, args);
  }
 
  /**
   * 用于接受 shutdown 事件
   */
  @bean
  public gracefulshutdown gracefulshutdown() {
    return new gracefulshutdown();
  }
 
  /**
   * 配置tomcat
   *
   * @return
   */
  @bean
  public servletwebserverfactory servletcontainer() {
    tomcatservletwebserverfactory tomcat = new tomcatservletwebserverfactory();
    tomcat.addconnectorcustomizers(gracefulshutdown());
    return tomcat;
  }
 
  /**
   * 优雅关闭 spring boot。容器必须是 tomcat
   */
  private class gracefulshutdown implements tomcatconnectorcustomizer, applicationlistener<contextclosedevent> {
    private final logger log = loggerfactory.getlogger(gracefulshutdown.class);
    private volatile connector connector;
    private final int waittime = 10;
 
    @override
    public void customize(connector connector) {
      this.connector = connector;
    }
 
    @override
    public void onapplicationevent(contextclosedevent contextclosedevent) {
      this.connector.pause();
      executor executor = this.connector.getprotocolhandler().getexecutor();
      if (executor instanceof threadpoolexecutor) {
        try {
          threadpoolexecutor threadpoolexecutor = (threadpoolexecutor) executor;
          threadpoolexecutor.shutdown();
          if (!threadpoolexecutor.awaittermination(waittime, timeunit.seconds)) {
            log.warn("tomcat 进程在" + waittime + " 秒内无法结束,尝试强制结束");
          }
        } catch (interruptedexception ex) {
          thread.currentthread().interrupt();
        }
      }
    }
  }
}

undertow容器 (没有使用过,不保证可用)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@springbootapplication
public class application {
  public static void main(string[] args) {
    springapplication.run(application.class, args);
  }
 
  /**
   * 优雅关闭 spring boot
   */
  @component
  public class gracefulshutdown implements applicationlistener<contextclosedevent> {
    @autowired
    private gracefulshutdownwrapper gracefulshutdownwrapper;
    @autowired
    private servletwebserverapplicationcontext context;
    @override
    public void onapplicationevent(contextclosedevent contextclosedevent){      gracefulshutdownwrapper.getgracefulshutdownhandler().shutdown();
      try {
        undertowservletwebserver webserver = (undertowservletwebserver)context.getwebserver();
        field field = webserver.getclass().getdeclaredfield("undertow");
        field.setaccessible(true);
        undertow undertow = (undertow) field.get(webserver);
        list<undertow.listenerinfo> listenerinfo = undertow.getlistenerinfo();
        undertow.listenerinfo listener = listenerinfo.get(0);
        connectorstatistics connectorstatistics = listener.getconnectorstatistics();
        while (connectorstatistics.getactiveconnections() > 0){}
      }catch (exception e){
        // application shutdown
      }
    }
  }
  @component
  public class gracefulshutdownwrapper implements handlerwrapper{
    private gracefulshutdownhandler gracefulshutdownhandler;
    @override
    public httphandler wrap(httphandler handler) {
      if(gracefulshutdownhandler == null) {
        this.gracefulshutdownhandler = new gracefulshutdownhandler(handler);
      }
      return gracefulshutdownhandler;
    }
    public gracefulshutdownhandler getgracefulshutdownhandler() {
      return gracefulshutdownhandler;
    }
 
  }
  @component
  @allargsconstructor
  public class undertowextraconfiguration {
    private final gracefulshutdownwrapper gracefulshutdownwrapper;
 
    @bean
    public undertowservletwebserverfactory servletwebserverfactory() {
      undertowservletwebserverfactory factory = new undertowservletwebserverfactory();
      factory.adddeploymentinfocustomizers(deploymentinfo -> deploymentinfo.addouterhandlerchainwrapper(gracefulshutdownwrapper));
      factory.addbuildercustomizers(builder -> builder.setserveroption(undertowoptions.enable_statistics, true));
      return factory;
    }
  }
}

2. 使用 kill命令杀死进程

使用下面的命令杀死进程。该命令是向 某个进程发送终止信号。

?
1
kill -15 [pid]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u013451048/article/details/80194001