1、pom.xml中增加依赖
org.springframework.boot
spring-boot-starter-actuator
2、application.properties配置属性
#启用shutdown
management.endpoint.shutdown.enabled=true
management.endpoints.web.exposure.include=*
# 自定义管理端前缀
management.endpoints.web.base-path=/saulala
# 自定义管理端端口
management.server.port=12581
# 指定本机地址作为管理端地址,不允许远程连接,保证安全
management.server.address=127.0.0.1
3、关闭服务的接口
curl -X POST 127.0.0.1:12581/saulala/shutdown
4、封装关闭脚本/home/myservice/scripts/shutdown.sh
#!/bin/bash
PORT=$(netstat -ntulp | grep 12580)
# 如果PORT不为空说明有程序占用该端口号
if [[ -n $PORT ]]; then
echo "The server is open, ready to close"
RESULT="000"
while [[ $RESULT == "000" ]]; do
echo "Closing down ..."
# 优雅关闭springboot项目
RESULT=$(curl -X POST 127.0.0.1:12581/saulala/shutdown)
sleep 2
done
echo "Close off success"
fi
echo "Being started ..."
————————————————
版权声明:本文为CSDN博主「Twilight.c」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/XlxfyzsFdblj/article/details/82054744
5、配置服务/usr/lib/systemd/system/myservice.service,并启用systemctl enable myservice.service
[Unit]
Description=myservice service
After=myservice.service
[Service]
Type=simple
NotifyAccess=all
TimeoutStartSec=0
Restart=on-failure
ExecStart=/home/myservice/jdk1.8.0_65/bin/java -jar /home/myservice/myservice-0.0.1.jar --spring.config.location=/home/myservice/application.properties> log.info 2>log.error &
ExecStop=/home/myservice/scripts/shutdown.sh
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
6、启动、关闭服务
service start myservice
service stop myservice