前期大家分库分表相关的springboot工程,并通过打包脚本打成zip包的形式。如果放在linux环境上运行,还需要相关的启动脚本 前文文档如下:
整体思路
基于shell脚本开发3个脚本,分别是
- jvm启动参数相关的setjvment.sh,主要是jvm启动相关参数配置
- start.sh,主要是利用java命令启动进程,包含classpath和执行结果日志保存相关设置
- stop.sh,主要是停止java进程,包括优雅停止和强制停止
如果linux没有安装jdk,可使用如下命令安装jdk:
shell
sudo apt update
sudo apt install openjdk-8-jdk
然后在启动的文件.bash_profile中设置环境变量JAVA_HOME,
shell
#!/bin/sh
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
相关脚本
setjvm
bash
#!/bin/bash
# 设置Java堆栈大小
JAVA_OPTS="$JAVA_OPTS -Xmn512m -Xms512m -Xmx1024m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m"
JAVA_OPTS="$JAVA_OPTS -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=8 -XX:-UseAdaptiveSizePolicy"
# 垃圾收集器设置
JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=60"
JAVA_OPTS="$JAVA_OPTS -XX:+CMSClassUnloadingEnabled"
# 打印GC日志
JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCApplicationStoppedTime -XX:+UseGCLogFileRotation"
JAVA_OPTS="$JAVA_OPTS -XX:+PrintHeapAtGC -Xloggc:$work_home/logs/gc.log -XX:GCLogFileSize=1m -XX:NumberOfGCLogFiles=10"
# 打印DUMP日志
JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$work_home/logs"
# 其他设置
JAVA_OPTS="$JAVA_OPTS -XX:+DisableExplicitGC -XX:+UseFastAccessorMethods -XX:-UseBiasedLocking"
# 指定随机数生成器的种子源
JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/urandom"
# 设置java.ext.dirs
JAVA_OPTS="$JAVA_OPTS -Djava.ext.dirs=$JAVA_HOME/jre/lib/ext:$work_home/lib"
# 其他环境变量或配置设置
# export OTHER_ENV_VARIABLE=example_value
start
bash
#!/bin/bash
CUR="$0"
while [ -h "$CUR" ]; do
ls=$(ls -ld "$CUR")
link=$(expr "$ls" : '.-> (.)$')
if expr "$link" : '/.*' >/dev/null; then
CUR="$link"
else
CUR=$(dirname "$CUR")/"$link"
fi
done
CUR_DIR=$(dirname "$CUR")
work_home=$(
cd "$CUR_DIR/.." >/dev/null
pwd
)
if [ -z $JAVA_HOME ]; then
echo "plz set JAVA_HOME env."
exit 1
fi
if [ -r "$work_home/bin/setenv.sh" ]; then
. "$work_home/bin/setjvmenv.sh"
fi
CLASSPATH=$CLASSPATH:$work_home/lib/conf
LOG_DIR="$work_home/logs"
if [ ! -d $LOG_DIR ]; then
mkdir "$LOG_DIR"
fi
main_class="com.toby.dynamic.data.source.start.PointApplication"
count=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|wc -l`
if [ $count != 0 ]; then
app_id=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|awk '{print $2}'`
echo "already running with pid $app_id, start aborted."
else
echo "use java_home $JAVA_HOME"
echo "use work_home $work_home"
exec $JAVA_HOME/bin/java $JAVA_OPTS -cp $CLASSPATH \
-Dwork.home=$work_home -Djava.io.tmpdir=$work_home \
$main_class >$LOG_DIR/server.log 2>&1 &
echo "server started."
fi
stop
bash
#!/bin/bash
PRG="$0"
while [ -h "$PRG" ]; do
ls=$(ls -ld "$PRG")
link=$(expr "$ls" : '.-> (.)$')
if expr "$link" : '/.*' >/dev/null; then
PRG="$link"
else
PRG=$(dirname "$PRG")/"$link"
fi
done
PRGDIR=$(dirname "$PRG")
work_home=$(
cd "$PRGDIR/.." >/dev/null
pwd
)
main_class="com.toby.dynamic.data.source.start.PointApplication"
count=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|wc -l`
echo " stop server start."
if [ $count == 0 ]; then
echo "already stop,return."
exit 0
fi
app_id=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|awk '{print $2}'`
# 请求优雅退出
kill -15 $app_id
SLEEP=5
STOPPED=0
while [ $SLEEP -ge 0 ]; do
count=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|wc -l`
if [ $count == 0 ]; then
STOPPED=1
echo " server stopped."
break;
fi
if [ $SLEEP -gt 0 ]; then
sleep 1
fi
if [ $SLEEP -eq 0 ]; then
echo "server did not stopped in time."
fi
SLEEP=`expr $SLEEP - 1 `
done
if [ $STOPPED -eq 0 ]; then
echo "server killed fore"
SLEEP=5
kill -15 $app_id
while [ $SLEEP -ge 0 ]; do
count=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|wc -l`
if [ $count == 0 ]; then
STOPPED=1
echo " server has been killed."
break;
fi
if [ $SLEEP -gt 0 ]; then
sleep 1
fi
if [ $SLEEP -eq 0 ]; then
echo "server did not stopped in time."
fi
SLEEP=`expr $SLEEP - 1 `
done
if [ $STOPPED -eq 0 ]; then
echo "server has not been killed completely yet."
fi
fi