美文网首页Spring Cloud 相关文章Springboot
如何优雅的关闭Springboot程序

如何优雅的关闭Springboot程序

作者: V哥带你写程序 | 来源:发表于2018-11-24 23:01 被阅读0次

    springboot程序部署之后了,如果代码更新,需要重新部署咋办,粗暴的关闭方式是 kill -9 直接干掉,然后替换程序再启动,太粗暴了点。

    实际上springboot自带的关闭程序的方法,有个模块叫actuator。当然actuator能做的事情有很多,其中就包含关闭程序。

    配置很简单:

    1. pom 文件中引入actuator

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

    2. 在application.properties中启用shutdown管理

    //actuator中远程关闭程序默认是不打开的,需要打开

    management.endpoint.shutdown.enabled=true

    //actuator默认是使用JMX来做管理的,启动HTTP方式控制也要配置一下

    management.endpoints.web.exposure.include=shutdown,info,health

    就好了! 发一个HTTP POST请求就可以关闭正在运行的springboot程序了

    请求地址: http://你的服务器IP/actuator/shutdown

    服务器的应答如下,程序给说bye bye

    3. 这样实现目的了,但其他人也可以不要密码直接就可以关闭你的程序。最好还是把请求路径中的actuator改一改吧,不然谁都可以POST一个请求把你的程序给关了。配置也只需在springboot配置文件中加一行即可

    management.endpoints.web.base-path=/xxx_manage

    改之后,就要通过发送POST请求到 http://服务器IP/xxx_manage/shutdown 来关闭应用程序了

    相关文章

      网友评论

        本文标题:如何优雅的关闭Springboot程序

        本文链接:https://www.haomeiwen.com/subject/kykuqqtx.html