Packaging and publishing Spring Boot applications

1. Create a project (example-fast)
Create a WEB project example-fast based on Spring Boot.

2. Compile and package
2.1 It is super easy to use IDEA integrated Maven environment to compile and package Spring Boot projects
insert image description here

2.2 mvn command packaging

# mvn clean 清理编译
# install 打包
# -Dmaven.test.skip=true 跳过单元测试
# -P dev 指定dev配置

mvn clean install -Dmaven.test.skip=true -P dev

3. Application release

3.1 Evolution of one-line command
The way to run the jar packaged by Spring Boot is also simple, only one line of command is required.

java -jar /usr/local/example-fast-1.0.0.jar

At this time, the service is started, but the window cannot be closed. Once the service is closed, it will stop. I have to consider running in the background and want to see the log;

java -jar /usr/local/example-fast-1.0.0.jar > example-fast.out &

When executing startup, the effect is as follows, and then you can easily see the log output.

If you are both R&D and O&M in the project team, the job is basically over here, because I believe that through proficient operation, a muscle response will be formed, and you will definitely remember this line of command.
However, if the division of labor is clear and the production permissions are isolated, it is usually the operation and maintenance colleagues who operate and release, so we have to find ways to save the operation and maintenance colleagues labor, and have to consider scripting.

First create a project directory such as example-fast, and then create bin, lib, and logs directories respectively; copy example-fast-0.0.1-SNAPSHOT.jar to the lib directory; the bin directory mainly stores scripts. Create start.sh application start script; stop.sh application stop script

Startup script (start.sh)

#!/bin/bash

#配置 Java 环境变量
export JAVA_HOME=/usr/local/java/jdk/jdk1.8.0_231
export PATH=.:$JAVA_HOME/bin:$PATH


#定义应用名
App_Name=example-fast

#定义应用所在目录
App_Path=/home/${App_Name}

#定义可执行文件的路径
JAR_PATH=${App_Path}/example-fast-1.0.0.jar

#jvm启动参数
JAVA_OPTS="-Duser.timezone=GMT+8 -server -Xms4096m -Xmx4096m  -XX:MaxMetaspaceSize=256m -Xloggc:${App_Path}/logs/${App_Name}.gc.log -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=5 -XX:+PrintGC -XX:+PrintGCTimeStamps  -XX:+PrintGCDetails  -XX:+PrintGCApplicationStoppedTime"

#启动JAVA进程函数
CURRENT_COUNT=`ps -ef|grep java |grep ${
     
     App_Name} |grep -vc grep`
if [ $CURRENT_COUNT -eq 0 ]
then
        Log_Name=$(echo ${
     
     App_Name}|awk  -F"-" '{ print $NF }')
        nohup java -Dfunc_type=${App_Name}  $JAVA_OPTS  -Dfile.encoding=utf-8  -jar  $JAR_PATH  > ${App_Path}/logs/${App_Name}.out  2>&1 &
  PROCESS_ID=`ps -ef | grep  "${App_Name}" |grep -v grep | awk '{ print $2 }'`
      echo " ☺☺☺☺☺启动应用 ${App_Name} 成功,进程 id 为 ${PROCESS_ID} ☺☺☺☺☺ "
else
    echo " ☺☺☺☺☺ 应用 ${App_Name} 已经被启动了 ☺☺☺☺☺ "
fi
# 执行应用启动 start.sh
sh start.sh

Stop script (stop.sh)

#!/bin/bash

# 定义应用名称
App_Name=example-fast

# 停止JAVA进程函数
echo " ☺☺☺☺☺ 开始停止 ${App_Name} 应用 ☺☺☺☺☺ "
ps -ef | grep ${App_Name} | grep -v grep | awk '{print$2}' | xargs kill
echo " ☺☺☺☺☺ 应用 ${App_Name} 停止成功!☺☺☺☺☺ "
# 执行应用停止 stop.sh
sh stop.sh

So far, the most basic release method of the Spring Boot project is finished. You can directly execute the script to complete the start and stop of the project, and the operation is simpler!

Guess you like

Origin blog.csdn.net/liulihui1988/article/details/129896602