12

Have a Spring Boot (1.5.4.RELEASE) based microservice which I deploy a jar to an AWS EC Instance (Linux environment). Now, I am also deploying an external log4j.properties file so I have to start the microservice like this:

java -jar myapp.jar -Dlogging.config=/path/to/log4j.properties

How can I configure this Spring Boot Microservice as a Linux service where I can start and stop it using these flags:

sudo service myapp start | stop | status | restart

Thank you very much.

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • you need to write a java service wrapper. this example could be useful. http://www.jcgonzalez.com/linux-java-service-wrapper-example – edt Sep 15 '17 at 08:52
  • 1
    This is all pretty well explained in the official documentation at https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-initd-service – Val Sep 15 '17 at 11:37
  • I hope the answer you will get here https://stackoverflow.com/questions/21503883/spring-boot-application-as-a-service – Jaffer Wilson Sep 15 '17 at 13:47
  • Is your question about stopping and starting it on Linux, externalising the properties, or both ? – PaulNUK Sep 20 '17 at 15:43

2 Answers2

5

Using a symbolic link to your springboot app you can make it controllable as service...

sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp

Placing an application.properties into your myapp folder you can override the one bundled inside your app. This way you don't need to use command line switches. Simply specify the path to your log configuration as value to property key logging.config inside of it.

NOTE, though, that this solution is not really best practice. Once you're running a whole bunch of services in production, you probably rather want to go for something along the lines of spring cloud config for externalizing configuration and you probably also want your logs aggregated at a centralized service that allows for an overview of all your services' logs in one place.

Jörg
  • 2,434
  • 23
  • 37
1

As per spring-boot deployment,

A fully executable jar can be executed like any other executable binary or it can be registered with init.d or systemd

Make sure you build your app using the plug-in below (gradle version in shared link):

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

and as shown by Jörg, create a symbolic link inside init.d:

sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp

That is the simplified version :)

More to your question, you need to customize the init and this can be done by a conf file - all specified in the documentation.

With the exception of JARFILE and APP_NAME, the settings can be configured using a .conf file. The file is expected next to the jar file and have the same name but suffixed with .conf rather than .jar. For example, a jar named /var/myapp/myapp.jar will use the configuration file named /var/myapp/myapp.conf.

such as: myapp.conf

JAVA_OPTS=-Xmx1024M
LOG_FOLDER=/custom/log/folder
JC Carrillo
  • 986
  • 6
  • 17