mirror of
https://gitee.com/52itstyle/spring-boot-seckill.git
synced 2025-12-30 10:22:26 +00:00
最佳实践秒杀六(Redis原子递减)
This commit is contained in:
@@ -180,4 +180,45 @@ public class SeckillDistributedController {
|
|||||||
}
|
}
|
||||||
return Result.ok();
|
return Result.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value="秒杀六(Redis原子递减)",nickname="爪哇笔记")
|
||||||
|
@PostMapping("/startRedisCount")
|
||||||
|
public Result startRedisCount(long secKillId){
|
||||||
|
/**
|
||||||
|
* 还原数据
|
||||||
|
*/
|
||||||
|
seckillService.deleteSeckill(secKillId);
|
||||||
|
int count = 1000;
|
||||||
|
/**
|
||||||
|
* 初始化商品个数
|
||||||
|
*/
|
||||||
|
redisUtil.cacheValue(secKillId+"-num",100);
|
||||||
|
final long killId = secKillId;
|
||||||
|
LOGGER.info("开始秒杀六");
|
||||||
|
for(int i=0;i<count;i++){
|
||||||
|
final long userId = i;
|
||||||
|
Runnable task = () -> {
|
||||||
|
/**
|
||||||
|
* 原子递减
|
||||||
|
*/
|
||||||
|
long number = redisUtil.decr(secKillId+"-num",1);
|
||||||
|
if(number>=0){
|
||||||
|
seckillService.startSeckilAopLock(secKillId, userId);
|
||||||
|
LOGGER.info("用户:{}秒杀商品成功",userId);
|
||||||
|
}else{
|
||||||
|
LOGGER.info("用户:{}秒杀商品失败",userId);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
executor.execute(task);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(10000);
|
||||||
|
redisUtil.cacheValue(killId+"", null);
|
||||||
|
Long secKillCount = seckillService.getSeckillCount(secKillId);
|
||||||
|
LOGGER.info("一共秒杀出{}件商品",secKillCount);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return Result.ok();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.itstyle.seckill.web;
|
package com.itstyle.seckill.web;
|
||||||
|
|
||||||
|
import com.itstyle.seckill.common.redis.RedisUtil;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
|
||||||
@@ -9,6 +10,8 @@ import javax.jms.Destination;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.apache.activemq.command.ActiveMQQueue;
|
import org.apache.activemq.command.ActiveMQQueue;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
@@ -30,12 +33,17 @@ import com.itstyle.seckill.service.ISeckillService;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/seckillPage")
|
@RequestMapping("/seckillPage")
|
||||||
public class SeckillPageController {
|
public class SeckillPageController {
|
||||||
|
|
||||||
@Autowired
|
private final static Logger LOGGER = LoggerFactory.getLogger(SeckillPageController.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
private ISeckillService seckillService;
|
private ISeckillService seckillService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ActiveMQSender activeMQSender;
|
private ActiveMQSender activeMQSender;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisUtil redisUtil;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private HttpClient httpClient;
|
private HttpClient httpClient;
|
||||||
@@ -82,4 +90,20 @@ public class SeckillPageController {
|
|||||||
return Result.error("验证失败");
|
return Result.error("验证失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value="最佳实践)",nickname="爪哇笔记")
|
||||||
|
@PostMapping("/startRedisCount")
|
||||||
|
public Result startRedisCount(long secKillId,long userId){
|
||||||
|
/**
|
||||||
|
* 原子递减
|
||||||
|
*/
|
||||||
|
long number = redisUtil.decr(secKillId+"-num",1);
|
||||||
|
if(number>=0){
|
||||||
|
seckillService.startSeckilDBPCC_TWO(secKillId, userId);
|
||||||
|
LOGGER.info("用户:{}秒杀商品成功",userId);
|
||||||
|
}else{
|
||||||
|
LOGGER.info("用户:{}秒杀商品失败",userId);
|
||||||
|
}
|
||||||
|
return Result.ok();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user