Undertow的学习和使用

本文是基于CentOS 7.3系统环境和JDK 1.8,学习和使用undertow高吞吐量的web容器:

  • CentOS 7.3

1. 引入依赖

  • pom.xml
<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.chaoyue</groupId>
    <artifactId>undertow-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
    </parent>

    <dependencies>
        <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>
        <dependency>
            <groupId>io.undertow</groupId>
            <artifactId>undertow-core</artifactId>
            <version>2.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>io.undertow</groupId>
            <artifactId>undertow-servlet</artifactId>
            <version>2.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>io.undertow</groupId>
            <artifactId>undertow-websockets-jsr</artifactId>
            <version>2.1.0.Final</version>
        </dependency>
    </dependencies>
</project>

2. 添加配置文件

  • application.yaml
server:
  port: 8442

  undertow:
    #    http post请求数量的最大值,0表示不限制
    max-http-post-size: 0

    accesslog:
      dir: D://
      enabled: true
      pattern: common
      prefix: access_log
      suffix: .log

3. 自定义主类

  • UndertowApplication.class
package com.chaoyue.undertow.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UndertowApplication {
    public static void main(String[] args) {

        SpringApplication.run(UndertowApplication.class, args);
    }
}

4. 自定义controller类

  • HelloController.class
package com.chaoyue.undertow.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String getUserById() {

        return "hello";
    }
}

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