SpringBoot 中的 Servlet Web 容器

1.前言

SpringBoot支持一下嵌入式Servlet容器:
SpringBoot 中的Servlet Web容器

SpringBoot 2.0.3.RELEASE需要Java 8或9以及Spring Framework 5.0.7.RELEASE或更高版本。为Maven 3.2+和Gradle 4提供了明确的构建支持。

2.使用Tomcat

SpringBoot默认的Servlet容器是Tomcat,无需额外配置,只需要引入spring-boot-starter-web依赖。

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

启动Application运行截图如下:
SpringBoot 中的Servlet Web容器

3.使用Jetty

SpringBoot将默认的Tomcat切换为Jetty,步骤如下。

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
</dependencies>

启动Application运行截图如下:
SpringBoot 中的Servlet Web容器

3.1.为什么要使用Jetty

Google App Engine放弃了Tomcat而选择Jetty。主要是因为Jetty相比Tomcat更小更灵活,在云中服务器大小非常重要,因为可能在10秒内运行几千个Jetty实例,如果每个服务器能省下1M,那么10秒内就会省下GB量级的内存。

4.使用Undertow

SpringBoot将默认的Tomcat切换为Undertow,步骤如下。

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
</dependencies>

启动Application运行截图如下:

SpringBoot 中的 Servlet Web 容器

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!