微服務(wù)實(shí)戰(zhàn)之怎樣提升springboot服務(wù)吞吐量
背景
生產(chǎn)環(huán)境偶爾會(huì)有一些慢請(qǐng)求導(dǎo)致系統(tǒng)性能下降,吞吐量下降,下面介紹幾種優(yōu)化建議。
方案
1、undertow替換tomcat
電子商務(wù)類型網(wǎng)站大多都是短請(qǐng)求,一般響應(yīng)時(shí)間都在100ms,這時(shí)可以將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重新啟動(dòng)可以在控制臺(tái)看到容器已經(jīng)切換為undertow了
2、緩存
將部分熱點(diǎn)數(shù)據(jù)或者靜態(tài)數(shù)據(jù)放到本地緩存或者redis中,如果有需要可以定時(shí)更新緩存數(shù)據(jù)
3、異步
在代碼過(guò)程中我們很多代碼都不需要等返回結(jié)果,也就是部分代碼是可以并行執(zhí)行,這個(gè)時(shí)候可以使用異步,最簡(jiǎn)單的方案是使用springboot提供的@Async注解,當(dāng)然也可以通過(guò)線程池來(lái)實(shí)現(xiàn),下面簡(jiǎn)單介紹下異步步驟。
1、pom依賴一般springboot引入web相關(guān)依賴就行
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>2、在啟動(dòng)類中增加@EnableAsync注解
@EnableAsync
@SpringBootApplication
public class AppApplication
{
public static void main(String[] args)
{
SpringApplication.run(AppApplication.class, args);
}
}3、需要時(shí)在指定方法中增加@Async注解,如果是需要等待返回值,則demo如下
@Async
public Future<String> doReturn(int i){
try {
// 這個(gè)方法需要調(diào)用500毫秒
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 消息匯總
return new AsyncResult<>("異步調(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 () -> {
try {
MDC.setContextMap(contextMap);
runnable.run();
} finally {
MDC.clear();
}
};
}
}5、有時(shí)候異步需要增加阻塞
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) -> {
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ù)拆分
可以將比較耗時(shí)或者不同的業(yè)務(wù)拆分出來(lái)提供單節(jié)點(diǎn)的吞吐量
5、集成消息隊(duì)列
有很多場(chǎng)景對(duì)數(shù)據(jù)實(shí)時(shí)性要求不那么強(qiáng)的,或者對(duì)業(yè)務(wù)進(jìn)行業(yè)務(wù)容錯(cuò)處理時(shí)可以將消息發(fā)送到kafka,然后延時(shí)消費(fèi)。
舉個(gè)例子,根據(jù)條件查詢指定用戶發(fā)送推送消息,這里可以時(shí)按時(shí)、按天、按月等等,這時(shí)就

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
ElasticSearch學(xué)習(xí)之ES Mapping實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了ElasticSearch學(xué)習(xí)之ES Mapping實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
如何在Maven項(xiàng)目中運(yùn)行JUnit5測(cè)試用例實(shí)現(xiàn)
這篇文章主要介紹了如何在Maven項(xiàng)目中運(yùn)行JUnit5測(cè)試用例實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
如何利用Java?AWT?創(chuàng)建一個(gè)簡(jiǎn)易計(jì)算器
這篇文章主要介紹了如何利用Java?AWT?創(chuàng)建一個(gè)簡(jiǎn)易計(jì)算器,AWT?是一個(gè)有助于構(gòu)建?GUI?的?API?基于?java?應(yīng)用程序,下面關(guān)于其相關(guān)資料實(shí)現(xiàn)計(jì)算器的內(nèi)容詳細(xì),需要的朋友可以參考一下2022-03-03
手把手教你實(shí)現(xiàn)Java第三方應(yīng)用登錄
本文主要介紹了手把手教你實(shí)現(xiàn)Java第三方應(yīng)用登錄,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
java中Supplier知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于java中Supplier知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-04-04
詳解Spring Boot 部署jar和war的區(qū)別
本篇文章主要介紹了詳解Spring Boot 部署jar和war的區(qū)別,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
springboot tomcat的maxHttpFormPostSize參數(shù)示例解析
這篇文章主要介紹了springboot tomcat的maxHttpFormPostSize參數(shù)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

