springboot有什么好的方案实现 数据实时更新吗?_你还在为 SpringBoot 服务吞吐量而烦扰吗?如何提升?...

点击上方 "程序员小乐"关注, 星标或置顶一起成长

每天凌晨00点00分, 第一时间与你相约

每日英文

The more you know who you are, and what you want, the less you let things upset you.

你越了解自己,越懂得自己想要什么,能令你沮丧的事情就越少。

每日掏心话

不要总是羡慕别人的美好,要用心经营自己的幸福。放弃了,就不该后悔。失去了,就不该回忆。

来自:lipengHeke | 责编:乐乐

链接:my.oschina.net/u/560547/blog/3162343

130c5518df74da89b8a4a1debb9c157c.png

程序员小乐(ID:study_tech)第 793 次推文 图片来自百度

往日回顾:美团面试题:Hashmap的结构,1.7和1.8有哪些区别,史上最深入的分析

正文

背景

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

方案

1、undertow替换tomcat

电子商务类型网站大多都是短请求,一般响应时间都在100ms,这时可以将web容器从tomcat替换为undertow,下面介绍下步骤:

1、增加pom配置

org.springframework.boot
spring-boot-starter-weborg.springframework.boot
spring-boot-starter-tomcatorg.springframework.boot
spring-boot-starter-undertow
2、增加相关配置server:
undertow:
direct-buffers: true
io-threads: 4
worker-threads: 160

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

2、缓存

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

3、异步

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

1、pom依赖

一般springboot引入web相关依赖就行

org.springframework.boot
spring-boot-starter-web
2、在启动类中增加@EnableAsync注解@EnableAsync
@SpringBootApplication
public class AppApplication
{
public static void main(String[] args)
{
SpringApplication.run(AppApplication.class, args);
}
}
3、需要时在指定方法中增加@Async注解,如果是需要等待返回值,则demo如下 @Async
public Future doReturn(int i){
try {
// 这个方法需要调用500毫秒
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 消息汇总
return new AsyncResult> }
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 contextMap = MDC.getCopyOfContextMap();
return () -< {
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) -< {
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,然后延时消费。

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

5bf5c9d189621d8b455ae222986a7ffb.png
7ee3ae063ebebda15faf9934c0c7f0be.png

欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,学习能力的提升上有新的认识,欢迎转发分享给更多人。

猜你还想看

阿里、腾讯、百度、华为、京东最新面试题汇集

Undertow技术:为什么很多Spring Boot开发者放弃了Tomcat?

一文了解 Kafka 分布式!

IntelliJ IDEA 最常用配置详细图解,适合刚刚用的新人!

关注订阅号「程序员小乐」,收看更多精彩内容
嘿,你在看吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值