1 说明
官方文档:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#deployment.installing
除了使用java -jar运行SpringBoot应用程序之外,还可以为Unix系统创建完全可执行的应用程序。
完全可执行的jar通过在文件的前面嵌入额外的脚本来工作。目前,一些工具不接受这种格式,所以可能不能总是使用这种技术。例如,jar -xf可能无法提取已完全可执行的jar或war。建议只在打算直接执行jar或war时才完全执行它,而不是使用java -jar运行它或将它部署到servlet容器中。
2 maven配置
添加如下打包配置,假如打包的jar名称:myapp.jar
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
此时,可以通过./myapp.jar 来执行程序
3 添加 Unix/Linux 服务支持
通过使用 init.d 或 systemd, 将Spring-Boot应用程序作为Unix/Linux服务启动。
3.1 作为 init.d 服务安装
如果您配置了 Spring Boot 的 Maven 或 Gradle 插件以生成完全可执行的 jar,并且您不使用自定义的 embeddedLaunchScript ,则您的应用程序可以用作 init.d 服务。为此,将 jar 软链接到 init.d 以支持标准的 start , stop , restart 和 status 命令。
默认脚本支持以下功能:
- 以拥有 jar 文件的用户身份启动服务
- 使用 /var/run//.pid 跟踪应用程序的 PID
- 将控制台日志写入 /var/log/.log
给予运行权限
chmod +x /opt/demo/myapp.jar
创建软链接
ln -s /opt/demo/myapp.jar /etc/init.d/myapp
至此,已经可以使用service 启动了
service myapp start|restart|stop|status
此时,默认会在/var/log/<appname>.log 文件打印程序启动的信息
3.2 安装为systemd服务
新建一个systemd service脚本,这里命名为:myapp.service
vi /etc/systemd/system/myapp.service
内容
[Unit]
Description=myapp
Documentation=https://www.myapp.com
After=syslog.target
[Service]
User=root
ExecStart=/opt/demo/myapp.jar start
ExecReload=/opt/demo/myapp.jar restart
ExecStop=/opt/demo/myapp.jar stop
SuccessExitStatus=143
Restart=always
[Install]
WantedBy=multi-user.target
开机自启动
systemctl enable myapp
至此,可以使用systemctl来管理应用了
systemctl start|restart|stop|status myapp
4 定制化配置
程序支持使用.conf 文件来配置一些运行参数,具体如下:
Variable | Description |
---|
MODE | The “mode” of operation. The default depends on the way the jar was built but is usually 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 | RUN_AS_USER | The user that will be used to run the application. When not set, the user that owns the jar file will be used. | USE_START_STOP_DAEMON | Whether the start-stop-daemon command, when it is available, should be used to control the process. Defaults to true . | PID_FOLDER | The root name of the pid folder (/var/run by default). | LOG_FOLDER | The name of the folder in which to put log files (/var/log by default). | CONF_FOLDER | The name of the folder from which to read .conf files (same folder as jar-file by default). | LOG_FILENAME | The name of the log file in the LOG_FOLDER (<appname>.log by default). | APP_NAME | The name of the app. If the jar is run from a symlink, the script guesses the app name. If it is not a symlink or you want to explicitly set the app name, this can be useful. | RUN_ARGS | The arguments to pass to the program (the Spring Boot app). | JAVA_HOME | 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_OPTS | Options that are passed to the JVM when it is launched. | JARFILE | The explicit location of the jar file, in case the script is being used to launch a jar that it is not actually embedded. | DEBUG | If not empty, sets the -x flag on the shell process, allowing you to see the logic in the script. | STOP_WAIT_TIME | The time in seconds to wait when stopping the application before forcing a shutdown (60 by default). |
在myapp.jar 同一级目录下,创建一个myapp.conf 的配置文件(名称与jar包同名)
# 将日志重定向到/dev/null,因为程序里已经自定义配置了日志打印
LOG_FOLDER=/dev
LOG_FILENAME=null
JAVA_OPTS="-Xmx256m -Xms256m -Dfile.encoding=utf-8"
RUN_ARGS="--spring.profiles.active=prod"
|