In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to solve the pit of undertow in springboot", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to solve the pit of undertow in springboot" article.
The pit of using undertow in springboot
Scenario: prepare static resources based on springboot to play mp4 resources. Different versions of springboot have different effects, which may cause normal resources to become unavailable. This article tests several versions and puts forward some suggestions to solve this situation. I hope it will be helpful to your work.
It is well known that springboot has built-in web-like middleware to hand over the management of the web server to the container. You only need to make a declaration when using it.
The experimental environment of this paper is as follows
Windows7+JDK1.8+Eclipse+Maven3.3.9+SpringBoot2.2.x+Undertow2.2.x
Environmental preparation
The first step is to configure maven environment
4.0.0 com.yelang undertowdemo 0.0.1-SNAPSHOT Undertow Test Undertow Middleware Test org.springframework.boot spring-boot-starter-parent 2.2.10.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat Org.springframework.boot spring-boot-starter-undertow
The second step, configuration statement
# the development environment configures the HTTP port of server: # server. The default is 8080 port: 8080 servlet: # application access path context-path: / # undertow configuration undertow: # maximum size of HTTP post content. When the value is-1 The default value is unlimited max-http-post-size:-1 # configuration will affect buffer, these buffer will be used for server connection IO operations, somewhat similar to netty pooled memory management # the space size of each buffer, the smaller the space is utilized, the more fully buffer-size: 512 # whether to allocate direct memory direct-buffers: true threads: # set the number of I / O threads It mainly performs non-blocking tasks, which are responsible for multiple connections. By default, each CPU core has a thread io: 8 # blocking task thread pool. When performing similar servlet request blocking operations, undertow will get threads from this thread pool. Its value setting depends on the load of the system worker: 25 loads # tomcat configuration # tomcat:# # tomcat URI encoding # uri-encoding: UTF-8# # tomcat maximum number of threads The default is the number of threads initialized by 20 max-threads: 50 Tomcat, and the default is 2 threads min-spare-threads: 30
The third step, static resource mapping
Package com.yelang.config; import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer / * Universal configuration * @ author wzh * / @ Configurationpublic class ResourcesConfig implements WebMvcConfigurer {@ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {/ * * Local File upload path * / registry.addResourceHandler ("/ profile/**"). AddResourceLocations ("file:D:/wzh/uploadPath/"); / * swagger configuration * / registry.addResourceHandler ("swagger-ui.html"). AddResourceLocations ("classpath:/META-INF/resources/") Registry.addResourceHandler ("/ webjars/**") .addResourceLocations ("classpath:/META-INF/resources/webjars/");}}
The above code marks the static resources that the system is open to the outside world. Normally, if you copy the resources to the corresponding directory, you can access the corresponding resources.
Http://localhost:8080/profile/2.mp4
Use springboot2.2.11, springboot2.2.12, springboot2.2.13
These three versions of normal mp4 will not be able to load. It is estimated that there are some settings in these versions.
If the production uses the above versions of sringboot
If you need to preview resources such as mp4.
The suggestions are as follows: first, adjust the version of springboot to the supported version. Second, instead of using profile to provide video resources, we use components such as nginx. Third, the use of third-party file system. Fourth, replace the undertow container with other containers such as tomcat.
Quiz: do you use built-in containers in your production environment? How many nio containers like undertow are used?
Does springboot need to give up Tomcat and choose Undertow?
In the SpringBoot framework, we use Tomcat most often, which is the default container technology of SpringBoot, and it is embedded Tomcat.
At the same time, SpringBoot also supports Undertow containers, we can easily replace Tomcat with Undertow, and Undertow is better than Tomcat in performance and memory usage, so how do we use Undertow technology? This article will explain it in detail for you.
Tomcat Container in SpringBoot
SpringBoot is arguably the most popular Java Web framework at present. It rescues developers from the heavy xml and allows developers to create a complete Web service in a few minutes, which greatly improves the productivity of developers. Web container technology is an essential part of the Web project, because any Web project needs to run with container technology.
In the SpringBoot framework, we use Tomcat most often, which is the default container technology of SpringBoot, and it is embedded Tomcat.
SpringBoot sets Undertow
Java programmers should be very familiar with Tomcat technology, which is the most commonly used container technology for Web applications. Most of our earliest projects are deployed and run under Tomcat, so what other container technologies can we use in SpringBoot besides Tomcat containers? Yes, it is the Undertow container technology in the title. SrpingBoot has completely inherited the Undertow technology, we only need to introduce the dependency of Undertow, as shown in the following figure.
Once configured, we start the application and find that the container has been replaced with Undertow.
So why do we need to replace Tomcat with Undertow technology?
Comparison of advantages and disadvantages between Tomcat and Undertow
Tomcat is a lightweight Servlet container under the Apache fund that supports Servlet and JSP. Tomcat has the unique functions of Web server, including Tomcat management and control platform, security bureau management and Tomcat valve, etc. Tomcat itself contains a HTTP server, so it can also be treated as a separate Web server.
However, Tomcat and ApacheHTTP servers are not the same thing. ApacheHTTP servers are HTTP Web servers implemented in C language. Tomcat is completely free and is loved by developers.
Undertow is an open source product of Red Hat. It is developed entirely in Java language. It is a flexible, high-performance Web server that supports blocking IO and non-blocking IO. Because Undertow is developed in the Java language, it can be directly embedded in the Java project. At the same time, Undertow fully supports Servlet and Web Socket and performs very well in high concurrency situations.
We stress test Tomcat and Undertow under the same machine configuration, and the test results are as follows:
Comparison of QPS test results:
Tomcat
Undertow
Memory usage comparison:
Tomcat
Undertow
Through testing, it is found that in high concurrency systems, Tomcat is relatively weak. Under the same machine configuration, simulating an equal number of requests, Undertow is optimal in terms of performance and memory usage. And the new version of Undertow uses persistent connections by default, which will further improve its concurrent throughput. Therefore, if it is a highly concurrent business system, Undertow is the best choice.
The above is about the content of this article on "how to solve the pit of undertow in springboot". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.