首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring boot 部署脚本

spring boot 部署脚本

作者头像
用户1499526
发布2019-07-15 17:56:59
5690
发布2019-07-15 17:56:59
举报
文章被收录于专栏:简单的日记简单的日记
  1. spring boot 采用 maven assemble 进行打包 tar.zip文件
  2. tar -xvf boot.tar -C /usr/loca/software 解压到指定目录

在po.xml 加入plugin

<plugin>
				<!-- NOTE: We don't need a groupId specification because the group is 
					org.apache.maven.plugins ...which is assumed by default. -->
				<artifactId>maven-assembly-plugin</artifactId>
			<!-- 	<version>2.5.1</version> -->
				<configuration>
					<descriptor>src/main/assemble/service-impl-jar-with-dependency.xml</descriptor>
					<!-- <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> 
						<mainClass>com.alibaba.dubbo.container.Main</mainClass> </manifest> </archive> -->
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id> <!-- this is used for inheritance merges -->
						<phase>package</phase> <!-- bind to the packaging phase -->
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

打包的文件版本

<assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd">
    <id>package</id>
    <formats>
        <format>tar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>/bin</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/logs</directory>
            <outputDirectory>/logs</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
    </files>
    <dependencySets>
        <dependencySet>
        	<unpack>false</unpack>  
            <outputDirectory>/lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>
  1. 启动脚本
#!/bin/bash
# version: 1.0.0
# modify: 2016/07/13
  
cd `dirname $0`             #这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这个目录来定位所要运行程序的相对位置(绝对位置除外)。
BIN_DIR=`pwd`             #得到当前的路径,即:项目根路径下的bin目录
cd ..                          #返回到项目的根目录
DEPLOY_DIR=`pwd`                #将根目录保存下来
CONF_DIR=$DEPLOY_DIR/conf              #将配置文件的目录保存下来
SERVER_NAME=`basename $DEPLOY_DIR`      #获取到当前目录的名称,basename 命令会将路径截取根路径,比如:basename /data/had/hadoop 得到的结果是:hadoop,由此作为项目的名称
PIDS=`ps -ef | grep java | grep "$CONF_DIR" |awk '{print $2}'`     #查找项目是否已经启动,得到PID,先打印出所有的进程,然互过滤出java的进程,再找是否有带有本项目路径的进程,如果有,则截取出PID
if [ -n "$PIDS" ]; then                                         #判断字符串PIDS是否为空,如果不为空,则说明进程已经存在                      
    echo "ERROR: The $SERVER_NAME already started!"            #弹出进程存在的提示信息
    echo "PID: $PIDS"                                          #打印出PID的值
    exit 1                                                      #脚本执行结束
fi
  
LOGS_DIR=$DEPLOY_DIR/logs                                 #设置日志文件的输出目录
  
if [ ! -d $LOGS_DIR ]; then                               #如果目录不存在,就创建目录 
    mkdir $LOGS_DIR
fi
STDOUT_FILE=$LOGS_DIR/stdout.log                        #创建标准日志的输出文件
  
LIB_DIR=$DEPLOY_DIR/lib                                #得到java项目依赖jar包的存放目录
LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"`   #遍历整个目录的jar包,将其添加进来
MAIN_CLASS_JAR=`ls $DEPLOY_DIR|grep .jar|awk '{print "'$DEPLOY_DIR'/"$0}'|tr "\n" ":"`     #得到要部署的jar包
JAVA_OPTS=" -Djava.net.preferIPv4Stack=true -Dlog.home=$LOGS_DIR"      #设置java的启动参数
JAVA_MEM_OPTS=""                 #设置java的JVM参数
BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
    JAVA_MEM_OPTS=" -server -Xms2g -Xmx2g -XX:PermSize=128m -XX:MaxPermSize=128m -XX:SurvivorRatio=6 -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 -XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark -XX:+PrintGCDateStamps -verbose:gc -XX:+PrintGCDetails -Xloggc:gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=dump.hprof "
else
    JAVA_MEM_OPTS=" -server -Xms1g -Xmx1g -XX:PermSize=128m -XX:MaxPermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi
  
echo -e "Starting the $SERVER_NAME ...\c"   #打印启动日志
nohup java $JAVA_OPTS $JAVA_MEM_OPTS -classpath $CONF_DIR:$LIB_JARS:$MAIN_CLASS_JAR  com.juanpi.lux.trace.ws.LuxTraceWsApplication > $STDOUT_FILE 2>&1 &   #启动java项目,注意要设置classpath,并且给出项目的main class ,并将输出重定向
  
