1.打包是,插入以下插件配置,将jar包弄为一个“完全可执行”的jar,配置如下:
要使用Maven创建一个“完全可执行”的jar,请使用以下插件配置:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
以下示例显示了等效的Gradle配置:
bootJar {
launchScript()
}
2.将jar包放入服务器,同目录下创建同名启动配置文件xxx.conf
在xxx.conf加入jar包启动配置,jvm参数,指定jar包使用配置等等。
模板文件:
# --------------------------------------------------------------------------
# The “mode” of operation. The default depends on the way the jar was built,
# but will usually be auto (meaning it tries to guess if it is an init script by checking
# if it is a symlink in a directory called init.d).
# You can explicitly set it to service so that the stop|start|status|restart commands work,
# or to run if you just want to run the script in the foreground.
# --------------------------------------------------------------------------
# MODE=
# --------------------------------------------------------------------------
# If the start-stop-daemon command, when it’s available, should be used to control the process.
# Defaults to true.
# --------------------------------------------------------------------------
# USE_START_STOP_DAEMON=
# --------------------------------------------------------------------------
# The root name of the pid folder (/var/run by default).
# --------------------------------------------------------------------------
# PID_FOLDER=
# --------------------------------------------------------------------------
# The name of the folder to put log files in (/var/log by default).
# --------------------------------------------------------------------------
# LOG_FOLDER=
# --------------------------------------------------------------------------
# The name of the log file in the LOG_FOLDER (<appname>.log by default).
# --------------------------------------------------------------------------
# LOG_FILENAME=
# --------------------------------------------------------------------------
# The name of the app. If the jar is run from a symlink the script guesses the app name,
# but if it is not a symlink, or you want to explicitly set the app name this can be useful.
# --------------------------------------------------------------------------
# APP_NAME=
# --------------------------------------------------------------------------
# The arguments to pass to the program (the Spring Boot app).
# --------------------------------------------------------------------------
# 启动参数配置
RUN_ARGS="--spring.profiles.active=production"
# --------------------------------------------------------------------------
# The location of the java executable is discovered by using the PATH by default,
# but you can set it explicitly if there is an executable file at $JAVA_HOME/bin/java.
# --------------------------------------------------------------------------
# JAVA_HOME=
# --------------------------------------------------------------------------
# Options that are passed to the JVM when it is launched.
# --------------------------------------------------------------------------
#JAVA_OPTS="-Xmx1200M -Xms1200M -Xmn600M -XX:PermSize=500M -XX:MaxPermSize=500M -Xss256K -XX:+UseParNewGC -Djava.rmi.server.hostname=172.0.0.1 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9009 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
# --------------------------------------------------------------------------
# The explicit location of the jar file,
# in case the script is being used to launch a jar that it is not actually embedded in.
# --------------------------------------------------------------------------
# JARFILE=
# --------------------------------------------------------------------------
# if not empty will set the -x flag on the shell process, making it easy to see the logic in the script.
# --------------------------------------------------------------------------
# DEBUG=
3.到服务器/etc/systemd/system目录下创建一个服务脚本:xxx.service
4.编辑服务脚本文件xxx.service
[Unit]
Description=xxx
After=syslog.target
[Service]
User=root
ExecStart=/var/myapp/myapp.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
配置解释:Description=xxx 这里配置为服务脚本文件名xxx
User=root 启动服务的服务器用户
ExecStart=/var/myapp/xxx.jar jar包路径
5. 启动服务: systemctl start xxx --xxx为服务名,也就是服务脚本文件名
查看服务状态:systemctl status xxx
停止服务:systemctl stop xxx
注意:1.服务无法加载,先看看jar包文件在当前用户下权限是否可执行。
2.如果错误信息提示java not found 之类的,将jdk加入软连接
命令:ln -s /usr/java/jdk1.8.0_11/bin/java /sbin/java --前面为jdk路径