高并发场景下,Spring Boot应用的默认线程池配置和缺乏保护机制的接口设计是系统崩溃的常见诱因。后端开发中,高并发设计需要从线程池、限流、降级、缓存四个维度构建防护体系。本文以实际生产案例为基础,演示如何在Spring Boot中实现可水平扩展的高并发接口。
Tomcat线程池调优与连接器配置
Spring Boot内置Tomcat默认最大线程数200,在高并发请求下会成为瓶颈。服务器硬件资源充足时,应根据CPU核心数和请求类型调整线程池参数。
# application.yml
server:
tomcat:
threads:
max: 500 # 最大工作线程数
min-spare: 50 # 最小空闲线程数
max-connections: 10000 # 最大连接数
accept-count: 200 # 等待队列长度
connection-timeout: 5000 # 连接超时5秒
keep-alive-timeout: 15000 # 长连接超时
# 线程数经验公式
# CPU密集型:线程数 = CPU核心数 + 1
# IO密集型:线程数 = CPU核心数 * (1 + IO等待时间/CPU时间)
# 混合型:根据监控数据动态调整
对于IO密集型接口(数据库查询、远程调用),线程数不宜过高,否则频繁上下文切换反而降低吞吐量。配合异步非阻塞处理可以进一步提升单机并发能力。
自定义异步线程池与任务隔离
Spring Boot的@Async默认使用SimpleAsyncTaskExecutor,每次创建新线程无上限。生产环境必须自定义线程池并实施任务隔离,防止不同业务相互影响。
@Configuration
@EnableAsync
public class ThreadPoolConfig {
@Bean("orderExecutor")
public ThreadPoolTaskExecutor orderExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(20);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(500);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("order-async-");
executor.setRejectedExecutionHandler(
new ThreadPoolExecutor.CallerRunsPolicy()
);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(30);
executor.initialize();
return executor;
}
@Bean("notificationExecutor")
public ThreadPoolTaskExecutor notificationExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(30);
executor.setQueueCapacity(200);
executor.setThreadNamePrefix("notify-async-");
executor.setRejectedExecutionHandler(
new ThreadPoolExecutor.DiscardOldestPolicy()
);
executor.initialize();
return executor;
}
}
@Service
public class OrderService {
@Async("orderExecutor")
public CompletableFuture<OrderResult> processOrder(OrderRequest req) {
OrderResult result = doProcess(req);
return CompletableFuture.completedFuture(result);
}
@Async("notificationExecutor")
public void sendNotification(Long orderId) {
// 发送通知,失败不影响主流程
}
}
Sentinel限流与熔断降级
微服务架构中,单个服务的故障可能引发雪崩。Sentinel提供流控、熔断、热点参数限流等功能,是服务治理的重要组件。
# application.yml
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8858
filter:
enabled: true
@RestController
public class OrderController {
@GetMapping("/api/orders")
@SentinelResource(
value = "queryOrders",
blockHandler = "queryOrdersBlockHandler",
fallback = "queryOrdersFallback"
)
public Result<List<Order>> queryOrders(
@RequestParam String userId,
@RequestParam(defaultValue = "1") int page
) {
List<Order> orders = orderService.queryByUser(userId, page);
return Result.success(orders);
}
public Result<List<Order>> queryOrdersBlockHandler(
String userId, int page, BlockException ex
) {
return Result.error(429, "请求过于频繁,请稍后重试");
}
public Result<List<Order>> queryOrdersFallback(
String userId, int page, Throwable e
) {
List<Order> cached = cacheService.getOrdersFromCache(userId);
return Result.success(cached);
}
}
分布式锁防止重复提交
高并发场景下,用户快速重复点击提交按钮会产生重复请求。基于Redis实现分布式事务级别的幂等控制:
@Service
public class OrderSubmitService {
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private OrderMapper orderMapper;
public Result<String> submitOrder(OrderRequest request) {
String lockKey = "order:submit:" + request.getUserId()
+ ":" + request.getRequestToken();
Boolean locked = redisTemplate.opsForValue()
.setIfAbsent(lockKey, "1", 10, TimeUnit.SECONDS);
if (locked == null || !locked) {
return Result.error(409, "请勿重复提交");
}
try {
Order order = buildOrder(request);
orderMapper.insert(order);
// 使用消息中间件异步处理后续逻辑
mqProducer.send("order.created", order);
return Result.success(order.getId());
} catch (Exception e) {
redisTemplate.delete(lockKey);
throw e;
}
}
}
接口缓存与空值穿透防护
热点数据缓存是高并发系统的基础设施。直接缓存查询结果时需要注意缓存穿透、缓存击穿和缓存雪崩三个问题。
@Service
public class ProductCacheService {
@Autowired
private ProductMapper productMapper;
@Autowired
private StringRedisTemplate redisTemplate;
@Cacheable(value = "product", key = "#id", unless = "#result == null")
public Product getProductById(Long id) {
Product product = productMapper.selectById(id);
if (product == null) {
redisTemplate.opsForValue().set(
"product:null:" + id, "1", 30, TimeUnit.SECONDS
);
return null;
}
return product;
}
public Product getProductWithLock(Long id) {
String cacheKey = "product:" + id;
String data = redisTemplate.opsForValue().get(cacheKey);
if (data != null) {
if ("NULL".equals(data)) return null;
return JSON.parseObject(data, Product.class);
}
String lockKey = "lock:product:" + id;
try {
Boolean locked = redisTemplate.opsForValue()
.setIfAbsent(lockKey, "1", 5, TimeUnit.SECONDS);
if (locked != null && locked) {
data = redisTemplate.opsForValue().get(cacheKey);
if (data != null) {
return "NULL".equals(data) ? null
: JSON.parseObject(data, Product.class);
}
Product product = productMapper.selectById(id);
String value = product != null
? JSON.toJSONString(product) : "NULL";
int ttl = product != null ? 3600 : 30;
ttl += ThreadLocalRandom.current().nextInt(300);
redisTemplate.opsForValue()
.set(cacheKey, value, ttl, TimeUnit.SECONDS);
return product;
} else {
Thread.sleep(50);
return getProductWithLock(id);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取商品信息超时");
} finally {
redisTemplate.delete(lockKey);
}
}
}
接口超时与请求级降级
业务中台建设中,一个接口依赖多个下游服务。任何下游超时都会阻塞线程池,引发连锁故障。通过设置合理的超时和降级策略隔离故障域。
@Configuration
public class CircuitBreakerConfig {
@Bean
public CircuitBreakerRegistry circuitBreakerRegistry() {
io.github.resilience4j.circuitbreaker.CircuitBreakerConfig config =
io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.slowCallRateThreshold(60)
.slowCallDurationThreshold(Duration.ofSeconds(2))
.waitDurationInOpenState(Duration.ofSeconds(30))
.slidingWindowSize(100)
.minimumNumberOfCalls(20)
.permittedNumberOfCallsInHalfOpenState(10)
.build();
return CircuitBreakerRegistry.of(config);
}
}
@FeignClient(name = "inventory-service", fallback = InventoryFallback.class)
public interface InventoryClient {
@GetMapping("/api/stock/{productId}")
Result<Integer> getStock(@PathVariable Long productId);
}
@Component
public class InventoryFallback implements InventoryClient {
@Override
public Result<Integer> getStock(Long productId) {
return Result.success(-1);
}
}
API接口规范中应明确定义超时阈值:内部服务调用不超过2秒,数据库查询不超过500ms,缓存读取不超过50ms。超过阈值的请求触发降级逻辑返回兜底数据,保证核心链路可用。Spring Boot高并发设计的本质是通过分层保护,让系统在局部故障时仍能提供降级服务,而非整体瘫痪。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/springboot-gao-bing-fa-jie-kou-she-ji-cong-xian-cheng-chi/