1 概述
SpringBoot使得咱们能够快速地上手以及开发Spring项目。咱们能够把工程打成一个jar包,而后部署到服务器上(这里只讨论Linux,由于没多少人会拿Windows当服务器)。nohup命令可让程序做为后台进程执行,可是它很差管理维护,也显得很不专业。更好的方法是将SpringBoot做为Service启动。html
2 步骤
2.1 Maven打包
经过package命令打jar包:java
mvn clean package
这里注意一点,必定要将org.springframework.boot plugin添加到pom文件里面,其中“<executable>true</executable>”必定要加,标示该jar为可执行,不然机器启动SpringBoot服务会报错。plugin以下所示:spring
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> </build>
2.2 配置SpringBoot服务
要将程序注册成服务,必须保证jar有执行(下面例子中的x)的权限,不然服务没法启动。shell
[nightfield@mthf2mulsvr001 ~]$ ls -l total 10904 -rwx------. 1 nightfield nightfield 11164464 Mar 5 13:01 myApp.jar
有两种比较主流的方式配置springBoot服务bash
2.2.1 System V Init服务
System V Init服务都在目录/etc/init.d/下面。只需在此目录下建立一个到SpringBoot Jar的连接,就能够注册Service。假设咱们的jar的目录为/home/nightfield/myApp.jar:服务器
sudo ln -s /home/nightfield/myApp.jar /etc/init.d/myApp
这里,必须指定jar的绝对路径。 而后,咱们就能够经过以下命令来启动服务了:app
sudo service myApp start
这个服务以那个用户来运行,取决于jar包所属的用户。在该例子中,jar包属于用户nightfield,那么它将以nightfield用户来运行。maven
[nightfield@mthf2mulsvr001 ~]$ ps -ef | grep myApp nightfi+ 19741 1 0 13:44 ? 00:00:00 /bin/bash /home/nightfield/myApp.jar nightfi+ 19756 19741 99 13:44 ? 00:00:16 /usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -jar /home/nightfield/myApp.jar nightfi+ 19851 7759 0 13:45 pts/0 00:00:00 grep --color=auto myApp
能够看到,应用正以nightfield用户跑在后台,其实服务只是如下命令的包装:spring-boot
/usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -jar /home/nightfield/myApp.jar
同时,服务对应的PID会放在/var/run/myApp/myApp.pid,而程序运行的日志则放在/var/log/myApp.log。ui
2.2.2 Systemd服务
Systemd服务的目录在/etc/systemd/system/,咱们须要在此目录下建立一个名叫myApp.service的文件,并将以下内容写入文件:
[Unit] Description=My Spring Boot Service After=syslog.target [Service] User=nightfield ExecStart=/home/nightfield/myApp.jar SuccessExitStatus=143 [Install] WantedBy=multi-user.target
这里要把ExecStart和Descriptino改为本身的,把ExecStart指定到jar所在的目录,同样,也须要文件的绝对路径。同时别忘了设置myApp.service的执行权限。 服务启动命令为:
sudo systemctl start myApp
将服务设置为开机启动:
sudo systemctl enable myApp
Systemd做为后起之秀,功能更增强大,支持的命令和参数也更多,具体能够参考这里。
3 自定义JVM参数
若是是用java -jar的方式启动的java应用,咱们能够直接在命令行中指定JVM参数,那以Service形式启动的Java程序,该如何指定JVM参数呢? 通常,咱们在用maven编jar包的时候,能够指定JVM参数,好比用以下方式:
mvn clean package -DargLine="-Xmx1024m"
可是若是咱们但愿在服务器上独立额外设置一些参数呢? 其实也很简单,在启动SpringBoot服务以前,会先去jar包所在的同级目录下查找,有没有此jar的同名配置文件。在这里,咱们只须要在/home/nightfield/目录下,添加一个叫myApp.conf的配置文件(名字要和jar的名字相同),在文件里面自定义JVM参数JAVA_OPTS:
[nightfield@mthf2mulsvr001 ~]$ pwd /home/nightfield [nightfield@mthf2mulsvr001 ~]$ ls -l total 27532 -rwx------. 1 nightfield nightfield 39 Mar 5 14:10 myApp.conf -rwx------. 1 nightfield nightfield 28186505 Mar 5 13:12 myApp.jar [nightfield@mthf2mulsvr001 ~]$ cat myApp.conf export JAVA_OPTS="-Xmx4096m -Xms4096m" [nightfield@mthf2mulsvr001 ~]$
添加配置文件以后,重启服务,再次查看服务进程:
[nightfield@mthf2mulsvr001 bin]$ ps -ef | grep myApp nightfi+ 11343 1 0 14:13 ? 00:00:00 /bin/bash /homt/nightfield/myApp.jar nightfi+ 11358 11343 48 14:13 ? 00:00:38 /usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -Xmx4096m -Xms4096m -jar /homt/nightfield/myApp.jar nightfi+ 11908 11884 0 14:14 pts/0 00:00:00 grep --color=auto myApp
能够看到,Java进程的启动参数上多了“-Xmx4096m -Xms4096m”。
4 总结
本文介绍了将SpringBoot在Linux下做为服务启动的两种方式,同时介绍了自定义JVM启动参数的方法。
5 参考
Spring Boot Application as a Service Deploying Spring Boot Applications