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. elementUi+table实现表格数据滚动

    elementUi+table实现表格数据滚动 引用vue和elementUI CDN // 引用elementUI CDN <script src="https://unpkg.co ...

  2. CKS 考试题整理 (12)-Trivy扫描镜像安全漏洞

    Task 使用Trivy开源容器扫描器检测namespace kamino中 Pod 使用的具有严重漏洞的镜像. 查找具有High或Critical严重性漏洞的镜像,并删除使用这些镜像的Pod. 注意 ...

  3. 使用 Sa-Token 实现 [记住我] 模式登录、七天免登录

    一.需求分析 如图所示,一般网站的登录界面都会有一个 [记住我] 按钮,当你勾选它登录后,即使你关闭浏览器再次打开网站,也依然会处于登录状态,无须重复验证密码: 本文将详细介绍在 Sa-Token中, ...

  4. springboot使用Websocket写一个聊天室

    1 <!--websocket 依赖--> 2 <dependency> 3 <groupId>org.springframework.boot</group ...

  5. APP中Web容器的核心实现

      现在的业务型APP中,采用纯原生开发策略的已经很少了,大部分都使用的混合开发.如原生,H5,ReactNative,Flutter,Weex它们之间任意的组合就构成了混合开发. 其中原生+H5是出 ...

  6. 每日一题 力扣 445 https://leetcode.cn/problems/add-two-numbers-ii/

    可以直接用栈去做就行,逆序想到栈的做法 然后算完一个就直接赋值给答案数组  我用的是常见 public ListNode addTwoNumbers(ListNode l1, ListNode l2) ...

  7. 2023-07-08:RabbitMQ如何做到消息不丢失?

    2023-07-08:RabbitMQ如何做到消息不丢失? 答案2023-07-08: 1.持久化 发送消息时设置delivery_mode属性为2,使消息被持久化保存到磁盘,即使RabbitMQ服务 ...

  8. [GIT]解决:failed to push some refs to ...(过程重现)

    本问题有很多种情况,解决方法也很多,本文只针对笔者本人的自身诉求和情况,选择了一种适合我的解决方法.仅供参考. 1 问题描述 johnnyzen@XXDSSS MINGW64 /e/source_co ...

  9. 模型部署 — PaddleNLP 基于 Paddle Serving 快速使用(服务化部署 - Docker)— 图像识别 + 信息抽取(UIE-X)

    目录 流程 版本 安装 Docker 安装 PaddleNLP 安装 环境准备 模型准备 压缩模型 下载模型 模型部署 环境配置 启动服务 测试 -- 暂时还没通过 重启 图像识别 + 信息抽取(UI ...

  10. python移动文件

    #移动文件(目录) shutil.move("oldpos","newpos") shutil.move("D:/知乎日报/latest/一张优惠券, ...