当前位置: 首页 > 工具软件 > Undertow > 使用案例 >

在Spring Boot使用Undertow服务

东云
2023-12-01

1.undertow简介

Undertow是RedHAT红帽公司开源的产品,采用JAVA开发,是一款灵活,高性能的web服务器,提供了NIO的阻塞/非阻塞API,也是Wildfly的默认Web容器。在javaweb容器的世界里,Tomcat和jetty是大众熟知的,undertow目前逐步进入大众的视角,它是一款能和tomcat媲美的神器,在性能方面吊打tomcat。目前Undertow已经成为springboot 默认集成的三大容器之一。

2.undertow特点

undertow在高并发业务场景中,性能优于tomcat

  1. 高性能,在多款同类产品的压测对比中,高并发情况下表现出色。
  2. Servlet4.0支持,它提供了对Servlet4.0的支持。
  3. Web Socket完全支持,包含JSR-356,用以满足Web应用巨大数量的客户端。
  4. 内嵌式,它不需要容器,只需要通过api即可快速搭建Web服务器。
  5. 灵活性,交由链式Handler配置和处理请求,可以最小化按需加载模块,无须加载多余功能。
  6. 轻量级,它是一个内嵌Web服务器,由两个核心jar包组成。

3.springboot集成的web服务器

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
    </parent>
    <groupId>com.konne</groupId>
    <artifactId>push-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>push-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 移除掉默认支持的 Tomcat -->
            <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
   
</project>

配置启动成功显示如下信息:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.4)

2022-12-13 09:41:21.625  INFO 2416 --- [           main] com.konne.pushdemo.PushDemoApplication   : Starting PushDemoApplication using Java 1.8.0_202 on 2F-KF03 with PID 2416 (D:\i-word\push-demo\target\classes started by Administrator in D:\i-word\push-demo)
2022-12-13 09:41:21.628  INFO 2416 --- [           main] com.konne.pushdemo.PushDemoApplication   : No active profile set, falling back to 1 default profile: "default"
2022-12-13 09:41:22.226  WARN 2416 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2022-12-13 09:41:22.241  INFO 2416 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2022-12-13 09:41:22.241  INFO 2416 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 578 ms
2022-12-13 09:41:22.482  INFO 2416 --- [           main] io.undertow                              : starting server: Undertow - 2.2.19.Final
2022-12-13 09:41:22.486  INFO 2416 --- [           main] org.xnio                                 : XNIO version 3.8.7.Final
2022-12-13 09:41:22.490  INFO 2416 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.8.7.Final
2022-12-13 09:41:22.514  INFO 2416 --- [           main] org.jboss.threads                        : JBoss Threads version 3.1.0.Final
2022-12-13 09:41:22.544  INFO 2416 --- [           main] o.s.b.w.e.undertow.UndertowWebServer     : Undertow started on port(s) 8080 (http)
2022-12-13 09:41:22.552  INFO 2416 --- [           main] com.konne.pushdemo.PushDemoApplication   : Started PushDemoApplication in 1.217 seconds (JVM running for 1.807)
 类似资料: