search
HomeJavajavaTutorialHow to package spring boot? How to deploy under centos?

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

spring boot打包以及部署

一、打包

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

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

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

在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 &#39;s/^[SK][0-9]*//&#39; -e &#39;s/\.sh$//&#39;)
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

The above is the detailed content of How to package spring boot? How to deploy under centos?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),