优雅关机使用指南

使用指南

一、spring-boot-starter-actuator 方式

1. pom.xml


  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

2. application.properties


management.endpoint.shutdown.enabled=true

二、spring-boot-graceful-shutdown 方式

1. pom.xml


        <dependency>
            <groupId>com.github.timpeeters</groupId>
            <artifactId>spring-boot-graceful-shutdown</artifactId>
            <version>2.1.1</version>
        </dependency>

2. application.properties


graceful.shutdown.enabled=true
graceful.shutdown.timeout=60s
graceful.shutdown.wait=30s

3.配置说明


配置项默认值描述
graceful.shutdown.enabledfalse是否启用优雅停机
graceful.shutdown.timeout60在关闭 Tomcat Connector 之前等待活跃线程完成的秒数
graceful.shutdown.wait30优雅停机前,服务处于 OUT_OF_SERVICE 的时间

4.优雅停机流程


  1. JVM 接受 SIGTERM 信号,开始关闭 Spring Container
  2. Spring EventListener 监听 ContextClosedEvent 事件,关闭开始后回调
  3. EventListener 更新 Spring Boot HealthIndicator 为 “OUT_OF_SERVICE”
  4. 使用 Thread.sleep 延迟 Context 关闭,以便负载均衡器查看更新的 HealthIndicator 状态,并停止向此实例转发请求
  5. 当 Thread.sleep 结束, Tomcat container 正常关闭。首先通过暂停 Connector,不再接受新的请求,然后通过配置的时间让 Tomcat 线程池处理活跃的线程
  6. 最后, Spring Context 关闭
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值