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 &

每次启动后,比如要更新下服务代码,就要停止服务,

每次我都是这样操作的,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中测试。添加完后。。。启动测试

注意:

记住一定是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

基本操作我们就做完了,,,,但是有个问题,别人要是通过扫描工具扫描到了,把我们服务停了怎么办,,,,,

安全认证待续。。。。

 

 

 

 

 

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值