当前位置: 首页 > news >正文

K8S下SpringCloud应用无损下线

废话不多说直接上代码,一种2个步骤

步骤一: 添加以下代码到SpringCloud应用中

 

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;/*** 使用了nacos注册中心的服务关闭端点配置*/
@ConditionalOnClass(NacosAutoServiceRegistration.class)
@RestController
@RequestMapping("actuator")
@RequiredArgsConstructor
@Slf4j
public class NacosStopEndpoint {private final NacosAutoServiceRegistration nacosAutoServiceRegistration;/*** 注销服务后关闭应用前等待的时间(毫秒)*/private int waitTime = 30000;/*** 关闭服务 <br>* 只接收localhost发起的请求** @param request* @return*/@PostMapping("stopService")public ResponseEntity<Boolean> stopNacosService(HttpServletRequest request) throws UnknownHostException {if (!request.getRemoteHost().equalsIgnoreCase("127.0.0.1")) {log.warn("非法访问 尝试关闭应用 远程地址[{}]", request.getRemoteHost());return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);//非本机访问直接return 防止外部攻击}new Thread(() -> {log.info("Nacos instance has been de-registered");nacosAutoServiceRegistration.stop();log.info("Waiting {} milliseconds...", waitTime);try {Thread.sleep(waitTime);} catch (InterruptedException e) {log.info(Thread.currentThread().getName() + " interrupted!", e);}log.info("Spring close……");((ConfigurableApplicationContext) SpringUtil.getApplicationContext()).close();log.info("Spring closed……");System.exit(0);}, "Application Closed Thread").start();return ResponseEntity.ok(true);}
}

步骤二:

配置K8S的工作负载的preStop函数 , 如下
 

["curl","-X","POST","http://127.0.0.1:8080/actuator/stopService","&&","sleep","30"]

大功告成!从此以后应用下线再也不会丢流量了!

http://www.lryc.cn/news/119772.html

相关文章:

  • CEC2013(MATLAB):遗传算法(Genetic Algorithm,GA)求解CEC2013的28个函数
  • Linux tar包安装 Prometheus 和 Grafana
  • 新一代分布式融合存储,数据场景All In One
  • CGroupAndroid实践篇】三、Android CGroup控制组初始化
  • lscpu的各个参数是什么意思?
  • Linux学习————redis服务
  • 【C++手撕系列】——设计日期类实现日期计算器
  • FFmpeg常见命令行(四):FFmpeg流媒体
  • ftp访问ubuntu文件系统
  • 网络防御(6)
  • 【Nginx15】Nginx学习:HTTP核心模块(十二)内嵌变量
  • 2023年中国HPV宫颈癌疫苗需求量、竞争格局、市场规模及行业细分产品规模分析[图]
  • 基于LMK2572的FPGA逻辑实现
  • 数据通信——VRRP
  • 第二章:CSS基础进阶-part2:CSS过渡与动画
  • 华为OD真题---玩牌高手--带答案
  • 案例14 Spring MVC文件上传案例
  • 如何用Python实现多线程
  • Chrome浏览器导出插件并安装到其他电脑浏览器上的解决方案
  • 对话即数据分析,网易数帆ChatBI做到了
  • 简单记录牛客top101算法题(初级题C语言实现)BM17 二分查找 BM21 旋转数组的最小数字 BM23 二叉树的前序遍历
  • 日常BUG——Java使用Bigdecimal类型报错
  • 为Windows Terminal设置背景图片
  • 【Spring】-Spring中Bean对象的存取
  • 机器人CPP编程基础-03变量类型Variables Types
  • 或许有用的开源项目平台——物联网、区块链、商城、CMS、客服系统、低代码、可视化、ERP等
  • 火车头采集伪原创插件【php源码】
  • 【数学】CF1514 C
  • SqlServer基础之(触发器)
  • 数据结构刷题训练:队列实现栈