赞
踩
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
基本操作我们就做完了,,,,但是有个问题,别人要是通过扫描工具扫描到了,把我们服务停了怎么办,,,,,
安全认证待续。。。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。