当前位置:   article > 正文

Spring Cloud/Boot 微服务 优雅停止(非kill -9)_spring cloud 优雅停机

spring cloud 优雅停机

Spring Cloud/Boot 微服务 优雅停止(非kill -9杀死程序进程)

说明

之前一直用kill -9来停止自己的程序,后来觉得这种方式太简单粗暴了,Spring Cloud/Boot应该有比较优雅方式停止程序(服务),后来在网上找到了一些中文版的通过非kill -9来停止服务的帖子,但是按照文章中的去操作都不成功,于是自己通过看英文的官方文档再加上自己的实践终于实现了目标。以下是具体的实现步骤。(ps:我的Spring Boot版本是2.0.3)

步骤

  1. 在要实现此功能的模块下的pom.xml文件中添加spring-boot-starter-actuator依赖,如下所示:
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
  2. 在配置文件application.ymlapplication.properties中增加以下配置,此处以application.yml为例,如下所示:
    management:
     endpoints:
       web:
         exposure:
           include: "*"
     endpoint:
       shutdown:
         enabled: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  3. 启动程序(服务);
  4. 调用关闭程序(服务)的接口。此处通过curl命令 curl -X POST localhost:port/actuator/shutdown调用该接口,如下所示:
    curl -X POST 192.168.20.177:1025/actuator/shutdown
    
    • 1
  5. 调用命令如下图所示:
    调用命令截图
  6. 控制台日志输出如下图所示:
    控制台日志输出
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/745504
推荐阅读
相关标签
  

闽ICP备14008679号