栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot executable方式部署项目,日志重定向到/dev/null

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot executable方式部署项目,日志重定向到/dev/null

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


    org.springframework.boot
    spring-boot-maven-plugin
    
        true
    

此时,可以通过./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/.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文件来配置一些运行参数,具体如下:

VariableDescription
MODEThe “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_USERThe 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_DAEMONWhether the start-stop-daemon command, when it is available, should be used to control the process. Defaults to true.
PID_FOLDERThe root name of the pid folder (/var/run by default).
LOG_FOLDERThe name of the folder in which to put log files (/var/log by default).
CONF_FOLDERThe name of the folder from which to read .conf files (same folder as jar-file by default).
LOG_FILENAMEThe name of the log file in the LOG_FOLDER (.log by default).
APP_NAMEThe 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_ARGSThe arguments to pass to the program (the Spring Boot app).
JAVA_HOMEThe 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_OPTSOptions that are passed to the JVM when it is launched.
JARFILEThe explicit location of the jar file, in case the script is being used to launch a jar that it is not actually embedded.
DEBUGIf not empty, sets the -x flag on the shell process, allowing you to see the logic in the script.
STOP_WAIT_TIMEThe 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"
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995144.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号