java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot切换成其它嵌入式Servlet容器

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

作者:Piconjo_Official

这篇文章主要介绍了SpringBoot如何切换成其它的嵌入式Servlet容器(Jetty和Undertow),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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

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

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

SpringBoot还支持Jetty和Undertow

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

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

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

您可能感兴趣的文章:
阅读全文