In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Springboot to achieve high concurrency under the time-consuming operation, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Time-consuming operations under high concurrency
High concurrency, that is, when there are many requests at a point in time, when many written requests come, your server is under great pressure. When one of your requests takes a long time, these requests will deplete your server threads, that is, the threads in your main thread pool will no longer be idle, and the next request will be 502. Add Q group: 478052716 free (Java framework materials, video materials, BATJ interview materials)
Request flow chart
Http1 http2 http3thread1 thread2 thread3
Solution
DeferredResult is used to implement asynchronous operations. When a request is called, it is first put in a queue, and then there is a subscriber in the background, and when messages related to the topic are sent, the subscriber consumes it. This step can be distributed, such as a second kill scenario. When more than N requests come, when some requests hit, they will write, and the write operation will be very stressful. A request can be processed for 3 seconds, which is not allowed for high concurrency scenarios, because the server thread resources you occupy are too long, and soon your server will have no thread resources available, so DeferredResult can be used for processing.
Code implementation
Establish the interface of the order, which is only responsible for the release of simple checksum events
/ * create highly concurrent orders asynchronously. * * @ return * / @ GetMapping ("/ create-order") public DeferredResult createOrder () {DeferredResult deferredResult = new DeferredResult ((long) 3000, "error order"); logger.info ("publish the event to set up an order"); applicationEventPublisher.publishEvent (deferredResult); return deferredResult;}
Asynchronous order processing core logic, which is also a time-consuming operation
@ Component@EnableAsyncpublic class OrderListener {static Logger logger = LoggerFactory.getLogger (OrderListener.class); / * it is actually a consumer of an order queue, writing orders in the background. In this example, a simple event listener is used to implement asynchronous processing. * * @ return * / @ EventListener @ Async public String processOrder (DeferredResult deferredResult) throws InterruptedException {logger.info ("processing the order and returning to the corresponding Http context"); String order = UUID.randomUUID () .toString (); Thread.sleep (2000) / / assume that it takes 5 seconds to process the data, and the front end needs to block for 5 seconds, but the http main thread has been released, which is more suitable for IO-intensive situations / / when set up, create-order will successfully respond to deferredResult.setResult (order); return order;}}
Test result
After the request / create-order, the server returns the result after 2 seconds of processing, but what the spring background really does is that thread 1 becomes idle after the event is issued, and other requests can reuse it. When the processOrder background processes the result, spring will use a new thread in the thread pool to handle the rest of the logic!
These are all the contents of the article "how to achieve time-consuming operations under high concurrency in Springboot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.