好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用springboot的jar包能够以service方式启动

springboot 的 jar包 以 service 方式 启动

场景

打出的jar包用java -jar肯定是可以启动的。 这种方式原生简单,但是对运维不友好。

于是要求改造,希望可以用service命令来启动。

过程

技术上完全可以实现的。

pom.xml配置

pom.xml 中有2个配置点:

?

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 >

      <!-- 可执行 -->

         < executable >true</ executable >

     </ configuration >

</ plugin >

注: finalName要保证唯一性,不要带 .1.1.SNAPSHOT 这样的版本号。

打包(maven),授权,启动

先打包,然后执行如下脚本:

?

1

2

3

4

# 授权

chmod 755 custom-app ;

# 启动

./custom-app.jar

如果能够执行,表示maven配置生效了,jar包成了执行文件。

注: 查看jar包,发现前2,300行加入了shell脚本,这就是 <executable>true</executable> 生成的内容。

另: java -jar仍然是可以使用的,不会受到影响。

建立软连接,通过service命令来启动

命令如下:

?

1

2

3

4

# 建立软连接

ln -s /data/custom-app.jar /etc/init.d/custom-app

# 然后就可以用service命令启动了

service custom-app start

发现并没输出日志,那么怎么是否启动了? 如何看日志?

?

1

2

3

4

# 这里可以看启动的日志

/var/log/custom-app.log

# 查看pid,模板为: /var/run/<appname>/<appname>.pid

/var/run/custom-app/custom-app.pid

systemctl配置

因没用到,暂略。

最下面的spring文档里也有systemctl配置的用法。

其他

关于配置的 官网文档

Springboot以jar包方式启动、关闭、重启脚本

启动

?

1

2

3

4

5

6

编写启动脚本startup.sh

#!/bin/bash

echo Starting application

nohup java -jar activiti_demo- 0.0 . 1 -SNAPSHOT.jar &

授权

chmod +x startup.sh

关闭

?

1

2

3

4

5

6

7

8

9

10

11

12

编写关闭脚本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

授权

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

授权

chmod +x restart.sh

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/enthan809882/article/details/109291502

查看更多关于使用springboot的jar包能够以service方式启动的详细内容...

  阅读:17次