通过HTTP请求终止自定义Spring Cloud微服务节点(用不了我吃屎)

26 篇文章 2 订阅
12 篇文章 1 订阅
本文介绍了如何在Spring Cloud微服务中利用HTTP请求来终止服务。首先,在微服务的pom.xml中添加spring-boot-actuator依赖,并配置actuator的相关属性。然后,通过POST方式发送HTTP请求,如果服务有安全设置,需要在请求中包含cookie。执行此请求后,对应的微服务节点将被自动停止。
摘要由CSDN通过智能技术生成
  1. 在可能需要被终止的微服务的pom.xml中引入spring-boot-actuator的依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    
  2. 在application.yml或者application.properties中配置actuator的相关属性

    #基础路径
    management.endpoints.web.base-path=/actuator
    #手动开启服务终止功能
    management.endpoint.shutdown.enabled=true
    #可暴露的服务
    management.endpoints.web.exposure.include=*
    
  3. 终止微服务的核心逻辑,通过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);
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值