spring boot packaging and centos deployment

Keywords: Java Spring network github

Packaging and deployment of spring boot

First, packing

There are many ways to package spring boot. Some of them are war, jar, or directly submitted to github for packaging and deployment through jekins. This is mainly about how to make jar for deployment. War is not recommended, because springboot is suitable for front-end and back-end separation, so it is more suitable to build jar for deployment.

Need to add main program entry in pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

 

  • In the idea tool, visual tools are packaged, as shown in the figure

  • Package from the command line

    mvn clean package -Dmaven.test.skip=true

Two, deployment

Official website document deployment instructions

According to the above deployment, it was ruined..

Let's organize our own sh scripts

  1. 30. SH, this sh can be placed in the unified directory with 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}
#Here you need to configure the environment, dev test stg prd
APP_ENV="dev"
#Configure 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
  1. Create your own service name file under / etc/init.d /, such as 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
  1. chmod +x myapp.sh under / etc/init.d /

  2. chkconfig --list view the list of services. If not, add chkconfig --add myapp to the service.

  3. Set startup chkconfig myapp on

Posted by seanlyons on Wed, 25 Dec 2019 13:47:46 -0800