秒杀系统的实现主要有两步:
1.分布式限流 :使用消息队列的方式,来实现削峰
2.分布式锁
1.基于数据库来实现分布式锁
2.基于redis中的实现分布式锁
3.基于zookeeper实现分布式锁
这里以redis实现分布式锁举例
有以下几个操作:
1.加锁
加锁实际上就是在redis中,给Key键设置一个值,为避免死锁,并给定一个过期时间。
SET lock_key random_value NX PX 5000
值得注意的是:
random_value 是客户端生成的唯一的字符串。
NX 代表只在键不存在时,才对键进行设置操作。
PX 5000 设置键的过期时间为5000毫秒。
这样,如果上面的命令执行成功,则证明客户端获取到了锁。
2.解锁
解锁的过程就是将Key键删除。但也不能乱删,不能说客户端1的请求将客户端2的锁给删除掉。这时候random_value的作用就体现出来。
为了保证解锁操作的原子性,我们用LUA脚本完成这一操作。先判断当前锁的字符串是否与传入的值相等,是的话就删除Key,解锁成功。
if redis.call('get',KEYS[1]) == ARGV[1] then
return redis.call('del',KEYS[1])
else
return 0
end
3.锁过期
为了避免发生死锁,设置过期时间。
依赖
<!--rabbitmq消息队列-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.43</version>
</dependency>
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis数据库索引(默认为0)
spring.redis.database=0
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=50
#连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=20
#连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=2
##配置rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#自定义参数:并发消费者的初始化值
spring.rabbitmq.listener.concurrency=10
#自定义参数:并发消费者的最大值
spring.rabbitmq.listener.max-concurrency=20
#自定义参数:每个消费者每次监听时可拉取处理的消息数量
spring.rabbitmq.listener.prefetch=5
#用于处理订单的消息队列
order.queue.name=order.queue.name
order.exchange.name =order.exchange.name
order.routing.key.name=order.routing.key.name
/**
* rabbitmq配置类
*/
@Configuration
public class RabbitmqConfig {
@Autowired
private Environment env;
@Autowired
private CachingConnectionFactory connectionFactory;
@Autowired
private SimpleRabbitListenerContainerFactoryConfigurer factoryConfigurer;
//日志记录器
private static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
/**
* 单一消费者,即为队列消息
* @return
*/
@Bean(name = "singleListenerContainer")
public SimpleRabbitListenerContainerFactory listenerContainer(){
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
factory.setConcurrentConsumers(1);
factory.setMaxConcurrentConsumers(1);
factory.setPrefetchCount(1);
factory.setTxSize(1);
factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
return factory;
}
/**
* 多个消费者,即主题消息
* @return
*/
@Bean(name = "multiListenerContainer")
public SimpleRabbitListenerContainerFactory multiListenerContainer(){
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factoryConfigurer.configure(factory,connectionFactory);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
factory.setAcknowledgeMode(AcknowledgeMode.NONE);
//关于并发配置
factory.setConcurrentConsumers(env.getProperty("spring.rabbitmq.listener.concurrency",int.class));
factory.setMaxConcurrentConsumers(env.getProperty("spring.rabbitmq.listener.max-concurrency",int.class));
factory.setPrefetchCount(env.getProperty("spring.rabbitmq.listener.prefetch",int.class));
return factory;
}
/**
*RabbitTemplate工具类
* @return
*/
@Bean
public RabbitTemplate rabbitTemplate(){
connectionFactory.setPublisherConfirms(true);
connectionFactory.setPublisherReturns(true);
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMandatory(true);
//默认是开启应答成功后的回调函数
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
logger.info("消息发送成功:correlationData({}),ack({}),cause({})",correlationData,ack,cause);
}
});
// 消息是否从Exchange路由到Queue, 注意: 这是一个失败回调, 只有消息从Exchange路由到Queue失败才会回调这个方法
rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
logger.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}",exchange,routingKey,replyCode,replyText,message);
}
});
return rabbitTemplate;
}
/**生成rabbitmq组件
* 队列:用来存储消息的数据结构,位于硬盘或内存中。
* 交换机:接收发送到RabbitMQ中的消息并决定把他们投递到那个队列的组件;
* 绑定:一套规则,用于告诉交换器消息应该被存储到哪个队列。
*
* **/
/**处理订单的消息队列**/
//定义队列
@Bean(name ="orderQueue")
public Queue orderQueue(){
return new Queue(env.getProperty("order.queue.name"),true);
}
//定义交换机 TopicExchange为主题消息交换器
@Bean
public DirectExchange orderExchange(){
return new DirectExchange(env.getProperty("order.exchange.name"),true,false);
}
//定义绑定
@Bean
public Binding orderBinging(){
return BindingBuilder.bind(orderQueue()).to(orderExchange()).with( env.getProperty("order.routing.key.name"));
}
}
/**
* redis实现分布式锁机制
*/
@Component
public class RedisLock {
//日志记录器
private static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 加锁
* @param key
* @param value 当前事件+超时事件
* @return
*/
public boolean lock(String key,String value){
//加锁成功
if (redisTemplate.opsForValue().setIfAbsent(key,value)){
return true;
}
//假如currentValue=A先占用了锁 其他两个线程的value都是B,保证其中一个线程拿到锁
String currentValue = redisTemplate.opsForValue().get(key);
//锁过期 防止出现死锁
if (!StringUtils.isEmpty(currentValue) &&
Long.parseLong(currentValue) < System.currentTimeMillis()){
//获取上一步锁的时间
String oldValue = redisTemplate.opsForValue().getAndSet(key, value);
if (!StringUtils.isEmpty(oldValue) &&
oldValue.equals(currentValue)){
return true;
}
}
return false;
}
/**
* 解锁
* @param key
* @param value
*/
public void unlock(String key,String value){
try {
String currentValue = redisTemplate.opsForValue().get(key);
if (!StringUtils.isEmpty(currentValue) &&
currentValue.equals(value)){
redisTemplate.opsForValue().getOperations().delete(key);
}
}catch (Exception e){
logger.error("【redis分布式锁】 解锁异常,{}",e);
}
}
}
/**
* 这是用户模块的Controller
*/
@RestController
@RequestMapping("/attendance/commodity")
@Api(value = "商品模块")
public class CommodityController {
@Autowired
CommodityService commodityService;
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
RedisTemplate redisTemplate;
@Autowired
private Environment env;
//日志记录器
private static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
/**
* 新建商品秒杀
*/
@PostMapping("/createSeckill")
@ApiOperation(value = "新建商品秒杀")
public ResponseResult createSeckill(HttpServletRequest request) {
logger.info("start UserController.createSeckill");
CommodityPojo commodityPojo=new CommodityPojo();
commodityPojo.setCommodityAllNum(1000);
commodityPojo.setCommodityName("测试商品一");
commodityService.createSeckill(commodityPojo);
logger.info("end UserController.createSeckill");
return ResponseResult.success(ConstantsUtil.OPERATE_SUCCESS);
}
/**
* 进行商品秒杀
*/
@PostMapping("/seckill")
@ApiOperation(value = "进行商品秒杀")
public ResponseResult seckill(HttpServletRequest request) {
logger.info("start UserController.seckill");
String productId ="43486796d2871dfbec0f62951750ea4c";
String user ="admin";
OrderPojo orderPojo =new OrderPojo();
orderPojo.setUserId(user);
orderPojo.setProductId(productId);
orderPojo.setOrderPrice(100L);
orderPojo.setTempId(CommonUtil.getUUID());
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
rabbitTemplate.setExchange(env.getProperty("order.exchange.name"));
rabbitTemplate.setRoutingKey(env.getProperty("order.routing.key.name"));
try {
Message message = MessageBuilder.withBody(JacksonUtils.toJson(orderPojo).getBytes("UTF-8")).build();
//设置请求编码格式
message.getMessageProperties().setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME, MessageProperties.CONTENT_TYPE_JSON);
rabbitTemplate.convertAndSend(message);
}catch (Exception e){
logger.error("订单消息生产者发送消息失败"+e.getMessage(),e);
return ResponseResult.error(ConstantsUtil.OPERATE_ERROR);
}
logger.info("end UserController.seckill");
return ResponseResult.success(orderPojo.getTempId(),ConstantsUtil.OPERATE_SUCCESS);
}
/**
* 获取进行商品秒杀的结果
*/
@PostMapping("/getSeckillResultMent")
@ApiOperation(value = "获取进行商品秒杀的结果")
public ResponseResult getSeckillResultMent(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
logger.info("start UserController.getSeckillResultMent");
ResponseResult responseResult=null;
String id=jsonObject.get("id").toString();
Object o = redisTemplate.opsForValue().get("seckill_rep_"+id);
if(o !=null){
String tmp =o.toString();
try {
responseResult=JacksonUtils.toEntity(tmp,ResponseResult.class);
}catch (Exception e){
logger.error("获取进行商品秒杀的结果异常"+e.getStackTrace(),e);
}
}else {
responseResult =ResponseResult.error(ConstantsUtil.QUERY_ERROR);
}
logger.info("end UserController.getSeckillResultMent");
return responseResult;
}
}
/**
* 这里是消息队列的消费者
*/
@Component
public class CommonMqListener {
@Autowired
OrderService orderService;
@Autowired
RedisTemplate redisTemplate;
private static ObjectMapper objectMapper = new ObjectMapper();
//日志记录器
private static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
/**
* 监听消费订单消息消息
* @param message
*/
@RabbitListener(queues = "${order.queue.name}",containerFactory = "singleListenerContainer")
public void consumeOrderQueue(@Payload byte[] message) {
try {
OrderPojo orderPojo = objectMapper.readValue(message,OrderPojo.class);
ResponseResult responseResult= orderService.seckillNewOrder(orderPojo);
//将结果存放在redis中 10s
redisTemplate.opsForValue().set("seckill_rep_"+orderPojo.getTempId(), JacksonUtils.toJson(responseResult),10, TimeUnit.SECONDS);
logger.info("监听消费 监听到消息: {} ,响应结果:{}", orderPojo.toString(),responseResult.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Service
@Transactional
public class OrderService extends ServiceImpl<OrderPojoMapper, OrderPojo> implements IOrderService {
@Autowired
OrderPojoMapper orderPojoMapper;
/** 超时时间 */
private static final int TIMEOUT = 5000;
@Autowired
RedisLock redisLock;
@Autowired
RedisTemplate redisTemplate;
//日志记录器
private static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
/**
* 秒杀中订单
* @param orderPojo
* @return
*/
@Override
public ResponseResult seckillNewOrder(OrderPojo orderPojo) {
String msg="";
String productId=orderPojo.getProductId();
long time = System.currentTimeMillis() + TIMEOUT;
//加锁
String lockKey = "seckill:"+productId;
if (!redisLock.lock(lockKey,String.valueOf(time))){
return ResponseResult.error(ConstantsUtil.seckill_fail);
}
//进行商品抢购(商品数减一)
int stockNum = 0;
Object tmp =redisTemplate.opsForValue().get("seckill_"+productId);
if(tmp!=null){
String stockNumStr =tmp.toString();
if(StringUtils.isNotBlank(stockNumStr)){
stockNum = Integer.valueOf(stockNumStr);
}
if (stockNum == 0) {
//库存不足
return ResponseResult.error(ConstantsUtil.seckill_fail2);
} else {
redisTemplate.opsForValue().set("seckill_"+productId,String.valueOf(stockNum-1));
msg="恭喜你抢到商品,剩余商品数量为"+(stockNum-1);
//创建订单
orderPojoMapper.insert(orderPojo);
}
}else {
return ResponseResult.error(ConstantsUtil.seckill_fail3);
}
//解锁
redisLock.unlock(lockKey, String.valueOf(time));
return ResponseResult.success(msg);
}
}