Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Extern log4j.properties file with Spring Boot Microservice and Run It As a Linux Service?

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.

like image 430
PacificNW_Lover Avatar asked Sep 12 '17 19:09

PacificNW_Lover


2 Answers

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.

like image 116
Jörg Avatar answered Sep 22 '22 10:09

Jörg


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
like image 23
JC Carrillo Avatar answered Sep 20 '22 10:09

JC Carrillo