一、目录结构
》bin
-- health.sh
-- run.sh
-- hosts
-- startup.sh
》dump
》logs
-- *.log
》packages
-- *.jar
文件pid(进程号)
二、设计
重启: ./bin/startup.sh restart
启动: ./bin/startup.sh start
停止: ./bin/startup.sh stop
-
bin目录下,有时候会需要一个hosts文件,用于汇总应用程序需要发布的节点列表。
bin下的文件列表.png
-
dump 目录,主要是存放java在内存溢出的时候,保存当时的内存快照。
-
packages目录,存放jar包
-
logs目录,保存日志文件
三、详细实现
1、health.sh
ip=192.168.xx.xx
port=80
msg=$(curl -s -X GET "http://${ip}:${port}/health")
up=$(echo $msg | grep \"UP\")
down=$(echo $msg | grep \"DOWN\")
if [ -n "$up" -a "$down" = '' ]; then
echo "UP"
else
echo "DOWN"
fi
2、run.sh
ip=$1
cmd=$2
service=order-service
LOCAL_IP=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
# 本地执行
if [ "$ip" = "$LOCAL_IP" ]; then
/opt/xxx/${service}/bin/startup.sh $cmd
else
# ssh远程执行
ssh root@${ip} "/opt/xxx/${service}/bin/startup.sh $cmd"
fi
3、startup.sh
source /etc/profile
today=`date +%Y%m%d`
LOCAL_IP=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
service=order-service
basepath=/opt/xxx/${service}
start() {
nohup java -Dserver.ip=${LOCAL_IP} -Xms64m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${basepath}/dump/ -jar ${basepath}/packages/${service}.jar >> ${basepath}/logs/${service}-$today.log 2>&1 &
# 把进程号写入pid文件里
echo $! > ${basepath}/pid
}
stop() {
kill -9 `cat $basepath/pid`
rm -rf $basepath/pid
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
health)
$basepath/bin/health.sh
;;
*)
echo "Usage: run.sh {start|stop|restart}"
;;
esac
exit 0
网友评论