1. Problems encountered In the process of distributed project deployment, it is often required that the application (including database) can be automatically restored after the server is restarted. Although using 2. SolutionUse Shell script to control, the idea is as follows Detect the database port to verify whether the database is started successfully. After the database is started successfully, detect the ports of the configuration center and the service registration center to verify whether they are started successfully. After the database and the configuration center are started, start other microservice applications. 3. Port detectionThe command used for port detection is host: the IP address of the target host port: the port the service listens on If the service is started, this command will return 200, otherwise it will return nothing. 4. Shell ScriptPaste the code directly, the configuration center used is nacos #!/bin/bash #chkconfig: 2345 80 90 #description:autoStartMaintenanceService.sh # #premise: #1.Docker must be able to start automatically when the computer is turned on#2.Docker can start the operation and maintenance service normally#3.This script must be run on the machine where the microservice is located# ##Configuration that needs to be modified-----Start##IP of the machine where the database is located DATABASE_HOST=192.169.1.52 ##Database listening port DATABASE_PORT=3306 ##IP of the machine where the microservice is located LOCAL_HOST=192.169.1.46 ##Microservice access port Maintenance_Port=8180 ##IP of the machine where NACOS is located NACOS_HOST=192.169.1.82 ##NACOS listening port NACOS_PORT=8848 ##Microservice container name (NAMES column) Maintenance_Container_Name="umc-maintenance" ##The log path generated by the script Log_Path=/home/test/log ##Configuration that needs to be modified-----End## ##Loop delay time (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 the log path does not exist, create it if [ ! -d "$Log_Path" ]; then mkdir -p $Log_Path fi while true; do ##Judge whether the database is started 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 } ##Judge whether Nacos is started 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 } ##Start the microservice 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 input and output redirectionWhen writing this script, the blogger also became more familiar with Shell input and output redirection Generally, each Unix/Linux command opens three files when it is run:
Command Description If you want to merge stdout and stderr and redirect them to a file (that is, output both correct information and error information to a file), you can write: command > file 2>&1 Or command >> file 2>&1 /dev/null file /dev/null is a special file. Anything written to it will be discarded; if you try to read from it, you will get nothing. However, the /dev/null file is very useful. Redirecting the output of a command to it will have the effect of disabling output. refer to Novice Tutorial - Shell This is the end of this article about Shell script controlling the startup order of docker containers. For more relevant Shell script control docker content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to wrap HTML title attribute
>>: Vue routing lazy loading details
1. Review Vue responsive usage Vue responsivenes...
Docker virtualizes a bridge on the host machine. ...
Method 1: Use the target event attribute of the E...
Use the Vue-Cropper component to upload avatars. ...
After the image is built successfully, it can be ...
There are many purposes for exporting MySQL data,...
Statistics of QPS values in the last N seconds ...
Sorting Problem I recently read "45 Lectures...
Windows Server 2012 and Windows Server 2008 diffe...
<br />Original text: http://www.mikkolee.com...
In order to facilitate the storage and access of ...
Rich text editors are often used when doing backg...
W3C recently released two standards, namely "...
Achieve results step 1. Initial index.html To bui...
Preface Samba is a free software that implements ...