SpringBoot如何切换成其它的嵌入式Servlet容器(Jetty和Undertow)

目录
  • 如何切换成其它的嵌入式Servlet容器(Jetty和Undertow)
    • SpringBoot默认使用的内置Servlet容器是Tomcat
    • SpringBoot还支持Jetty和Undertow
  • SpringBoot web开发_嵌入式Servlet容器
    • 1、切换嵌入式Servlet容器
    • 2、定制Servlet容器

如何切换成其它的嵌入式Servlet容器(Jetty和Undertow)

SpringBoot默认使用的内置Servlet容器是Tomcat

然而 SpringBoot还支持其它的Servlet容器 默认的Tomcat只是其中一种

SpringBoot还支持Jetty和Undertow

  • Jetty适合用于长链接应用 例如聊天
  • Undertow是一个高性能非阻塞的Servlet容器 并发性能非常好 但不支持jsp

若要切换 只需要在pom文件中排除自带的Tomcat启动器 再引入其它Servlet容器的启动器即可

spring-boot-starter-web里面引入了spring-boot-starter-tomcat 因此默认使用Tomcat

因此 只需排除原先的Tomcat 再引入要添加的Servlet容器的依赖 即可:

切换成Jetty:

<!-- 引入Web模块 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- 排除tomcat容器 -->
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>
<!-- 引入其它的Servlet容器 -->
<dependency>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>

Jetty启动成功

同理 切换成undertow:

<!-- 引入Web模块 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- 排除tomcat容器 -->
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>
<!-- 引入其它的Servlet容器 -->
<dependency>
    <artifactId>spring-boot-starter-undertow</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>

Undertow启动成功

SpringBoot web开发_嵌入式Servlet容器

1、切换嵌入式Servlet容器

1.1、SpringBoot支持的Servlet种类及其切换

1)默认支持的webServer:Tomcat、Jrtty、Undertow

2)切换服务器

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

1.2、原理

1)SpringBoot应用启动发现当前是Web应用。web场景包-导入tomcat

2)web应用会创建一个web版的ioc容器 ServletWebServerApplicationContext

3)ServletWebServerApplicationContext  启动的时候寻找 ServletWebServerFactory(Servlet 的web服务器工厂---> Servlet 的web服务器)

4)SpringBoot底层默认有很多的WebServer工厂;

  • TomcatServletWebServerFactory
  • JettyServletWebServerFactory
  • UndertowServletWebServerFactory

5)底层直接会有一个自动配置类。

ServletWebServerFactoryAutoConfiguration导入了ServletWebServerFactoryConfiguration(配置类)

6)ServletWebServerFactoryConfiguration 配置类 根据动态判断系统中到底导入了那个Web服务器的包。(默认是web-starter导入tomcat包),容器中就有 TomcatServletWebServerFactory

7)TomcatServletWebServerFactory 创建出Tomcat服务器并启动;TomcatWebServer 的构造器拥有初始化方法initialize---this.tomcat.start();

8)内嵌服务器,就是手动把启动服务器的代码调用(tomcat核心jar包存在)

2、定制Servlet容器

2.1、修改配置文件

通过对application.properties配置文件的设置,定制Servlet容器

#修改servlet容器
server.port=8000
#server.undertow.accesslog.dir=/tmp

2.2、实现 WebServerFactoryCustomizer

xxxxxCustomizer:定制化器,可以改变xxxx的默认规则

通过WebServerFactoryCustomizer修改 Servlet 容器的响应端口号:

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(9000);
    }
}

2.3、ConfigurableServletWebServerFactory

直接自定义 ConfigurableServletWebServerFactory 的接口实现类

public interface ConfigurableWebServerFactory extends WebServerFactory, ErrorPageRegistry {
    void setPort(int port);
    void setAddress(InetAddress address);
    void setErrorPages(Set<? extends ErrorPage> errorPages);
    void setSsl(Ssl ssl);
    void setSslStoreProvider(SslStoreProvider sslStoreProvider);
    void setHttp2(Http2 http2);
    void setCompression(Compression compression);
    void setServerHeader(String serverHeader);
    default void setShutdown(Shutdown shutdown) {
    }
}

ConfigurableServletWebServerFactory 接口实现类(框架中默认实现类):

