黑马Redis实战篇优惠券秒杀目录
实战篇1
实战篇2
生成时间戳的方法
public static void main(String[] args) {
LocalDateTime time = LocalDateTime.of(2022, 1, 1, 0, 0, 0);
long second =time.toEpochSecond(ZoneOffset.UTC);
System.out.println("second ="+second);
}
package com.hmdp.utils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
@Component
public class RedisIdWorker {
/**
* 开始时间戳
*/
private static final long BEGIN_TIMESTAMP =1640995200L;
/**
*序列号位数
*/
private static final long COUNT_BITS = 32;
private StringRedisTemplate stringRedisTemplate;
public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
public long nextId(String keyPrefix){
//1,生成时间戳
LocalDateTime now = LocalDateTime.now();
long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
long timestamp = nowSecond - BEGIN_TIMESTAMP;
//2,生成序列号
//2.1 获取当前日期,精确到天
String date = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
//2.2 自增长
long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);
//3,拼接并返回
return timestamp << COUNT_BITS | count;
}
}
实战篇3
bug1:postman无法发送请求之Error: connect ECONNREFUSED 127.0.0.1:80
这里有两篇很好的解决bug的文章
postman 无法发送请求之Error: connect ECONNREFUSED 127.0.0.1:8080-CSDN博客
我再做一次整理。
解决bug的步骤:
1,
2,
{
"shopId": 1,
"title": "100元代金券",
"subTitle": "周一至周五均可使用",
"rules": "全场通用\\n无需预约\\n可无限叠加\\不兑现\\n仅限堂食",
"payValue": 8000,
"actualValue": 10000,
"type": 1,
"stock": 100,
"beginTime": "2023-01-26T10:09:17",
"endTime": "2023-11-30T23:00:00"
}
bug2:postman请求后是404状态码
解决方法:要将endTime修改为现在真实时间之后。
@RestController
@RequestMapping("/voucher-order")
public class VoucherOrderController {
@Resource
private IVoucherOrderService voucherOrderService;
@PostMapping("seckill/{id}")
public Result seckillVoucher(@PathVariable("id") Long voucherId) {
return voucherOrderService.seckillVoucher(voucherId);
}
}
@Service
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {
@Resource
private ISeckillVoucherService seckillVoucherService;
@Resource
private RedisIdWorker redisIdWorker;
@Override
//加上事务,因为这里有两张表
@Transactional
public Result seckillVoucher(Long voucherId) {
//1,查询优惠券
SeckillVoucher voucher = seckillVoucherService.getById(voucherId);
//2,判断秒杀是否开始
if(voucher.getBeginTime().isAfter(LocalDateTime.now())){
//尚未开始
return Result.fail("秒杀尚未开始!");
}
//3,判断秒杀是否已经结束
if(voucher.getBeginTime().isBefore(LocalDateTime.now())){
//已经结束
return Result.fail("秒杀已经结束!");
}
//4,判断库存是否充足
if(voucher.getStock()<1){
//库存不足
return Result.fail("库存不足!");
}
//5,扣减库存
boolean success = seckillVoucherService
.update().setSql("stock = stock - 1")
.eq("voucher_id", voucherId)
.update();
if(!success){
//扣除失败
return Result.fail("库存不足!");
}
//6,创建订单
VoucherOrder voucherOrder = new VoucherOrder();
//6.1 订单id
long orderId = redisIdWorker.nextId("order");
voucherOrder.setId(orderId);
//6.2 用户id
Long userId = UserHolder.getUser().getId();
voucherOrder.setUserId(userId);
//6.3 优惠券id
voucherOrder.setVoucherId(voucherId);
save(voucherOrder);
//7,返回订单id
return Result.ok(orderId);
}
}
实战篇4
方法一
方法二:与方法一的思想是一样的,只是省略了version,直接用stock(库存)来判断。
实战篇5
但不一定要相等才可以成功执行,只要stock大于零即可以执行
实战篇6
目的:防黄牛 刷单
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
实战篇7
只对createVoucherOrder做了事务管理,而没有对seckillVouch做事务管理,在
这里相当于是this.createVoucherOrder,而this. 是不纳入事务管理的。因此,要继续完善代码。
同时还有:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
添加@EnableAspectJAutoProxy
package com.hmdp.service.impl;
import com.hmdp.dto.Result;
import com.hmdp.entity.SeckillVoucher;
import com.hmdp.entity.User;
import com.hmdp.entity.Voucher;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.mapper.VoucherOrderMapper;
import com.hmdp.service.ISeckillVoucherService;
import com.hmdp.service.IVoucherOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisIdWorker;
import com.hmdp.utils.UserHolder;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
/**
* <p>
* 服务实现类
* </p>
*
* @author 虎哥
* @since 2021-12-22
*/
@Service
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {
@Resource
private ISeckillVoucherService seckillVoucherService;
@Resource
private RedisIdWorker redisIdWorker;
@Override
public Result seckillVoucher(Long voucherId) {
//1,查询优惠券
SeckillVoucher voucher = seckillVoucherService.getById(voucherId);
//2,判断秒杀是否开始
if (voucher.getBeginTime().isAfter(LocalDateTime.now())) {
//尚未开始
return Result.fail("秒杀尚未开始!");
}
//3,判断秒杀是否已经结束
if (voucher.getBeginTime().isBefore(LocalDateTime.now())) {
//已经结束
return Result.fail("秒杀已经结束!");
}
//4,判断库存是否充足
if (voucher.getStock() < 1) {
//库存不足
return Result.fail("库存不足!");
}
//一人一锁,提高效率
//intern 保证指定的userId对应指定的锁
//先获取锁,再完成以下方法,先完成方法,再释放锁,才能确保线程安全
Long userId = UserHolder.getUser().getId();
synchronized(userId.toString().intern()){
//获取代理对象(事务)
IVoucherOrderService proxy = (IVoucherOrderService) AopContext.currentProxy();
return proxy.createVoucherOrder(voucherId);
}
}
//加上事务,因为这里有两张表
@Transactional
public Result createVoucherOrder(Long voucherId) {
//5,一人一单
Long userId = UserHolder.getUser().getId();
//5.1 查询订单
int count = query().eq("user_id", userId).eq("voucher_id", voucherId).count();
//5.2 判断是否存在
if (count > 0) {
// 用户已经购买过了
return Result.fail("用户已经购买过一次!");
}
//6,扣减库存
boolean success = seckillVoucherService
.update().setSql("stock = stock - 1")
.eq("voucher_id", voucherId)
.gt("stock", 0)
.update();
if (!success) {
//扣除失败
return Result.fail("库存不足!");
}
//7,创建订单
VoucherOrder voucherOrder = new VoucherOrder();
//7.1 订单id
long orderId = redisIdWorker.nextId("order");
voucherOrder.setId(orderId);
//7.2 用户id
voucherOrder.setUserId(userId);
//7.3 优惠券id
voucherOrder.setVoucherId(voucherId);
save(voucherOrder);
//8,返回订单id
return Result.ok(orderId);
}
}