Using Tomcat in Production for Spring Boot apps

一世执手 提交于 2019-12-08 05:50:37

问题


As a novice with Spring Boot , I need to know the following as I could not find google results in a straight forward manner. What application servers do they really use for deploying those Spring Boot applications in real life? Is Tomcat really used by companies - if so do they achieve it using clustering?


回答1:


Spring boot has an inbuilt Tomcat server, it's simply run from Java.

The Tomcat is built into the jar, so it's the same in any environment.

Here is a typical spring boot jar, with the tomcat jars shown:

greg@greg-XPS-13-9360:~/work/boot-docker/target$ jar tvf boot-docker-1.0.3.jar | grep tomcat
  2293 Mon Jan 30 19:45:14 GMT 2017 BOOT-INF/lib/spring-boot-starter-tomcat-1.5.1.RELEASE.jar
241640 Tue Jan 10 21:03:52 GMT 2017 BOOT-INF/lib/tomcat-embed-websocket-8.5.11.jar
3015953 Tue Jan 10 21:03:50 GMT 2017 BOOT-INF/lib/tomcat-embed-core-8.5.11.jar
239791 Tue Jan 10 21:03:50 GMT 2017 BOOT-INF/lib/tomcat-embed-el-8.5.11.jar

We run our spring boot applications as docker images (complete virtual Unix server) on Redhat Openshift cloud, which is typical.

BTW Tomcat is used commercially and is very reliable.




回答2:


With Spring Boot application, you can generate an application jar which contains Embedded Tomcat. You can run a web application as a normal Java application.

If you still want to deploy your application with Tomcat Server.

First, you need to package a WAR application instead of a JAR. For this, you need to change pom.xml with the following content:

<profiles>    
         <profile>
            <id>war</id>
            <properties>
                <packaging.type>war</packaging.type>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
            <build>
                <finalName>ROOT</finalName>
            </build>
        </profile>
</profiles>

Let’s modify the final WAR file name in finalName element. (for my case, output is ROOT.war)

Next, to initialize the Servlet context required by Tomcat by implementing the SpringBootServletInitializer interface:

@SpringBootApplication
public class YourApplication extends SpringBootServletInitializer {
}

Then, execute Maven command package with war profile:

mvn clean package -Pwar


来源:https://stackoverflow.com/questions/53377757/using-tomcat-in-production-for-spring-boot-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!