linux搭建springBoot环境,SpringBoot Linux服务化部署
除了使用java -jar運行SpringBoot應(yīng)用程序之外,還可以為Unix系統(tǒng)創(chuàng)建可執(zhí)行的應(yīng)用程序。可執(zhí)行的jar可以其他 Unix 系統(tǒng)程序一樣運行,也可以注冊到init.d或systemd。這使我們可以很方便的在生成應(yīng)用環(huán)境中安裝和管理SpringBoot應(yīng)用程序。
提示
可執(zhí)行的jar是通過 jar 文件前面嵌入額外的腳本來工作。目前,一些工具不接受這種格式,所以這種技術(shù)還不是完全穩(wěn)定的。例如,jar -xf可能無法提取可執(zhí)行的jar或war。如果你不能確保此方式可行,建議還是使用java -jar運行它或?qū)⑵洳渴鸬絪ervlet容器中。
使用Maven創(chuàng)建一個“可執(zhí)行”的jar,插件配置:
org.springframework.boot
spring-boot-maven-plugin
true
在Gradle中
bootJar {
launchScript()
}
配置之后,就可以輸入./my-application.jar(my-application是打包的 jar 文件名稱)來運行SpringBoot程序了。jar所在目錄為應(yīng)用程序的work目錄,相當(dāng)于 classPath路徑。
支持操作系統(tǒng)
默認(rèn)腳本支持大多數(shù)Linux發(fā)行版,并在CentOS和Ubuntu上進行了測試。其他平臺,如OS X和FreeBSD,需要使用定制的embeddedLaunchScript。
Unix / Linux服務(wù)
通過使用init.d或systemd,可以輕松地將Spring引導(dǎo)應(yīng)用程序啟動為Unix/Linux服務(wù)。
安裝為init.d服務(wù)
如果配置了Spring Boot的Maven或Gradle插件來生成一個可執(zhí)行的jar,并且不使用自定義embeddedLaunchScript,那么可用將應(yīng)用程序配置為init.d服務(wù)。就可以支持標(biāo)準(zhǔn)的start、stop、restart和status命令。
該腳本支持以下功能:
作為擁有jar文件的用戶啟動服務(wù)
使用/var/run//.pid跟蹤應(yīng)用程序的PID
將控制臺日志寫入/var/log/.log
假設(shè)你的SpringBoot程序部署在/var/myapp路徑,要將SpringBoot程序作為init.d服務(wù),需要創(chuàng)建一個軟鏈接,如下:
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
安裝后,就可以按系統(tǒng)服務(wù)的方式啟動和停止。例如,在基于debian的系統(tǒng)上,可以使用以下命令啟動它:
$ service myapp start
如果應(yīng)用程序啟動失敗,可以在/var/log/.log日志文件中查找錯誤。
將程序標(biāo)記為自動啟動。例如,在Debian上,可以使用以下命令:
$ update-rc.d myapp defaults
init.d服務(wù)安全
下面是一組關(guān)于如何保護以init.d形式運行的SpringBoot應(yīng)用程序的指導(dǎo)原則。注意,這里只是列出部分為增強應(yīng)用程序及其運行環(huán)境的安全性而應(yīng)該做的事情。
當(dāng)以 root 身份執(zhí)行應(yīng)用程序,應(yīng)用程序的擁有者就是 root。而我們不應(yīng)該使用 root 用戶作為程序的擁有者,而是要創(chuàng)建一個特定的用戶,并使用chown使其成為jar文件的所有者,如下面的示例所示:
$ chown bootapp:bootapp your-app.jar
在這種情況下,程序?qū)⑹褂胋ootapp用戶運行。
為了減少應(yīng)用程序的用戶帳戶被破壞的可能性,應(yīng)該考慮阻止它使用shell登錄。例如,可以將帳戶的shell設(shè)置為/usr/sbin/nologin。
防止應(yīng)用程序jar文件被篡改
首先,配置其權(quán)限,使其不能被寫入,只能被其所有者讀取或執(zhí)行,如下例所示:
$ chmod 500 your-app.jar
其次,如果應(yīng)用程序或運行它的帳戶受到損害,應(yīng)該采取措施來限制損害。如果攻擊者確實獲得了訪問權(quán),他們可以篡改jar文件。防止這種情況發(fā)生的一種方法是使用chattr使其不可變,如下面的示例所示:
$ sudo chattr +i your-app.jar
這將阻止任何用戶(包括root)修改jar。
如果root用于控制應(yīng)用程序的服務(wù),而程序是使用.conf文件自定義啟動配置,則root需要可以將讀取.conf文件。使用chmod使文件只能被所有者讀取,使用chown使root成為所有者,如下例所示:
$ chmod 400 your-app.conf
$ sudo chown root:root your-app.conf
安裝為systemd服務(wù)
systemd是System V init.d系統(tǒng)的繼承者,現(xiàn)在許多現(xiàn)代Linux發(fā)行版都在使用它。盡管你可以繼續(xù)使用init.d。通過systemd腳本,也可以使用systemd ' service '腳本啟動SpringBoot應(yīng)用程序。
假設(shè)在/var/myapp中部署了一個SpringBoot應(yīng)用程序,要將SpringBoot應(yīng)用程序安裝為systemd服務(wù),請創(chuàng)建一個名為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
注意:請記住對應(yīng)更改應(yīng)用程序的描述、用戶和ExecStart字段為你運行的環(huán)境值
ExecStart字段不聲明腳本操作命令,這意味著默認(rèn)情況下使用run命令。
注意,這與作為init.d運行時不同。init.d服務(wù)運行應(yīng)用程序的用戶、PID文件和控制臺日志文件由systemd本身管理,因此必須通過在“service”腳本中使用適當(dāng)?shù)淖侄芜M行配置。有關(guān)詳細信息,請參閱服務(wù)單元配置手冊頁。
標(biāo)記自動啟動,請使用以下命令:
$ systemctl enable myapp.service
自定義啟動腳本
Maven或Gradle插件編寫的默認(rèn)嵌入式啟動腳本可以通過多種方式定制。對于大多數(shù)人來說,使用默認(rèn)腳本和一些定制通常就足夠了。如果您發(fā)現(xiàn)無法定制需要的內(nèi)容,可以使用embeddedLaunchScript選項完全編寫自己的文件。
在編寫開始腳本時自定義它
在將開始腳本的元素寫入jar文件時,對其進行自定義通常是有意義的。例如,init.d腳本可以提供“描述”。因為您已經(jīng)預(yù)先知道了描述(并且它不需要更改),所以最好在生成jar時提供它。
要自定義編寫的元素,請使用Spring Boot Maven插件的embeddedLaunchScriptProperties選項或Spring Boot Gradle插件的launchScript的properties屬性。
默認(rèn)腳本支持以下屬性替換:
屬性名
說明
Gradle 默認(rèn)值
Maven 默認(rèn)值
mode
The script mode.
auto
auto
initInfoProvides
The Provides section of “INIT INFO”
${task.baseName}
${project.artifactId}
initInfoRequiredStart
Required-Start section of “INIT INFO”.
syslog $network
syslog $network
initInfoRequiredStop
Required-Stop section of “INIT INFO”.
syslog $network
syslog $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
{task.baseName})
${project.name}
initInfoDescription
Description section of “INIT INFO”.
{task.baseName})
{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之后需要定制的腳本項,可以使用環(huán)境變量或配置文件。
默認(rèn)腳本支持以下環(huán)境屬性:
變量名
說明
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文件配置上一節(jié)中列出的設(shè)置。這個文件應(yīng)該和 jar 文件在同級目錄中,并且名字和 jar 文件名字一致。但是后綴是.conf而不是.jar。例如,名為/var/myapp/myapp.jar使用名為/var/myapp/myapp的配置文件。conf,如下例所示:
myapp.conf
JAVA_OPTS=-Xmx1024M
LOG_FOLDER=/custom/log/folder
如果不喜歡將配置文件放在jar文件旁邊,可以設(shè)置CONF_FOLDER環(huán)境變量來定制配置文件的位置。
總結(jié)
以上是生活随笔為你收集整理的linux搭建springBoot环境,SpringBoot Linux服务化部署的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 魅族20系列Pandaer官方壳亮相!镜
- 下一篇: iOS 16.3.1正式推送!车祸检测功