SpringBoot支持哪些嵌入式Web服务器?默认使用哪一个?

Spring Boot 支持以下几种嵌入式Web服务器:

  1. Tomcat: Apache Tomcat 是Spring Boot默认使用的嵌入式容器。Tomcat是一个开放源代码的轻量级Web应用服务器,非常适合用于提供Java Servlet和JSP页面的环境。

  2. Jetty: Eclipse Jetty 是另一种流行的Web服务器和Servlet容器,它以轻量级和模块化而闻名。与Tomcat相比,Jetty通常用在更小的应用和微服务中。

  3. Undertow: Undertow 是一个来自WildFly(前称JBoss)社区的Web服务器。它是一个灵活的高性能Web服务器,提供阻塞和非阻塞API。

  4. Netty: 虽然Netty本身不是一个Web服务器,但它是一个异步事件驱动的网络应用框架,用于快速开发高性能、高可靠性的网络服务器和客户端应用程序。Spring WebFlux依赖于Netty作为其内嵌的容器。

默认情况下,如果你使用的是Spring Boot的起步Web依赖(spring-boot-starter-web),那么Spring Boot会使用Apache Tomcat作为应用的默认Web服务器。如果你想要使用Jetty或Undertow作为你的Web服务器,你可以通过排除Tomcat依赖然后添加对应的Jetty或Undertow的起步依赖来实现:

例如,如果想要使用Jetty而不是Tomcat,你可以在pom.xml中这样配置:

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

通过这样的配置,Spring Boot将会使用Jetty作为应用的嵌入式服务器。同样的,如果想用Undertow,只需要将spring-boot-starter-jetty替换为spring-boot-starter-undertow即可。

发表评论

后才能评论