詳解SpringBoot 應(yīng)用如何提高服務(wù)吞吐量
意外和明天不知道哪個先來。沒有危機是最大的危機,滿足現(xiàn)狀是最大的陷阱。
背景
生產(chǎn)環(huán)境偶爾會有一些慢請求導(dǎo)致系統(tǒng)性能下降,吞吐量下降,下面介紹幾種優(yōu)化建議。
方案
1、undertow替換tomcat
電子商務(wù)類型網(wǎng)站大多都是短請求,一般響應(yīng)時間都在100ms,這時可以將web容器從tomcat替換為undertow,下面介紹下步驟:
1、增加pom配置
<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>
2、增加相關(guān)配置
server: undertow: direct-buffers: true io-threads: 4 worker-threads: 160
重新啟動可以在控制臺看到容器已經(jīng)切換為undertow了
2、緩存
將部分熱點數(shù)據(jù)或者靜態(tài)數(shù)據(jù)放到本地緩存或者redis中,如果有需要可以定時更新緩存數(shù)據(jù)
3、異步
在代碼過程中我們很多代碼都不需要等返回結(jié)果,也就是部分代碼是可以并行執(zhí)行,這個時候可以使用異步,最簡單的方案是使用springboot提供的@Async注解,當然也可以通過線程池來實現(xiàn),下面簡單介紹下異步步驟。
1、pom依賴 一般springboot引入web相關(guān)依賴就行
<dependency> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency>
2、在啟動類中增加@EnableAsync注解
import org.springframework.boot.SpringApplication @EnableAsync @SpringBootApplication public class AppApplication { public static void main(String[] args) { SpringApplication.run(AppApplication.class, args); } }
3、需要時在指定方法中增加@Async注解,如果是需要等待返回值,則demo如下
@Async public Future<String> doReturn(int i) { try { // 這個方法需要調(diào)用500毫秒 Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } // 消息匯總 return new AsyncResult<String>("異步調(diào)用"); }
4、如果有線程變量或者logback中的mdc,可以增加傳遞
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 @Configuration public 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<string, string> contextMap = MDC.getCopyOfContextMap(); return () - & gt; { try { MDC.setContextMap(contextMap); runnable.run(); } finally { MDC.clear(); } }; } }
5、有時候異步需要增加阻塞
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 @Slf4j public class TaskExecutorConfig { @Bean("localDbThreadPoolTaskExecutor") public Executor threadPoolTaskExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(5); taskExecutor.setMaxPoolSize(200); taskExecutor.setQueueCapacity(200); taskExecutor.setKeepAliveSeconds(100); taskExecutor.setThreadNamePrefix("LocalDbTaskThreadPool"); taskExecutor.setRejectedExecutionHandler((Runnable r, ThreadPoolExecutor executor) - & gt; { 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、業(yè)務(wù)拆分
可以將比較耗時或者不同的業(yè)務(wù)拆分出來提供單節(jié)點的吞吐量
5、集成消息隊列
有很多場景對數(shù)據(jù)實時性要求不那么強的,或者對業(yè)務(wù)進行業(yè)務(wù)容錯處理時可以將消息發(fā)送到kafka,然后延時消費。舉個例子,根據(jù)條件查詢指定用戶發(fā)送推送消息,這里可以時按時、按天、按月等等,這時就
到此這篇關(guān)于詳解SpringBoot 應(yīng)用如何提高服務(wù)吞吐量的文章就介紹到這了,更多相關(guān)SpringBoot 提高服務(wù)吞吐量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java一維數(shù)組和二維數(shù)組元素默認初始化值的判斷方式
這篇文章主要介紹了Java一維數(shù)組和二維數(shù)組元素默認初始化值的判斷方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08MyBatis-Plus使用sl4j日志打印SQL的代碼詳解
以下是關(guān)于使用 Spring Boot 起始器替換 slf4j-api 和 logback 依賴的詳細步驟和注意事項,包括 MyBatis-Plus 的默認日志級別信息,需要的朋友可以參考下2024-10-10Java在Map轉(zhuǎn)Json字符串時出現(xiàn)"\"轉(zhuǎn)義字符的解決辦法
當一個Map被轉(zhuǎn)成Json字符串后,被添加到另一個Map中,會出現(xiàn)被加上“\”轉(zhuǎn)義字符的情況,這個時候該如何解決呢,下面就來和小編一起了解一下2023-07-07Spring Boot接收單個String入?yún)⒌慕鉀Q方法
這篇文章主要給大家介紹了關(guān)于Spring Boot接收單個String入?yún)⒌慕鉀Q方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-11-11深入淺析Java Object Serialization與 Hadoop 序列化
序列化是指將結(jié)構(gòu)化對象轉(zhuǎn)化為字節(jié)流以便在網(wǎng)絡(luò)上傳輸或者寫到磁盤永久存儲的過程。下面通過本文給大家分享Java Object Serialization與 Hadoop 序列化,需要的朋友可以參考下2017-06-06