SpringBoot正确安全的关闭服务
我们利用远程关闭功能可以实现优雅地关闭指定地服务。
正文
本文依然使用v1.5.8.RELEASE ,讲地是利用actuator的Endpoints实现关闭服务
首先准备一个eureka服务,然后启动他。
然后准备一个eureka客户端服务,客户端的pom除了必要的springboot的web依赖还需要添加依赖如下
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-actuator</ artifactId > </ dependency > |
在eureka客户端服务的application.properties文件开启shutdown endpoint,SpringBoot的endpoints.shutdown.enabled默认是关闭的。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
eureka.client.service-url.defaultZone=http: //admin:admin@localhost:1111/eureka/ server.port= 8762 spring.application.name=eureka-client #启用shutdown endpoints.shutdown.enabled= true #禁用密码验证 endpoints.shutdown.sensitive= false #如果用的 2 .x版本的 就用注释的那四行配置 #management.endpoints.shutdown.enabled= true #management.endpoints.health.enabled= true #management.endpoints.web.base-path=/ #management.endpoints.web.exposure.include=* |
配置已经配好,这时可以启动服务了,将他注册在eureka上面,这时我们可以看到下面
然后在终端执行 curl -X POST 127.0.0.1:8762/shutdown ,可以看到message:Shutting down,bye...说明成功关闭了服务
下面笔者要教给大家一种高级使用的方法,做了一个安全的认证,上面关闭服务的缺点大家显而易见,知道服务端口和ip的就能关闭,这种做法很不安全,接下来要在客户端服务配置一下安全认证。
首先在eureka客户端服务的application.properties文件追加配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
eureka.client.service-url.defaultZone=http: //admin:admin@localhost:1111/eureka/ server.port= 8762 spring.application.name=eureka-client management.security.enabled= true #启用shutdown endpoints.shutdown.enabled= true #禁用密码验证 endpoints.shutdown.sensitive= true #验证用户名 security.user.name=admin #验证密码 security.user.password=admin #角色 management.security.role=SUPERUSER #指定shutdown endpoint的路径 endpoints.shutdown.path=/custompath #也可以统一指定所有endpoints的路径`management.context-path=/manage` #指定管理端口和IP management.port= 8081 management.address= 127.0 . 0.1 |
我们使用了security,就需要在pom添加依赖
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-security</ artifactId > </ dependency > |
大功告成,是不是很简单,下面启动你的客户端服务,这里我就不贴一些多余的图片了,成功注册到eureka上面了,和上面的图一样。
接下来使用终端访问 curl -X POST -u admin:admin 127.0.0.1:8081/custompath
看见了你的服务又和你say byebye了吧!
这个命令 curl -X POST -u admin:admin 127.0.0.1:8081/custompath 每一个位置对应的参数值大家可以看application.properties文件分别对应了哪些配置就明白了。
SpringBoot2.0.4关闭程序,我走过的那些坑
首次接触springboot项目,在本地测试的时候,发现不知道怎么关闭程序,虽然后来不得不用杀死进程的方式解决,但总觉得这种方式太简单粗暴。就准备问问度娘别人都是怎么做的。
结果普遍答案是:
步骤:
第一步:引入依赖
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-actuator</ artifactId > </ dependency > |
第二步:application.properties配置
1
2
3
4
5
|
# 启用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以上版本怎么解决,结果大多数是在启动类加一推代码……可能是我不会用吧,反正没成功。继续找……
后来看到大多数人又说,下面的方式配置:
1
2
3
4
5
|
management: endpoints: web: exposure: include: "*" |
然后看日志,发现所有的端点都打开了,就shutdown没打开o(╥﹏╥)o
实在找不到相关博客了,就去官网找答案
官网链接https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
原来人家默认是关着的,那就打开呀!于是我以为发现了新大陆,就去打开,据需看官网,看到这样一句。
1
|
management.endpoint.shutdown.enabled= true |
添加上去,果然成功!
但是,过程中我曾经写成了这样:
1
2
3
4
5
6
7
8
|
##错误写法!!!!!!!!!!!!!!!!! management: endpoints: web: exposure: include: "*" shutdown: enabled: true |
注意哈,这是错误写法,我把endpoints当成了endpoint!!!他们可是不一样的啊!
最终写法:
1
2
3
4
5
6
7
8
9
|
management: endpoints: web: exposure: include: shutdown #注意下面这个位置!! endpoint: shutdown: enabled: true |
注:include后面可以添加你想用到的端点 。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_38003389/article/details/83241504