1.遇到的問題
在分布式項(xiàng)目部署的過程中,經(jīng)常要求服務(wù)器重啟之后,應(yīng)用(包括數(shù)據(jù)庫)能夠自動恢復(fù)使用.雖然使用docker update --restart=always containerid 能夠讓容器自動隨docker啟動,但是并不能保證是在數(shù)據(jù)庫啟動之后啟動,如果數(shù)據(jù)庫未啟動,那么將導(dǎo)致應(yīng)用啟動失敗;網(wǎng)上還有一種解決方法是通過docker-compose容器編排來控制啟動順序,這個博主研究的比較少.
2.解決思路
使用Shell腳本來控制,思路大致如下
- 探測數(shù)據(jù)庫端口來檢驗(yàn)數(shù)據(jù)庫是否啟動成功.
- 數(shù)據(jù)庫啟動成功后,探測配置中心及服務(wù)注冊中心的端口來檢驗(yàn)其是否啟動成功.
- 當(dāng)數(shù)據(jù)庫及配置中心都啟動之后,再啟動其他微服務(wù)應(yīng)用.
3.端口探測
端口探測使用的命令是
nc -w 1 host port </dev/null && echo "200"
host:目標(biāo)主機(jī)的ip
port:服務(wù)監(jiān)聽的端口
如果服務(wù)啟動了 這條命令會返回 200,未啟動則返回空.
4.Shell腳本
直接貼代碼了,使用的配置中心是nacos
#!/bin/bash
#chkconfig: 2345 80 90
#description:autoStartMaintenanceService.sh
#
#前提:
#1.docker必須能開機(jī)自啟
#2.docker能夠正常啟動運(yùn)維服務(wù)
#3.此腳本須運(yùn)行微服務(wù)所在的機(jī)器上
#
##需要修改的配置-----開始
##數(shù)據(jù)庫所在的機(jī)器IP
DATABASE_HOST=192.169.1.52
##數(shù)據(jù)庫監(jiān)聽的端口
DATABASE_PORT=3306
##微服務(wù)所在機(jī)器IP
LOCAL_HOST=192.169.1.46
##微服務(wù)訪問端口
Maintenance_Port=8180
##NACOS所在機(jī)器的ip
NACOS_HOST=192.169.1.82
##NACOS的監(jiān)聽端口
NACOS_PORT=8848
##微服務(wù)容器名稱(NAMES列)
Maintenance_Container_Name="umc-maintenance"
##該腳本生成的日志路徑
Log_Path=/home/test/log
##需要修改的配置-----結(jié)束
##
##循環(huán)延時時間(s)秒
LOOP_TIME=5
at_time=""
at_date=""
getAtTime() {
at_time="$(date +%Y-%m-%d-%H:%M:%S) --- "
at_date=$(date +%Y-%m-%d)
}
autoStartWebService() {
##如果日志路徑不存在則創(chuàng)建
if [ ! -d "$Log_Path" ]; then
mkdir -p $Log_Path
fi
while true; do
##判斷數(shù)據(jù)庫是否啟動
req_message=$(nc -w 1 ${DATABASE_HOST} ${DATABASE_PORT} </dev/null && echo "200")
if [ -n "$req_message" ]; then
getAtTime
echo "$at_time Database is running" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
waitNacosStarting
else
getAtTime
echo "$at_time Database is not running and please wait for Database starting" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
sleep $LOOP_TIME
fi
done
}
##判斷Nacos是否啟動
waitNacosStarting() {
req_message=$(nc -w 1 ${NACOS_HOST} ${NACOS_PORT} </dev/null && echo "200")
if test $((req_message)) -eq 200; then
getAtTime
echo "$at_time Nacos is running" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
startMaintenanceService
sleep $LOOP_TIME
else
getAtTime
echo "$at_time Nacos is not running and please wait for nacos starting" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
sleep $LOOP_TIME
fi
}
##啟動微服務(wù)
startMaintenanceService() {
req_message=$(nc -w 1 ${LOCAL_HOST} ${Maintenance_Port} </dev/null && echo "200")
if test $((req_message)) -eq 200; then
getAtTime
echo "$at_time Maintenance service is running" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
else
container_id=$(docker ps -a | grep $Maintenance_Container_Name | grep -v grep | awk '{print $1}')
getAtTime
echo "$at_time Maintenance service container id is ${container_id}" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
docker start ${container_id}
fi
}
autoStartWebService
5.Shell輸入輸出重定向
寫這個腳本的時候,也讓博主對Shell輸入輸出重定向更加熟悉
一般情況下,每個 Unix/Linux 命令運(yùn)行時都會打開三個文件:
- 標(biāo)準(zhǔn)輸入文件(stdin):stdin的文件描述符為0,Unix程序默認(rèn)從stdin讀取數(shù)據(jù)。
- 標(biāo)準(zhǔn)輸出文件(stdout):stdout 的文件描述符為1,Unix程序默認(rèn)向stdout輸出數(shù)據(jù)。
- 標(biāo)準(zhǔn)錯誤文件(stderr):stderr的文件描述符為2,Unix程序會向stderr流中寫入錯誤信息。
命令 |
說明 |
command > file |
將輸出重定向到 file且會覆蓋file |
command < file |
將輸入重定向到 file |
command >> file |
將輸出以追加的方式重定向到file |
command 2> file |
將錯誤輸出到file且會覆蓋file |
command 2>> file |
將錯誤以追加的方式重定向到file |
<< tag |
將開始標(biāo)記 tag 和結(jié)束標(biāo)記 tag 之間的內(nèi)容作為輸入 |
如果希望將 stdout 和 stderr 合并后重定向到 file(即將正確信息和錯誤信息都輸出到file),可以這樣寫:
command > file 2>&1
或者
command >> file 2>&1
/dev/null文件
/dev/null是一個特殊的文件,寫入到它的內(nèi)容都會被丟棄;如果嘗試從該文件讀取內(nèi)容,那么什么也讀不到。但是 /dev/null 文件非常有用,將命令的輸出重定向到它,會起到禁止輸出的效果
command > /dev/null 2>&1 可以屏蔽stdout和stderr
參考
菜鳥教程-Shell
|