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”.
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之后需要定制的腳本項,可以使用環境變量或配置文件。
默認腳本支持以下環境屬性:
變量名
說明
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環境變量來定制配置文件的位置。
總結
以上是生活随笔為你收集整理的linux搭建springBoot环境,SpringBoot Linux服务化部署的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 魅族20系列Pandaer官方壳亮相!镜
- 下一篇: linux+显卡+停止运行,Linux