首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot正确、安全地关闭服务

SpringBoot正确、安全地关闭服务

作者头像
胖虎
发布2019-06-26 15:56:56
9990
发布2019-06-26 15:56:56
举报
文章被收录于专栏:晏霖晏霖

前言 我们利用远程关闭功能可以实现优雅地关闭指定地服务。

正文 本文依然使用v1.5.8.RELEASE ,讲地是利用actuator的Endpoints实现关闭服务

首先准备一个eureka服务,然后启动他。

然后准备一个eureka客户端服务,客户端的pom除了必要的springboot的web依赖还需要添加依赖如下

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

在eureka客户端服务的application.properties文件开启shutdown endpoint,SpringBoot的endpoints.shutdown.enabled默认是关闭的。

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文件追加配置

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添加依赖

<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文件分别对应了哪些配置就明白了。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-01-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 晏霖 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档