Undertow是Red Hat公司的开源产品, 它完全采用Java语言开发,是一款灵活的高性能Web服务器,支持阻塞IO和非阻塞IO。由于Undertow采用Java语言开发,可以直接嵌入到Java项目中使用。同时, Undertow完全支持Servlet和Web Socket,在高并发情况下表现非常出色。
我们在相同机器配置下压测Tomcat和Undertow,得到的测试结果如下所示:
Tomcat
Undertow
通过测试发现,在高并发系统中,Tomcat相对来说比较弱。在相同的机器配置下,模拟相等的请求数,Undertow在性能和内存使用方面都是最优的。并且Undertow新版本默认使用持久连接,这将会进一步提高它的并发吞吐能力。所以,如果是高并发的业务系统,Undertow是最佳选择。
用undertow替换默认的tomcat
<?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>
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;
}
}
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);
}
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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)