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. gitlab docker升级报错

    背景 使用docker部署gitlab(9.5.4)后,发现合并代码有问题 日志: 看gitlab官网此问题已修复,由于上传了一批代码,又懒得重建,决定对gitlab升级 docker启动命令: do ...

  2. Java 统计大串中小串出现的次数 举例:在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"中java出现了5次

    代码如下: public static void main(String[] args) { //大串 String max = "woaijavawozhenaijavawozhendea ...

  3. PostgreSQL 12 文档: PostgreSQL 服务端程序

    PostgreSQL 服务器应用   这一部分包含PostgreSQL服务器应用和支持工具的参考信息.这些命令只在数据库服务器所在的主机上运行才有用.其他工具程序在PostgreSQL 客户端应用中列 ...

  4. 从头学Java17-Modules模块

    模块Modules 了解module系统如何塑造 JDK,如何使用,使项目更易于维护. 烧哥注 从头讲JDK17的文章比较少,英文为主,老外虽能讲清原理,但写的比较绕,所以决定翻译一下,也有个别细节完 ...

  5. 基于.Net Core实现的飞书文档一键导出服务(支持多系统)

    feishu-doc-export 一个支持Windows.Mac.Linux系统的飞书文档一键导出服务,仅需一行命令即可将飞书知识库的全部文档同步到本地电脑.导出速度嘎嘎快,实测700多个文档导出只 ...

  6. CSS_相关问题及解决_持续更新

    css_margin塌陷问题 问题描述 <div class="father"> <div class="child1"></di ...

  7. Atcoder ABC244E - King Bombee 题解

    原题: Atcoder ABC244E - King Bombee 题意 给你一张图,从 \(S\) 到 \(T\),经过 \(k\) 条边, 经过 \(X\) 号点偶数次的方案数. 做法 设 \(f ...

  8. Federated Learning001

    联邦学习--笔记001 2022.11.16周三 今天学习了联邦学习的开山之作---Communication-Efficient Learning of Deep Networks from Dec ...

  9. go项目实现在配置文件实现配置项统一管理

    转载请注明出处: go项目中实现配置项统一管理,实现逻辑:将 配置项整理为一个json的数据结构,并保存到go.conf文件中,然后在go项目启动main方法中加载 go.conf 文件,读取go.c ...

  10. bash: pip3:未找到命令

    输入以下命令: 1 sudo apt-get install python3-pip 参考链接: https://www.cnblogs.com/banshaohuan/p/10963547.html