当把打包好的jar包发布到服务器,并通过java -jar运行,一般要把springboot项目关闭大多数都是先找到项目的pid,然后直接kill pid,不过这种方法在特殊需求场景下不太合适(不安全),同时也不优雅。下面通过actuator来让springboot项目关闭优雅化。
先导入maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
然后切换到application.yaml文件,添加
management: server: # 自定义端口 port: 12358 # 不允许远程管理连接(不允许外部调用保证安全) address: 127.0.0.1 endpoints: web: exposure: # 公开的端点 include: shutdown # 自定义管理端点的前缀(保证安全) base-path: /myshutdown endpoint: shutdown: enabled: true
这样配置后,目前的关闭地址是: http://127.0.0.1:12358/myshutdown/shutdown
只是只需要在本地运行: curl -X POST http://127.0.0.1:12358/myshutdown/shutdown
命令就可以关闭spring boot 项目了
还没有评论,来说两句吧...