bootstrap.yaml配置项目的pid输出位置

spring:
pid:
file: F:/cloud-nacos/cloud_gateway/application.pid

springboot项目修改启动类启动方式

原始启动类

SpringApplication.run(MainApplication.class, args);

@SpringBootApplication
@EnableDiscoveryClient
public class GateWayApp {
/**
* @author: GuoTong
* @date: 2022-10-05 10:42:59
*/
public static void main(String[] args) {
try {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication springApplication = new SpringApplication(MainApplication.class);
springApplication.addListeners(new ApplicationPidFileWriter());
springApplication.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}

优雅停机和暴力停机的接口

package com.gton.shutdown;

import com.alibaba.cloud.commons.io.FileUtils;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.gton.config.Resp;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.PreDestroy;
import java.io.File;
import java.io.IOException; /**
* @description: 优雅停机?
* 简单的说,就是向应用进程发出停止指令之后,能保证正在执行的业务操作不受影响,
* 直到操作运行完毕之后再停止服务。
* @author: GuoTong
* @createTime: 2023-07-14 22:32
* @since JDK 1.8 OR 11
**/
@RestController
@Slf4j
public class ApplicationContextShutDown implements ApplicationContextAware { private ApplicationContext context; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
} /**
* Description: 优雅停机
*
* @author: GuoTong
* @date: 2023-07-14 22:46:01
* @return:void
*/
@GetMapping(value = {"/ShutDown", "/shutdown"})
public void shutdownFunction() {
log.info("Application Context Doing ShutDown .......");
((ConfigurableApplicationContext) context).close();
} /**
* Description: 获取进程号
*
* @author: GuoTong
* @date: 2023-07-14 22:46:01
* @return:void
*/
@GetMapping(value = {"/queryPid", "/getPid"})
public Resp queryApplicationPid() {
String fileInputStreamPid = getPidFunction();
return Resp.Ok(fileInputStreamPid);
} /**
* Description: 强行终止进程
*
* @author: GuoTong
* @date: 2023-07-14 22:46:01
* @return:void
*/
@GetMapping(value = {"/kill", "/stop", "/termination"})
public void termination() {
// 获取上下文环境信息{application.yml}
String pid = getPidFunction();
String endingApplicationShell = "kill -9 " + pid;
// java主要通过Runtime和Process执行Linux命令, Process是Runtime.exec返回值,可以用来对执行过程进行后续操作(获取结果,发送命令,等待结果)。
try {
log.info("强行终止进程 ,{}", endingApplicationShell);
Runtime.getRuntime().exec(endingApplicationShell);
} catch (IOException e) {
log.error("强行终止进程失败", e);
}
} /**
* Description: 容器销毁前调用
*
* @author: GuoTong
* @date: 2023-07-14 22:42:19
* @return:
*/
@PreDestroy
public void PreDestroy() {
log.info("springBoot项目已经优雅关闭 .......");
} /**
* Description:Springboot自身提供的优雅停机
*
* @param applicationContext
* @author: GuoTong
* @date: 2023-07-14 22:42:08
* @return:
*/
public void exitApplication(ApplicationContext applicationContext) {
// SpringApplication.exit()方法也可以安全的退出程序
int exit = SpringApplication.exit(applicationContext, (ExitCodeGenerator) () -> 0);
//同时会返回一个退出码,这个退出码可以传递给所有的context最后通过调用System.exit()可以将这个错误码也传给JVM。
System.exit(exit);
} /**
* Description: 获取上下文环境信息{application.yml}PID
*
* @author: GuoTong
* @date: 2023-07-15 00:15:14
* @return:java.lang.String
*/
private String getPidFunction() {
Environment environment = context.getEnvironment();
String pidFileName = environment.getProperty("spring.pid.file");
if (StringUtils.isEmpty(pidFileName)) {
pidFileName = "application.pid";
}
String fileInputStreamPid = null;
try {
fileInputStreamPid = FileUtils.readFileToString(new File(pidFileName), CharsetUtil.UTF_8);
} catch (IOException e) {
log.info("获取Pid失败", e);
}
return fileInputStreamPid;
} }

SpringBoot项目优雅停机+Pid暴力停机的更多相关文章

  1. 「SpringBoot」如何优雅地管理SpringBoot项目

    本文主要讲述一下如何优雅地管理SpringBoot项目. 背景 课堂上,当小明形如流水地回答完沐芳老师提出来的问题时,却被至今没有对象的胖虎无情嘲讽了? 沐芳老师:小明,你平时是如何启动.停止你的Sp ...

  2. SpringBoot项目整合Retrofit最佳实践,这才是最优雅的HTTP客户端工具!

    大家都知道okhttp是一款由square公司开源的java版本http客户端工具.实际上,square公司还开源了基于okhttp进一步封装的retrofit工具,用来支持通过接口的方式发起http ...

