Spring Boot :使用shell脚本(.sh)启动多个服务

vhmi4jdf  于 2023-03-23  发布在  Spring
关注(0)|答案(2)|浏览(414)

我喜欢使用Sping Boot (v2.1.1)为后端服务器编写一个shell脚本,以特定顺序启动多个微服务-一些服务依赖于其他服务运行。
什么是“最佳实践”?
当然,我可以像这样运行.jaroriginal post):

#!/bin/bash

java -jar myjar1.jar &
java -jar myjar2.jar &
java -jar myjar3.jar &

但这会同时启动.jar s,afaik。
我如何确保某个服务myjar1.jar正确启动,然后另一个服务myjar2.jar启动。因为每个服务都是SpringBootApplication,所以我假设有一定的可能性这样做?!
我读了this SO solution,但我不想创建任何符号链接,因为我只需要用于开发目的。

dl5txlt9

dl5txlt91#

好吧,它是非常具体的服务,当它开始。在进程级别,只要你执行命令服务正在运行,所以你需要你的服务共享的状态时,它的了。一种方法,我能想到的是在你的脚本启动服务,暴露健康的API,并检查它是否了。如果它是移动到下一个。你需要在你的脚本中使用curl和sleep命令。
但是我想知道你为什么要这样做。特别是对于你的微服务,你的服务不应该相互依赖。它们可能需要一些数据,但是它们应该能够适应服务可能来来去去的事实。你应该有一个非常强的理由去做你正在做的事情,因为在真实的世界的环境中,很难确保秩序得到维护。

pw9qyyiw

pw9qyyiw2#

我有一个shell脚本,用于在多个应用程序中启动微服务(test1,test 2和test 3是不同的应用程序,其中有几个服务)。
示例:-

  • 在“/etc/systemd/system/”位置创建.service(例如:- umm.service)文件
[Unit]
    Description=Umm Service
    After=syslog.target remote-fs.target nss-lookup.target gateway.service

    [Service]
    Type=simple
    User=user
    WorkingDirectory=/home/user/umm.service
    ExecStart=/bin/bash -c "/home/user/java8/bin/java -Xms128m -Xmx256m -jar umm-service.jar"
    KillMode=process
  • 然后复制脚本并运行它
#!/bin/bash
#prerequisites **** install rlwrap package, create .txt file with including service names.
#please note that service names should be in the right order in the particular .txt file and a particular port should be in front of the service name.
#Ex:- If you need to start config service first, discovery service second and gateway service third, you need to add services as below.
#config.service <port>
#discovery.service <port>
#gateway.service <port>
#and so on ..........

echo "$(tput setaf 6)

███    ███ ██   ██ ██████   ██████      ███████   ██ ██████  ██ ██████  ████████ 
████  ████ ██  ██  ██   ██ ██  ████     ██       ██  ██   ██ ██ ██   ██    ██    
██ ████ ██ ██ ██   ██████  ██ ██ ██     ███████ ██   ██████  ██ ██████     ██    
██  ██  ██     ██  ██   ██ ████  ██          ██  ██  ██   ██    ██         ██    
██      ██ ██   ██ ██   ██  ██████      ███████   ██ ██   ██ ██ ██         ██    
                                                                                 
                                                                                 
$(tput sgr0)"

echo "$(tput setaf 2)
Version 3.0
Enter TAB For Auto Completion
Enter exit at every stage for EXIT entire programme
$(tput sgr0)"
echo "$(tput setaf 3)
`date +"%A, %e %B %Y, %r"`
`uname -srmo`$(tput setaf 1)
$(tput sgr0)"

unset getservice
unset service
unset port
unset startsingle
unset portofservice
unset stopsingle
unset Kservice
unset Kport
unset stopsingleorall
unset startsingleorall

singleorall=(single all exit) #custom bash completion
servicearray=(test1 test2 test3) 
test1singlearray=(test1config.service test1discovery.service test1gateway.service test1auth.service test1umm.service test1backend.service test1frontend.service test1imagesystem.service exit)
test2singlearray=(test2config.service test2discovery.service test2gateway.service test2auth.service test2umm.service test2backend.service test2frontend.service exit)
test3singlearray=(test3config.service test3discovery.service test3gateway.service test3auth.service test3umm.service test3dashboard.service test3samlauth.service exit)

next=(yes no)

service_check=( "test1" "test2" "test3" )
stopsingleorall_check=( "single" "all" "exit" )
startsingleorall_check=( "single" "all" "exit" )
next_check=( "yes" "no" )

VAR1="single"
VAR2="all"

########################################################################* FUNCTIONS *##############################################################################