COUNT=0
while [ $COUNT -lt 1 ]; do                 #定时检测是否成功
    echo -e ".\c"                           #打点
    sleep 1                                  #等待一秒
    COUNT=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l`
    if [ $COUNT -gt 0 ]; then
        break
    fi
done
  
echo "OK!"                                      
PIDS=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`   #获取启动后的PID
echo "PID: $PIDS"
echo "STDOUT: $STDOUT_FILE"
  1. 停止脚本
#!/bin/bash
# version: 1.0.0
# modify: 2016/07/13

cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf

SERVER_NAME=`basename $DEPLOY_DIR`

PIDS=`ps -ef | grep java | grep "$CONF_DIR" |awk '{print $2}'`
if [ -z "$PIDS" ]; then
    echo "ERROR: The $SERVER_NAME does not started!"
    exit 1
fi

if [ "$1" == "dump" ]; then
    $BIN_DIR/dump.sh
fi

echo -e "Stopping the $SERVER_NAME ...\c"
for PID in $PIDS ; do
    kill $PID > /dev/null 2>&1
done

COUNT=0
while [ $COUNT -lt 1 ]; do
    echo -e ".\c"
    sleep 1
    COUNT=1
    for PID in $PIDS ; do
        PID_EXIST=`ps -f -p $PID | grep java`
        if [ -n "$PID_EXIST" ]; then
            COUNT=0
            break
        fi
    done
done

echo "OK!"
echo "PID: $PIDS"

1dump脚本

#!/bin/bash
# version: 1.0.0
# modify: 2016/07/13

cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf

SERVER_NAME=`basename $DEPLOY_DIR`

PIDS=`ps -ef | grep java | grep "$CONF_DIR" |awk '{print $2}'`
if [ -z "$PIDS" ]; then
    echo "ERROR: The $SERVER_NAME does not started!"
    exit 1
fi

LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi
DUMP_DIR=$LOGS_DIR/dump
if [ ! -d $DUMP_DIR ]; then
    mkdir $DUMP_DIR
fi
DUMP_DATE=`date +%Y%m%d%H%M%S`
DATE_DIR=$DUMP_DIR/$DUMP_DATE
if [ ! -d $DATE_DIR ]; then
    mkdir $DATE_DIR
fi

echo -e "Dumping the $SERVER_NAME ...\c"
for PID in $PIDS ; do
    jstack $PID > $DATE_DIR/jstack-$PID.dump 2>&1
    echo -e ".\c"
    jinfo $PID > $DATE_DIR/jinfo-$PID.dump 2>&1
    echo -e ".\c"
    jstat -gcutil $PID > $DATE_DIR/jstat-gcutil-$PID.dump 2>&1
    echo -e ".\c"
    jstat -gccapacity $PID > $DATE_DIR/jstat-gccapacity-$PID.dump 2>&1
    echo -e ".\c"
    jmap $PID > $DATE_DIR/jmap-$PID.dump 2>&1
    echo -e ".\c"
    jmap -heap $PID > $DATE_DIR/jmap-heap-$PID.dump 2>&1
    echo -e ".\c"
    jmap -histo $PID > $DATE_DIR/jmap-histo-$PID.dump 2>&1
    echo -e ".\c"
    if [ -r /usr/sbin/lsof ]; then
    /usr/sbin/lsof -p $PID > $DATE_DIR/lsof-$PID.dump
    echo -e ".\c"
    fi
done

if [ -r /bin/netstat ]; then
/bin/netstat -an > $DATE_DIR/netstat.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/iostat ]; then
/usr/bin/iostat > $DATE_DIR/iostat.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/mpstat ]; then
/usr/bin/mpstat > $DATE_DIR/mpstat.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/vmstat ]; then
/usr/bin/vmstat > $DATE_DIR/vmstat.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/free ]; then
/usr/bin/free -t > $DATE_DIR/free.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/sar ]; then
/usr/bin/sar > $DATE_DIR/sar.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/uptime ]; then
/usr/bin/uptime > $DATE_DIR/uptime.dump 2>&1
echo -e ".\c"
fi

echo "OK!"
echo "DUMP: $DATE_DIR"
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档