首页 >Java >java教程 >spring boot如何打包?如何在centos下部署?

spring boot如何打包?如何在centos下部署?

不言
不言原创
2018-09-26 15:30:342027浏览

本篇文章给大家带来的内容是关于spring boot如何打包?如何在centos下部署?有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

spring boot打包以及部署

一、打包

springboot的打包方式有很多种。有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的。这里主要介绍如何打成jar进行部署。不推荐用war,因为springboot适合前后端分离,打成jar进行部署更合适。

需要在pom.xml中增加主程序入口

5ac54a929980b32fdd2e48b6a8da067c
    c4222ff3007372f15ec728b963e717b6
        33ca7039399a4759a8764ea38765dd46
            05a8acc5c31084a4f61ade01873802caorg.springframework.boot192ca2f7b8c770b01c8f81e6bdd5b947
            9bc4cbb67f66e148869423c0d27e5f90spring-boot-maven-pluginb68fb17cb904a46b73e6272850323873
            f9d9f4a8f32d97f3ef0c10742ed31240
                4d6b55d605eb8d0c7af56f43bec10592true809b1d48cc5c99feb234a8a86d85e2b7
            4b1b9d85fe86862ae3eab7e2045cf8a0
        9424ec9162a9b85f34067d0c54f34b32
    f3567a435ac6132b26525998d793443bf82c24fb6fac67972f6e79c18ccef56a

在idea工具中可视化工具打包,如图

通过命令行来打包

mvn clean package -Dmaven.test.skip=true

二、部署

官网文档部署说明

按照这上面部署被坑惨了。。

下面整理下自己部署的sh脚本

1、XXX.sh,此sh放到和jar统一目录即可

#!/bin/sh
### BEGIN INIT INFO
# Provides:          lanwei
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: XXX service
# Description:       XXX service
### END INIT INFO
echo "Starting...."
APP_NAME=$(echo $(basename $0) | sed -e 's/^[SK][0-9]*//' -e 's/\.sh$//')
APP_HOME=/usr/software/${APP_NAME}
#这里需要配置环境,dev test stg prd
APP_ENV="dev"
#配置jar
APP_JAR=${APP_HOME}/XXXX.jar
usage() {
    echo "Usage: sh ${APP_NAME} [start|stop|restart]"
    exit 1
}
##################################################
# Some utility functions
##################################################
findDirectory()
{
  local L OP=$1
  shift
  for L in "$@"; do
    [ "$OP" "$L" ] || continue
    printf %s "$L"
    break
  done
}
echo "APP_ENV   :   ${APP_ENV}"
echo "APP_HOME  :   ${APP_HOME}"
echo "APP_NAME  :   ${APP_NAME}"
echo "APP_JAR   :   ${APP_JAR}"
#####################################################
# Find a location for the pid file
#####################################################
if [ -z "$APP_RUN" ]
then
  APP_RUN=$(findDirectory -w /var/run /usr/var/run /tmp)
fi
#APP_RUN=/var/run
echo "APP_RUN   :   ${APP_RUN}"
#####################################################
# Find a pid
#####################################################
if [ -z "$APP_PID" ]
then
  APP_PID="$APP_RUN/${APP_NAME}.pid"
fi
echo "APP_PID   :   ${APP_PID}"
LOG=${APP_HOME}/logs/${APP_ENV}.log
ERROR_LOG=${APP_HOME}/logs/${APP_ENV}_err.log
case $1 in
    start)
        echo "Starting ${APP_NAME} ..."
        if [ ! -f $APP_PID ]; then
            cd ${APP_HOME}
            nohup java -jar $APP_JAR --spring.profiles.active=${APP_ENV} > $LOG 2> $ERROR_LOG &
                        echo $! > $APP_PID
            echo "$APP_NAME started ..."
        else
            echo "$APP_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $APP_PID ]; then
            PID=$(cat $APP_PID);
                        echo "$APP_NAME PID is ${PID}"
            echo "$APP_NAME stoping ..."
            kill $PID;
            echo "$APP_NAME stopped ..."
            rm $APP_PID
        else
            echo "$APP_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $APP_PID ]; then
            PID=$(cat $APP_PID);
                        echo "$APP_NAME PID is ${PID}"
            echo "$APP_NAME stopping ...";
            kill $PID;
            echo "$APP_NAME stopped ...";
            rm $APP_PID
            echo "$APP_NAME starting ..."
            cd ${APP_HOME}
            nohup java -jar $APP_JAR --spring.profiles.active=${APP_ENV} > $LOG 2> $ERROR_LOG &
                        echo $! > $APP_PID
            echo "$APP_NAME started ..."
        else
            echo "$APP_NAME is not running ..."
            echo "$APP_NAME starting ..."
                        cd ${APP_HOME}
            nohup java -jar $APP_JAR --spring.profiles.active=${APP_ENV} > $LOG 2> $ERROR_LOG &
                        echo $! > $APP_PID
            echo "$APP_NAME started ..."
        fi
    ;;
esac

2、在/etc/init.d/下创建自己的服务名称文件这里比如myapp.sh

#!/bin/sh
#
# /etc/init.d/sms-web
# chkconfig: 345 63 37
# description: activemq servlet container.
# processname: activemq 5.14.1

# Source function library.
#. /etc/init.d/functions
# source networking configuration.
#. /etc/sysconfig/network

export JAVA_HOME=/usr/local/jdk1.8.0_144
export PATH=$JAVA_HOME/bin:$PATH
export MYAPP_WEB_HOME=/usr/software/myapp

case $1 in
    start)
        sh $MYAPP_WEB_HOME/myapp.sh start
    ;;
    stop)
        sh $MYAPP_WEB_HOME/myapp.sh stop
    ;;
    restart)
        sh $MYAPP_WEB_HOME/myapp.sh restart
    ;;

esac
exit 0

在/etc/init.d/下 chmod +x myapp.sh赋权限   

chkconfig --list查看服务列表,如果没有, 添加chkconfig --add myapp到服务中。  

设置开机启动chkconfig myapp on

以上是spring boot如何打包?如何在centos下部署?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn