大规模并发抢购,需要细致地优化代码以应对高并发压力。以下是一些关键点:
一个简单的例子如下所示:
演示如何使用分布式锁(基于Redis的分布式锁)和消息队列(基于Spring Boot和RabbitMQ)来优化高并发抢购场景。请注意,这只是一个基本示例,实际场景可能需要更多的细节和安全性考虑。
首先,添加相关的依赖:
<!-- Spring Boot Starter for RabbitMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- Spring Data Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
然后,配置文件中添加相关配置:
# RabbitMQ Configuration
spring.rabbitmq.host=your-rabbitmq-host
spring.rabbitmq.port=5672
spring.rabbitmq.username=your-username
spring.rabbitmq.password=your-password
# Redis Configuration
spring.redis.host=your-redis-host
spring.redis.port=6379
spring.redis.password=your-redis-password
接下来,创建分布式锁工具类:
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class DistributedLock {
private final RedisTemplate<String, String> redisTemplate;
public DistributedLock(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public boolean acquireLock(String lockKey, String clientId, long expireTime) {
Boolean lock = redisTemplate.opsForValue().setIfAbsent(lockKey, clientId, expireTime, TimeUnit.MILLISECONDS);
return lock != null && lock;
}
public void releaseLock(String lockKey, String clientId) {
String lockValue = redisTemplate.opsForValue().get(lockKey);
if (clientId.equals(lockValue)) {
redisTemplate.delete(lockKey);
}
}
}
创建抢购服务:
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.TimeUnit;
@Service
public class PurchaseService {
@Autowired
private DistributedLock distributedLock;
@Autowired
private AmqpTemplate amqpTemplate;
@Transactional
public boolean purchase(Long productId, int quantity, String clientId) {
// 使用分布式锁
boolean lockAcquired = distributedLock.acquireLock("product_" + productId, clientId, 5000L);
if (!lockAcquired) {
return false; // 未获取到锁,购买失败
}
try {
// 模拟业务处理时间
TimeUnit.MILLISECONDS.sleep(1000);
// 执行抢购逻辑,此处省略...
// 发送购买消息到消息队列
amqpTemplate.convertAndSend("purchase-exchange", "purchase", "ProductID: " + productId + ", Quantity: " + quantity);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 释放分布式锁
distributedLock.releaseLock("product_" + productId, clientId);
}
return true; // 购买成功
}
}
创建消息队列监听器:
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class PurchaseMessageListener {
@RabbitListener(queues = "purchase-queue")
public void processPurchaseMessage(String message) {
// 处理购买消息,可以在此处进行订单生成、库存扣减等操作
System.out.println("Received purchase message: " + message);
}
}
这个示例中,使用了分布式锁来确保同一时刻只有一个用户能够成功抢购。购买服务在获取锁后,执行抢购逻辑,然后发送购买消息到消息队列。消息队列监听器负责处理购买消息,进行订单生成、库存扣减等操作。