182 lines
5.2 KiB
Markdown
182 lines
5.2 KiB
Markdown
|
---
|
|||
|
title: 毕业设计(5)
|
|||
|
tags:
|
|||
|
- java
|
|||
|
- ssm
|
|||
|
- react
|
|||
|
- ant design
|
|||
|
- typescript
|
|||
|
categories: 毕业设计
|
|||
|
date: 2022-12-15 12:40:32
|
|||
|
summary: 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的依赖中添加如下节点
|
|||
|
|
|||
|
```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文件翻译成对应的代码就行了
|
|||
|
|
|||
|
关键代码
|
|||
|
|
|||
|
```java
|
|||
|
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**,在初始化方法中为一些变量赋值。
|
|||
|
|
|||
|
```java
|
|||
|
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配置文件
|
|||
|
|
|||
|
```properties
|
|||
|
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方法
|
|||
|
|
|||
|
```java
|
|||
|
public static void main(String[] args) {
|
|||
|
new MyServer(new UndertowConfig(UndertowConfig.class)).start();
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
### 6.全部代码
|
|||
|
|
|||
|
```java
|
|||
|
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,启动速度显著提升
|