天天看點

springboot項目優雅的停止服務

springboot項目在啟動的時候,使用java -jar  styy_auth_server.jar

比如我自己在公司測試伺服器上寫的啟動檔案start.sh

#!/bin/sh
nohup java -jar styy_auth_server.jar 2>&1 > styy_auth_server.out &
           

每次啟動後,比如要更新下服務代碼,就要停止服務,

springboot項目優雅的停止服務

每次我都是這樣操作的,kill -9 xxx

很麻煩,,,,,,,

是以今天在浏覽csdn的時候發現别的大佬寫了個優雅的關閉springboot我也試了試。。。效果很不錯\(^o^)/

步驟:

1.在maven中先添加actuator的引用

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

2.在springboot的全局配置檔案application.yml 中添加

management:
  endpoint:
    shutdown:
      enabled: true  #啟用shutdown
  endpoints:
    web:
      exposure:
        include: "*"
      base-path: /MyActuator # 自定義管理端點的字首(保證安全)
  server:
    port: 12888
    address: 127.0.0.1 # 不允許遠端管理連接配接(不允許外部調用保證安全)
           

3.在springboot的主啟動函數類中添加tomcat的的停機支援

package smartt.styy.auth;

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Connector;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;

import org.springframework.context.event.ContextClosedEvent;
import smartt.styy.auth.filter.HttpServletRequestReplacedFilter;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author shangtengfei
 * 啟動方法 ,入口
 */
@SpringBootApplication
@ServletComponentScan
@MapperScan("smartt.styy.auth.mapper")
@Slf4j
public class AuthSpringBootApplication 
{
    public static void main( String[] args ){
    	SpringApplication.run(AuthSpringBootApplication.class, args);
    }
    
    

	/**
	 * 9     * 用于接受 shutdown 事件
	 * 10
	 */
	@Bean
	public GracefulShutdown gracefulShutdown() {
		return new GracefulShutdown();
	}

	@Bean
	public ServletWebServerFactory servletContainer() {
		TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
		tomcatServletWebServerFactory.addConnectorCustomizers(gracefulShutdown());
		return tomcatServletWebServerFactory;
	}

	private class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {
		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();
			try {
				if (executor instanceof ThreadPoolExecutor) {
					ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
					threadPoolExecutor.shutdown();
					if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {
						log.warn("Tomcat 程序在" + waitTime + " 秒内無法結束,嘗試強制結束");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				Thread.currentThread().interrupt();
			}
		}
	}

}
           

4.在postman中測試。添加完後。。。啟動測試

springboot項目優雅的停止服務

注意:

記住一定是post送出, url為127.0.0.1:12888\MyActuator\shutdown,此時idea裡面的項目就停止了。。。

5.編寫linux的停止檔案shutdown.sh  ,對照自己的測試伺服器進行相應的修改

curl -X POST 192.168.55.242(ip):12888(端口)/MyActuator/shutdown
           

基本操作我們就做完了,,,,但是有個問題,别人要是通過掃描工具掃描到了,把我們服務停了怎麼辦,,,,,

安全認證待續。。。。

繼續閱讀