首次接触springboot项目,在本地测试的时候,发现不知道怎么关闭程序,虽然后来不得不用杀死进程的方式解决,但总觉得这种方式太简单粗暴。就准备问问度娘别人都是怎么做的。
结果普遍答案是:
步骤:
第一步:引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
第二步:application.properties配置
# 启用shutdown
endpoints.shutdown.enabled=true
# 禁用密码验证
endpoints.shutdown.sensitive=false
第三步:http://IP:端口号/actuator/shutdown或者http://IP:端口号/shutdown
结果:
404!!!!!!!
为什么总是404?
后来幡然醒悟,别人都是springboot 1.X,而我的是2.X。(springboot变化好大o(╥﹏╥)o)
接着,我继续查2.0以上版本怎么解决,结果大多数是在启动类加一推代码……可能是我不会用吧,反正没成功。继续找……
后来看到大多数人又说,下面的方式配置:
management:
endpoints:
web:
exposure:
include: "*"
然后看日志,发现所有的端点都打开了,就shutdown没打开o(╥﹏╥)o
实在找不到相关博客了,就去官网找答案
官网链接https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
原来人家默认是关着的,那就打开呀!于是我以为发现了新大陆,就去打开,据需看官网,看到这样一句。
management.endpoint.shutdown.enabled=true
添加上去,果然成功!
但是,过程中我曾经写成了这样:
##错误写法!!!!!!!!!!!!!!!!!
management:
endpoints:
web:
exposure:
include: "*"
shutdown:
enabled: true
注意哈,这是错误写法,我把endpoints当成了endpoint!!!他们可是不一样的啊!
最终写法:
management:
endpoints:
web:
exposure:
include: shutdown
#注意下面这个位置!!
endpoint:
shutdown:
enabled: true
注:include后面可以添加你想用到的端点 。