function start_all_services () {

            echo "getservice='$getservice'"

            nuofline=`cat $getservice.txt | wc -l`   #get number of lines in defined .txt file

            echo $nuofline
            for ((i=1; i<=$nuofline; i++ ));      #for loop until i=nuofline
            do
            #echo $i

            service=$(awk -v "i=$i" '{ if (NR == i) print $1}' $getservice.txt)    #get service name from the text file
            port=$(awk -v "i=$i" '{ if (NR == i) print $2}' $getservice.txt)       #get port number from the text file

            systemctl start $service                #start the service

            echo "Starting $service"

            while ! netstat -tna | grep 'LISTEN\>' | grep -q "$port"   #sleep until assign the particular port for the service
            do
            echo "$service Is Not Started Yet"
            sleep 10 # time in seconds, tune it as needed
            done
            echo "$service Started Successfully with Port $port !"  #output
            done
}

function stop_all_services () {

            nuofline=`cat $getservice.txt | wc -l`   #get number of lines in defined .txt file
            echo $nuofline

            for ((i=1; i<=$nuofline; i++ ));      #for loop until i=nuofline
            do
            Kport=$(awk -v "i=$i" '{ if (NR == i) print $2}' $getservice.txt)
            Kservice=$(lsof -i :$Kport | awk '{print $2}' | grep -Eiv 'PID')
            kill -9  $Kservice
done
} 

function start_single_service () {

if [ $startsingle != "exit" ]
             then
             systemctl start $startsingle
             watch -n1 systemctl status $startsingle
             while ! netstat -tna | grep 'LISTEN\>' | grep -q "$portofservice"   #sleep until assign the particular port for the service
             do
             echo "$startsingle Is Not Started Yet"
             sleep 10 # time in seconds, tune it as needed
             done
             echo "$startsingle Started Successfully with Port $portofservice !"  #output
             echo
             fi

}

function stop_single_service () {
if [ $stopsingle != "exit" ]
            then
            Kport=$portofservice
            Kservice=$(lsof -i :$Kport | awk '{print $2}' | grep -Eiv 'PID')
            kill -9 $Kservice
            echo "$stopsingle Stoped Successfully"  #output
            echo
fi
}

function switchcase() {

            case "$getservice" in
            #case 1
            "test1") arr=("${test1singlearray[@]}")
                   stopsingle_check=( "test1config.service" "test1discovery.service" "test1gateway.service" "test1auth.service" "test1umm.service" "test1backend.service" "test1frontend.service" "test1imagesystem.service" "exit" )
                   startsingle_check=( "test1config.service" "test1discovery.service" "test1gateway.service" "test1auth.service" "test1umm.service" "test1backend.service" "test1frontend.service" "test1imagesystem.service" "exit" );;

            #case 2
            "test2") arr=("${test2singlearray[@]}")
                   stopsingle_check=( "test2config.service" "test2discovery.service" "test2gateway.service" "test2auth.service" "test2umm.service" "test2backend.service" "test2frontend.service" "exit" )
                   startsingle_check=( "test2config.service" "test2discovery.service" "test2gateway.service" "test2auth.service" "test2umm.service" "test2backend.service" "test2frontend.service" "exit" );;

            #case 3
            "test3") arr=("${test3singlearray[@]}")
                     stopsingle_check=( "test3config.service" "test3discovery.service" "test3gateway.service" "test3auth.service" "test3umm.service" "test3dashboard.service" "test3samlauth.service" "exit" )
                     startsingle_check=( "test3config.service" "test3discovery.service" "test3gateway.service" "test3auth.service" "test3umm.service" "test3dashboard.service" "test3samlauth.service" "exit" );;

            esac

}

function check_micro() {
           
           nuofline=`cat $getservice.txt | wc -l`   #get number of lines in defined .txt file
           echo
           echo "APPLICATION STATUS ( UP $(tput setaf 2)██ $(tput sgr0) DOWN $(tput setaf 1)██$(tput sgr0) )"
           echo "NUMBER OF SERVICES $nuofline"
           echo
           for ((i=1; i<=$nuofline; i++ ));      #for loop until i=nuofline
           do
           service=$(awk -v "i=$i" '{ if (NR == i) print $1}' $getservice.txt)    #get service name from the text file
           port=$(awk -v "i=$i" '{ if (NR == i) print $2}' $getservice.txt)       #get port number from the text file
           if ! netstat -tna | grep 'LISTEN\>' | grep -q "$port"
           then
           echo " $(tput setaf 1)$service $(tput sgr0)"
           else
           echo " $(tput setaf 2)$service $(tput sgr0)"
           fi
           done
}

#########################################################* MAIN SCRIPT *################################################################################

