SpringBoot替換默認(rèn)的tomcat服務(wù)器的方法
1.為什么要替換默認(rèn)tomcat?
Tomcat是Apache基金下的一個輕量級的Servlet容器,支持Servlet和JSP。Tomcat具有Web服務(wù)器特有的功能,包括 Tomcat管理和控制平臺、安全管理和Tomcat閥等。Tomcat本身包含了HTTP服務(wù)器,因此也可以視作單獨(dú)的Web服務(wù)器。在SpringBoot框架中,我們使用最多的是Tomcat,這是SpringBoot默認(rèn)的容器技術(shù),而且是內(nèi)嵌式的Tomcat。
Tomcat與Undertow的優(yōu)劣對比
Undertow是Red Hat公司的開源產(chǎn)品, 它完全采用Java語言開發(fā),是一款靈活的高性能Web服務(wù)器,支持阻塞IO和非阻塞IO。由于Undertow采用Java語言開發(fā),可以直接嵌入到Java項(xiàng)目中使用。同時, Undertow完全支持Servlet和Web Socket,在高并發(fā)情況下表現(xiàn)非常出色。
我們在相同機(jī)器配置下壓測Tomcat和Undertow,得到的測試結(jié)果如下所示:
QPS測試結(jié)果對比:
Tomcat
Undertow
通過測試發(fā)現(xiàn),在高并發(fā)系統(tǒng)中,Tomcat相對來說比較弱。在相同的機(jī)器配置下,模擬相等的請求數(shù),Undertow在性能和內(nèi)存使用方面都是最優(yōu)的。并且Undertow新版本默認(rèn)使用持久連接,這將會進(jìn)一步提高它的并發(fā)吞吐能力。所以,如果是高并發(fā)的業(yè)務(wù)系統(tǒng),Undertow是最佳選擇。
2.代碼工程
實(shí)驗(yàn)?zāi)繕?biāo)
用undertow替換默認(rèn)的tomcat
pom.xml
先排除tomcat,然后添加undertow依賴包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springboot-demo</artifactId> <groupId>com.et</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Undertow</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> <!--jetty--> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>--> </dependencies> </project>
controller
package com.et.undertow.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RestController public class HelloWorldController { @RequestMapping("/hello") public Map<String, Object> showHelloWorld(){ Map<String, Object> map = new HashMap<>(); map.put("msg", "HelloWorld"); return map; } }
DemoApplication.java
package com.et.undertow; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
代碼倉庫
3.測試
啟動Spring Boot應(yīng)用,控制臺日志發(fā)現(xiàn)已經(jīng)使用undertow了
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE) 2024-08-12 13:40:21.429 INFO 34216 --- [ main] com.et.undertow.DemoApplication : Starting DemoApplication on BJDPLHHUAPC with PID 34216 (D:\IdeaProjects\ETFramework\Undertow\target\classes started by Dell in D:\IdeaProjects\ETFramework) 2024-08-12 13:40:21.433 INFO 34216 --- [ main] com.et.undertow.DemoApplication : No active profile set, falling back to default profiles: default 2024-08-12 13:40:22.663 WARN 34216 --- [ main] io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used 2024-08-12 13:40:22.684 INFO 34216 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext 2024-08-12 13:40:22.684 INFO 34216 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1196 ms 2024-08-12 13:40:22.827 INFO 34216 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2024-08-12 13:40:22.947 INFO 34216 --- [ main] io.undertow : starting server: Undertow - 2.0.29.Final 2024-08-12 13:40:22.956 INFO 34216 --- [ main] org.xnio : XNIO version 3.3.8.Final 2024-08-12 13:40:22.966 INFO 34216 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.3.8.Final 2024-08-12 13:40:23.065 INFO 34216 --- [ main] o.s.b.w.e.u.UndertowServletWebServer : Undertow started on port(s) 8088 (http) with context path '' 2024-08-12 13:40:23.067 INFO 34216 --- [ main] com.et.undertow.DemoApplication : Started DemoApplication in 2.021 seconds (JVM running for 2.409)
到此這篇關(guān)于SpringBoot替換默認(rèn)的tomcat服務(wù)器的方法的文章就介紹到這了,更多相關(guān)SpringBoot替換tomcat內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus 查詢指定字段的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus 查詢指定字段的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12springboot在filter中如何用threadlocal存放用戶身份信息
這篇文章主要介紹了springboot中在filter中如何用threadlocal存放用戶身份信息,本文章主要描述通過springboot的filter類,在過濾器中設(shè)置jwt信息進(jìn)行身份信息保存的方法,需要的朋友可以參考下2024-07-07SpringBoot整合JWT(JSON?Web?Token)生成token與驗(yàn)證的流程及示例
JSON Web Token(JWT)是一種開放的標(biāo)準(zhǔn)(RFC 7519),定義了一種緊湊的、自包含的方式來安全地在各方之間傳輸信息作為JSON對象,這篇文章主要給大家介紹了關(guān)于SpringBoot整合JWT(JSON?Web?Token)生成token與驗(yàn)證的相關(guān)資料,需要的朋友可以參考下2024-07-07解決報(bào)java.lang.AssertionError錯誤的問題
這篇文章主要介紹了解決報(bào)java.lang.AssertionError錯誤的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05SpringBoot如何實(shí)現(xiàn)接口版本控制
這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)接口版本控制,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Mybatis中typeAliases標(biāo)簽和package標(biāo)簽使用
這篇文章主要介紹了Mybatis中typeAliases標(biāo)簽和package標(biāo)簽使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09hutool實(shí)戰(zhàn):IoUtil 流操作工具類(將內(nèi)容寫到流中)
這篇文章主要介紹了Go語言的io.ioutil標(biāo)準(zhǔn)庫使用,是Golang入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下,如果能給你帶來幫助,請多多關(guān)注腳本之家的其他內(nèi)容2021-06-06Java實(shí)戰(zhàn)項(xiàng)目之校園跑腿管理系統(tǒng)的實(shí)現(xiàn)
只有理論是不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+vue+maven+elementui+mysql實(shí)現(xiàn)一個校園跑腿管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01