spring cloud shutdown graceful 优雅停机

当一个服务启动后,会注册到eureka中,其他的服务也可以从eureka获取到新注册的服务。但当我们要停止一个服务的时候,如果直接kill -9 pid,未免有些太过暴力。

直接杀进程有什么问题

eureka要经过一段时间才会把已经挂掉的服务踢出,其他的服务上也还会保留住这个服务一段时间,他们都认为该服务可用。eureka的管理界面中还可以看到该服务,其他服务则还保留这个服务副本一段时间。这时候,如果发起一个请求,就会发生错误,服务不存在或降级。

解决办法

spring cloud中,提供了shutdown端点,可以实现优雅停机。eureka可以马上把这个服务剔除,其他的服务中保留的这个服务也将被马上踢出。

pom.xml增加依赖

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

application.properties增加配置

# 启用shutdown
endpoints.shutdown.enabled=true # 禁用密码验证
endpoints.shutdown.sensitive=false

服务启动后,可以通过linux的curl命令发送POST请求的方式优雅的停止服务。

curl -X POST host:port/shutdown

如果配置了management.context-path=/manage,则命令变为:

curl -X POST host:port/manage/shutdown

额外的拓展

如果调用了curl -X POST host:port/shutdown命令后,还想做一些别的操作,可以在主类中增加监听。

代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.validation.constraints.NotNull; @SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@NotNull
@Bean
ServletListenerRegistrationBean<ServletContextListener> myServletListener() {
ServletListenerRegistrationBean<ServletContextListener> srb =
new ServletListenerRegistrationBean<>();
srb.setListener(new ExampleServletContextListener());
return srb;
} public class ExampleServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(
ServletContextEvent sce) {
// Context Initialised
} @Override
public void contextDestroyed(
ServletContextEvent sce) {
// Here - what you want to do that context shutdown
System.out.println("==============调用了shutdown后输出==================");
}
}
}

参考:

https://blog.csdn.net/qq276726581/article/details/55520762

https://blog.csdn.net/chinrui/article/details/78685032

https://stackoverflow.com/questions/26678208/spring-boot-shutdown-hook/48181392#48181392

spring cloud shutdown graceful 优雅停机的更多相关文章

  1. spring cloud 优雅停机

    spring cloud 优雅停机 大部分部署项目如果要停掉项目一般都是用kill -9 来杀进程 但是由于Eureka采用心跳的机制来上下线服务,会导致服务消费者调用已经kill的服务提供者然后出错 ...

  2. Spring Boot 2.3.0正式发布:优雅停机、配置文件位置通配符新特性一览

    当大潮退去,才知道谁在裸泳..关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis. ...

  3. Spring Boot 系列:最新版优雅停机详解

    爱生活,爱编码,本文已收录架构技术专栏关注这个喜欢分享的地方. 开源项目: 分布式监控(Gitee GVP最有价值开源项目 ):https://gitee.com/sanjiankethree/cub ...

  4. Spring Boot 2.3 新特性优雅停机详解

    什么是优雅停机 先来一段简单的代码,如下: @RestController public class DemoController { @GetMapping("/demo") p ...

  5. Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现

    Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现 Anoyi 精讲JAVA 精讲JAVA 微信号 toooooooozi 功能介绍 讲解java深层次 ...

  6. 【译文】用Spring Cloud和Docker搭建微服务平台

    by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...

  7. SpringCloud微服务如何优雅停机及源码分析

    目录 方式一:kill -9 java进程id[不建议] 方式二:kill -15 java进程id 或 直接使用/shutdown 端点[不建议] kill 与/shutdown 的含义 Sprin ...

  8. Spring Cloud和Docker搭建微服务平台

    用Spring Cloud和Docker搭建微服务平台 This blog series will introduce you to some of the foundational concepts ...

  9. Dubbo 优雅停机演进之路

    一.前言 在 『ShutdownHook- Java 优雅停机解决方案』 一文中我们聊到了 Java 实现优雅停机原理.接下来我们就跟根据上面知识点,深入 Dubbo 内部,去了解一下 Dubbo 如 ...

随机推荐

  1. ES6学习--箭头函数

    1. 箭头函数基本形式 let func = (num) => num; let func = () => num; let sum = (num1,num2) => num1 + ...

  2. Python入门之面向对象的__init__和__new__方法

    Python入门之面向对象的__init__和__new__方法

  3. 04: python常用模块

    目录: 1.1 时间模块time() 与 datetime() 1.2 random()模块 1.3 os模块 1.4 sys模块 1.5 tarfile用于将文件夹归档成 .tar的文件 1.6 s ...

  4. 基于qml创建最简单的android机图像采集程序

    前提是在已经搭建为android编写程序的qt平台上面,我们只需要简单几部就可以搭建最简单的android机图像采集程序 1.生成新的ququick app 2.在配置中添加 multimedia,因 ...

  5. 20165211 2017-2018-2 《Java程序设计》第7周学习总结

    20165211 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 本周,我学习了书本上第十一章的内容,以下是我整理的主要知识. 第十一章 JDBC和MySQL ...

  6. 网站精准查询IP

    分享一些网站能精准查询IP的 https://www.chaidu.com/App/Web/IP/ http://www.ipip.net/ip.html http://www.hao7188.com ...

  7. 详解C中的volatile关键字【转】

    本文转载自:http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764231.html volatile提醒编译器它后面所定义的变量随时都有 ...

  8. BZOJ2724 [Violet]蒲公英 分块

    题目描述 经典区间众数题目 然而是权限题,所以题目链接放Luogu的 题解 因为太菜所以只会$O(n*\sqrt{n}+n*\sqrt{n}*log(n))$的做法 就是那种要用二分的,并不会clj那 ...

  9. 01_Spark基础

    1.1.Spark Ecosystem BlinkDB: 允许用户定义一个错误范围,BlinkDB将在用户给定的错误范围内,尽可能快的提供查询结果 1.2.Spark愿景 1.3.Spark简介 1) ...

  10. EasyUI ---- draggable可拖动的用法

    <link href="~/Scripts/easyui1.5/themes/default/easyui.css" rel="stylesheet" / ...