How to run spring boot jar in production environment?

Spring's preferred method of deploying downloads is through the jar executable, which contains tomcat inside.

It java -jar myapp.jar with a simple java -jar myapp.jar .

Now I want to deploy this jar on my Linux server on EC2, am I missing something or do I really need to create an init script to run the application correctly as a daemon?

If I just call java -jar , the application dies when I log out.

I could run it on screen or nohup, but it is not very elegant, and rebooting on my server will force me to log in and start the process manually.

So, is there something already for the task in spring boot?

+55
java spring linux spring-boot
Apr 05 '14 at 19:59
source share
9 answers

Note that with Spring Boot 1.3.0.M1 you can create fully executable jars using maven and gradle.

For maven just include the following in your pom.xml

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

For gradle, add the following snippet to your build.gradle

 springBoot { executable = true } 

These fully executable jars contain an additional script at the beginning of the file that allows you to simply symbolize your Spring boot jar for init.d or use the systemd script

init.d example:

 $ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp 

This allows you to start, stop, and restart the application, for example:

 $/etc/init.d/yourapp start|stop|restart 

Or use systemd script

 [Unit] Description=yourapp After=syslog.target [Service] ExecStart=/var/yourapp/yourapp.jar [Install] WantedBy=multi-user.target 

Additional information at the following link: http://docs.spring.io/spring-boot/docs/1.3.0.BUILD-SNAPSHOT/reference/htmlsingle/#deployment-service

+45
Jul 27 '15 at 13:45
source share

There are two initializers in my Spring boot application. One for development, another for production. For development, I use the main method as follows:

 @SpringBootApplication public class MyAppInitializer { public static void main(String[] args) { SpringApplication.run(MyAppInitializer .class, args); } } 

My desktop initializer extends SpringBootServletInitializer and looks like this:

 @SpringBootApplication public class MyAppInitializerServlet extends SpringBootServletInitializer{ private static final Logger log = Logger .getLogger(SpringBootServletInitializer.class); @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder builder) { log.trace("Initializing the application"); return builder.sources(MyAppInitializerServlet .class); } } 

I use gradle, and the WAR plugin is used in the build.gradle file. When I run it in the development environment, I use the bootrun task. Where, when I want to deploy it for production, I use the collect task to create a WAR and deploy.

I can work as a regular Spring application in production without discounting the benefits provided by the built-in cat during development. Hope this helps.

+3
Apr 28 '15 at 19:52
source share

In a production environment, you want your application to start again when you restart your computer, etc., by creating /etc/init.d/ script and contacting the appropriate launch level to start and stop, this is the right approach. Spring Boot will not extend to this, since this is a specific setting of the operating system, as well as tons of other parameters, you want it to work in the chroot prison, whether to stop / start in front of other software, etc.

+2
Apr 08 '15 at 12:52
source share

If you use gradle, you can just add this to your build.gradle

 springBoot { executable = true } 

Then you can launch the application by typing. / your -app.jar

In addition, you can find a complete guide to setting up your application as a service.

56.1.1 Installing as an init.d service (System V)

http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

amuses

+2
Dec 24 '16 at 1:46
source share

To date, the easiest and most reliable way to run Spring Boot applications in production is Docker. Use Docker Compose, Docker Swarm or Kubernetes if you need to use several connected services.

Here is a simple Dockerfile from the official Spring Docker Download Guide to get started:

 FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp ADD YOUR-APP-NAME.jar app.jar RUN sh -c 'touch /app.jar' ENV JAVA_OPTS="" ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ] 

Here is an example command line to start the container as a daemon:

 docker run \ -d --restart=always \ -e "SPRING_PROFILES_ACTIVE=prod" \ -p 8080:8080 \ prefix/imagename 
+2
May 26 '17 at 15:35
source share

I am launching applications that I want to run persistently or at least semi-permanently through the -dmS NAME / path / to / script screen. As far as I know, this is the most elegant solution.

+1
Apr 6 '14 at 20:41
source share

You can use the Supervisor app. In the supervisor configuration, you can define several services and how to execute them.

For Java and Spring boot applications, the command will be java -jar springbootapp.jar .

Parameters can be provided to always keep the application running. Therefore, if restarting EC2 then Supervisor will restart your application

I found Supervisor easy to use compared to installing startup scripts in /etc/init.d/. Startup scripts freeze or go to standby in the event of errors.

+1
Mar 25 '16 at 11:34
source share

It's simple, you can use the w760 plugin to download maven to complete the code deployment.

Plugin Configuration:

 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debug.port} </jvmArguments> <profiles> <profile>test</profile> </profiles> <executable>true</executable> </configuration> </plugin> 

And, jvmArtuments adds jvm for you. profiles will select the profile to launch your application. executable can make your application run aimlessly.

and if you add mvnw to your project or you have maven enveriment. You can simply call ./mvnw spring-boot:run for mvnw or mvn spring-boot:run for maven.

0
Nov 08 '16 at 15:49
source share

On Windows without maintenance.

start.bat

 @ECHO OFF call run.bat start 

stop.bat:

 @ECHO OFF call run.bat stop 

run.bat

 @ECHO OFF IF "%1"=="start" ( ECHO start myapp start "myapp" java -jar -Dspring.profiles.active=staging myapp.jar ) ELSE IF "%1"=="stop" ( ECHO stop myapp TASKKILL /FI "WINDOWTITLE eq myapp" ) ELSE ( ECHO please, use "run.bat start" or "run.bat stop" ) pause 
0
Sep 18 '17 at 10:28 on
source share



All Articles