#!/bin/bash
SERVICE_NAME=$1
#使用方法:deploy.sh servicename start|restart|stop|update|rollback
#参数1: 微服务名称
#参数2: 动作名
case "$1" in
gateway)
PORT=9900
;;
file)
PORT=5064
;;
user)
PORT=5062
;;
micro-service1)
PORT=5030
;;
micro-service2)
PORT=5023
;;
websocket)
PORT=5066
;;
micro-service3)
PORT=5024
;;
system)
PORT=5069
;;
job-scheduler)
JAVA_OPTS2="--spring.profiles.active=${PROFILE}"
;;
log-server)
JAVA_OPTS2="--spring.profiles.active=${PROFILE}"
;;
micro-service4)
PORT=5025
;;
micro-service5)
PORT=5026
;;
-h)
echo "Usage:$0 service_name start|stop|restart|update|rollback"
exit 0
;;
*)
echo "Please input correct service_name: dboss-gateway|dboss-file|dboss-websocket|dboss-system|dboss-job-scheduler|dboss-log-serve|dboss-feed|user|market|coin-exchange|coin-otc|wallet"
echo "Usage:$0 service_name start|stop|restart|update|rollback"
exit 1
;;
esac
#PORT=$2
APP_VERSION=1.0-SNAPSHOT #微服务版本
PROFILE=prod #prod为生产环境
JAR_FILENAME=${SERVICE_NAME}-${APP_VERSION}.jar #.jar文件名
TAG=$(date +%Y%m%d%H%M%S) #TAG 当前时间戳
CURRENT_VERSION=${TAG} #当前版本号=当前时间戳
BASE_DIR=/opt/service
if [ ! -d ${BASE_DIR} ]; then
mkdir -p ${BASE_DIR}
fi
SERVICE_SOURCE_DIR=${BASE_DIR}/source #jenkins自动部署上传的.jar资源目录
SERVICE_SOURCE_FILE=${SERVICE_SOURCE_DIR}/${JAR_FILENAME} #.jar资源文件
if [ ! -d ${SERVICE_SOURCE_DIR} ]; then
mkdir -p ${SERVICE_SOURCE_DIR}
fi
WORK_BASE_DIR=${BASE_DIR}/work
if [ ! -d ${WORK_BASE_DIR} ]; then
mkdir -p ${WORK_BASE_DIR}
fi
SERVICE_WORK_DIR=${WORK_BASE_DIR}/${SERVICE_NAME} #工作目录(当前版本真实目录软链接)
SERVICE_REAL_DIR=${BASE_DIR}/resouce/${SERVICE_NAME}/${CURRENT_VERSION} #当前版本真实目录
LOG_DIR=${BASE_DIR}/log #日志目录
LOG_FILE=${LOG_DIR}/${SERVICE_NAME}.log #日志文件
if [ ! -d ${LOG_DIR} ]; then
mkdir -p ${LOG_DIR}
fi
PID_FILE_DIR=${BASE_DIR}/pid #pid目录
PID_FILE=${PID_FILE_DIR}/${SERVICE_NAME}.pid #保存pid的文件名:服务名.pid
if [ ! -d ${PID_FILE_DIR} ]; then
mkdir -p ${PID_FILE_DIR}
fi
VERSION_DIR=${BASE_DIR}/version
VERSION_FILE=${VERSION_DIR}/${SERVICE_NAME}.txt #保存版本号的文件名:服务名.txt
if [ ! -d ${VERSION_DIR} ]; then
mkdir -p ${VERSION_DIR}
fi
JAVA_OPTS1="-Xms256m -Xmx2048m"
JAVA_OPTS2="--spring.profiles.active=${PROFILE} --server.port=${PORT}"
# dockerfile configuration
# CONTAINER_NAME=docker-${SERVICE_NAME}
# REGISTRY=hub.pxe.party
# 下个版本更新docker部署
isrun()
{
if [ -f ${PID_FILE} ]; then
PID=$(cat ${PID_FILE})
if [ ! -z ${PID} ]; then
ISRUN=$(ps -p ${PID} | wc -l) # ISRUN > 1,表示程序运行
else
#echo "${SERVICE_NAME} pid: ${PID} is not running!"
ISRUN=0
fi
else
#echo "${SERVICE_NAME} ${PID_FILE} is not exist!"
ISRUN=-1
fi
}
update()
{
if [ ! -d ${SERVICE_REAL_DIR} ]; then
mkdir -p ${SERVICE_REAL_DIR}
fi
if [ ! -d ${WORK_BASE_DIR} ]; then
mkdir -p ${WORK_BASE_DIR}
fi
SOURCE_FILE_MD5=$(md5sum ${SERVICE_SOURCE_FILE} | awk '{print $1}')
SERVICE_WORK_FILE_MD5=$(md5sum ${SERVICE_WORK_DIR}/${JAR_FILENAME} | awk '{print $1}')
echo ${SOURCE_FILE_MD5} ${SERVICE_WORK_FILE_MD5}
if [ ${SOURCE_FILE_MD5} == ${SERVICE_WORK_FILE_MD5} ]; then
echo "It is the latest version, no need to upgrade."
exit 200
else
cp -a ${SERVICE_SOURCE_FILE} ${SERVICE_REAL_DIR}/
update-alternatives --install ${SERVICE_WORK_DIR} ${SERVICE_NAME} ${SERVICE_REAL_DIR} ${CURRENT_VERSION} #写入当前版本号到版本库
update-alternatives --auto ${SERVICE_NAME} #更新当前版本为最新版
echo ${CURRENT_VERSION} >> ${VERSION_FILE} #写入当前版本号到版本文件
fi
}
rollback()
{
if [ -s ${VERSION_FILE} ]; then
ISLATEST=$(wc -l ${VERSION_FILE} | awk '{print $1}')
if [ ${ISLATEST} -eq 1 ]; then
CURRENT_VERSION=$(cat ${VERSION_FILE})
echo "CURRENT_VERSION:${CURRENT_VERSION} is the latest version!"
exit 3
else
CURRENT_VERSION=$(tail -1 ${VERSION_FILE})
PREVIOUS_VERSION=$(tail -2 ${VERSION_FILE} | head -1)
SERVICE_REAL_DIR=${BASE_DIR}/resouce/${SERVICE_NAME}/${CURRENT_VERSION}
fi
fi
update-alternatives --remove ${SERVICE_NAME} ${SERVICE_REAL_DIR} #删除当前版本号
update-alternatives --auto ${SERVICE_NAME} #回退到上一个版本
ISEXIST=$(alternatives --display ${SERVICE_NAME} | grep ${PREVIOUS_VERSION})
if [ ! -z ISEXIST ]; then
sed -i '$d' ${VERSION_FILE} #删除当前版本号
echo "${SERVICE_NAME} version rollback is sucseeded."
echo "The current version is ${PREVIOUS_VERSION}."
else
echo "${SERVICE_NAME} version rollback is failed,becouse version ${PREVIOUS_VERSION} is not exists,please check."
fi
# echo ${CURRENT_VERSION} >> ${VERSION_FILE} #写入当前版本号到版本文件
}
case "$2" in
start)
isrun
if [ ${ISRUN} -eq 0 ]; then
echo "Wanning:${PID_FILE} is exists but ${SERVICE_NAME} pid: ${PID} is not running!"
else
if [ ${ISRUN} -gt 1 ];then
echo "${SERVICE_NAME} pid: ${PID} is running,please check it."\
exit 100
fi
fi
nohup java -jar ${SERVICE_WORK_DIR}/${JAR_FILENAME} ${JAVA_OPTS1} ${JAVA_OPTS2} > ${LOG_FILE} 2>&1 &\
echo $! > ${PID_FILE}
echo "=== start ${SERVICE_NAME} service"
;;
stop)
isrun
if [ ${ISRUN} -le 0 ]; then
echo "Wanning:${SERVICE_NAME} pid file:${PID_FILE} is not exists. or ${SERVICE_NAME} pid:${PID} is not running!"
else
kill ${PID}
echo "=== Stopping ${SERVICE_NAME} service"
echo "=== ${SERVICE_NAME} service pid is:${PID}"
sleep 20
isrun
if [ ${ISRUN} -gt 1 ]; then
echo "=== ${SERVICE_NAME} service pid is:${PID}"
echo "=== Force to stop ${SERVICE_NAME} service"
kill -9 ${PID}
fi
rm -f ${PID_FILE}
fi
;;
restart)
$0 $1 stop
sleep 2
$0 $1 start
;;
update)
update
$0 $1 restart
;;
rollback)
rollback
$0 $1 restart
;;
*)
echo "Usage:$0 service_name start|stop|restart|update|rollback"
exit 2
esac
exit 0
利用alternatives实现版本管理java微服务,部署升级回滚shell脚本
最新推荐文章于 2024-07-16 14:16:43 发布