当前位置:   article > 正文

springboot项目优雅的停止服务_springboot终止服务

springboot终止服务

springboot项目在启动的时候,使用java -jar  styy_auth_server.jar

比如我自己在公司测试服务器上写的启动文件start.sh

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

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

每次我都是这样操作的,kill -9 xxx

很麻烦,,,,,,,

所以今天在浏览csdn的时候发现别的大佬写了个优雅的关闭springboot我也试了试。。。效果很不错\(^o^)/

步骤:

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

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>

2.在springboot的全局配置文件application.yml 中添加

  1. management:
  2. endpoint:
  3. shutdown:
  4. enabled: true #启用shutdown
  5. endpoints:
  6. web:
  7. exposure:
  8. include: "*"
  9. base-path: /MyActuator # 自定义管理端点的前缀(保证安全)
  10. server:
  11. port: 12888
  12. address: 127.0.0.1 # 不允许远程管理连接(不允许外部调用保证安全)

3.在springboot的主启动函数类中添加tomcat的的停机支持

  1. package smartt.styy.auth;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.catalina.connector.Connector;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
  8. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
  9. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  10. import org.springframework.boot.web.servlet.ServletComponentScan;
  11. import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
  12. import org.springframework.context.ApplicationListener;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.event.ContextClosedEvent;
  15. import smartt.styy.auth.filter.HttpServletRequestReplacedFilter;
  16. import java.util.concurrent.Executor;
  17. import java.util.concurrent.ThreadPoolExecutor;
  18. import java.util.concurrent.TimeUnit;
  19. /**
  20. * @author shangtengfei
  21. * 启动方法 ,入口
  22. */
  23. @SpringBootApplication
  24. @ServletComponentScan
  25. @MapperScan("smartt.styy.auth.mapper")
  26. @Slf4j
  27. public class AuthSpringBootApplication
  28. {
  29. public static void main( String[] args ){
  30. SpringApplication.run(AuthSpringBootApplication.class, args);
  31. }
  32. /**
  33. * 9 * 用于接受 shutdown 事件
  34. * 10
  35. */
  36. @Bean
  37. public GracefulShutdown gracefulShutdown() {
  38. return new GracefulShutdown();
  39. }
  40. @Bean
  41. public ServletWebServerFactory servletContainer() {
  42. TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
  43. tomcatServletWebServerFactory.addConnectorCustomizers(gracefulShutdown());
  44. return tomcatServletWebServerFactory;
  45. }
  46. private class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {
  47. private volatile Connector connector;
  48. private final int waitTime = 10;
  49. @Override
  50. public void customize(Connector connector) {
  51. this.connector = connector;
  52. }
  53. @Override
  54. public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
  55. this.connector.pause();
  56. Executor executor = this.connector.getProtocolHandler().getExecutor();
  57. try {
  58. if (executor instanceof ThreadPoolExecutor) {
  59. ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
  60. threadPoolExecutor.shutdown();
  61. if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {
  62. log.warn("Tomcat 进程在" + waitTime + " 秒内无法结束,尝试强制结束");
  63. }
  64. }
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. Thread.currentThread().interrupt();
  68. }
  69. }
  70. }
  71. }

 

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

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

安全认证待续。。。。

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/334243
推荐阅读
相关标签
  

闽ICP备14008679号