How do I run a spring boot executable jar in a Production environment?

后端 未结 9 2109
一向
一向 2020-12-02 04:19

Spring boot\'s preferred deployment method is via a executable jar file which contains tomcat inside.

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

相关标签:
9条回答
  • 2020-12-02 04:46

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

    0 讨论(0)
  • 2020-12-02 04:54

    If you are using gradle you can just add this to your build.gradle

    springBoot {
        executable = true
    }
    

    You can then run your application by typing ./your-app.jar

    Also, you can find a complete guide here to set up your app as a service

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

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

    cheers

    0 讨论(0)
  • 2020-12-02 04:55

    My Spring boot application has two initializers. One for development and another for production. For development, I use the main method like this:

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

    My Initializer for production environment extends the 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 my build.gradle file applies 'WAR' plugin. When I run it in the development environment, I use bootrun task. Where as when I want to deploy it to production, I use assemble task to generate the WAR and deploy.

    I can run like a normal spring application in production without discounting the advantages provided by the inbuilt tomcat while developing. Hope this helps.

    0 讨论(0)
  • 2020-12-02 04:55

    This is a simple, you can use spring boot maven plugin to finish your code deploy.

    the plugin config like:

    <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, the jvmArtuments is add for you jvm. profiles will choose a profile to start your app. executable can make your app driectly run.

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

    0 讨论(0)
  • 2020-12-02 04:59

    On Windows OS without Service.

    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 讨论(0)
  • 2020-12-02 05:01

    In a production environment you want your app to be started again on a machine restart etc, creating a /etc/init.d/ script and linking to the appropriate runlevel to start and stop it is the correct approach. Spring Boot will not extend to covering this as it is a operating system specific setup and the are tonnes of other options, do you want it running in a chroot jail, does it need to stop / start before some other software etc.

    0 讨论(0)
提交回复
热议问题