zoukankan      html  css  js  c++  java
  • 实现SpringBoot应用的优雅关机

    1、pom.xml中增加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    

    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
    

      

  • 相关阅读:
    打开控制面板中的程序
    内存使用情况监控
    监视剪切板内容
    检测系统启动模式
    启动Windows服务
    开机启动项管理
    docker部署war+tomcat8
    docker安装mysql5.6和redis3.2
    nginx命令和配置
    nginx安装
  • 原文地址:https://www.cnblogs.com/yiyitong/p/11613268.html
Copyright © 2011-2022 走看看