Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot application as a service + VM Options

I have a Spring boot application that is started as a service using Linux's systemd.

It is based on this documentation : http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

With the default script, the jar file start. It works fine.

/etc/systemd/system/myapp.service :

[Unit]
Description=myapp
After=syslog.target

[Service]
User=myapp
ExecStart=/var/myapp/myapp.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Now I want to add VM Option when the jar start. I tried to add a .conf file to the project but it doesn't work.

/var/myapp/myapp.conf :

JAVA_OPTS=-Xms256M -Xmx512M

How can I add JVM option to start the application with systemd ?

like image 784
YLombardi Avatar asked Jul 29 '16 14:07

YLombardi


People also ask

What is the use of @service in spring boot?

It is used to mark the class as a service provider. So overall @Service annotation is used with classes that provide some business functionalities. Spring context will autodetect these classes when annotation-based configuration and classpath scanning is used.

Can I run a Spring Boot application as a service?

This article explores some options of running Spring Boot applications as a service. Firstly, we are going to explain web applications' packaging options and system services. In the subsequent sections, we explore different alternatives we have when setting up a service for both Linux as Windows based systems.

How to configure custom JVM properties in Spring Boot?

If you want to configure custom JVM properties or Spring Boot application run arguments, then you can create a .conf file with the same name as the Spring Boot application name and place it parallel to jar file. Considering that your-app.jar is the name of your Spring Boot application, then you can create the following file.

How to set up systemd services in Spring Boot?

Systemd is the latest and being used by many modern Linux distributions.Setting up systemd services is really easy.Let’s assume that we have installed our Spring Boot application on /var/rest-app .To create systemd service. Create a script with name your-app.service (rest-app.service).

How to deploy a Spring Boot application with Maven?

You can deploy the app (for example, with a Maven plugin) by adding the project ID to the build configuration, as shown in the following example: Then deploy with mvn appengine:deploy (if you need to authenticate first, the build fails). 2. Installing Spring Boot Applications


Video Answer


3 Answers

I finally found a solution here : how to configure heap size when start a spring-boot application with embedded tomcat?

The content of my .conf file was wrong. I need too write this :

export JAVA_OPTS="-Xms256m -Xmx512m"

Now when I run "service myapp start", it start with the good heap size.

like image 129
YLombardi Avatar answered Oct 18 '22 00:10

YLombardi


According to the documentation you can simply add an environment variable JAVA_OPTS if that's enough for you.

The way we start the apps with custom ENV variables and systemd would look like this for your project:

[Unit]
Description=myapp
After=syslog.target

[Service]
User=myapp
ExecStart=source /var/myapp/myapp.conf; java -jar /var/myapp/myapp.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Basically sourcing the ENV config directly will expose the ENV variables to the application

like image 2
Ulises Avatar answered Oct 17 '22 23:10

Ulises


Another good way is to create a configuration file myapp.conf in the same directory with the myapp.jar file

/var/myapp/

and put the following content:

JAVA_OPTS="-Xms256m -Xmx512m"

Then restart the application.

Note that the name of configuration file should be the same with the jar file except the extension, .conf instead of .jar or .war.

This way have some other advantages:

  • Keep the settings separately for the myapp application only.
  • We can have additional parameters in the file too.

More detail can be found here.

like image 1
Tom Avatar answered Oct 17 '22 23:10

Tom