替换掉Springboot框架中的Tomcat,使用undertow

替换掉Springboot框架中的Tomcat,使用undertow

1、前言

在SpringBoot框架中,我们使用最多的是Tomcat,这是SpringBoot默认的容器技术,而且是内嵌式的Tomcat。

同时,SpringBoot也支持Undertow容器,我们可以很方便的用Undertow替换Tomcat,而Undertow的性能和内存使用方面都优于Tomcat。

2、四种WEB服务器介绍

2.1、Tomcat

Tomcat是Apache基金下的一个轻量级的Servlet容器,支持Servlet和JSP。Tomcat具有Web服务器特有的功能,包括 Tomcat管理和控制平台、安全局管理和Tomcat阀等。Tomcat本身包含了HTTP服务器,因此也可以视作单独的Web服务器。但是,Tomcat和ApacheHTTP服务器不是一个东西,ApacheHTTP服务器是用C语言实现的HTTP Web服务器。Tomcat是完全免费的,深受开发者的喜爱。

2.2、Undertow

Undertow是Red Hat公司的开源产品, 它完全采用Java语言开发,是一款灵活的高性能Web服务器,支持阻塞IO和非阻塞IO。由于Undertow采用Java语言开发,可以直接嵌入到Java项目中使用。同时, Undertow完全支持Servlet和Web Socket,在高并发情况下表现非常出色。

2.3、Jetty

Jetty: 一个快速、高性能且轻量级的 Servlet 容器和 HTTP 服务器。若要使用 Jetty 替换 Tomcat,只需在项目中排除 Tomcat 并引入 Jetty 的起步依赖 spring-boot-starter-jetty。

2.4、Netty

Netty: 虽然 Netty 不是传统的基于 Servlet 的 Web 容器,但在 Spring Boot 中可以用于构建响应式的 Web 应用,尤其是通过 Reactive Stack(如 WebFlux)时,会使用 Netty 作为底层网络通信层。要使用 Netty,通常引入的是 spring-boot-starter-webflux 依赖。

3、如何替换Tomcat?并使用Undertow

3.1、pom修改

xml 复制代码
	<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>

	<dependency>
		<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-undertow</artifactId>
	</dependency>

替换完成后我们启动项目并查看日志打印:

bash 复制代码
2024-05-09 10:31:30.965  INFO 3456 --- [           main] io.undertow                              : starting server: Undertow - 2.2.20.Final
2024-05-09 10:31:30.968  INFO 3456 --- [           main] org.xnio                                 : XNIO version 3.8.7.Final
2024-05-09 10:31:30.971  INFO 3456 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.8.7.Final
2024-05-09 10:31:30.995  INFO 3456 --- [           main] org.jboss.threads                        : JBoss Threads version 3.1.0.Final
2024-05-09 10:31:31.026  INFO 3456 --- [           main] o.s.b.w.e.undertow.UndertowWebServer     : Undertow started on port(s) 8080 (http)
2024-05-09 10:31:31.032  INFO 3456 --- [           main] c.example.undertwo.UndertwoApplication   : Started UndertwoApplication in 1.125 seconds (JVM running for 2.583)

4、参考目录

[1]:【官方】在 Spring Boot 中使用 Undertow 作为嵌入式服务器

[2]:【CSDN优秀文章】 在 Spring Boot 中使用 Undertow 作为嵌入式服务器

[3]:【评论区比较有意思】 在 Spring Boot 中使用 Undertow 作为嵌入式服务器

相关推荐
计算机-秋大田2 小时前
基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计
字节全栈_rJF3 小时前
性能测试 —— Tomcat监控与调优:status页监控_tomcat 自带监控
java·tomcat
加油,旭杏4 小时前
【go语言】接口
开发语言·后端·golang
谢大旭4 小时前
ASP.NET Core 中间件
后端·中间件·c#
customer085 小时前
【开源免费】基于SpringBoot+Vue.JS景区民宿预约系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
uzong5 小时前
Java函数式接口:代码艺术的诗意绽放
后端
HelloDam6 小时前
基于 mzt-biz-log 实现接口调用日志记录
后端
SomeB1oody7 小时前
【Rust自学】15.6. RefCell与内部可变性:“摆脱”安全性限制
开发语言·后端·rust
精通HelloWorld!9 小时前
使用HttpClient和HttpRequest发送HTTP请求
java·spring boot·网络协议·spring·http