背景:如下脚本实现当微服务重启后,检查微服务的启动端口正常,可通过轮询的方式来实现
所需要用到配置文件config.properties信息如下:
onlineService:8001
algorthService:8002
recallService:8003
sortService:8004
实现方案:定义一个flag标记与重试的次数,使用do-while循环,当检查命令执行成功、或者达到重试最大次数时终止,
代码如下:
if [ "X${whoami}" != "Xbk" ];then
{
echo "current user i not bk"
exit 1
}
fi
#修改配置
for service in `cat config.properties|awk -F ':' '{print $1}'`;
do
sed -i "s#^ name:{{getv.*#name:test#g" /opt/easyconf/etc/bk/$service/config.yaml
sed -i "s#^ regoin:{{getv.*#regoin:delivery#g" /opt/easyconf/etc/bk/$service/config.yaml
sed -i "s#^ availableZone:{{getv.*#availableZone:online#g" /opt/easyconf/etc/bk/$service/config.yaml
echo "$service的easyconf下microservice文件修改成功"
done
echo "等待60s,easyconf同步时间"
#重启
for service in in `cat config.properties|awk -F ':' '{print $1}'`;
do
#echo /opt/bk/$service/browser-$service/bin
sh /opt/bk/$service/$service/bin/start.sh > /dev/null 2>&1
echo "${service}服务开始重启"
done
#循环检查5次
#循环检查5次
echo "等待60s,验证服务状态,最多重试间隔10s*5次"
sleep 60
count = 0 #记录重试次数
flag = 0 #重试标识,flag=0表示任务正常,flag=1表示需要进行重试
for i in `cat config.properties|awf -F ':' '{print $1}'`;done
i1 = `echo "$i"|awk -F ':' '{print $1}'`
i2 = `echo "$i"|awk -F ':' '{print $2}'`
while [ 0 -eq 0]
do
echo "....................job begin check-${i1}服务....................."
#.....添加要执行的内容,flag的值在这个逻辑中更改为1,或者不变..........
netstat -ntpl |grep ${i2} > /dev/null 2>&1
flag = $?
#检查和重试过程
if [ $flag -eq 0 ];then#执行成功,不重试
echo "....................job complete(${i1}服务重启成功................."
break;
else #执行失败,重试
count = $[${count}+1]
if [ ${count} -eq 5];then #执行重试次数,重试超过5次即失败
echo 'timeout,exit.'
break
fi
echo "....................retry ${count} in 10 seconds................."
sleep 10
fi
done
done
echo "执行成功,微服务均配置与重启成功"