SpringBoot 中的 Servlet Web 容器

Mr_Yan發表於2019-07-02

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 容器

相關文章