Skip to content

Commit 90c4cb5

Browse files
committed
Spring Boot应用的后台运行配置
1 parent a82ae1b commit 90c4cb5

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
### Spring Boot应用的后台运行配置 ###
2+
3+
酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式。在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式:
4+
5+
* 运行Spring Boot的应用主类
6+
* 使用Maven的Spring Boot插件mvn spring-boot:run来运行
7+
* 打成jar包后,使用java -jar运行
8+
9+
我们在开发的时候,通常会使用前两种,而在部署的时候往往会使用第三种。但是,我们在使用java -jar来运行的时候,并非后台运行。下面我们分别针对Windows和Linux/Unix两种环境,整理一下如何配置后台运行的方法。
10+
11+
#### Windows ####
12+
Windows下比较简单,我们可以直接使用这款软件:AlwaysUp。如下图所示,简单、暴力、好用。
13+
14+
![](https://github.com/scalad/Note/blob/master/SpringBoot_Run_In_Background/image/13213400_9s9B.png)
15+
16+
配置方式很简单,我们只需要把Spring Boot应用通过mvn install打成jar包,然后编写一个java -jar yourapp.jar的bat文件。再打开AlwaysUp,点击工具栏的第一个按钮,如下图所示,选择上面编写的bat文件,并填写服务名称。
17+
18+
![](https://github.com/scalad/Note/blob/master/SpringBoot_Run_In_Background/image/13213417_TI8z.png)
19+
20+
完成了创建之后,在列表中可以看到我们配置的服务,通过右键选择Start xxx就能在后台将该应用启动起来了
21+
22+
#### Linux/Unix ####
23+
下面我们来说说服务器上该如何来配置。实际上,实现的方法有很多种,这里就列两种还比较好用的方式:
24+
25+
nohup和Shell
26+
27+
该方法主要通过使用nohup命令来实现,该命令的详细介绍如下:
28+
29+
nohup 命令
30+
31+
用途:不挂断地运行命令。
32+
33+
语法:nohup Command [ Arg … ][ & ]
34+
35+
描述:nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用 nohup 命令运行后台中的程序。要运行后台中的 nohup 命令,添加 &到命令的尾部。
36+
37+
所以,我们只需要使用nohup java -jar yourapp.jar &命令,就能让yourapp.jar在后台运行了。但是,为了方便管理,我们还可以通过Shell来编写一些用于启动应用的脚本,比如下面几个:
38+
39+
* 关闭应用的脚本:stop.sh
40+
41+
```shell
42+
#!/bin/bash
43+
PID=$(ps -ef | grep yourapp.jar | grep -v grep | awk '{ print $2 }')
44+
if [ -z "$PID" ]
45+
then
46+
echo Application is already stopped
47+
else
48+
echo kill $PID
49+
kill $PID
50+
fi
51+
```
52+
53+
* 启动应用的脚本:start.sh
54+
55+
```shell
56+
#!/bin/bash
57+
nohup java -jar yourapp.jar --server.port=8888 &
58+
```
59+
60+
* 整合了关闭和启动的脚本:run.sh,由于会先执行关闭应用,然后再启动应用,这样不会引起端口冲突等问题,适合在持续集成系统中进行反复调用。
61+
62+
```shell
63+
#!/bin/bash
64+
echo stop application
65+
source stop.sh
66+
echo start application
67+
source start.sh
68+
```
69+
70+
#### 系统服务 ####
71+
在Spring Boot的Maven插件中,还提供了构建完整可执行程序的功能,什么意思呢?就是说,我们可以不用java -jar,而是直接运行jar来执行程序。这样我们就可以方便的将其创建成系统服务在后台运行了。主要步骤如下:
72+
73+
* 在pom.xml中添加Spring Boot的插件,并注意设置executable配置
74+
75+
```xml
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-maven-plugin</artifactId>
81+
<configuration>
82+
<executable>true</executable>
83+
</configuration>
84+
</plugin>
85+
</plugins>
86+
</build>
87+
```
88+
89+
* 在完成上述配置后,使用mvn install进行打包,构建一个可执行的jar包
90+
91+
* 创建软连接到/etc/init.d/目录下
92+
93+
sudo ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
94+
95+
* 在完成软连接创建之后,我们就可以通过如下命令对yourapp.jar应用来控制启动、停止、重启操作了
96+
97+
/etc/init.d/yourapp start|stop|restart
98+
99+
原文地址:[http://blog.didispace.com/spring-boot-run-backend/](http://blog.didispace.com/spring-boot-run-backend/)

0 commit comments

Comments
 (0)