[toc]

Register the Spring Boot application as a system service

In previous articles, we covered a lot of Spring Boot tips, so how do we run Spring Boot applications in a generated environment once we’ve created them? If you simply run the application as a raw Java-JAR, the robustness and stability of the application cannot be guaranteed. The best way is to register the application as a service for use.

This article explains how to register a Spring Boot application as a Linux and Windows service.

preparation

First we need to package the application as an executable JAR package. We need to add the following dependencies:

<packaging>jar</packaging>
 
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
</parent>
 
<dependencies>.</dependencies>
 
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>
Copy the code

For packaging, we need to select the JAR. Spring-boot-maven-plugin was added to package the app as an executable JAR package.

Package as an executable JAR package

With the application written, we can perform:

mvn clean package
Copy the code

Springboot-run-as-service-0.0.1 – snapshot.jar = springboot-run-as-service-0.0.1- snapshot.jar = springboot-run-as-service-0.0.1- snapshot.jar = springboot-run-as-service-0.0.1- snapshot.jar

Register as a Liunx service

In Linux, we can choose between System V init script or Systemd configuration files, and the former is gradually being replaced by the latter.

To be safe, we need to create a run user and give the jar package the appropriate permissions:

$ sudo useradd flydean
$ sudo passwd flydean
$ sudo chown flydean:flydean your-app.jar
$ sudo chmod 500 your-app.jar
Copy the code

System V Init

Create a file link to the init.d directory as follows:

sudo ln -s /path/to/your-app.jar /etc/init.d/your-app
Copy the code

Now we can start the application:

sudo service your-app start
Copy the code

The service command supports start, stop, restart, and status. It also provides the following functions:

  • Your-app will launch as the Flydean user
  • The pid of the program run is stored in /var/run/your-app/your-app.pid
  • The application log is in /var/log/your-app.log

Systemd

To use Systemd, we need to create a your-app.service file in /etc/systemd/system:

[Unit]
Description=A Spring Boot application
After=syslog.target
 
[Service]
User=flydean
ExecStart=/path/to/your-app.jar SuccessExitStatus=100 
 
[Install] 
WantedBy=multi-user.target
Copy the code

Then we can use systemctl start | stop | restart | status your – app to manage your services.

Upstart

Upstart is an event-driven service manager that will be installed by default if you use Ubuntu.

Let’s create a your-app.conf:

# Place in /home/{user}/.config/upstart
 
description "Some Spring Boot application"
 
respawn # attempt service restart if stops abruptly
 
exec java -jar /path/to/your-app.jar
Copy the code

Install on Windows

In Windows, we also have a number of ways, as follows:

Windows Service Wrapper

Windows Service Wrapper (also called Winsw) is an open source software that needs to be used with a configuration file called your-app.xml:

<service>
    <id>MyApp</id>
    <name>MyApp</name>
    <description>This runs Spring Boot as a Service.</description>
    <env name="MYAPP_HOME" value="%BASE%"/>
    <executable>java</executable>
    <arguments>-Xmx256m -jar "%BASE%\your-app.jar"</arguments>
    <logmode>rotate</logmode>
</service>
Copy the code

Note that you need to modify winsw.exe to be your-app.exe to work with your-app.xml.

Java Service Wrapper

Java Service Wrapper provides a very powerful configuration that can be used by your applications under Windows and Linux. Students who are interested can study on their own.

See flydean’s blog for more tutorials