说明
之前一直用kill -9来停止自己的程序,后来觉得这种方式太简单粗暴了,Spring Cloud/Boot应该有比较优雅方式停止程序(服务),后来在网上找到了一些中文版的通过非kill -9来停止服务的帖子,但是按照文章中的去操作都不成功,于是自己通过看英文的官方文档再加上自己的实践终于实现了目标。以下是具体的实现步骤。(ps:我的Spring Boot版本是2.0.3)
步骤
- 在要实现此功能的模块下的pom.xml文件中添加spring-boot-starter-actuator依赖,如下所示:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- 在配置文件application.yml或application.properties中增加以下配置,此处以application.yml为例,如下所示:
management: endpoints: web: exposure: include: "*" endpoint: shutdown: enabled: true
- 启动程序(服务);
- 调用关闭程序(服务)的接口。此处通过curl命令 curl -X POST localhost:port/actuator/shutdown调用该接口,如下所示:
curl -X POST 192.168.20.177:1025/actuator/shutdown
- 调用命令如下图所示:
- 控制台日志输出如下图所示: