面试题:Spring Boot 支持哪些嵌入 Web 容器?

Spring Boot 支持多种嵌入式的 Web 容器,开发者可以根据需求选择合适的容器来运行应用。以下是 Spring Boot 支持的主要嵌入式 Web 容器:


1. Tomcat

  • 默认容器:Spring Boot 的 spring-boot-starter-web 默认使用 Tomcat 作为嵌入式 Web 容器。
  • 特点
    • 轻量级、性能优秀。
    • 支持 Servlet 和 JSP。
    • 广泛使用,社区支持强大。
  • 依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

2. Jetty

  • 特点
    • 轻量级,启动速度快。
    • 适合资源受限的环境。
    • 支持异步处理和 WebSocket。
  • 依赖
    如果需要使用 Jetty,需要排除默认的 Tomcat 依赖,并引入 Jetty 依赖:
    <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>

3. Undertow

  • 特点
    • 高性能,支持异步和非阻塞 I/O。
    • 内存占用低,适合高并发场景。
    • 支持 WebSocket 和 HTTP/2。
  • 依赖
    如果需要使用 Undertow,需要排除默认的 Tomcat 依赖,并引入 Undertow 依赖:
    <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>

4. Reactor Netty(用于 WebFlux)

  • 特点
    • 专为响应式编程设计,支持非阻塞 I/O。
    • 适用于 Spring WebFlux 应用。
    • 高性能,适合高并发场景。
  • 依赖
    如果使用 Spring WebFlux,默认使用 Reactor Netty:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

5. 其他容器

  • Spring Boot 还支持其他嵌入式容器,例如:
    • Undertow:高性能,支持异步和非阻塞 I/O。
    • Jetty:轻量级,启动速度快。
    • Tomcat:默认容器,广泛使用。

如何切换 Web 容器?

Spring Boot 默认使用 Tomcat,但可以通过修改 Maven 或 Gradle 依赖来切换为 Jetty 或 Undertow。以下是切换步骤:

切换到 Jetty:

  1. 排除默认的 Tomcat 依赖。
  2. 添加 Jetty 依赖。

切换到 Undertow:

  1. 排除默认的 Tomcat 依赖。
  2. 添加 Undertow 依赖。

示例:切换到 Undertow

Maven 配置:

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

运行 HTML

Gradle 配置:

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    implementation 'org.springframework.boot:spring-boot-starter-undertow'
}

总结:

Spring Boot 支持多种嵌入式 Web 容器,包括 Tomcat(默认)、JettyUndertow 和 Reactor Netty。开发者可以根据应用的需求(如性能、资源占用、功能支持等)选择合适的容器。通过简单的依赖配置,可以轻松切换 Web 容器,而无需修改代码。这种灵活性是 Spring Boot 的重要优势之一。

THE END
点赞11 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容