作者:lipengHeke

my.oschina.net/u/560547/blog/3162343

背景

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

方案

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,然后延时消费。

举个例子,根据条件查询指定用户发送推送消息,这里可以时按时、按天、按月等等,这时就

猜你喜欢

1、GitHub 标星 3.2w!史上最全技术人员面试手册!FackBoo发起和总结

2、如何才能成为优秀的架构师?

3、从零开始搭建创业公司后台技术栈

4、程序员一般可以从什么平台接私活?

5、37岁程序员被裁,120天没找到工作,无奈去小公司,结果懵了...

6、滴滴业务中台构建实践,首次曝光

7、不认命,从10年流水线工人,到谷歌上班的程序媛,一位湖南妹子的励志故事

8、15张图看懂瞎忙和高效的区别!

你还在为Springboot服务吞吐量而烦扰吗?如何提升本文告诉你相关推荐

  1. 如何提升springboot服务吞吐量

    背景 生产环境偶尔会有一些慢请求导致系统性能下降,吞吐量下降,下面介绍几种优化建议. 方案 1.undertow替换tomcat 电子商务类型网站大多都是短请求,一般响应时间都在100ms,这时可以将 ...

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

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

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

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

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

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

  5. SpringBoot 服务端接口公网远程调试,并实现 HTTP 服务监听

    前后端分离项目中,在调用接口调试时候,可以通过cpolar内网穿透将本地服务端接口模拟公共网络环境远程调用调试,本次教程以Java服务端接口为例. 1. 本地环境搭建 1.1 环境参数 JDK1.8 ...

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

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

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

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

  8. SpringBoot服务监控之Actuate

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

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

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

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

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

最新文章

  1. java 中的访问修饰符
  2. 软件项目组织管理(二、三)项目管理与信息技术环境、项目管理过程组
  3. Spring Cloud 入门 之 Zuul 篇(五)
  4. android封装aidl接口,Android远端接口AIDL及服务回调用法
  5. 译 | 像使用一台主机一样管理集群
  6. 智能升级新阶段,新云原生企业如何加速出圈?
  7. 洛谷——P1151 子数整数
  8. ajax加载数据到页面无法打印的解决办法
  9. OpenCV教程(42) xml/yaml文件的读写
  10. QQ文件路径,QQ图片保存地址
  11. FusionStorage原理及组件,Java面试回忆录
  12. python正则表达式(2)
  13. “嗨聊SPACE”项目测试:利用Selenium+Firefox自动化测试对用户注册、登录、上下线提示功能以及页面之间的跳转进行测试(python脚本编写)
  14. 新书上市 | Python办公自动化(好友新书,值得一看,文末有福利)
  15. 低代码平台为何要融入BPM
  16. Android图片查看器
  17. 化工行业B2B电商通过空中分账,重塑交易生态模式
  18. 关于初学者出现Springboot启动后 服务器可以访问但不能跳转到页面
  19. 中国三角警示牌行业市场供需与战略研究报告
  20. python中的缩进快捷键_python如何缩进

热门文章

  1. HDU 1711 Number Sequence (KMP)
  2. 对象 复制构造函数
  3. 商业领域中的IT技术应用之二-POS收款机及收款系统介绍
  4. 架构之路(二):性能
  5. (转)我在赶集网的两个月(完整版)(一)
  6. 【安全系列】IPSEC ×××之安全基础篇
  7. .NET多线程编程(2)——Thread类
  8. inDesign教程,如何控制文档中的页数?
  9. InDesign入门教程,如何链接图形?
  10. MacBook Pro 如何删除多余专注模式?