while [[ $next != "no" ]]
    do
    while [[ " ${service_check[*]} " != *" ${getservice} "* ]]
    do
    getservice=$(rlwrap -S 'Select The Application Name You Need To Start Or Stop? (Enter "CTRL + C" For EXIT): ' -H ~/.service.history -e '' -i -f <(echo "${servicearray[@]}") -o cat) #geting service name need to start from the user
    getservice="${getservice// /}"

    if [[ " ${service_check[*]} " != *" ${getservice} "* ]]
    then
    echo "$(tput setaf 1) YOU MUST SELECT AN APPLICATION TO PROCEED FURTHER!!!$(tput sgr0)"
    echo "$(tput setaf 2) press the TAB key twice for list the applications$(tput sgr0)"
    else
    check_micro

    fi
    echo
    done

##########

while [[ " ${stopsingleorall_check[*]} " != *" ${stopsingleorall} "* ]]
    do
    stopsingleorall=$(rlwrap -S 'Do You Need To Stop All The Services or Stop Single Service? (Enter exit For EXIT): ' -H ~/.service.history -e '' -i -f <(echo "${singleorall[@]}") -o cat) #geting service name need to start from the user
    stopsingleorall="${stopsingleorall// /}"

    if [[ " ${stopsingleorall_check[*]} " != *" ${stopsingleorall} "* ]]
    then
    echo "$(tput setaf 1) INVALID ENTRY!!! $(tput sgr0)"
    echo "$(tput setaf 2) press the TAB key twice for list  $(tput sgr0)"
    fi

    echo
    done

    if [[ "$stopsingleorall" == "$VAR1" ]];then

    switchcase
#############

while [ "$stopsingle" != "exit" ]
     do
    stopsingle=$(rlwrap -S 'Enter The Service Name You Need To Stop? (Enter exit For EXIT): ' -H ~/.single.history -e '' -i -f <(echo "${arr[@]}") -o cat) #geting service name need to start from the user
    stopsingle="${stopsingle// /}"
    portofservice=$(cat $getservice.txt | awk -v i="$stopsingle" '$0~i' | awk '{ print $2 }')
    if [[ " ${stopsingle_check[*]} " == *" ${stopsingle} "* ]]
    then
    stop_single_service
  
     
    else
    echo "$(tput setaf 1) INVALID ENTRY!!! $(tput sgr0)"
    echo "$(tput setaf 2) press the TAB key twice for list the services $(tput sgr0)"
    fi
     
    done
#############

elif [[ "$stopsingleorall" == "$VAR2" ]]; then
    stop_all_services
fi
############

while [[ " ${startsingleorall_check[*]} " != *" ${startsingleorall} "* ]]
    do
    startsingleorall=$(rlwrap -S 'Do You Need To Start All The Services or Start Single Service? (Enter exit For EXIT): ' -H ~/.service.history -e '' -i -f <(echo "${singleorall[@]}") -o cat) #geting service name need to start from the user
    startsingleorall="${startsingleorall// /}"

    if [[ " ${startsingleorall_check[*]} " != *" ${startsingleorall} "* ]]
    then
    echo "$(tput setaf 1) INVALID ENTRY!!! $(tput sgr0)"
    echo "$(tput setaf 2) press the TAB key twice for list  $(tput sgr0)"
    fi
    echo
    done

if [[ "$startsingleorall" == "$VAR1" ]];then

    switchcase
#############

while [ "$startsingle" != "exit" ]
    do
#     echo ${arr[@]}
#     echo ${test1singlearray[@]}
    startsingle=$(rlwrap -S 'Enter The Service Name You Need To Start? (Enter exit For EXIT):' -H ~/.single.history -e '' -i -f <(echo "${arr[@]}") -o cat) #geting service name need to start from the user
    startsingle="${startsingle// /}"
    portofservice=$(cat $getservice.txt | awk -v i="$startsingle" '$0~i' | awk '{ print $2 }')
    if [[ " ${startsingle_check[*]} " == *" ${startsingle} "* ]]
    then
    start_single_service
     
    else
    echo "$(tput setaf 1) INVALID ENTRY!!!$(tput sgr0)"
    echo "$(tput setaf 2) press the TAB key twice for list the services $(tput sgr0)"
    fi

    done
#############

elif [[ "$startsingleorall" == "$VAR2" ]]; then
    start_all_services
fi
##############

unset getservice
unset service
unset port
unset startsingle
unset portofservice
unset stopsingle
unset Kservice
unset Kport
unset stopsingleorall
unset startsingleorall

echo

next=$(rlwrap -S 'Do You Need To Start or Stop Another Application? (Enter "no" For EXIT): ' -H ~/.service.history -e '' -i -f <(echo "${next[@]}") -o cat) #geting service name need to start from the user
next="${next// /}"
echo

done

相关问题