温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot怎么切换成其它的嵌入式Servlet容器

发布时间:2022-07-08 10:11:32 来源:亿速云 阅读:151 作者:iii 栏目:开发技术

本篇内容介绍了“SpringBoot怎么切换成其它的嵌入式Servlet容器”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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

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

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

SpringBoot还支持Jetty和Undertow

  • Jetty适合用于长链接应用 例如聊天

  • Undertow是一个高性能非阻塞的Servlet容器 并发性能非常好 但不支持jsp

SpringBoot怎么切换成其它的嵌入式Servlet容器

若要切换 只需要在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>

SpringBoot怎么切换成其它的嵌入式Servlet容器

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>

SpringBoot怎么切换成其它的嵌入式Servlet容器

Undertow启动成功

SpringBoot web开发_嵌入式Servlet容器

1、切换嵌入式Servlet容器

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

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

2)切换服务器

SpringBoot怎么切换成其它的嵌入式Servlet容器

<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 接口实现类(框架中默认实现类):

SpringBoot怎么切换成其它的嵌入式Servlet容器

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

“SpringBoot怎么切换成其它的嵌入式Servlet容器”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI