linux搭建springBoot环境,SpringBoot Linux服务化部署

除了使用java -jar运行SpringBoot应用程序之外,还可以为Unix系统创建可执行的应用程序。可执行的jar可以其他 Unix 系统程序一样运行,也可以注册到init.d或systemd。这使我们可以很方便的在生成应用环境中安装和管理SpringBoot应用程序。

提示

可执行的jar是通过 jar 文件前面嵌入额外的脚本来工作。目前,一些工具不接受这种格式,所以这种技术还不是完全稳定的。例如,jar -xf可能无法提取可执行的jar或war。如果你不能确保此方式可行,建议还是使用java -jar运行它或将其部署到servlet容器中。

使用Maven创建一个“可执行”的jar,插件配置:

org.springframework.boot

spring-boot-maven-plugin

true

在Gradle中

bootJar {

launchScript()

}

配置之后,就可以输入./my-application.jar(my-application是打包的 jar 文件名称)来运行SpringBoot程序了。jar所在目录为应用程序的work目录,相当于 classPath路径。

支持操作系统

默认脚本支持大多数Linux发行版,并在CentOS和Ubuntu上进行了测试。其他平台,如OS X和FreeBSD,需要使用定制的embeddedLaunchScript。

Unix / Linux服务

通过使用init.d或systemd,可以轻松地将Spring引导应用程序启动为Unix/Linux服务。

安装为init.d服务

如果配置了Spring Boot的Maven或Gradle插件来生成一个可执行的jar,并且不使用自定义embeddedLaunchScript,那么可用将应用程序配置为init.d服务。就可以支持标准的start、stop、restart和status命令。

该脚本支持以下功能:

作为拥有jar文件的用户启动服务

使用/var/run//.pid跟踪应用程序的PID

将控制台日志写入/var/log/.log

假设你的SpringBoot程序部署在/var/myapp路径,要将SpringBoot程序作为init.d服务,需要创建一个软链接,如下:

$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp

安装后,就可以按系统服务的方式启动和停止。例如,在基于debian的系统上,可以使用以下命令启动它:

$ service myapp start

如果应用程序启动失败,可以在/var/log/.log日志文件中查找错误。

将程序标记为自动启动。例如,在Debian上,可以使用以下命令:

$ update-rc.d myapp defaults

init.d服务安全

下面是一组关于如何保护以init.d形式运行的SpringBoot应用程序的指导原则。注意,这里只是列出部分为增强应用程序及其运行环境的安全性而应该做的事情。

当以 root 身份执行应用程序,应用程序的拥有者就是 root。而我们不应该使用 root 用户作为程序的拥有者,而是要创建一个特定的用户,并使用chown使其成为jar文件的所有者,如下面的示例所示:

$ chown bootapp:bootapp your-app.jar

在这种情况下,程序将使用bootapp用户运行。

为了减少应用程序的用户帐户被破坏的可能性,应该考虑阻止它使用shell登录。例如,可以将帐户的shell设置为/usr/sbin/nologin。

防止应用程序jar文件被篡改

首先,配置其权限,使其不能被写入,只能被其所有者读取或执行,如下例所示:

$ chmod 500 your-app.jar

其次,如果应用程序或运行它的帐户受到损害,应该采取措施来限制损害。如果攻击者确实获得了访问权,他们可以篡改jar文件。防止这种情况发生的一种方法是使用chattr使其不可变,如下面的示例所示:

$ sudo chattr +i your-app.jar

这将阻止任何用户(包括root)修改jar。

如果root用于控制应用程序的服务,而程序是使用.conf文件自定义启动配置,则root需要可以将读取.conf文件。使用chmod使文件只能被所有者读取,使用chown使root成为所有者,如下例所示:

$ chmod 400 your-app.conf

$ sudo chown root:root your-app.conf

安装为systemd服务

systemd是System V init.d系统的继承者,现在许多现代Linux发行版都在使用它。尽管你可以继续使用init.d。通过systemd脚本,也可以使用systemd ' service '脚本启动SpringBoot应用程序。

假设在/var/myapp中部署了一个SpringBoot应用程序,要将SpringBoot应用程序安装为systemd服务,请创建一个名为myapp.service脚本,并将其放在/etc/systemd/system目录中。下面的脚本提供了一个例子:

[Unit]

Description=myapp

After=syslog.target

[Service]

User=myapp

ExecStart=/var/myapp/myapp.jar

SuccessExitStatus=143

[Install]

WantedBy=multi-user.target

注意:请记住对应更改应用程序的描述、用户和ExecStart字段为你运行的环境值

ExecStart字段不声明脚本操作命令,这意味着默认情况下使用run命令。

注意,这与作为init.d运行时不同。init.d服务运行应用程序的用户、PID文件和控制台日志文件由systemd本身管理,因此必须通过在“service”脚本中使用适当的字段进行配置。有关详细信息,请参阅服务单元配置手册页。

标记自动启动,请使用以下命令:

$ systemctl enable myapp.service

自定义启动脚本

Maven或Gradle插件编写的默认嵌入式启动脚本可以通过多种方式定制。对于大多数人来说,使用默认脚本和一些定制通常就足够了。如果您发现无法定制需要的内容,可以使用embeddedLaunchScript选项完全编写自己的文件。

在编写开始脚本时自定义它

在将开始脚本的元素写入jar文件时,对其进行自定义通常是有意义的。例如,init.d脚本可以提供“描述”。因为您已经预先知道了描述(并且它不需要更改),所以最好在生成jar时提供它。

要自定义编写的元素,请使用Spring Boot Maven插件的embeddedLaunchScriptProperties选项或Spring Boot Gradle插件的launchScript的properties属性。

默认脚本支持以下属性替换:

属性名

说明

Gradle 默认值

Maven 默认值

mode

The script mode.

auto

auto

initInfoProvides

The Provides section of “INIT INFO”

${task.baseName}

${project.artifactId}

initInfoRequiredStart

Required-Start section of “INIT INFO”.

math?formula=remote_fssyslog $network

math?formula=remote_fssyslog $network

initInfoRequiredStop

Required-Stop section of “INIT INFO”.

math?formula=remote_fssyslog $network

math?formula=remote_fssyslog $network

initInfoDefaultStart

Default-Start section of “INIT INFO”.

2 3 4 5

2 3 4 5

initInfoDefaultStop

Default-Stop section of “INIT INFO”.

0 1 6

0 1 6

initInfoShortDescription

Short-Description section of “INIT INFO”.

Single-line version of

math?formula=%7Bproject.description%7D%20(falling%20back%20to{task.baseName})

${project.name}

initInfoDescription

Description section of “INIT INFO”.

math?formula=%7Bproject.description%7D%20(falling%20back%20to{task.baseName})

math?formula=%7Bproject.description%7D%20(falling%20back%20to{project.name})

initInfoChkconfig

chkconfig section of “INIT INFO”

2345 99 01

2345 99 01

confFolder

The default value for CONF_FOLDER

Folder containing the jar

Folder containing the jar

inlinedConfScript

Reference to a file script that should be inlined in the default launch script. This can be used to set environmental variables such as JAVA_OPTS before any external config files are loaded

logFolder

Default value for LOG_FOLDER. Only valid for an init.d service

logFilename

Default value for LOG_FILENAME. Only valid for an init.d service

pidFolder

Default value for PID_FOLDER. Only valid for an init.d service

pidFilename

Default value for the name of the PID file in PID_FOLDER. Only valid for an init.d service

useStartStopDaemon

Whether the start-stop-daemon command, when it’s available, should be used to control the process

true

true

stopWaitTime

Default value for STOP_WAIT_TIME in seconds. Only valid for an init.d service

60

60

在脚本运行时自定义脚本

对于在编写jar之后需要定制的脚本项,可以使用环境变量或配置文件。

默认脚本支持以下环境属性:

变量名

说明

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

start

status

restart commands work or to run if you want to run the script in the foreground.

USE_START_STOP_DAEMON

Whether the start-stop-daemon command, when it’s 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 (.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, making it easy 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).

PID_FOLDER、LOG_FOLDER和LOG_FILENAME变量仅对init.d 方式有效。对于systemd,通过使用“service”脚本进行相同的定制。

除了JARFILE和APP_NAME之外,可以使用.conf文件配置上一节中列出的设置。这个文件应该和 jar 文件在同级目录中,并且名字和 jar 文件名字一致。但是后缀是.conf而不是.jar。例如,名为/var/myapp/myapp.jar使用名为/var/myapp/myapp的配置文件。conf,如下例所示:

myapp.conf

JAVA_OPTS=-Xmx1024M

LOG_FOLDER=/custom/log/folder

如果不喜欢将配置文件放在jar文件旁边,可以设置CONF_FOLDER环境变量来定制配置文件的位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值