-
在可能需要被终止的微服务的pom.xml中引入spring-boot-actuator的依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
-
在application.yml或者application.properties中配置actuator的相关属性
#基础路径 management.endpoints.web.base-path=/actuator #手动开启服务终止功能 management.endpoint.shutdown.enabled=true #可暴露的服务 management.endpoints.web.exposure.include=*
-
终止微服务的核心逻辑,通过post方式发起http请求
如果微服务有加密的话,需要给post请求设置cookie,发起请求后,该地址的微服务会自动停止运行
HttpPost、HttpClient等需要引入对应依赖#服务ip,可自定义获取,此处举例写死 String address = "http://localhost:8080"; String url = address + "/actuator/shutdown"; HttpPost httpPost = new HttpPost(url); HttpClient.createDeafulrtClient().execute(httpPost);
通过HTTP请求终止自定义Spring Cloud微服务节点(用不了我吃屎)
最新推荐文章于 2022-06-08 07:32:29 发布
本文介绍了如何在Spring Cloud微服务中利用HTTP请求来终止服务。首先,在微服务的pom.xml中添加spring-boot-actuator依赖,并配置actuator的相关属性。然后,通过POST方式发送HTTP请求,如果服务有安全设置,需要在请求中包含cookie。执行此请求后,对应的微服务节点将被自动停止。
摘要由CSDN通过智能技术生成