Heim > Java > javaLernprogramm > So wechseln Sie SpringBoot zu anderen eingebetteten Servlet-Containern

So wechseln Sie SpringBoot zu anderen eingebetteten Servlet-Containern

王林
Freigeben: 2023-05-13 22:58:12
nach vorne
1582 Leute haben es durchsucht

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

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

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

SpringBoot还支持Jetty和Undertow

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

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

So wechseln Sie SpringBoot zu anderen eingebetteten Servlet-Containern

若要切换 只需要在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>
Nach dem Login kopieren

So wechseln Sie SpringBoot zu anderen eingebetteten Servlet-Containern

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>
Nach dem Login kopieren

So wechseln Sie SpringBoot zu anderen eingebetteten Servlet-Containern

Undertow启动成功

SpringBoot web开发_嵌入式Servlet容器

1、切换嵌入式Servlet容器

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

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

2)切换服务器

So wechseln Sie SpringBoot zu anderen eingebetteten Servlet-Containern

<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>
Nach dem Login kopieren

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
Nach dem Login kopieren

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);
    }
}
Nach dem Login kopieren

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) {
    }
}
Nach dem Login kopieren

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

So wechseln Sie SpringBoot zu anderen eingebetteten Servlet-Containern

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

Das obige ist der detaillierte Inhalt vonSo wechseln Sie SpringBoot zu anderen eingebetteten Servlet-Containern. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage