blog/毕业论文/毕业论文-5.md

5.2 KiB
Raw Blame History

title tags categories date summary
毕业设计(5)
java
ssm
react
ant design
typescript
毕业设计 2022-12-15 12:40:32 Undertow的配置何使用

十二、Undertow使用

因为在开发过程中使用tomact运行过程中每一次更改后启动的过程中启动时间大概要30s-60s左右为了加速启动过程我搜索了适配servlet的容器找到了undertow这个容器并成功适配。

1.underwot介绍

Undertow 是红帽公司开发的一款基于 NIO 的高性能 Web 嵌入式服务器

特点:

    轻量级:它是一个 Web 服务器,但不像传统的 Web 服务器有容器概念,它由两个核心     Jar 包组成,加载一个 Web 应用可以小于 10MB 内存

    Servlet3.1 支持:它提供了对 Servlet3.1 的支持

    WebSocket 支持:对 Web Socket 完全支持,用以满足 Web 应用巨大数量的客户端

    嵌套性:它不需要容器,只需通过 API 即可快速搭建 Web 服务器

2.添加依赖

在pom.xml的依赖中添加如下节点

<!-- https://mvnrepository.com/artifact/com.jfinal/jfinal-undertow -->
    <dependency>
      <groupId>com.jfinal</groupId>
      <artifactId>jfinal-undertow</artifactId>
      <version>3.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.jfinal/jfinal -->
    <dependency>
      <groupId>com.jfinal</groupId>
      <artifactId>jfinal</artifactId>
      <version>5.0.7</version>
    </dependency>

3.配置webbuilder

webbuilder相当于tomact的web.xml我们只需要将web.xml文件翻译成对应的代码就行了

关键代码

    Consumer<WebBuilder> webBuilderConsumer = e -> { };


    protected void config(){
        WebBuilder wb = new WebBuilder(this);
        wb.addServlet("mvc", "org.springframework.web.servlet.DispatcherServlet")
                .addServletMapping("mvc","/")
                .addServletInitParam("mvc","contextConfigLocation","classpath:spring/spring-mvc.xml")
                .addInitParameter("contextConfigLocation","classpath:spring/spring.xml")
                .addListener("org.springframework.web.context.ContextLoaderListener")
                .addInitParameter("log4jConfigLocation","classpath:log4j.properties")
                .addFilter("Encoding","org.springframework.web.filter.CharacterEncodingFilter")
                .addFilterInitParam("Encoding","encoding","utf-8")
                .addFilterUrlMapping("Encoding","/*");

        webBuilder.accept(wb);
    }

4.继承UndertowServer

创建自定义类 Myserver,继承自 UndertowServer,在初始化方法中为一些变量赋值。

public class MyServer extends UndertowServer {

    protected MyServer(UndertowConfig undertowConfig) {
        super(undertowConfig);
    }



    protected void init() {

        super.builder = Undertow.builder();
        super.webBuilder = webBuilderConsumer;
        super.configUndertow();
        config();
    }
}

5.重写main方法

因为undertow会读取配置文件 undertow.txt,所以我们先准备undertow.txt配置文件

undertow.devMode=true
undertow.port=8080
undertow.host=0.0.0.0
undertow.contextPath=/
undertow.gzip.enable=true
undertow.gzip.level=5
undertow.gzip.minLength=1024
undertow.ioThreads=4

然后重写main方法

public static void main(String[] args) {
        new MyServer(new UndertowConfig(UndertowConfig.class)).start(); 
 }

6.全部代码

package org.gjs.undertow;

import com.jfinal.server.undertow.UndertowConfig;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.server.undertow.WebBuilder;
import io.undertow.Undertow;
import java.util.function.Consumer;

public class MyServer extends UndertowServer {

    protected MyServer(UndertowConfig undertowConfig) {
        super(undertowConfig);
    }



    protected void init() {

        super.builder = Undertow.builder();
        super.webBuilder = webBuilderConsumer;
        super.configUndertow();
        config();
    }


    Consumer<WebBuilder> webBuilderConsumer = e -> {
    };
    protected void config(){
        WebBuilder wb = new WebBuilder(this);
        wb.addServlet("mvc", "org.springframework.web.servlet.DispatcherServlet")
                .addServletMapping("mvc","/")
                .addServletInitParam("mvc","contextConfigLocation","classpath:spring/spring-mvc.xml")
                .addInitParameter("contextConfigLocation","classpath:spring/spring.xml")
                .addListener("org.springframework.web.context.ContextLoaderListener")
                .addInitParameter("log4jConfigLocation","classpath:log4j.properties")
                .addFilter("Encoding","org.springframework.web.filter.CharacterEncodingFilter")
                .addFilterInitParam("Encoding","encoding","utf-8")
                .addFilterUrlMapping("Encoding","/*");

        webBuilder.accept(wb);
    }

    public static void main(String[] args) {
        new MyServer(new UndertowConfig(UndertowConfig.class)).start();
    }


}

运行测试

启动时间只需要7.2s,启动速度显著提升