1.win10环境下
springboot
应用可以打包成可运行的jar包运行,用java -jar myapp.jar
命令就可启动项目,但是在生产环境下,不可能启动项目就要开一个命令窗口,一般的方案是将应用设置成服务启动,如果打包成war
包形式,放在tomcat等web服务器里启动,则将tomcat设为服务即可,如果是jar
包,则该如何设置,spring
官方文档里推荐的是用winsw
这款工具。
安装使用
winsw的使用比较简单。从github上下载:winsw下载 下载以下两个文件即可
使用
将下载的文件放到同一个文件夹(将下载的两个文件 改为相同的文件名 我这改成了 filesf.exe 和filesf.xml)
- 1.为了方便,将打包的项目jar和winsw放在同一目录,我这里放在F盘
- 2.将
WinSW.NET4.exe
重命名为和bjapp-web.jar
同名:bjapp-web.exe
- 3.将
sample-minimal.xml
重命名为和bjapp-web.jar
同名:bjapp-web.xml,内容为:
-
<configuration>
<!-- ID of the service. It should be unique across the Windows system 系统服务id-->
<id>bjapp-web</id>
<!-- Display name of the service 显示的服务名称 -->
<name>bjapp-web Service</name>
<!-- Service description 服务说明 -->
<description>This service is a service created from a minimal configuration</description>
<!-- 应该启动的可执行文件的路径 例如/java/jdk1.8.1_181/bin/java.exe -->
<!-- 本机已安装java环境可直接执行 java -->
<executable>java</executable>
<!-- 执行参数 -->
<arguments>-jar bjapp-web.jar --server.port=1234</arguments>
<!-- 添加为开机启动 -->
<startmode>Automatic</startmode>
<!-- 日志保存 -->
<log mode="append">
<logpath>fileLog.log</logpath>
</log></configuration>
相关参数说明如下:
- id:id是安装成windows服务后的服务名,id必须是唯一的。
- name:name是服务的简写名字,name也必须是唯一的,这里我设为和id相同。
- description:服务的文字说明。
- executable:执行的命令,因为启动springboot应用的命令是
java -jar myapp.jar
,所以这里写java,如果java没有安装到系统path里,则这里必须全路径。 - arguments:命令执行参数, 如果端口号要在这里设置,可以在后面添上:
--server.port=8080
- 当然还有其它属性,请参考官网上的说明。
注册服务
配置完成 进入改文件夹(将jar包文件放入同一文件夹)
cmd 超级管理员打开 执行
bjapp-web
.exe install 添加服务
bjapp-web
.exe uninstall 删除服务
其它命令:
- uninstall:删除服务
- start:启动服务
- stop:停止服务
- restart:重启服务
- status:输出当前服务的状态
2.CentOS 7环境下 将jar包配置成服务
Linux下运行的软件我们通常把他注册为服务,这样我们就可以通过命令开启、关闭以及保持开机启动等功能。
若想使用此项功能,我们需要将代码中关于spring-boot-maven-plugin的配置修改为:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
主流的Linux大多使用init.d或systemd来注册服务
(1).基于Linux的Systemd部署(自己使用成功)
首先确定linux安装了 jdk(我的虚拟机安装路径为 /usr/local/java/jdk1.8.0_144/bin/java)
在/etc/systemd/system/目录下新建文件biapp-web.service,填入下面内容:
[Unit]
Description=bjapp-web
After=network.target[Service]
WorkingDirectory=/usr/local/tomcat
ExecStart=/usr/local/java/jdk1.8.0_144/bin/java -jar /usr/local/tomcat/bjapp-web.jar
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=ps -ef | grep doctor-pc-0.01| grep -v grep |awk '{print $2}' | xargs sudo kill -9[Install]
WantedBy=multi-user.target
注意,在实际使用中修改Description和ExecStart后面的内容
命令操作服务
启动服务:systemctl start bjapp-web或 systemctl start bjapp-web.service
停止服务:systemctl stop bjapp-web或 systemctl stop bjapp-web.service
服务状态:systemctl status bjapp-web或 systemctl status bjapp-web.service
开机启动:systemctl enable bjapp-web或 systemctl enable bjapp-web.service
项目日志:journalctl -u bjapp-web或 journalctl -u bjapp-web.service
(2)基于Linux的int.d部署(未实践过)
注册服务,在centOS 7终端执行:
sudo ln -s /var/apps/test-0.0.1-SNAPSHOT.jar /etc/init.d/bjapp-web.service
其中bjapp-web.service是我们的服务名。
启动服务:service bjapp-web.servicestart
停止服务:service bjapp-web.servicestop
服务状态:service bjapp-web.servicestatus
开机启动:chkconfig bjapp-web.serviceon
项目日志存放于/var/log/test.log下,可以用cat或tail等命令查看。