通过自定义 ConfigurableServletWebServerFactory 接口的实现类,即可定制Servlet容器

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • SpringBoot配置嵌入式Servlet容器和使用外置Servlet容器的教程图解

    配置嵌入式Servlet容器 SpringBoot默认使用Tomcat作为嵌入式的Servlet容器: 问题? 1).如何定制和修改Servlet容器的相关配置: 1.修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCustomizer]): server.port=8081 server.context-path=/crud server.tomcat.uri-encoding=UTF-8 //通用的Servlet容器设置 s

  • springboot2.3.1替换为其他的嵌入式servlet容器的详细方法

    现阶段,springboot内嵌了Tomcat服务器,如果你不想使用Tomcat,springboot也是支持其他的服务器切换的. 如果你想了解底层springboot所支持的服务器你可以使用idea的快捷键快速按两次shift查询一个ServerProperties 的类,通过这个类你可以知道你想要了解的情况: springboot里面支持的服务器有Jetty.Netty-等等,大家有兴趣的话可以百度一下. 接着通过在pom文件的视图依赖分析可以得知: springboot里面的Tomcat是

  • Spring Boot如何支持嵌入式Servlet容器

    Spring Boot支持Tomcat.Jetty和Undertow三种Servlet容器嵌入到Web应用程序中,开发者使用starter即可方便嵌入,默认情况下,嵌入服务器的访问端口为8080. Servlets.Filters & Listeners 这些组件可以同组件扫描注册,即把他们定义为Spring Bean. 默认情况下,如果只有一个servlet,则把它映射到/:如果有多个servlet,则加上bean name作为前缀然后映射到/*. 如果默认策略不能满足你,你可以通过Servl

  • SpringBoot如何切换成其它的嵌入式Servlet容器(Jetty和Undertow)

    目录 如何切换成其它的嵌入式Servlet容器(Jetty和Undertow) SpringBoot默认使用的内置Servlet容器是Tomcat SpringBoot还支持Jetty和Undertow SpringBoot web开发_嵌入式Servlet容器 1.切换嵌入式Servlet容器 2.定制Servlet容器 如何切换成其它的嵌入式Servlet容器(Jetty和Undertow) SpringBoot默认使用的内置Servlet容器是Tomcat 然而 SpringBoot还支持

  • SpringBoot嵌入式Servlet容器与定制化组件超详细讲解

    目录 嵌入式Servlet容器 1.原理分析 2.Servlet容器切换 3.定制Servlet容器配置 定制化组件 嵌入式Servlet容器 在Spring Boot中,默认支持的web容器有 Tomcat, Jetty, 和 Undertow 1.原理分析 那么这些web容器是怎么注入的呢?我们一起来分析一下 当SpringBoot应用启动发现当前是Web应用,它会创建一个web版的ioc容器ServletWebServerApplicationContext 这个类下面有一个createW

  • springboot配置嵌入式servlet容器的方法

    配置嵌入式Servlet容器 springboot默认tomcat为嵌入式servlet容器,所以不用在配置tomcat. 1.如何定制修改servlet容器? 1.在applicatio.properties里修改和server有关的配置(推荐) 如: server.tomcat server.tomcat.connection-timeout= 连接超时时间 server.tomcat.uri-encoding=UTF-8 修改编码 server.servlet.XXX 通用servlet容

  • Spring Boot 中嵌入式 Servlet 容器自动配置原理解析

    目录 1.参照 Spring Boot 自动配置包里面的web模块 2.EmbeddedServletContainerFactory(嵌入式Servlet容器工厂) 3.EmbeddedServletContainer(嵌入式的Servlet容器) 4.以TomcatEmbeddedServletContainerFactory为例 5.嵌入式容器的配置修改生效原理 1.参照 Spring Boot 自动配置包里面的web模块 EmbeddedServletContainerAutoConfi

  • SpringBoot 嵌入式web容器的启动原理详解

    目录 SpringBoot应用启动run方法 SpringApplication.java 中执行的代码 ServletWebServerApplicationContext.java执行的方法 SpringBoot 2.x 版本 嵌入式Servlet容器自动配置原理以及启动原理 一.版本说明 二.总结 三.嵌入式Servlet容器自动配置原理(以Tomcat为例) 四.嵌入式Servlet容器启动原理(以Tomcat为例) SpringBoot应用启动run方法 SpringApplicati

  • 详解如何将springboot项目导出成war包

    以demo-3项目为例: 1.将pom.xml中的jar改成war     2.添加依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> 3.移除插件 如果已经嵌入了tomcat插件,还要移除tomcat插件

  • SpringBoot嵌入式Web容器原理与使用介绍

    目录 原理 应用 1. 切换Web服务器 2. 定制服务器规则 嵌入式 Web 容器:应用中内置服务器(Tomcat),不用在外部配置服务器了 原理 SpringBoot 项目启动,发现是 web 应用,引入 web 场景包 ----- 如:Tomcat web 应用创建一个 web 版的 IOC 容器 ServletWebServerApplicationContext ServletWebServerApplicationContext 启动的时候寻找 ServletWebServerFac

随机推荐