[Java实战]Springboot项目高并发性能优化
zhezhongyun 2025-06-10 04:05 2 浏览
一、测试验证结果
1.1 功能测试
#优化前
curl -X POST http://127.0.0.1:8080/api/users -H "Content-Type: application/json" -d '{"name":"张三","email":"zhangsan@example.com"}' -v
#优化后
curl -X POST http://127.0.0.1:8081/api/users -H "Content-Type: application/json" -d '{"name":"张三","email":"zhangsan@example.com"}' -v
1.2 性能测试
优化前:12000 qps
优化后:36000 qps
1.3 代码运行截图
二、问题根因分析
2.1 线程模型瓶颈
Spring Boot默认的Tomcat容器使用BIO/NIO模型,每个请求占用1个线程,当长连接数超过线程池容量时会发生排队甚至拒绝
2.2 同步阻塞处理
若业务逻辑中存在同步阻塞操作(如同步数据库查询),会快速耗尽线程资源
2.3 上下文切换开销
大量线程导致CPU时间片频繁切换,实际有效利用率降低
三、Netty替代方案(针对高并发长连接场景)
3.1 原架构
package com.example.controller;
import com.example.model.User;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
@RestController
@RequestMapping("/api")
public class ApiController {
private final List<User> users = new ArrayList<>();
private final AtomicLong counter = new AtomicLong();
// 测试接口
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
// 获取所有用户
@GetMapping("/users")
public List<User> getAllUsers() {
return users;
}
// 创建用户
@PostMapping("/users")
public User createUser(@RequestBody User user) {
user.setId(counter.incrementAndGet());
users.add(user);
return user;
}
// 根据ID查询用户
@GetMapping("/users/{id}")
public User getUserById(@PathVariable Long id) {
return users.stream()
.filter(u -> u.getId().equals(id))
.findFirst()
.orElseThrow(() -> new RuntimeException("User not found"));
}
// 更新用户
@PutMapping("/users/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User userDetails) {
User user = users.stream()
.filter(u -> u.getId().equals(id))
.findFirst()
.orElseThrow(() -> new RuntimeException("User not found"));
user.setName(userDetails.getName());
user.setEmail(userDetails.getEmail());
return user;
}
// 删除用户
@DeleteMapping("/users/{id}")
public String deleteUser(@PathVariable Long id) {
users.removeIf(u -> u.getId().equals(id));
return "User deleted successfully";
}
}
3.2. 整体架构调整(推荐方案)
package com.example.demo;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.handler.traffic.ChannelTrafficShapingHandler;
import io.netty.util.concurrent.DefaultThreadFactory;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.TimeUnit;
public class NettyServer {
// 监控指标(生产环境建议使用Prometheus等专业工具)
private static int activeConnections = 0;
public static void main(String[] args) {
// 优化线程池配置
EventLoopGroup bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("boss"));
EventLoopGroup workerGroup = new NioEventLoopGroup(
Runtime.getRuntime().availableProcessors() * 2,
new DefaultThreadFactory("worker")
);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
// 内存池优化
pipeline.addLast(new ChannelTrafficShapingHandler(1024 * 1024, 1024 * 1024));
// HTTP协议处理
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(10 * 1024 * 1024));
// 超时控制(读超时60秒)
pipeline.addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS));
// 监控处理器
pipeline.addLast(new ConnectionMonitorHandler());
// 业务逻辑处理器
pipeline.addLast(new BusinessHandler());
// 统一异常处理
pipeline.addLast(new GlobalExceptionHandler());
}
})
// 服务器参数优化
.option(ChannelOption.SO_BACKLOG, 65535)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK,
new WriteBufferWaterMark(8 * 1024, 32 * 1024))
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
// 绑定端口并添加监听器
ChannelFuture f = b.bind(8081)
.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
System.out.println("================服务启动成功 端口: 8081====================");
printSystemInfo();
} else {
System.err.println("端口绑定失败: " + future.cause().getMessage());
System.exit(1);
}
})
.sync();
// 添加关闭钩子
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
System.out.println("服务已优雅关闭");
}));
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
// 监控处理器
@ChannelHandler.Sharable
private static class ConnectionMonitorHandler extends ChannelDuplexHandler {
@Override
public void channelActive(ChannelHandlerContext ctx) {
activeConnections++;
ctx.fireChannelActive();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
activeConnections--;
ctx.fireChannelInactive();
}
}
// 全局异常处理
@ChannelHandler.Sharable
private static class GlobalExceptionHandler extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
if (cause instanceof IOException && "Connection reset by peer".equals(cause.getMessage())) {
System.out.println("客户端强制断开: " + ctx.channel().remoteAddress());
ctx.close();
} else {
System.err.println("未处理异常: ");
cause.printStackTrace();
ctx.close();
}
}
}
// 系统信息打印
private static void printSystemInfo() {
System.out.println("当前配置参数:");
System.out.println("CPU核心数: " + Runtime.getRuntime().availableProcessors());
System.out.println("最大内存: " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "MB");
System.out.println("JVM参数: " + ManagementFactory.getRuntimeMXBean().getInputArguments());
}
}
3.3 关键优化点
- 异步非阻塞处理:所有IO操作在NIO线程完成,业务处理移交独立线程池
- 连接管理优化:
// 限制最大连接数
public class ConnectionLimiter extends ChannelInboundHandlerAdapter {
private static final AtomicInteger connections = new AtomicInteger(0);
private static final int MAX_CONN = 10000;
@Override
public void channelActive(ChannelHandlerContext ctx) {
if (connections.incrementAndGet() > MAX_CONN) {
ctx.close();
return;
}
// 正常处理
}
}
协议优化:采用二进制协议(如Protobuf)替代JSON,降低序列化开销
四、Spring Boot整合方案(兼容现有服务)
4.1 混合部署模式
// 在Spring Boot中启动Netty
@SpringBootApplication
public class HybridApp {
public static void main(String[] args) {
SpringApplication.run(HybridApp.class, args);
new NettyServer().start(); // 启动Netty服务
}
}
// 配置不同的端口
server.port=8080 # Spring MVC端口
netty.port=8081 # Netty服务端口
4.2 服务分流策略
- 将高频长连接请求(如即时消息、实时定位)路由到Netty服务
- 保留Spring MVC处理RESTful短连接请求
五、补充优化措施
5.1 业务逻辑优化
-// 异步处理模板
public class AsyncProcessor {
private static final Executor bizExecutor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors() * 2); // 独立业务线程池
public void process(ChannelHandlerContext ctx, Object msg) {
CompletableFuture.supplyAsync(() -> {
// 业务处理逻辑
return processBusiness(msg);
}, bizExecutor).thenAcceptAsync(result -> {
ctx.writeAndFlush(result);
}, ctx.executor()); // 回到IO线程写响应
}
}
5.2 监控配置
<!-- Netty自带监控 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-monitor</artifactId>
<version>${netty.version}</version>
</dependency>
// 连接数监控
public class ConnectionMonitor extends ChannelInboundHandlerAdapter {
private static final Counter connections = Metrics.counter("netty.connections");
@Override
public void channelActive(ChannelHandlerContext ctx) {
connections.increment();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
connections.decrement();
}
}
六、实施建议
6.1 分阶段改造
先对压力最大的接口进行Netty改造
6.2 压力测试
使用wrk进行对比测试
# 测试命令示例
./wrk -t12 -c400 -d30s -T5s -s post_test.lua http://localhost:8081/api/users
6.3 监控指标
重点关注qps、cpu利用率、gc次数等核心指标
改造后预期效果(经验值):
- 连接容量提升:从Tomcat默认200~500提升到10,000+
- CPU利用率下降:相同压力下可降低30%-50%
- 内存消耗:减少线程栈内存占用(每个线程约1MB)
建议先通过jstack分析当前线程状态,确认是否真的是线程数过多导致的问题,再针对性优化。同时要确保业务代码没有同步阻塞操作,必要时结合Arthas进行线上诊断。
希望这篇文章对你有所帮助!如果觉得不错,别忘了点赞收藏哦!
相关推荐
- 屏幕属性详解:DCI-P3、对比度、色域、Nit
-
屏幕属性详解:DCI-P3、对比度、色域、Nit---一、DCI-P3(色域标准)1.定义DCI-P3是由美国电影工业制定的广色域标准,覆盖CIE1931色彩空间的约96%,尤其强化红色和绿...
- Qt属性系统(Qt Property System)(qt的pro文件属性说明)
-
Qt提供了巧妙的属性系统,它与某些编译器支持的属性系统相似。然而,作为平台和编译器无关的库,Qt不能够依赖于那些非标准的编译器特性,比如__property或者[property]。Qt的解决方案...
- 用 Cursor 开发 10 +项目后,汇总了40 多条提示词
-
每次跟新手讲解Cursor的使用技巧时,他们总会说:"哎呀,这工具好是好,就是不知道该怎么跟它对话..."是的,不少小伙伴都在为这个困扰:手握着强大的AI编程工具,却像拿着一把...
- Excel常用技能分享与探讨(5-宏与VBA简介 VBA与数据库)
-
在VBA(VisualBasicforApplications)中使用数据库(如Access、SQLServer、MySQL等)具有以下优点,适用于需要高效数据管理和复杂业务逻辑的场景:1....
- 汽车诊断协议J1850-VPW 测试(汽车诊断协议工程师干啥的)
-
硬件说明:MCU:GD32C103120M,128K,32kRAM.输入:USB5V.OBD功能口定义:OBD(2,10)VPWM、OBD7(K线)、OBD6(CANH)、OBD14(...
- ssl证书有哪些类型?有什么区别?(ssl证书的区别)
-
以下是**Windows服务器常用快捷命令大全(100条)**,涵盖系统管理、网络诊断、安全维护、文件操作等场景,适合管理员快速操作和故障排查:---###**一、系统信息与配置**1.**`s...
- 嵌入式工程师竟然看不懂这些专业语句,那真别怪人说你菜
-
本文出自公众号:硬件笔记本,原创文章,转载请注明出处AASIC(专用集成电路)Application-SpecificIntegratedCircuit.Apieceofcustom-de...
- 同星提供SecOC信息安全解决方案(上海同星)
-
需求背景在传统的汽车电子结构中,车内的电控单元(ECU)数量和复杂性受到限制,通信带宽也受到限制。因此,人们普遍认为车内各个ECU之间的通信是可靠的。只要ECU节点接收到相应的消息,就会对其进行处理。...
- H3C MSR系列路由器EAA监控策略配置举例
-
1简介本文档介绍使用EAA监控策略进行网络监控的典型配置举例。2配置前提本文档适用于使用ComwareV7软件版本的MSR系列路由器,如果使用过程中与产品实际情况有差异,请参考相关产品手册,或以...
- 网卡DM9000裸机驱动开发详解(网卡驱动9462)
-
一、网卡1.概念网卡是一块被设计用来允许计算机在计算机网络上进行通讯的计算机硬件。由于其拥有MAC地址,因此属于OSI模型的第2层。它使得用户可以通过电缆或无线相互连接。每一个网卡都有一个被称为MA...
- 如何检验自己的手机有没有问题,实操干货!包学会
-
此贴供已购买二手苹果或正打算购买的朋友参考,已入新机的朋友也可以验一下你的手机有没有问题。无废话,直接上干货以小编随机挑的一台12为例1.外观:拿到手机先看整体外观,是否与商家描述的外观一致,磕碰和划...
- 再不看就删了!超详细的Ribbon源码解析
-
Ribbon简介什么是Ribbon?Ribbon是springcloud下的客户端负载均衡器,消费者在通过服务别名调用服务时,需要通过Ribbon做负载均衡获取实际的服务调用地址,然后通过httpcl...
- 细数ThreadLocal三大坑,内存泄露仅是小儿科
-
我在参加CodeReview的时候不止一次听到有同学说:我写的这个上下文工具没问题,在线上跑了好久了。其实这种想法是有问题的,ThreadLocal写错难,但是用错就很容易,本文将会详细总结Thre...
- 微服务架构下的Java服务监控:让程序“健康自检”不再难
-
微服务架构下的Java服务监控:让程序“健康自检”不再难引言:为什么需要服务监控?在微服务架构的世界里,我们的系统由众多小型且独立的服务组成。想象一下,这些服务就像一群跳舞的小精灵,在各自的舞台上表演...
- 6. 并发编程(并发编程模型)
-
本章深入解析Go语言并发编程核心机制,涵盖调度原理、通信模式及工程实践,结合性能优化与陷阱规避策略。6.1Goroutine基础6.1.1创建与调度//启动goroutinegofunc()...
- 一周热门
- 最近发表
- 标签列表
-
- HTML 教程 (33)
- HTML 简介 (35)
- HTML 实例/测验 (32)
- HTML 测验 (32)
- JavaScript 和 HTML DOM 参考手册 (32)
- HTML 拓展阅读 (30)
- HTML常用标签 (29)
- HTML文本框样式 (31)
- HTML滚动条样式 (34)
- HTML5 浏览器支持 (33)
- HTML5 新元素 (33)
- HTML5 WebSocket (30)
- HTML5 代码规范 (32)
- HTML5 标签 (717)
- HTML5 标签 (已废弃) (75)
- HTML5电子书 (32)
- HTML5开发工具 (34)
- HTML5小游戏源码 (34)
- HTML5模板下载 (30)
- HTTP 状态消息 (33)
- HTTP 方法:GET 对比 POST (33)
- 键盘快捷键 (35)
- 标签 (226)
- HTML button formtarget 属性 (30)
- CSS 水平对齐 (Horizontal Align) (30)