背景

生产环境偶尔会有一些慢请求导致系统性能下降,吞吐量下降,下面介绍几种优化建议。

方案

1、undertow替换tomcat

电子商务类型网站大多都是短请求,一般响应时间都在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、增加相关配置

server:undertow:direct-buffers: trueio-threads: 4worker-threads: 160

重新启动可以在控制台看到容器已经切换为undertow了

2、缓存

将部分热点数据或者静态数据放到本地缓存或者redis中,如果有需要可以定时更新缓存数据

3、异步

在代码过程中我们很多代码都不需要等返回结果,也就是部分代码是可以并行执行,这个时候可以使用异步,最简单的方案是使用springboot提供的@Async注解,当然也可以通过线程池来实现,下面简单介绍下异步步骤。 1、pom依赖 一般springboot引入web相关依赖就行

<dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-web</artifactid></dependency>

2、在启动类中增加@EnableAsync注解

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

3、需要时在指定方法中增加@Async注解,如果是需要等待返回值,则demo如下

 @Asyncpublic Future<string> doReturn(int i){try {// 这个方法需要调用500毫秒Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}// 消息汇总return new AsyncResult&lt;&gt;("异步调用");}

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 {@Overridepublic Executor getAsyncExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setTaskDecorator(new MdcTaskDecorator());executor.initialize();return executor;}
}class MdcTaskDecorator implements TaskDecorator {@Overridepublic 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、业务拆分

可以将比较耗时或者不同的业务拆分出来提供单节点的吞吐量

5、集成消息队列

有很多场景对数据实时性要求不那么强的,或者对业务进行业务容错处理时可以将消息发送到kafka,然后延时消费。举个例子,根据条件查询指定用户发送推送消息,这里可以时按时、按天、按月等等,这时就  </string,></string>

如何提升springboot服务吞吐量相关推荐

  1. 用代码优雅的终止springboot服务

    用代码优雅的终止springboot服务 1. 法一: configurableApplicationContext.close() 2. 法二: System.exit(SpringApplicat ...

  2. 第13章 Kotlin 集成 SpringBoot 服务端开发(1)

    第13章 Kotlin 集成 SpringBoot 服务端开发 本章介绍Kotlin服务端开发的相关内容.首先,我们简单介绍一下Spring Boot服务端开发框架,快速给出一个 Restful He ...

  3. SpringBoot系列九:SpringBoot服务整合(整合邮件服务、定时调度、Actuator监控)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 服务整合 2.背景 在进行项目开发的时候经常会遇见以下的几个问题:需要进行邮件发送.定时的任务调 ...

  4. AndroidStudio_安卓原生开发_获取系统S/N序列号_实现在springboot服务端_设备远程保活监控---Android原生开发工作笔记159

    android项目做好了,以后,所有pad设备,在springboot服务后端,需要实现监控,那么,这个时候就需要保活机制,这个保活机制,可以让服务端,实时监控,所有的pad设备的,工作状态. 实现思 ...

  5. SpringBoot服务监控之Actuate

    springboot服务监控之Actuate: 原理及使用前配置 使用详情

  6. 面试官:聊一聊SpringBoot服务监控机制

    面试官:聊一聊SpringBoot服务监控机制 前言 SpringBoot 监控 HTTP Endpoints 监控 内置端点 health 端点 loggers 端点 metrics 端点 自定义监 ...

  7. SpringBoot服务启动无法访问localhost8080问题处理

    问题:如下图. 解决方法: 1.首先查看自己配的controller路径是否和访问路径一致: 2.在确保springboot服务正常启动的情况,打开本地host,查看localhost的ip是否使用, ...

  8. 创城优化窗口服务器,提升窗口服务,助力创城工作

    原标题:提升窗口服务,助力创城工作 4月15日,在高新区新市区政务服务大厅,办事群众正在窗口办理相关事项.记者牟敏摄 新疆网讯 (记者 牟敏)高新区(新市区)发挥政务服务窗口优势,以打造"服 ...

  9. jenkins 使用pipline实现K8S中springboot服务部署

    jenkins 使用pipline实现K8S中springboot服务部署 关键词 1. springboot 项目配置 1.1 配置 dockerfile-maven-plugin: 1.2 配置D ...

最新文章

  1. 解密淘宝网的开源架构(转)
  2. ad19电气规则检查_PROTEL DXP电气规则检查
  3. nagios监控之(监控配置)
  4. 美团点评移动网络优化实践
  5. 5 ui自适应窗口_Qt编写地图综合应用5-自适应拉伸
  6. C语言中函数调用中的传值与传址
  7. 产品经理必须要掌握的12种思维模型
  8. 甘肃省计算机能力vf考试题库,计算机等级考试二级VF模拟试题十及答案解析
  9. java试卷_Java测试题及答案(Java干货完整试卷)
  10. IE6下链接onclick事件处理中的请求被aborted
  11. C语言从入门到放弃2022年8月2号
  12. 【python教程入门学习】拒绝反爬虫!教你搞定爬虫验证码
  13. python+pyecharts画地图
  14. fatal: unable to access ‘https://gitee.com/****/****.git/‘: The requested URL returned error
  15. 图算法入门4:活动网络-AOE网络和关键路径(critical path)
  16. 如何准备pmp考试?
  17. 20201022-成信大-C语言程序设计-20201学期《C语言程序设计B》C-trainingExercises02
  18. ZZULIOJ 1135: 算菜价,Java
  19. 异步FIFO逻辑设计部分
  20. 2022-2028年全球与中国低速电动汽车行业发展趋势及投资战略分析

热门文章

  1. CodeForces - 1291D Irreducible Anagrams(思维+构造)
  2. SPOJ - TOURS Travelling tours(最小费用最大流)
  3. HDU - 1527 取石子游戏(威佐夫博弈)
  4. java groovyshell_在java中使用groovy怎么搞
  5. UVA225Golygons 黄金图形
  6. python高级语法-套接字编程之UDP和TCP编程
  7. boost使用split分割字符串
  8. 1.2句柄及 WinMain函数
  9. Frida之安装和使用教程
  10. 第31讲:抓包利器 Charles 的使用