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

后端 未结 9 2139
一向
一向 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: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.

提交回复
热议问题