Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to improve Service Throughput by Spring Boot

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to improve the service throughput of Spring Boot". In the daily operation, I believe many people have doubts about how to improve the service throughput of Spring Boot. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to improve the service throughput of Spring Boot". Next, please follow the editor to study!

Background

There are occasional slow requests in the production environment that lead to a decline in system performance and throughput. Here are some optimization recommendations.

Option 1. Undertow replaces tomcat

Most e-commerce websites are short requests, and the response time is usually 100ms. You can replace the web container from tomcat to undertow. The following steps are described below:

1. Add pom configuration

Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-undertow

2. Add related configurations

Server: undertow: direct-buffers: true io-threads: 4 worker-threads: 160

If you restart, you can see that the container has been switched to undertow on the console.

Recommended reading: hang Tomcat, Undertow performance is very explosive!

2. Caching

Put some hot data or static data into the local cache or redis, and update the cached data regularly if necessary

3. Async

In the process of code, a lot of our code does not need to wait for the result to be returned, that is, some of the code can be executed in parallel. At this time, we can use asynchronous. The simplest solution is to use the @ Async annotation provided by springboot. Of course, it can also be achieved through thread pool. The asynchronous steps are briefly described below.

1. Pom dependencies General springboot introduces web-related dependencies

Org.springframework.boot spring-boot-starter-web

2. Add @ EnableAsync annotation to the startup class

@ EnableAsync@SpringBootApplicationpublicclass AppApplication {public static void main (String [] args) {SpringApplication.run (AppApplication.class, args);}}

3. Add @ Async annotation to the specified method as needed. If you need to wait for the return value, the demo is as follows

@ Asyncpublic Future doReturn (int I) {try {/ / this method requires 500ms Thread.sleep (500);} catch (InterruptedException e) {e.printStackTrace ();} / message summary return new AsyncResult ("Asynchronous call");}

4. If you have thread variables or mdc in logback, you can increase the pass

Import org.slf4j.MDC;import org.springframework.context.annotation.Configuration;import org.springframework.core.task.TaskDecorator;import org.springframework.scheduling.annotation.AsyncConfigurerSupport;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.Map;import java.util.concurrent.Executor;/** * @ Description: * / @ EnableAsync@Configurationpublic class AsyncConfig extends AsyncConfigurerSupport {@ Override public Executor getAsyncExecutor () {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor () Executor.setTaskDecorator (new MdcTaskDecorator ()); executor.initialize (); return executor;}} class MdcTaskDecorator implements TaskDecorator {@ Override public Runnable decorate (Runnable runnable) {Map contextMap = MDC.getCopyOfContextMap (); return ()-> {try {MDC.setContextMap (contextMap); runnable.run () } finally {MDC.clear ();}};}}

5. Sometimes asynchrony needs to increase blocking

Import lombok.extern.slf4j.Slf4j;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;import java.util.concurrent.ThreadPoolExecutor;@Configuration@Slf4jpublic class TaskExecutorConfig {@ Bean ("localDbThreadPoolTaskExecutor") public Executor threadPoolTaskExecutor () {ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor (); taskExecutor.setCorePoolSize (5); taskExecutor.setMaxPoolSize (200); taskExecutor.setQueueCapacity (200) TaskExecutor.setKeepAliveSeconds; taskExecutor.setThreadNamePrefix ("LocalDbTaskThreadPool"); taskExecutor.setRejectedExecutionHandler ((Runnable r, ThreadPoolExecutor executor)-> {if (! executor.isShutdown ()) {try {Thread.sleep (300); executor.getQueue () .put (r)) } catch (InterruptedException e) {log.error (e.toString (), e); Thread.currentThread () .interrupt ();}); taskExecutor.initialize (); return taskExecutor }} 4. Business split

More time-consuming or different services can be split to provide single-node throughput

5. Integrated message queue

There are many scenarios where the real-time requirements of the data are not so strong, or when the business is fault-tolerant, the message can be sent to kafka, and then delayed consumption.

For example, users are specified to send push messages according to the condition query, which can be timely, daily, monthly, and so on.

At this point, the study on "how to improve the service throughput of Spring Boot" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report