SpringBoot 生产环境部署,jar 启动配置 GC 命令

SpringBoot部署时打包成一个jar文件,启动命令如下

java -verbose:gc -Xloggc:/mnt/gc/spring-demo.jar/gc.20210608_220432.log -XX:+UseG1GC -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/mnt/gc/spring-demo.jar/ -jar spring-demo.jar

#-verbose:gc //输出GC情况
#-Xloggc:filepath  //将GC日志输出到指定文件中
#-XX:+PrintGCDetails  //输出详细的GC情况
#-XX:+HeapDumpOnOutOfMemoryError //表示当JVM发生OOM时,自动生成DUMP文件
#-XX:HeapDumpPath=filepath // 生成DUMP文件的路径

完整的启动脚本如下

#!/bin/bash
SpringBoot=$2
heapDumpPath="/mnt/gc/$SpringBoot/"
if [ $1 = "" ]
then
    echo  "not enter start-stop-restart-status"
    exit 1
fi

if [ "$SpringBoot" = "" ]
then
    echo -e "no app name"
    exit 1
fi

function start()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$SpringBoot is running..."
    else
         echo "-xx:HeapDumpPath = $heapDumpPath"
         if [ ! -d "$heapDumpPath" ];then
		mkdir -p "$heapDumpPath"
	 fi
         yearMonthDay=`date "+%Y%m%d_%H%M%S"`
         echo "gc log name ==== $yearMonthDay"
         if [ ! -d "${heapDumpPath}gc.${yearMonthDay}.log" ];then
             touch "${heapDumpPath}gc.${yearMonthDay}.log"
         fi
        echo "Start $SpringBoot success..."
         `ls -t | grep $SpringBoot | tail -n +10 | xargs rm -rf`
         BUILD_ID=dontKillMe  nohup java -verbose:gc -Xloggc:"${heapDumpPath}gc.${yearMonthDay}.log" -XX:+UseG1GC -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintGCDateStamps  -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$heapDumpPath -jar -Dsnow.flake.work.id=1 -Dsnow.flake.datacenter.id=1 $SpringBoot > ./app.log 2>&1 &
    fi
}

function stop()
{
    echo "Stop $SpringBoot"
    boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

    if [ $count != 0 ];then
        kill $boot_id
        count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

        boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
        kill -9 $boot_id
    fi
}

function restart()
{
    stop
    sleep 2
    start
}

function status()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$SpringBoot is running..."
    else
        echo "$SpringBoot is not running..."
    fi
}

case $1 in
    start)
    start
	;;
    stop)
    stop
	;;
    restart)
    restart
	;;
    status)
    status
	;;
    *)

    echo -e "command not exist"
esac

启动服务:
sh script.sh start spring-demo.jar

你可能感兴趣的:(SpringBoot,spring,boot,jar,java)