91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring boot2.0實現優雅停機的方法

發布時間:2020-09-11 04:25:10 來源:腳本之家 閱讀:187 作者:cm_zdRan 欄目:編程語言

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

1. 添加mavne依賴

<!-- springboot監控 --> 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-actuator</artifactId> 
</dependency> 

2. 啟用shutdown

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

#啟用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容器

@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容器 (沒有使用過,不保證可用)

@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命令殺死進程

使用下面的命令殺死進程。該命令是向 某個進程發送終止信號。

kill -15 [PID]

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

兴宁市| 锦屏县| 大理市| 福清市| 盱眙县| 五常市| 普陀区| 喀喇沁旗| 和龙市| 敦化市| 平陆县| 于田县| 连山| 临沂市| 金阳县| 沁阳市| 恭城| 星子县| 额敏县| 绥滨县| 乐安县| 泽普县| 东光县| 宁陵县| 资兴市| 榆中县| 新闻| 大厂| 凯里市| 儋州市| 新乡县| 友谊县| 社会| 鲁山县| 建德市| 西青区| 八宿县| 宣恩县| 福鼎市| 贺兰县| 金溪县|