Nacos微服务shell一键启动脚本(多jar)

前言

微服务架构下的jar数量过多,一次一个命令启动很麻烦,所以写了该脚本,该脚本是通过java -jar的方式启动;当然也可以通过docker 容器化部署


一、使用脚本

1.脚本赋权,可执行权限

chmod +x startupAll.sh

2.批量启动

./startupAll.sh start

3.批量关闭

./startupAll.sh stop

4.批量重启

./startupAll.sh restart

二、shell脚本

#!/bin/sh

## JAR包
export NACOS=register.jar
export CONFIG=upms-biz.jar
export GATEWAY=gateway.jar
export AUTH=auth.jar
export TASK=daemon-quartz.jar
export SCHEDULE=project-schedule-biz.jar

## 日志输入目录位置
export NACOS_log=./logs/register.log
export CONFIG_log=./logs/upms-biz.log
export GATEWAY_log=./logs/gateway.log
export AUTH_log=./logs/auth.log
export TASK_log=./logs/daemon-quartz.log
export SCHEDULE_log=./logs/project-schedule-biz.log

## 端口号
export NACOS_port=8848
export CONFIG_port=4000
export GATEWAY_port=9999
export AUTH_port=3000
export TASK_port=5007
export SCHEDULE_port=6060

case "$1" in

start)
        ## 启动nacos
        echo "--------nacos 开始启动--------------"
        nohup java -Xms200m -Xmx500m -jar $NACOS > $NACOS_log 2>&1 &
        NACOS_pid=`lsof -i:$NACOS_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$NACOS_pid" ]
            do
              NACOS_pid=`lsof -i:$NACOS_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "NACOS pid is $NACOS_pid"
        sleep 10
        echo "--------nacos 启动成功--------------"
 
        ## 启动config
        echo "--------开始启动CONFIG---------------"
        nohup java -Xms200m -Xmx500m -jar $CONFIG > $CONFIG_log 2>&1 &
        CONFIG_pid=`lsof -i:$CONFIG_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$CONFIG_pid" ]
            do
              CONFIG_pid=`lsof -i:$CONFIG_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "CONFIG pid is $CONFIG_pid"
        sleep 30
        echo "---------CONFIG 启动成功-----------"
 
        ## 启动gateway
        echo "--------开始启动GATEWAY---------------"
        nohup java -Xms200m -Xmx500m -jar $GATEWAY > $GATEWAY_log 2>&1 &
        GATEWAY_pid=`lsof -i:$GATEWAY_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$GATEWAY_pid" ]
            do
              GATEWAY_pid=`lsof -i:$GATEWAY_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "GATEWAY pid is $GATEWAY_pid"    
        echo "---------GATEWAY 启动成功-----------"
 
        ## 启动auth
        echo "--------开始启动AUTH---------------"
        nohup java -Xms200m -Xmx500m -jar $AUTH > $AUTH_log 2>&1 &
        AUTH_pid=`lsof -i:$AUTH_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$AUTH_pid" ]
            do
              AUTH_pid=`lsof -i:$AUTH_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "AUTH pid is $AUTH_pid"     
        echo "---------AUTH 启动成功-----------"
                 
        ## 启动task
        echo "--------开始启动TASK---------------"
        nohup java -Xms200m -Xmx500m -jar $TASK > $TASK_log 2>&1 &
        TASK_pid=`lsof -i:$TASK_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$TASK_pid" ]
            do
              TASK_pid=`lsof -i:$TASK_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "TASK pid is $TASK_pid"     
        echo "---------TASK 启动成功-----------"
        
        ## 启动SCHEDULE
        echo "--------开始启动SCHEDULE---------------"
        nohup java -Xms200m -Xmx500m -jar $SCHEDULE > $SCHEDULE_log 2>&1 &
        SCHEDULE_pid=`lsof -i:$SCHEDULE_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$SCHEDULE_pid" ]
            do
              SCHEDULE_pid=`lsof -i:$SCHEDULE_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "SCHEDULE pid is $SCHEDULE_pid"     
        echo "---------SCHEDULE 启动成功-----------"
        
        echo "===startAll success==="
        ;;

 stop)
        P_ID=`ps -ef | grep -w $NACOS | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===NACOS process not exists or stop success"
        else
            kill -9 $P_ID
            echo "NACOS killed success"
        fi
        
        P_ID=`ps -ef | grep -w $CONFIG | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===CONFIG process not exists or stop success"
        else
            kill -9 $P_ID
            echo "CONFIG killed success"
        fi
        
         P_ID=`ps -ef | grep -w $GATEWAY | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===GATEWAY process not exists or stop success"
        else
            kill -9 $P_ID
            echo "GATEWAY killed success"
        fi
        
         P_ID=`ps -ef | grep -w $AUTH | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===AUTH process not exists or stop success"
        else
            kill -9 $P_ID
            echo "AUTH killed success"
        fi
        
         P_ID=`ps -ef | grep -w $TASK | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===TASK process not exists or stop success"
        else
            kill -9 $P_ID
            echo "TASK killed success"
        fi
        
      
        
         P_ID=`ps -ef | grep -w $SCHEDULE | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===SCHEDULE process not exists or stop success"
        else
            kill -9 $P_ID
            echo "SCHEDULE killed success"
        fi
 
        echo "===stop success==="
        ;;   
 
restart)
        $0 stop
        sleep 10
        $0 start
        echo "===restart success==="
        ;;   
esac    
exit 0

运行效果

Nacos微服务shell一键启动脚本(多jar)_第1张图片

你可能感兴趣的:(1024程序员节)