广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot如何切换成其它的嵌入式Servlet容器(Jetty和Undertow)
  • 450
分享到

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

2024-04-02 19:04:59 450人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录如何切换成其它的嵌入式Servlet容器(Jetty和Undertow)SpringBoot默认使用的内置Servlet容器是TomcatspringBoot还支持Jetty和U

如何切换成其它的嵌入式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容器

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

--结束END--

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

本文链接: https://www.lsjlt.com/news/153841.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作