jar生产部署脚本

目录

[1. 使用脚本](#1. 使用脚本)

[2. 脚本方法](#2. 脚本方法)

[2.1 帮助说明](#2.1 帮助说明)

[2.2 参数判断](#2.2 参数判断)

[2.3 设置环境变量](#2.3 设置环境变量)

[2.4 日志](#2.4 日志)

[2.5 统计运行应用数](#2.5 统计运行应用数)

[2.6 获取pid](#2.6 获取pid)

[2.7 启动](#2.7 启动)

[2.8 停止](#2.8 停止)

[2.9 备份](#2.9 备份)

[2.10 删除过期的备份](#2.10 删除过期的备份)

[2.11 重启](#2.11 重启)

[2.12 打印启动状态](#2.12 打印启动状态)

[3. 完整脚本](#3. 完整脚本)


1. 使用脚本

复制代码
sh /tool/scripts/taishi-prod.sh restart  taishi.jar test dev 2 taishi 01 --nacos.address=127.0.0.1:8848 --spring.cloud.nacos.discovery.ip=10.16.110.11

2. 脚本方法

2.1 帮助说明

复制代码
# 帮助说明
function usage()
{
  echo "Usage:   sh /tool/scripts/hope-cloud-service-devops.sh [-options] [jarfile] [jardir] [env]"
  echo "Example: sh /tool/scripts/hope-cloud-service-devops.sh restart treasure-auth.jar treasure/auth"
  echo "where options include:"
  echo "     start        启动app"
  echo "     stop         停止app"
  echo "     restart      重启app"
  echo "     status       查看状态"
  echo "See http://127.0.0.1:8090/pages/viewpage.action?pageId=14520287 for more details."
}

2.2 参数判断

看自己实际情况可以增加减少参数

复制代码
if [ "$1" == "--help" ];
then
  usage;
  exit 1
fi

# 微服务名
SpringBoot=$2
# 项目目录
ProjectDir=$3
# 项目环境
Env=$4
# 内存配置级别
# 0:256m 1:512m 2:1G 3:2G 4:4G
MemConfLevel=$5
# 配置监控应用名称
AppName=$6
# 配置监控应用Id
AppId=$7
# springboot配置
RegisterConf=$8
#指定服务器IP --spring.cloud.inetutils.preferred-networks=服务器IP
Networks=$9

if [ "$1" = "" ];
then
    echo -e "\033[0;31m 未输入操作名 \033[0m"
    exit 1
fi

if [ "$2" = "" ];
then
    echo -e "\033[0;31m 未输入应用名 \033[0m"
    exit 1
fi

if [ "$3" = "" ];
then
    echo -e "\033[0;31m 未输入项目路径 \033[0m"
    exit 1
fi

if [ "$4" = "" ];
then
    echo -e "\033[0;31m 未输入项目环境 \033[0m"
    exit 1
fi
if [ "$5" = "" ];
then
    echo -e "\033[0;31m 未输入内存配置级别 \033[0m"
    exit 1
fi
if [ "$6" = "" ];
then
    echo -e "\033[0;31m 未输入应用名称 \033[0m"
    exit 1
fi
if [ "$7" = "" ];
then
    echo -e "\033[0;31m 未输入应用ID \033[0m"
    exit 1
fi

2.3 设置环境变量

复制代码
# 设置环境变量
function setVariable()
{
    export JAVA_HOME=/u01/software/jdk1.8.0_112
    export PATH=$JAVA_HOME/bin:$PATH
    export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
}

2.4 日志

复制代码
#日志
function log
{
  t=`date +"%Y-%m-%d %H:%M:%S"`
  echo "[$t] $1" >> $startup_log
  echo "[$t] $1"
}

2.5 统计运行应用数

复制代码
function getCount()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v 'server.port'|grep -v grep|wc -l`
    echo $count
}

2.6 获取pid

复制代码
function getPid()
{
    pid=`ps -ef |grep java|grep $SpringBoot|grep -v 'server.port'|grep -v grep|awk '{print $2}'`
    echo $pid
}

2.7 启动

复制代码
# 启动应用
function start()
{
    # 统计运行应用数
    count=`getCount`
    if [ $count != 0 ];then
        log "$SpringBoot is running..."
    else
        log "$SpringBoot is start..."
        cp -rf /u01/builds/$SpringBoot /u01/programs/$ProjectDir/
        cd /u01/programs/$ProjectDir
        if [ "$MemConfLevel" = 0 ];then
            XmsSize=256m
            MetaSize=125m
        elif [ "$MemConfLevel" = 1 ];then
            XmsSize=512m
            MetaSize=256m
        elif [ "$MemConfLevel" = 2 ];then
            XmsSize=1G
            MetaSize=512m
        elif [ "$MemConfLevel" = 3 ];then
            XmsSize=2G
            MetaSize=1G
        elif [ "$MemConfLevel" = 4 ];then
            XmsSize=4G
            MetaSize=2G
        fi
        if [[ "$Env" = "prod" ]] || [[ "$Env" = "prod1" ]] || [[ "$Env" = "uat" ]];then
           IP=`hostname -i`
           InstanceName=''${SpringBootName}'@'${IP}''
           MonitorSetting='-javaagent:/tool/software/skywalking-agent/skywalking-agent.jar -Dskywalking.agent.service_name='${AppName}-$Env' -Dskywalking.agent.instance_name='${InstanceName}''
        else
           MonitorSetting=
        fi
        Base_Setting='-Dfile.encoding=UTF-8'
        Mem_Setting='-Xms'${XmsSize}' -Xmx'${XmsSize}' -Xmn'${MetaSize}'  -XX:OldSize='${MetaSize}' -XX:MetaspaceSize='${MetaSize}' -XX:MaxMetaspaceSize='${MetaSize}''
        GC_Setting='-XX:+UseG1GC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:'$gc_log''
        Spring_Setting='--spring.profiles.active='$Env''
        log  "java -jar -server $Mem_Setting $GC_Setting $MonitorSetting $Base_Setting $SpringBoot $Spring_Setting $RegisterConf $Networks >/dev/null 2>$startup_log &"
        nohup java -jar -server $Mem_Setting $GC_Setting $MonitorSetting $Base_Setting $SpringBoot $Spring_Setting $RegisterConf $Networks >/dev/null 2>$startup_log &
    fi
}

2.8 停止

复制代码
function stop()
{
    log "stop $SpringBoot..."
    count=`getCount`
    if [ $count != 0 ];then
        pid=`getPid`
        log "stop $SpringBoot kill pid ${pid}"
        kill $pid
        sleep 2
        count=`getCount`
        if [ $count != 0 ];then
            log "stop $SpringBoot kill -9 pid ${pid}"
            kill -9 $pid
        fi
    fi
}

2.9 备份

复制代码
function bak()
{
    mv -f /tool/programs/$ProjectDir/$SpringBoot /u01/bak/${SpringBoot}_`date +%Y%m%d%H%M%S`
}

2.10 删除过期的备份

复制代码
function del_bak
{
    bak_DIR=/tool/bak
    if [ -d $bak_DIR ]; then
        cd $bak_DIR
        bak_count=`ls -lthr | grep -v 'total' | grep  ${SpringBoot} |  wc -l`
        if (($bak_count > 3));then
            del_count=`expr $bak_count - 3`
            ls -thr | grep -v 'total' | grep  ${SpringBoot}| head -${del_count} | xargs rm -rf
        fi
    fi
}

2.11 重启

复制代码
function restart()
{
    setVariable
    stop
    del_bak
    bak
    start
    status
}

2.12 打印启动状态

复制代码
function status()
{
    sleep 2
    count=`getCount`
    if [ $count != 0 ];then
        log "$SpringBoot is success..."
    else
        log "$SpringBoot is not success..."
    fi
}

3. 完整脚本

复制代码
# 帮助说明
function usage()
{
  echo "Usage:   sh /tool/scripts/hope-cloud-service-devops.sh [-options] [jarfile] [jardir] [env]"
  echo "Example: sh /tool/scripts/hope-cloud-service-devops.sh restart treasure-auth.jar treasure/auth"
  echo "where options include:"
  echo "     start        启动app"
  echo "     stop         停止app"
  echo "     restart      重启app"
  echo "     status       查看状态"
  echo "See http://127.0.01:8090/pages/viewpage.action?pageId=14520287 for more details."
}

if [ "$1" == "--help" ];
then
  usage;
  exit 1
fi

# 微服务名
SpringBoot=$2
# 项目目录
ProjectDir=$3
# 项目环境
Env=$4
# 内存配置级别
# 0:256m 1:512m 2:1G 3:2G 4:4G
MemConfLevel=$5
# 配置监控应用名称
AppName=$6
# 配置监控应用Id
AppId=$7
# springboot配置
RegisterConf=$8
#指定服务器IP --spring.cloud.inetutils.preferred-networks=服务器IP
Networks=$9

if [ "$1" = "" ];
then
    echo -e "\033[0;31m 未输入操作名 \033[0m"
    exit 1
fi

if [ "$2" = "" ];
then
    echo -e "\033[0;31m 未输入应用名 \033[0m"
    exit 1
fi

if [ "$3" = "" ];
then
    echo -e "\033[0;31m 未输入项目路径 \033[0m"
    exit 1
fi

if [ "$4" = "" ];
then
    echo -e "\033[0;31m 未输入项目环境 \033[0m"
    exit 1
fi
if [ "$5" = "" ];
then
    echo -e "\033[0;31m 未输入内存配置级别 \033[0m"
    exit 1
fi
if [ "$6" = "" ];
then
    echo -e "\033[0;31m 未输入应用名称 \033[0m"
    exit 1
fi
if [ "$7" = "" ];
then
    echo -e "\033[0;31m 未输入应用ID \033[0m"
    exit 1
fi

mkdir -p /u01/programs/$ProjectDir
mkdir -p /u01/logs/$ProjectDir/startup

SpringBootName=${SpringBoot%%.*}

mkdir -p /u01/logs/$ProjectDir/gc/$SpringBootName

gc_log=/u01/logs/$ProjectDir/gc/$SpringBootName/${SpringBootName}_gc-%t.log

startup_log=/u01/logs/qpaas/startup/$SpringBoot.log

# 设置环境变量
function setVariable()
{
    export JAVA_HOME=/u01/software/jdk1.8.0_112
    export PATH=$JAVA_HOME/bin:$PATH
    export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
}

#日志
function log
{
  t=`date +"%Y-%m-%d %H:%M:%S"`
  echo "[$t] $1" >> $startup_log
  echo "[$t] $1"
}

# 统计运行应用数
function getCount()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v 'server.port'|grep -v grep|wc -l`
    echo $count
}

# 获取pid
function getPid()
{
    pid=`ps -ef |grep java|grep $SpringBoot|grep -v 'server.port'|grep -v grep|awk '{print $2}'`
    echo $pid
}

# 启动应用
function start()
{
    # 统计运行应用数
    count=`getCount`
    if [ $count != 0 ];then
        log "$SpringBoot is running..."
    else
        log "$SpringBoot is start..."
        cp -rf /u01/builds/$SpringBoot /u01/programs/$ProjectDir/
        cd /u01/programs/$ProjectDir
        if [ "$MemConfLevel" = 0 ];then
            XmsSize=256m
            MetaSize=125m
        elif [ "$MemConfLevel" = 1 ];then
            XmsSize=512m
            MetaSize=256m
        elif [ "$MemConfLevel" = 2 ];then
            XmsSize=1G
            MetaSize=512m
        elif [ "$MemConfLevel" = 3 ];then
            XmsSize=2G
            MetaSize=1G
        elif [ "$MemConfLevel" = 4 ];then
            XmsSize=4G
            MetaSize=2G
        fi
        if [[ "$Env" = "prod" ]] || [[ "$Env" = "prod1" ]] || [[ "$Env" = "uat" ]];then
           IP=`hostname -i`
           InstanceName=''${SpringBootName}'@'${IP}''
           MonitorSetting='-javaagent:/tool/software/skywalking-agent/skywalking-agent.jar -Dskywalking.agent.service_name='${AppName}-$Env' -Dskywalking.agent.instance_name='${InstanceName}''
        else
           MonitorSetting=
        fi
        Base_Setting='-Dfile.encoding=UTF-8'
        Mem_Setting='-Xms'${XmsSize}' -Xmx'${XmsSize}' -Xmn'${MetaSize}'  -XX:OldSize='${MetaSize}' -XX:MetaspaceSize='${MetaSize}' -XX:MaxMetaspaceSize='${MetaSize}''
        GC_Setting='-XX:+UseG1GC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:'$gc_log''
        Spring_Setting='--spring.profiles.active='$Env''
        log  "java -jar -server $Mem_Setting $GC_Setting $MonitorSetting $Base_Setting $SpringBoot $Spring_Setting $RegisterConf $Networks >/dev/null 2>$startup_log &"
        nohup java -jar -server $Mem_Setting $GC_Setting $MonitorSetting $Base_Setting $SpringBoot $Spring_Setting $RegisterConf $Networks >/dev/null 2>$startup_log &
    fi
}

# 停止应用
function stop()
{
    log "stop $SpringBoot..."
    count=`getCount`
    if [ $count != 0 ];then
        pid=`getPid`
        log "stop $SpringBoot kill pid ${pid}"
        kill $pid
        sleep 2
        count=`getCount`
        if [ $count != 0 ];then
            log "stop $SpringBoot kill -9 pid ${pid}"
            kill -9 $pid
        fi
    fi
}

# 备份应用
function bak()
{
    mv -f /tool/programs/$ProjectDir/$SpringBoot /u01/bak/${SpringBoot}_`date +%Y%m%d%H%M%S`
}

# 删除过期的备份程序只保留最新3次的
function del_bak
{
    bak_DIR=/tool/bak
    if [ -d $bak_DIR ]; then
        cd $bak_DIR
        bak_count=`ls -lthr | grep -v 'total' | grep  ${SpringBoot} |  wc -l`
        if (($bak_count > 3));then
            del_count=`expr $bak_count - 3`
            ls -thr | grep -v 'total' | grep  ${SpringBoot}| head -${del_count} | xargs rm -rf
        fi
    fi
}


# 重启应用
function restart()
{
    setVariable
    stop
    del_bak
    bak
    start
    status
}

# 打印启动状态
function status()
{
    sleep 2
    count=`getCount`
    if [ $count != 0 ];then
        log "$SpringBoot is success..."
    else
        log "$SpringBoot is not success..."
    fi
}

case $1 in
    start)
    start;;
    stop)
    stop;;
    restart)
    restart;;
    status)
    status;;
    --help)
    usage;;
    *)
    echo -e "请使用--help察看可用选项" ;;
esac
相关推荐
张张张31223 分钟前
4.2学习总结 Java:list系列集合
java·学习
KATA~26 分钟前
解决MyBatis-Plus枚举映射错误:No enum constant问题
java·数据库·mybatis
xyliiiiiL42 分钟前
一文总结常见项目排查
java·服务器·数据库
shaoing43 分钟前
MySQL 错误 报错:Table ‘performance_schema.session_variables’ Doesn’t Exist
java·开发语言·数据库
腥臭腐朽的日子熠熠生辉2 小时前
解决maven失效问题(现象:maven中只有jdk的工具包,没有springboot的包)
java·spring boot·maven
ejinxian2 小时前
Spring AI Alibaba 快速开发生成式 Java AI 应用
java·人工智能·spring
杉之2 小时前
SpringBlade 数据库字段的自动填充
java·笔记·学习·spring·tomcat
圈圈编码2 小时前
Spring Task 定时任务
java·前端·spring
俏布斯2 小时前
算法日常记录
java·算法·leetcode
27669582922 小时前
美团民宿 mtgsig 小程序 mtgsig1.2 分析
java·python·小程序·美团·mtgsig·mtgsig1.2·美团民宿