集成actuator实现优雅关闭应用
优雅停机主要应用在版本更新的时候,为了等待正在工作的线程全部执行完毕,然后再停止。我们可以使用SpringBoot提供的Actuator
1、pom.xml中引入actuator依赖
// gradlecompile "org.springframework.boot:spring-boot-starter-actuator:$rootProject.ext.springBoot"
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
2、配置文件中endpoint开启shutdown
# 优雅关闭服务management.endpoint.shutdown.enabled=truemanagement.endpoints.web.exposure.include=shutdownmanagement.endpoints.web.base-path=/monitor# 自定义管理端端口management.server.port=12581# 指定本机地址作为管理端地址,不允许远程连接,保证安全management.server.address=127.0.0.1
management:server:port: 12581address: 127.0.0.1endpoint:shutdown:enabled: trueendpoints:web:exposure:include: "shutdown"base-path: /monitor
3、Post请求测试验证优雅停机 curl -X POST http://localhost:8080/monitor/shutdown
Spring Boot 之 actuator 配置不当的漏洞利用
https://www.freebuf.com/news/193509.html
实现 Spring Boot 应用的优雅关机
限制运行内存
nohup java -Xmx256m -Xms256m -jar emergency_management.jar > log.out 2>&1 &
“2>&1” 表示 错误和输出都传到 log.out 文件中
启动和关闭脚本
sed -i ‘s/\r//g’ start.sh
启动脚本, 创建 start.sh
#!/bin/shnohup java -jar 文件.jar >> log.out 2>&1 &cur_dir=$(pwd)echo $!> $cur_dir/obj.pid
关闭脚本,创建 shutdown.sh
#!/bin/shcur_dir=$(pwd)PID=$(cat $cur_dir/obj.pid)kill -9 $PID
actuator 实现优雅关闭应用脚本
#!/bin/shcurl -X POST http://127.0.0.1:12081/monitor/shutdown
自定义 maven 插件
做一个 idea一键部署到服务器的maven插件
https://blog.csdn.net/qq_20907623/article/details/89373540
Maven 自定义插件
指定外部jar位置
gradle 配置
bootJar {archiveClassifier = 'boot'excludes = ["*.jar"]// 指定依赖包的路径,运行时不再需要指定 java.ext.dir 或 loader.path 参数。manifest {attributes "Manifest-Version": 1.0,'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')}}
自动生成 lib 目录 , 需要将 dependencies 里面的 都改成 compile
dependencies {compile "org.springframework.boot:spring-boot-starter-quartz:$rootProject.ext.springBoot"compile "org.springframework.boot:spring-boot-starter-web:$rootProject.ext.springBoot"compile "org.springframework.boot:spring-boot-starter-validation:$rootProject.ext.springBoot"compile "org.springframework.boot:spring-boot-starter-aop:$rootProject.ext.springBoot"compile "org.springframework.boot:spring-boot-starter-data-redis:$rootProject.ext.springBoot"}
// 清除libtask myClearLib(type: Delete) {delete "$buildDir/libs/lib"}// 拷贝libtask myCopyLib(type: Copy) {into "$buildDir/libs/lib"from configurations.runtime}bootJar {//mainClassName = 'com.hn.Application'archiveBaseName = 'youli-shop-api'archiveVersion = '1.0.0'archiveClassifier = 'boot'excludes = ["*.jar"]// lib目录的清除和复制任务dependsOn myClearLibdependsOn myCopyLib// 指定依赖包的路径,运行时不再需要指定 java.ext.dir 或 loader.path 参数。manifest {attributes "Manifest-Version": 1.0,'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')}}
-Djava.ext.dirs=./lib
java -Djava.ext.dirs=./lib -jar youli-shop-api-1.0.jar



tomcat配置内存大小
在文件/bin/catalina.sh的前面,增加如下设置:
JAVA_OPTS='-Xmx256m -Xms256m'
