SpringBoot之怎么正确、安全的关闭服务(springboot,开发技术)

时间:2024-05-08 04:08:23 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

SpringBoot正确安全的关闭服务

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

正文

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

首先准备一个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=8762spring.application.name=eureka-client#启用shutdownendpoints.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上面,这时我们可以看到下面

SpringBoot之怎么正确、安全的关闭服务

然后在终端执行 curl -X POST 127.0.0.1:8762/shutdown,可以看到message:Shutting down,bye...说明成功关闭了服务

SpringBoot之怎么正确、安全的关闭服务

下面笔者要教给大家一种高级使用的方法,做了一个安全的认证,上面关闭服务的缺点大家显而易见,知道服务端口和ip的就能关闭,这种做法很不安全,接下来要在客户端服务配置一下安全认证。

首先在eureka客户端服务的application.properties文件追加配置

eureka.client.service-url.defaultZone=http://admin:admin@localhost:1111/eureka/server.port=8762spring.application.name=eureka-clientmanagement.security.enabled=true#启用shutdownendpoints.shutdown.enabled=true#禁用密码验证endpoints.shutdown.sensitive=true#验证用户名security.user.name=admin#验证密码security.user.password=admin#角色management.security.role=SUPERUSER#指定shutdownendpoint的路径endpoints.shutdown.path=/custompath#也可以统一指定所有endpoints的路径`management.context-path=/manage`#指定管理端口和IPmanagement.port=8081management.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

SpringBoot之怎么正确、安全的关闭服务

看见了你的服务又和你say byebye了吧!

这个命令 curl -X POST -u admin:admin 127.0.0.1:8081/custompath 每一个位置对应的参数值大家可以看application.properties文件分别对应了哪些配置就明白了。

SpringBoot2.0.4关闭程序,我走过的那些坑

首次接触springboot项目,在本地测试的时候,发现不知道怎么关闭程序,虽然后来不得不用杀死进程的方式解决,但总觉得这种方式太简单粗暴。就准备问问度娘别人都是怎么做的。

结果普遍答案是:

步骤:

第一步:引入依赖

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

第二步:application.properties配置

#启用shutdownendpoints.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以上版本怎么解决,结果大多数是在启动类加一推代码&hellip;&hellip;可能是我不会用吧,反正没成功。继续找&hellip;&hellip;

后来看到大多数人又说,下面的方式配置:

management:endpoints:web:exposure:include:"*"

然后看日志,发现所有的端点都打开了,就shutdown没打开o(╥﹏╥)o

实在找不到相关博客了,就去官网找答案

SpringBoot之怎么正确、安全的关闭服务

原来人家默认是关着的,那就打开呀!于是我以为发现了新大陆,就去打开,据需看官网,看到这样一句。

management.endpoint.shutdown.enabled=true

添加上去,果然成功!

SpringBoot之怎么正确、安全的关闭服务

但是,过程中我曾经写成了这样:

##错误写法!!!!!!!!!!!!!!!!!management:endpoints:web:exposure:include:"*"shutdown:enabled:true

注意哈,这是错误写法,我把endpoints当成了endpoint!!!他们可是不一样的啊!

最终写法:

management:endpoints:web:exposure:include:shutdown#注意下面这个位置!!endpoint:shutdown:enabled:true

注:include后面可以添加你想用到的端点 。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:SpringBoot之怎么正确、安全的关闭服务的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:ahooks useRequest怎么使用下一篇:

6 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18