In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to embed Tomcat concurrency capacity in SpringBoot". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to embed Tomcat concurrency capacity in SpringBoot" can help you solve the problem.
Discovery of concurrent container problems
The 6000 thread stress test is carried out on a single interface, and each thread requests 5 times, and the thread is created in 5 seconds. In the middle of the process, the request response time is too large and the error rate reaches 43%. This concurrency capacity is relatively weak for servers with better configurations.
Go deep into the bottom of SpringBoot to understand the reason.
The configuration of metadata is mentioned in the official SpringBoot documentation
As you can see, the default configuration for the port of our most commonly used setup project is in it.
Default embedded Tomcat configuration
1. Server.tomcat.accept-count: the length of the waiting queue. When the number of available threads is used up, subsequent requests will wait in the waiting queue and refuse to be processed when the waiting queue is full.
2. Server.tomcat.max-connections: the maximum number of connections that can be made. Default is 10000.
3. Server.tomcat.max-threads: maximum number of worker threads. Default is 200.
4. Server.tomcat.min-spare-threads: minimum number of worker threads, number of initialized allocated threads. Default is 10.
In the default configuration, a connection will be rejected when the connection exceeds 10000.
In the default configuration, requests triggered are rejected when they exceed 200: 100 (maximum number of worker threads + queue length)
These metadata Spring of course provide external configuration functions.
# change the embedded tomcat parameter server.port=8080## waiting queue length. Default is 100. The maximum number of worker threads in server.tomcat.accept-count=1000##. Default is 200. (4 core 8g memory, thread number experience value 800, the operating system does the switching scheduling between threads has system overhead, so it is not the more the better. The minimum number of working idle threads in server.tomcat.max-threads=800##, which defaults to 10. (increase it appropriately to cope with the sudden increase in traffic) server.tomcat.min-spare-threads=100
SpringBoot has built-in Tomcat. By default, the maximum number of threads for Tomcat is 200 and the maximum number of connections is 10000. The number of concurrency supported refers to the number of connections. How do 200 threads handle 10000 connections?
At present, Tomcat has three modes of dealing with connections, one is BIO, one thread handles only one connection, and the other is NIO, one thread handles multiple connections. Because HTTP requests are not too time-consuming and multiple connections generally do not send messages at the same time, it is not much of a problem for a thread to handle multiple connections.
The other is the apr mode, which will be described in detail later, and will not be discussed in depth here.
When Tomcat starts, you can see which operation mode Connector uses through log:
Starting ProtocolHandler ["http-bio-8080"] Starting ProtocolHandler ["http-nio-8080"] Starting ProtocolHandler ["http-apr-8080"]
The default values can be seen in the spring-boot-autoconfigure- version number .jar (for example: spring-boot-autoconfigure-2.1.0.RELEASE) package, and you can see the default configuration by decompressing the decompiled / web/ServerProperties.class file.
Customized embedded Tomcat development
About KeepAlive
KeepAlive is enabled by default in Http requests using Jmeter
The KeepAlive request of Http means that when our client sends a Http request to our server, if it takes the KeepAlive request header, it indicates that our Http client wants to establish a KeepAlive connection with the server. The corresponding use of this connection is that after sending the corresponding response to our server, our server does not immediately disconnect the connection, but waits to try to reuse the connection.
This solution is used to solve a time-consuming problem caused by a stateless Http response, disconnecting each time and creating a new connection.
However, if we maintain a long connection with the server after each web request is opened, the number of connections on our server will soon be used up, so the earliest Http1.0 did not design KeepAlive requests, but now Http1.1 plus KeepAlive requests are aimed at more and more mobile devices, even some very complex web interactions, which need to be in the process of browsing by users. Frequently send requests to the server, therefore, establishing a KeepAlive connection is not for the purpose of stress testing, but really has some performance benefits in the application scenario. Whether it is the client or the server, in the interaction of some network communication, there is no need to create a new connection every time, disconnect the connection, and consume the time of Tcp/Ip connection establishment, but only need to send data.
But such a design will also bring some problems, if our server does not have any restrictions on the operation of KeepAlive. If the connection does not do anything and does not respond, then the connection is a fee connection to the server. Some attackers maliciously use KeepAlive connections to send DDOS attacks to our servers, and the corresponding connections on the server side will only become the back door for attackers to attack. Therefore, for security, we need to customize Tomcat development.
Configuration
1. KeepAliveTimeOut: disconnect KeepAlive if the client does not respond after a few milliseconds
2. MaxKeepAliveRequests: how many times the KeepAlive disconnect fails after the request
The configuration of embedded containers is mentioned in the official SpringBoot documentation
/ / when there is no TomcatEmbeddedServletContainerFactory bean in the spring container, bean will be loaded into the spring container @ Configurationpublic class WebServerConfiguration implements WebServerFactoryCustomizer {@ Overridepublic void customize (ConfigurableWebServerFactory factory) {/ / customize our tomcat connector ((TomcatServletWebServerFactory) factory) .addConnectorCustomizers (new TomcatConnectorCustomizer () {@ Overridepublic void customize (Connector connector) {Http11NioProtocol protocol= (Http11NioProtocol) connector.getProtocolHandler ()) using the interface provided to us by the corresponding factory class. / / customize KeepAliveTimeout. Set the server to automatically disconnect keepalive connection protocol.setKeepAliveTimeout (30000) if there are no requests within 30 seconds; / / automatically disconnect keepalive connection protocol.setMaxKeepAliveRequests (10000) when the client sends more than 10000 requests;}}) }} this is the end of the introduction to "how to embed Tomcat concurrent capacity in SpringBoot". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.