在SpringBoot框架中,我们使用最多的是Tomcat,这是SpringBoot默认的容器技术,而且是内嵌式的Tomcat。
同时,SpringBoot也支持Undertow容器,Undertow 是基于java nio的web服务器,应用比较广泛,内置提供的PathResourceManager,可以用来直接访问文件系统;如果你有文件需要对外提供访问,除了ftp,nginx等,undertow 也是一个不错的选择,作为java开发,服务搭建非常简便。我们可以很方便的用Undertow替换Tomcat,而Undertow的性能和内存使用方面都优于Tomcat,那我们如何使用Undertow技术呢?
1. 配置Undertow
Spring boot内嵌容器默认为Tomcat,想要换成Undertow,非常容易,只需修改spring-boot-starter-web依赖,移除tomcat的依赖:
<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>
然后,添加undertow依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
这样即可,使用默认参数启动undertow服务器。如果需要修改undertow参数,继续往下看。
undertow的参数设置:
server:
port: 8084
http2:
enabled: true
undertow:
io-threads: 16
worker-threads: 256
buffer-size: 1024
buffers-per-region: 1024
direct-buffers: true
io-threads:IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接,默认设置每个CPU核心一个线程,不可设置过大,否则启动项目会报错:打开文件数过多。
worker-threads:阻塞任务线程池,当执行类似servlet请求阻塞IO操作,undertow会从这个线程池中取得线程。它的值取决于系统线程执行任务的阻塞系数,默认值是 io-threads*8
以下配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理。
buffer-size:每块buffer的空间大小,越小的空间被利用越充分,不要设置太大,以免影响其他应用,合适即可
buffers-per-region:每个区分配的buffer数量,所以pool的大小是buffer-size * buffers-per-region
direct-buffers:是否分配的直接内存(NIO直接分配的堆外内存)
2. 启动SpringBoot测试
Undertow启动成功提示语:[INFO ] 2020-08-13 10:38:32 [main] o.s.b.w.e.u.UndertowServletWebServer - Undertow started on port(s) 80 (http) with context path ''
Tomcat启动成功提示语: [INFO ] 2020-08-13 10:41:35 [main] o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 80 (http) with context path ''
3. Undertow的FileServer
import java.io.File; import io.undertow.Handlers; import io.undertow.Undertow; import io.undertow.server.handlers.resource.PathResourceManager; public class FileServer { public static void main(String[] args) { File file = new File("/"); Undertow server = Undertow.builder().addHttpListener(8080, "localhost") .setHandler(Handlers.resource(new PathResourceManager(file.toPath(), 100)) .setDirectoryListingEnabled(true)) .build(); server.start(); } }