spring cloud shutdown graceful 优雅停机
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 优雅停机的更多相关文章
- spring cloud 优雅停机
spring cloud 优雅停机 大部分部署项目如果要停掉项目一般都是用kill -9 来杀进程 但是由于Eureka采用心跳的机制来上下线服务,会导致服务消费者调用已经kill的服务提供者然后出错 ...
- Spring Boot 2.3.0正式发布:优雅停机、配置文件位置通配符新特性一览
当大潮退去,才知道谁在裸泳..关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis. ...
- Spring Boot 系列:最新版优雅停机详解
爱生活,爱编码,本文已收录架构技术专栏关注这个喜欢分享的地方. 开源项目: 分布式监控(Gitee GVP最有价值开源项目 ):https://gitee.com/sanjiankethree/cub ...
- Spring Boot 2.3 新特性优雅停机详解
什么是优雅停机 先来一段简单的代码,如下: @RestController public class DemoController { @GetMapping("/demo") p ...
- Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现
Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现 Anoyi 精讲JAVA 精讲JAVA 微信号 toooooooozi 功能介绍 讲解java深层次 ...
- 【译文】用Spring Cloud和Docker搭建微服务平台
by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...
- SpringCloud微服务如何优雅停机及源码分析
目录 方式一:kill -9 java进程id[不建议] 方式二:kill -15 java进程id 或 直接使用/shutdown 端点[不建议] kill 与/shutdown 的含义 Sprin ...
- Spring Cloud和Docker搭建微服务平台
用Spring Cloud和Docker搭建微服务平台 This blog series will introduce you to some of the foundational concepts ...
- Dubbo 优雅停机演进之路
一.前言 在 『ShutdownHook- Java 优雅停机解决方案』 一文中我们聊到了 Java 实现优雅停机原理.接下来我们就跟根据上面知识点,深入 Dubbo 内部,去了解一下 Dubbo 如 ...
随机推荐
- spring mvc @ModelAttribute 每次执行requestmapping前自动执行
在不少应用场景中,我们希望在每次执行requestmapping前自动执行一些操作,比如把某些数据(比如数据字典.系统配置.标准错误号,这在企业应用系统中极为常见)塞到model中供view访问,因为 ...
- 20145106java实验四
实验名称:Java网络编程 实验内容: 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 结对小伙伴 20145109竺文君 博客地址: 在本次实验中,是以我作为服务 ...
- 20145122《敏捷开发与XP实践 》实验三实验报告
实验名称 敏捷开发与XP实践 实验内容 1.团队代码要使用git在实验楼中托管,要使用结对同学中的一个同学的账号托管. 2.使用git推送代码并对结对同学的代码修改完成后再git推送. 3.掌握重构流 ...
- 20145322 Exp5 MS11_050
20145322 Exp5 MS11_050 实验过程 msfconsole命令进入控制台 使用命令search ms11_050查看针对MS11_050漏洞的攻击模块 使用命令 use exploi ...
- linux内核分析 第一周
计算机是如何工作的 冯·诺依曼理论的要点是: 数字计算机的数制采用二进制:计算机应该按照程序顺序执行. 冯·诺依曼体系结构 根据冯·诺依曼体系结构构成的计算机,必须具有如下功能:把需要的程序和数据送至 ...
- C++ 项目中直接使用JsonCpp源码文件
之前在网上看到使用JsonCpp都是以库的形式使用(编译源码为静态库或者动态库),这样引用很方便,但有时候报错调试看不到错误的地方,所以就想直接把源文件添加到项目中,方便调试 这是用到源码文件: 创建 ...
- AP与CP介绍【转】
本文转载子:https://blog.csdn.net/wqlinf/article/details/8663170 基带芯片加协处理器(CP,通常是多媒体加速器).这类产品以MTK方案为典型代表,M ...
- ubuntu查询某个库的相关情况
环境:Ubuntu 14.04 64bit 1.如:查询libjpeg库的位置 ldconfig -p |grep libjpeg 2.如:查询libjpeg库的相关名称 dpkg -l '*jpeg ...
- samba基本应用24-4及示例
samba smb:service message block(137/udp, 138/udp, 139/tcp, 445/tcp) 协议是:CIFS:Common Internet File Sy ...
- BZOJ 1015: [JSOI2008]星球大战starwar(并查集求连通块+离线处理)
http://www.lydsy.com/JudgeOnline/problem.php?id=1015 题意: 思路:好题啊!!! 这道题目需要离线处理,先把所有要删的点给保存下来,然后逆序加点,这 ...