分库分表实践:
// ShardingSphere分片策略配置
spring:
shardingsphere:
datasource:
names: ds0,ds1
rules:
sharding:
tables:
order:
actual-data-nodes: ds$->{0..1}.order_$->{0..3}
table-strategy:
standard:
sharding-column: user_id
sharding-algorithm-name: order-table-inline
性能对比数据:
优化维度 | 单库单表 | 分库分表 | 提升幅度 |
---|---|---|---|
QPS | 892 | 7,215 | 708% |
响应时间(p99) | 420ms | 68ms | 83.8% |
图2:本地缓存+Caffeine+Redis的三级缓存体系
布隆过滤器实现:
// Google Guava布隆过滤器
BloomFilter<String> filter = BloomFilter.create(
Funnels.stringFunnel(Charsets.UTF_8),
1000000,
0.01 // 误判率控制在1%
);
// 缓存查询流程
public Product getWithBloom(String productId) {
if (!filter.mightContain(productId)) {
return null; // 直接拦截无效请求
}
return redisTemplate.opsForValue().get("product:" + productId);
}
消息队列削峰方案:
// RabbitMQ延迟队列配置
@Bean
public DirectExchange delayExchange() {
return new DirectExchange("DELAY_EXCHANGE", true, false);
}
// 消费者重试机制
@RabbitListener(queues = "RETRY_QUEUE")
public void handleMessage(Message message) {
try {
// 业务处理逻辑
} catch (Exception e) {
// 三次重试机制
if (message.getMessageProperties().getRedelivered()) {
deadLetterHandler.handle(message);
} else {
rabbitTemplate.send("RETRY_EXCHANGE", "retry.key", message);
}
}
}
// Redis Lua脚本原子操作
public static final String STOCK_SCRIPT =
"local stock = tonumber(redis.call('get', KEYS[1]))\n" +
"if stock > 0 then\n" +
" redis.call('decr', KEYS[1])\n" +
" return 1\n" +
"else\n" +
" return 0\n" +
"end";
// 执行脚本
Long result = redisTemplate.execute(
new DefaultRedisScript<>(STOCK_SCRIPT, Long.class),
Collections.singletonList("seckill:stock:" + goodsId)
);
-XX:+UseG1GC
-XX:MaxGCPauseMillis=150
-XX:InitiatingHeapOccupancyPercent=45
-XX:ConcGCThreads=4
-XX:ParallelGCThreads=8
-XX:G1HeapRegionSize=4M
-XX:+PrintGCApplicationStoppedTime
-Xloggc:/var/log/gc.log
// 动态线程池监控端点
@RestController
public class ThreadPoolMonitorController {
@Autowired
private ThreadPoolTaskExecutor executor;
@GetMapping("/monitor")
public Map<String, Object> monitor() {
return Map.of(
"activeCount", executor.getActiveCount(),
"queueSize", executor.getThreadPoolExecutor().getQueue().size(),
"completedTaskCount", executor.getThreadPoolExecutor().getCompletedTaskCount()
);
}
}
// 分布式事务补偿机制
@Transactional
public void confirmOrder(String orderId) {
try {
// 业务逻辑
messageService.confirm(orderId);
} catch (Exception e) {
// 事务补偿
transactionCompensateService.add(new CompensateTask(orderId));
}
}
特别福利:关注公众号【XXX技术栈】回复"高并发"获取: