SpringBoot在linux系统中以服务形式启动

硅谷探秘者 Md java基础,springboot 1349 0 0

  SpringBoot使得我们可以快速地上手以及开发Spring项目。我们可以把工程打成一个jar包,然后部署到服务器上(这里只讨论Linux,因为没多少人会拿Windows当服务器)。nohup命令可以让程序作为后台进程执行,但是它不好管理维护,也显得很不专业。更好的方法是将SpringBoot作为Service启动。

一、Maven打包

pom.xml配置文件中添加

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

  其中<executable>true</executable>一定要加,标示该jar为可执行,否则机器启动SpringBoot服务会报错。

二、配置springboot服务

  要将程序注册成服务,必须保证jar有执行(下面例子中的x)的权限,否则服务无法启动。

System V Init服务

  System V Init服务都在目录/etc/init.d/下面。只需在此目录下创建一个到SpringBoot Jar的链接,就可以注册Service。假设我们的jar的目录为

/usr/local/project/test/myApp.jar

执行命令:

sudo ln -s /usr/local/project/test/myApp.jar /etc/init.d/myApp

这里,必须指定jar的绝对路径。
然后,我们就可以通过如下命令来启动服务了:

sudo service myApp start

[root@localhost test]# ps -ef | grep myApp
root       1945      1 16 12:23 pts/0    00:00:09 /bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -jar /usr/local/project/test/myApp.jar
root       1980   1754  0 12:24 pts/0    00:00:00 grep --color=auto myApp
[root@localhost test]#

  同时,服务对应的PID会放在/var/run/myApp/myApp.pid,而程序运行的日志则放在/var/log/myApp.log

Systemd服务

Systemd服务的目录在/etc/systemd/system/,我们需要在此目录下创建一个名叫myApp.service的文件,并将如下内容写入文件:

[Unit]
Description=My Spring Boot Service
After=syslog.target

[Service]
User=root
ExecStart=/usr/local/project/test/myApp.jar SuccessExitStatus=143 

[Install] 
WantedBy=multi-user.target

这里要把ExecStartDescriptino以及User改成自己的,把ExecStart指定到jar所在的目录,一样,也需要文件的绝对路径。同时别忘了设置myApp.service的执行权限。

服务的启动命令

sudo systemctl start myApp

如果报错:

[root@localhost system]# systemctl start myApp
Warning: myApp.service changed on disk. Run 'systemctl daemon-reload' to reload units.

则执行systemctl daemon-reload即可

如果User没有权限则会报错:

12月 21 12:36:14 localhost.localdomain systemd[1]: myApp.service: main process exited, code=exited, status=217/USER
12月 21 12:36:14 localhost.localdomain systemd[1]: Unit myApp.service entered failed state.
12月 21 12:36:14 localhost.localdomain systemd[1]: myApp.service failed.

则需要修改myApp.service配置文件中 Service->User 修改成正确的用户即可

将服务设置为开机启动:

sudo systemctl enable myApp

三、自定义JVM参数

  如果是用java -jar的方式启动的java应用,我们可以直接在命令行中指定JVM参数,那以Service形式启动的Java程序,该如何指定JVM参数呢?
  一般,我们在用maven编jar包的时候,可以指定JVM参数,比如用如下方式:

mvn clean package -DargLine="-Xmx512m -Xms512m"

  但是如果我们希望在服务器上独立额外设置一些参数呢?
  其实也很简单,在启动SpringBoot服务之前,会先去jar包所在的同级目录下查找,有没有此jar的同名配置文件。
  在这里,我们只需要在 /usr/local/project/test/ 目录下,添加一个叫myApp.conf 的配置文件(名字要和jar的名字相同),在文件里面自定义JVM参数JAVA_OPTS

export JAVA_OPTS="-Xmx512m -Xms512m"

重启服务:

[root@localhost test]# systemctl restart myApp
[root@localhost test]# ps -ef | grep myApp
root       2487      1  0 12:58 ?        00:00:00 /bin/bash /usr/local/project/test/myApp.jar SuccessExitStatus=143
root       2504   2487 91 12:58 ?        00:00:07 /usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -Xmx512m -Xms512m -jar /usr/local/project/test/myApp.jar SuccessExitStatus=143
root       2536   1754  0 12:58 pts/0    00:00:00 grep --color=auto myApp
[root@localhost test]#

可以看到,Java进程的启动参数上多了-Xmx512m -Xms512m

参考:https://nightfield.com.cn/index.php/archives/93/
参考:Deploying Spring Boot Applications


评论区
请写下您的评论...
暂无评论...
猜你喜欢
springboot,java基础 1218   从springboot的文档知道,springboot打包一个可systemV直接执行的jar文件。操作也很简单,只需要pom.xml加入
weblog 1139 centos7版本部署的tomcat器,需要将8080端口放开执行如下命令:firewall-cmd--zone=public--add-port=8080/tcp--permanentfirewall
其他 4096 -yhttpd.x86_642.线安装安装命令:yum-yinstallhttpd开apache:systemctlstarthttpd.service设置apache开机:systemctlenablehttp
linux 1013 linux运行级别  老的Linux发行版本运行分成不同的运行级别(runlevel),不同的级别所搭配有所不同。较新的Linux发行版本,比如CentOS7+,已经将运行级别替
框架 1280 activemq下载地址:http://activemq.apache.org/activemq-5140-release.html把下载的tar.gz文件放linux的/opt/文件夹下,解
official 730   chkconfig命令主要用来更新(或停止)和查询的运行级信息。谨记chkconfig不是立即自禁止或激活一个,它只是简单的改变了符号连接。 语法: chkconfig
linux系统 1836 软件版本:一、linux版本:CentOS-7-x86_64-DVD-1708.iso下载地址:http://mirror.nsc.liu.se/centos-store/7.4.1708
框架 1549 安装环境::centos7java环境:jdk1.8版本:elasticsearch-7.3.2elasticsearch下载官网:https://www.elastic.co/cn
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。