因springboot内嵌tomcat或jetty使得我们没法去操作服务:
因此,常常是服务起来后,要重启时会端口占用,我们只能无情的kill掉端口。
不过spring也设置有配置停止的请求:
Application.properties中添加:
endpoints.shutdown.enabled=true
endpoints.shutdown.sensitive=false
在pom.xml中添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
此时,需要停止服务时,只需要在网页上以post的形式请求:
http://ip:port/shutdown
也可通过控制台:
>curl -X POST http://127.0.0.1:8081/shutdown
最后结果为: {"message":"Shutting down, bye..."}
以上的停止方式不安全,会导致任何人都可以进行操作。因此需要添加安全限制。
endpoints.shutdown.enabled=true
endpoints.shutdown.sensitive=true
endpoints.shutdown.path=shutdown
security.user.name=root
security.user.password=denny#
management.security.role=SUPERUSER
management.context-path=/manage
#management.port=
#management.address=192.168.204.21
在pom.xml中添加security的服务:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>