springboot的jar包以service方式啟動(dòng)
場(chǎng)景
打出的jar包用java -jar肯定是可以啟動(dòng)的。 這種方式原生簡(jiǎn)單,但是對(duì)運(yùn)維不友好。
于是要求改造,希望可以用service命令來(lái)啟動(dòng)。
過程
技術(shù)上完全可以實(shí)現(xiàn)的。
pom.xml配置
pom.xml 中有2個(gè)配置點(diǎn):
1
2
3
4
5
6
7
8
9
|
< finalName >custom-app</ finalName > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > <!-- 可執(zhí)行 --> < executable >true</ executable > </ configuration > </ plugin > |
注: finalName要保證唯一性,不要帶 .1.1.SNAPSHOT 這樣的版本號(hào)。
打包(maven),授權(quán),啟動(dòng)
先打包,然后執(zhí)行如下腳本:
1
2
3
4
|
# 授權(quán) chmod 755 custom-app ; # 啟動(dòng) ./custom-app.jar |
如果能夠執(zhí)行,表示maven配置生效了,jar包成了執(zhí)行文件。
注: 查看jar包,發(fā)現(xiàn)前2,300行加入了shell腳本,這就是 <executable>true</executable> 生成的內(nèi)容。
另: java -jar仍然是可以使用的,不會(huì)受到影響。
建立軟連接,通過service命令來(lái)啟動(dòng)
命令如下:
1
2
3
4
|
# 建立軟連接 ln -s /data/custom-app.jar /etc/init.d/custom-app # 然后就可以用service命令啟動(dòng)了 service custom-app start |
發(fā)現(xiàn)并沒輸出日志,那么怎么是否啟動(dòng)了? 如何看日志?
1
2
3
4
|
# 這里可以看啟動(dòng)的日志 /var/log/custom-app.log # 查看pid,模板為: /var/run/<appname>/<appname>.pid /var/run/custom-app/custom-app.pid |
systemctl配置
因沒用到,暫略。
最下面的spring文檔里也有systemctl配置的用法。
其他
關(guān)于配置的官網(wǎng)文檔
Springboot以jar包方式啟動(dòng)、關(guān)閉、重啟腳本
啟動(dòng)
1
2
3
4
5
6
|
編寫啟動(dòng)腳本startup.sh #!/bin/bash echo Starting application nohup java -jar activiti_demo- 0.0 . 1 -SNAPSHOT.jar & 授權(quán) chmod +x startup.sh |
關(guān)閉
1
2
3
4
5
6
7
8
9
10
11
12
|
編寫關(guān)閉腳本stop.sh #!/bin/bash PID=$(ps -ef | grep activiti_demo- 0.0 . 1 -SNAPSHOT.jar | grep -v grep | awk '{ print $2 }' ) if [ -z "$PID" ] then echo Application is already stopped else echo kill $PID kill $PID fi 授權(quán) chmod +x stop.sh |
重啟
1
2
3
4
5
6
7
8
|
編寫重啟腳本restart.sh #!/bin/bash echo Stopping application source ./stop.sh echo Starting application source ./startup.sh 授權(quán) chmod +x restart.sh |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/enthan809882/article/details/109291502