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 ?
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.
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.
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.
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).
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
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.
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
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:
myapp
application only.More detail can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With