shell脚本安装rocketmq主从样例教程
自定义参数
ROCKETMQ_NAME= #服务全名
ROCKETMQ_PATH= #安装路径
TEMP_PATH= #临时目录
USER_NAME= #启动用户
部署参数检查
#!/bin/bash
# 检查参数是否设置
echo "=====检查参数是否设置====="
if [ ! $ROCKETMQ_PATH ];then
echo "=====$ROCKETMQ_PATH 此参数未设置,请检查!====="
exit 1
fi
if [ ! $ROCKETMQ_NAME ];then
echo "=====$ROCKETMQ_NAME 此参数未设置,请检查!====="
exit 1
fi
isServerExist=$(ps -ef | grep $ROCKETMQ_NAME | grep -v grep | awk '{print $2}' | wc -l)
# 检查服务是否已经安装
echo "=====检查服务是否已经安装====="
if [ "${isServerExist}" != "0" ]; then
echo "$ROCKETMQ_NAME 服务已启动,请先卸载"
exit 1
else
echo "$ROCKETMQ_NAME 服务未启动,符合预期!"
fi
# 检查目标目录是否存在
echo "=====检查目标目录是否存在====="
if [ ! -d $ROCKETMQ_PATH ];then
echo "=====$ROCKETMQ_PATH 目录不存在,创建目录====="
mkdir -p $ROCKETMQ_PATH
fi
echo "=====部署前检查通过====="
上传并解压安装包
#!/bin/bash
pwd
# 解压安装包到指定目录
echo "=====解压安装包到指定目录====="
unzip -o ${ROCKETMQ_NAME}.zip -d $ROCKETMQ_PATH
echo "=====查看解压后的内容====="
ls $ROCKETMQ_PATH/$ROCKETMQ_NAME
主从设置-a主b从+启停脚本
#!/bin/bash
#主机A上启动a主,b从
echo "=====开始设置启动a主b从======"
echo "#!/bin/bash
cd $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin
chmod +x $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin/*
nohup sh mqnamesrv >nohup.out 2>&1 &
nohup sh mqbroker -c $ROCKETMQ_PATH/$ROCKETMQ_NAME/conf/2m-2s-sync/broker-a.properties >nohup.out 2>&1 &
sh mqbroker -c $ROCKETMQ_PATH/$ROCKETMQ_NAME/conf/2m-2s-sync/broker-b-s.properties >nohup.out 2>&1
" > $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin/rocketmq.sh
chmod +x $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin/rocketmq.sh
chown -R ${USER_NAME}:${USER_NAME} $ROCKETMQ_PATH/$ROCKETMQ_NAME
echo "=====开始设置启动脚本====="
# 设置启动脚本
cat > /usr/lib/systemd/system/rocketmq.service <<-EOF
[Unit]
Description=rocketmq
[Service]
Type=simple
User=${USER_NAME}
Environment=JAVA_HOME=/usr/local/jdk
EnvironmentFile=-$ROCKETMQ_PATH/$ROCKETMQ_NAME/bin
ExecStart=/usr/bin/sh $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin/rocketmq.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
echo "=====启动并设置开机自启====="
systemctl daemon-reload
systemctl start rocketmq.service
systemctl enable rocketmq.service
echo "=====启停方式:systemctl start|stop|restart rocketmq====="
主从设置-b主a从+启停脚本
#!/bin/bash
#主机B上启动b主,a从
echo "=====开始设置b主a从====="
echo "#!/bin/bash
cd $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin
chmod +x $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin/*
nohup sh mqnamesrv >nohup.out 2>&1 &
nohup sh mqbroker -c $ROCKETMQ_PATH/$ROCKETMQ_NAME/conf/2m-2s-sync/broker-b.properties >nohup.out 2>&1 &
sh mqbroker -c $ROCKETMQ_PATH/$ROCKETMQ_NAME//conf/2m-2s-sync/broker-a-s.properties >nohup.out 2>&1
" > $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin/rocketmq.sh
chmod +x $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin/rocketmq.sh
chown -R ${USER_NAME}:${USER_NAME} $ROCKETMQ_PATH/$ROCKETMQ_NAME
echo "=====开始设置启动脚本====="
# 设置启动脚本
cat > /usr/lib/systemd/system/rocketmq.service <<-EOF
[Unit]
Description=rocketmq
[Service]
Type=simple
User=${USER_NAME}
Environment=JAVA_HOME=/usr/local/jdk
EnvironmentFile=-$ROCKETMQ_PATH/$ROCKETMQ_NAME/bin
ExecStart=/usr/bin/sh $ROCKETMQ_PATH/$ROCKETMQ_NAME/bin/rocketmq.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
echo "=====启动并设置开机自启====="
systemctl daemon-reload
systemctl start rocketmq.service
systemctl enable rocketmq.service
echo "=====启停方式:systemctl start|stop|restart rocketmq====="
验证服务
#!/bin/bash
#按照进程判断,默认启动时间2分钟,判断两次,按需修改循环次数
echo "=====开始判断服务是否已启动成功====="
for((i=1;i<=12;i++));
do
sleep 10
ISSTART=$(ps -ef | grep $ROCKETMQ_NAME | grep -v grep | awk '{print $2}' | wc -l)
# rocketmq正常启动成功应该有8个进程,若同时安装了rocketmq-console,则有9个进程,故判断条件为进程数大于等于8
if [ "$ISSTART" -ge "8" ]; then
echo "发现服务进程,为确保服务真正启动成功,等待15秒后进行第二次判断!"
sleep 15
ISSTART=$(ps -ef | grep $ROCKETMQ_NAME | grep -v grep | awk '{print $2}' | wc -l)
if [ "$ISSTART" -ge "8" ]; then
echo "等待15秒后端口仍存活,服务第二次判断通过,服务启动成功!"
exit 0
else
echo "等待15秒后端口未找到,服务第二次判断失败,服务启动失败!"
exit 1
fi
else
echo "服务启动中,请等待!"
fi
done
echo "等待2分钟后未发现服务进程,服务启动失败!"
exit 1
清理临时目录安装包
rm -rf ${TEMP_PATH}
mkdir -p ${TEMP_PATH}