  3. SpringBoot从入门到精通一(idea优雅搭建SpringBoot项目)

    前言 在没有SpringBoot之前,我们搭建的是SSM(SpingMVC+Spring+Mybatis)项目,在搭建SSM项目的时候,我们要经过一系列的繁琐配置,例如:application,web ...

  4. SpringBoot如何优雅关闭(SpringBoot2.3&Spring Boot2.2)

    SpringBoot如何优雅关闭(SpringBoot2.3&Spring Boot2.2) 优雅停止&暴力停止 暴力停止:像日常开发过程中,测试区或者本地开发时,我们并不会考虑项目关 ...

  5. SpringBoot如何优雅的使用RocketMQ

    目录 SpringBoot如何优雅的使用RocketMQ SpringBoot如何优雅的使用RocketMQ MQ,是一种跨进程的通信机制,用于上下游传递消息.在传统的互联网架构中通常使用MQ来对上下 ...

  6. 补习系列(1)-springboot项目基础搭建课

    目录 前言 一.基础结构 二.添加代码 三.应用配置 四.日志配置 五.打包部署 小结 前言 springboot 最近火的不行,目前几乎已经是 spring 家族最耀眼的项目了.抛开微服务.技术社区 ...

  7. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  8. Eclipse 中构建 Maven 项目的完整过程 - SpringBoot 项目

    进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件) 一.Maven项目的新建 1.鼠标右键---->New--- ...

  9. linux下后台启动springboot项目

    linux下后台启动springboot项目 我们知道启动springboot的项目有三种方式: 运行主方法启动 使用命令 mvn spring-boot:run”在命令行启动该应用 运行“mvn p ...

  10. Jenkins部署码云SpringBoot项目

    本文介绍jenkins如何从gitee上clone项目,然后使用maven打包并后台启动. 1.Jenkins介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续 ...

随机推荐

  1. Helm实战案例一:在Kubernetes上使用Helm搭建Prometheus Operator监控

    目录 一.系统环境 二.前言 三.Prometheus Operator简介 四.helm安装prometheus-operator 五.配置prometheus-operator 5.1 修改gra ...

  2. 14. SpringMVC执行流程

    14.1.SpringMVC 常用组件 DispatcherServlet:前端控制器,不需要工程师开发,由框架提供 作用:统一处理请求和响应,整个流程控制的中心,由它调用其它组件处理用户的请求 Ha ...

  3. 浅谈OpenCV的多对象匹配图像的实现,以及如何匹配透明控件,不规则图像

    浅谈OpenCV的多对象匹配透明图像的实现,以及如何匹配半透明控件 引子 OpenCV提供的templateMatch只负责将(相关性等)计算出来,并不会直接提供目标的对应坐标,一般来说我们直接遍历最 ...

  4. Python 一大坑,配置文件中字典引用问题(拷贝)。

    大坑 +1 python 配置文件中字典引用问题 最近在开发系统时发现一个传奇的BUG, 用户未登录就可进入系统内,而且含有真实身份信息. 此问题困扰多时,反复debug.由于找不到问题原因,复现具有 ...

  5. 华为云GaussDB亮相2023可信数据库发展大会,荣获三项评测证书!

    摘要:2023可信数据库发展大会上,华为云数据库服务产品部总经理苏光牛围绕华为云GaussDB的产品能力和实践进行了分享 本文分享自华为云社区<华为云GaussDB亮相2023可信数据库发展大会 ...

  6. 行行AI人才直播第7期:奇计AI创始人左晟《AI时代的商业挑战和机遇》

    行行AI人才是博客园和顺顺智慧共同运营的AI行业人才全生命周期服务平台,是园子商业化努力的一个重要方向. 行行AI人才直播希望以直播的方式让大家更多了解AI行业的现状与未来可能的发展方向. 随着人工智 ...

  7. Unity的IPreprocessBuild:深入解析与实用案例

    Unity IPreprocessBuild Unity IPreprocessBuild是Unity引擎中的一个非常有用的功能,它可以让开发者在构建项目时自动执行一些操作.这个功能可以帮助开发者提高 ...

  8. Visual Studio Code调试和发布ASP.NET Core Web应用

    前言 上一篇文章主要讲了Visual Studio Code安装C#开发工具包并编写ASP.NET Core Web应用有兴趣的同学可以去看看,今天咱们主要是要讲讲如何在VS Code中调试和发布AS ...

  9. langchain:Prompt在手,天下我有

    目录 简介 好的prompt 什么是prompt template 在langchain中创建prompt template Chat特有的prompt template 总结 简介 prompts是 ...

  10. Mysql基础5-用户及权限管理

    一.介绍 DCL:Data Control Language(数据控制语言),用来管理数据库用户,控制数据库的访问,权限. 二.用户管理 1.查询用户 语法: 1.use mysql; 2.selec ...