部署可以參考官網(wǎng)地址第59章節(jié):
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#deployment-install
官網(wǎng)有相對比較詳細(xì)得介紹骑疆,這里做簡單得總結(jié)參考如下,具體操作可以以官網(wǎng)為標(biāo)準(zhǔn):
Linux/Unix部署
【1】系統(tǒng)服務(wù)部署
系統(tǒng)服務(wù)
在Spring Boot的Maven插件中,可以構(gòu)建完整可執(zhí)行程序的功能,因此就可以不用java -jar命令掰派,直接運(yùn)行jar來執(zhí)行程序。這樣我們就可以方便的將其創(chuàng)建成系統(tǒng)服務(wù)在后臺(tái)運(yùn)行了妖泄。主要步驟如下:
在pom.xml中添加Spring Boot的插件中平挑,配置executable如下
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
關(guān)鍵配置:
<configuration>
<executable>true</executable>
</configuration>
具體可參考
To create a ‘fully executable’ jar with Maven use the following plugin configuration:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
With Gradle, the equivalent configuration is:
springBoot {
executable = true
}
在完成上述配置后,使用mvn install進(jìn)行打包惨恭,構(gòu)建一個(gè)可執(zhí)行的jar包
創(chuàng)建軟連接到/etc/init.d/目錄下
sudo ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
在完成軟連接創(chuàng)建之后秉馏,我們就可以通過如下命令對yourapp.jar應(yīng)用來控制啟動(dòng)、停止脱羡、重啟操作了
/etc/init.d/yourapp start|stop|restart
日志輸入路徑為:
/var/log/yourapp.log
當(dāng)然也可以通過配置指定日志輸出路徑:
With the exception of JARFILE and APP_NAME, the above settings can be configured using a .conf file. The file is expected next to the jar file and have the same name but suffixed with .conf rather than .jar. For example, a jar named /var/myapp/myapp.jar will use the configuration file named /var/myapp/myapp.conf.
myapp.conf.
JAVA_OPTS=-Xmx1024M
LOG_FOLDER=/custom/log/folder
【2】 nohup 命令部署
Linux/Unix
下面我們來說說服務(wù)器上該如何來配置萝究。實(shí)際上,實(shí)現(xiàn)的方法有很多種锉罐,這里就列兩種還比較好用的方式:
nohup和Shell
該方法主要通過使用nohup
命令來實(shí)現(xiàn)帆竹,該命令的詳細(xì)介紹如下:
nohup 命令
用途:不掛斷地運(yùn)行命令。
語法:nohup Command [ Arg … ][ & ]
描述:nohup 命令運(yùn)行由 Command 參數(shù)和任何相關(guān)的 Arg 參數(shù)指定的命令脓规,忽略所有掛斷(SIGHUP)信號(hào)栽连。在注銷后使用 nohup 命令運(yùn)行后臺(tái)中的程序。要運(yùn)行后臺(tái)中的 nohup 命令侨舆,添加 &
到命令的尾部秒紧。
所以,我們只需要使用nohup java -jar yourapp.jar &命令挨下,就能讓yourapp.jar在后臺(tái)運(yùn)行了熔恢。但是,為了方便管理复颈,我們還可以通過Shell來編寫一些用于啟動(dòng)應(yīng)用的腳本绩聘,具體參考如下:
[1]啟動(dòng)應(yīng)用的腳本:start.sh
#!/bin/bash
# 版本:version 1.0
# by liurongming
nohup java -jar yourapp.jar --server.port=8888 &
可以指定輸出日志目錄:
需求先 cd 到程序當(dāng)前目錄
中
#!/bin/bash
# 版本:version 1.0
# by liurongming
bin=$(cd `dirname $0`; pwd)
echo "Startup..."
LOGS_DIR=${bin}/"../logs"
if [ ! -d $LOGS_DIR ]; then
mkdir $LOGS_DIR
fi
nohup ./yourapp>${bin}/../logs/yourapp.out 2>&1 &
sleep 1
tail -f ${bin}/../logs/yourapp.out
[2]關(guān)閉應(yīng)用的腳本:stop.sh
#!/bin/bash
# 版本:version 1.0
# by liurongming
pid=$(ps -ef | grep yourapp.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$pid" ]
then
echo Application is already stopped
else
echo kill $pid
kill $pid
fi
或者使用強(qiáng)制停止
#!/bin/bash
# 版本:version 1.0
# by liurongming
bin=$(cd `dirname $0`; pwd)
pid=$(ps aux | grep yourapp.jar | grep -v grep | awk '{print $2}')
if [ -n "${pid}" ];
then
echo Application is already stopped
sleep 3
echo kill $pid
kill -9 ${pid}
sleep 1
fi
[3]整合了關(guān)閉和啟動(dòng)的腳本:run.sh
#!/bin/bash
# 版本:version 1.0
# by liurongming
echo stop application
source stop.sh
echo start application
source start.sh
備注:由于會(huì)先執(zhí)行關(guān)閉應(yīng)用,然后再啟動(dòng)應(yīng)用耗啦,這樣不會(huì)引起端口沖突等問題凿菩,適合在持續(xù)集成系統(tǒng)中進(jìn)行反復(fù)調(diào)用。
windos部署
【1】使用 AlwaysUp工具
【2】使用 winsw工具
參考官網(wǎng):(推薦使用)
59.3 Microsoft Windows services
Spring Boot application can be started as Windows service using winsw.
A sample maintained separately to the core of Spring Boot describes step-by-step how you can create a Windows service for your Spring Boot application.