Spring Boot : Undertow
Undertow
- Undertow is a NIO-based high-performance Web embedded server developed by Red Hat
- Untertow features:
- Lightweight: It's a Web server, but unlike traditional Web servers, which have container concept, it consists of two core Jar packages. Loading a Web application can be less than 10MB of memory.
- Servlet 3.1 support: It provides support for Servlet 3.1
- WebSocket support: Full support for Web Socket to meet the huge number of clients for Web applications
- Nesting: It doesn't need containers. It just needs API to build Web servers quickly.
- By default, Spring Cloud uses Tomcat as an embedded Servlet container to start a Tomcat Spring Book program and an Undertow Spring Book program. By comparing with Visual VM tools, we can see that Undertow outperforms Tomcat in performance.
Use Undertow
Add dependency
<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>
Support HTTP2
// Add @bean to the @Configuration class @Bean UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() { UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory(); // Other configurations can also be made here. factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true)); return factory; }
Configure Undertow
# Undertow Log Storage Directory server.undertow.accesslog.dir # Whether to start the log server.undertow.accesslog.enabled=false # Log format server.undertow.accesslog.pattern=common # Prefix of log file name server.undertow.accesslog.prefix=access_log # Log file name suffix server.undertow.accesslog.suffix=log # Maximum size of HTTP POST requests server.undertow.max-http-post-size=0 # Set the number of IO threads, which mainly perform non-blocking tasks. They are responsible for multiple connections. By default, one thread per CPU core is set. server.undertow.io-threads=4 # Blocking Task Thread Pool. When performing a servlet-like request blocking operation, undertow retrieves threads from this thread pool. Its value setting depends on the load of the system. server.undertow.worker-threads=20 # The following configuration affects buffer, which is used for server connection IO operations, somewhat similar to netty's pooled memory management # The smaller the size of each buffer, the more fully it will be utilized. server.undertow.buffer-size=1024 # The number of buffers allocated per region, so the size of the pool is buffer-size * buffers-per-region server.undertow.buffers-per-region=1024 # Whether direct memory is allocated or not server.undertow.direct-buffers=true