在分布式项目部署的过程当中,常常要求服务器重启以后,应用(包括数据库)可以自动恢复使用.虽然使用docker update --restart=always containerid
可以让容器自动随docker启动,可是并不能保证是在数据库启动以后启动,若是数据库未启动,那么将致使应用启动失败;网上还有一种解决方法是经过docker-compose容器编排来控制启动顺序,这个博主研究的比较少.html
使用Shell脚原本控制,思路大体以下linux
端口探测使用的命令是docker
nc -w 1 host port </dev/null && echo "200"
shell
host:目标主机的ip数据库
port:服务监听的端口bash
若是服务启动了 这条命令会返回 200,未启动则返回空.服务器
直接贴代码了,使用的配置中心是nacos运维
#!/bin/bash #chkconfig: 2345 80 90 #description:autoStartMaintenanceService.sh # #前提: #1.docker必须能开机自启 #2.docker可以正常启动运维服务 #3.此脚本须运行微服务所在的机器上 # ##须要修改的配置-----开始 ##数据库所在的机器IP DATABASE_HOST=192.169.1.52 ##数据库监听的端口 DATABASE_PORT=3306 ##微服务所在机器IP LOCAL_HOST=192.169.1.46 ##微服务访问端口 Maintenance_Port=8180 ##NACOS所在机器的ip NACOS_HOST=192.169.1.82 ##NACOS的监听端口 NACOS_PORT=8848 ##微服务容器名称(NAMES列) Maintenance_Container_Name="umc-maintenance" ##该脚本生成的日志路径 Log_Path=/home/test/log ##须要修改的配置-----结束 ## ##循环延时时间(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() { ##若是日志路径不存在则建立 if [ ! -d "$Log_Path" ]; then mkdir -p $Log_Path fi while true; do ##判断数据库是否启动 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 } ##启动微服务 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
写这个脚本的时候,也让博主对Shell输入输出重定向更加熟悉分布式
通常状况下,每一个 Unix/Linux 命令运行时都会打开三个文件:微服务
命令 | 说明 |
---|---|
command > file | 将输出重定向到 file且会覆盖file |
command < file | 将输入重定向到 file |
command >> file | 将输出以追加的方式重定向到file |
command 2> file | 将错误输出到file且会覆盖file |
command 2>> file | 将错误以追加的方式重定向到file |
<< tag | 将开始标记 tag 和结束标记 tag 之间的内容做为输入 |
若是但愿将 stdout 和 stderr 合并后重定向到 file(即将正确信息和错误信息都输出到file),能够这样写:
command > file 2>&1 或者 command >> file 2>&1
/dev/null是一个特殊的文件,写入到它的内容都会被丢弃;若是尝试从该文件读取内容,那么什么也读不到。可是 /dev/null 文件很是有用,将命令的输出重定向到它,会起到禁止输出的效果
command > /dev/null 2>&1
能够屏蔽stdout和stderr