(更新时间)2021年6月5日 商城高并发秒杀系统(.NET Core版) 32-秒杀回滚(rabbitmq发送消息失败)
【摘要】
/// <summary>
/// 4.5、创建订单(redis + 消息队列 + lua + 方法幂等 + 失败回滚)
/// </summary>
/// <param ...
/// <summary>
/// 4.5、创建订单(redis + 消息队列 + lua + 方法幂等 + 失败回滚)
/// </summary>
/// <param name="orderDto"></param>
[HttpPost]
public PaymentDto CreateOrder(SysUser sysUser, [FromForm]OrderPo orderPo)
{
// 1、秒杀参数准备
string ProductKey = Convert.ToString(orderPo.ProductId);// 商品key
string SeckillLimitKey = "seckill_stock_:SeckillLimit" + orderPo.ProductCount; // 单品限流key
string UserBuyLimitKey = "seckill_stock_:UserId" + sysUser.UserId + "ProductId" + orderPo.ProductId;// 用户购买限制key
int productCount = orderPo.ProductCount; // 购买商品数量
int requestCountLimits = 60000; // 单品限流数量
int seckillLimitKeyExpire = 60;// 单品限流时间:单位秒
string requestIdKey = "seckill_stock_:" + orderPo.RequestId; // requestIdKey
string orderSn = OrderUtil.GetOrderCode();// 订单号
//string orderSn = distributedOrderSn.CreateDistributedOrderSn(); // 分布式订单号
// 2、执行秒杀
var SeckillResult = RedisHelper.EvalSHA(memoryCache.Get<string>("luaSha"), ProductKey, UserBuyLimitKey, SeckillLimitKey, productCount, requestCountLimits, seckillLimitKeyExpire, requestIdKey, orderSn);
if (!SeckillResult.ToString().Equals("1"))
{
throw new BizException(SeckillResult.ToString());
}
try
{
// throw new Exception("222");
// 3、发送订单消息到rabbitmq 发送失败,消息回滚
SendOrderCreateMessage(sysUser.UserId, orderSn, orderPo);
}
catch (Exception)
{
// 3.1 秒杀回滚
RedisHelper.EvalSHA(memoryCache.Get<string>("luaShaCallback"), ProductKey, UserBuyLimitKey, productCount, requestIdKey, orderSn);
// 3.2 抢购失败
throw new BizException("抢购失败");
// 3.3 少卖问题是允许的,100个商品 99个 100 个 100票
}
// 4、创建支付信息
PaymentDto paymentDto = new PaymentDto();
paymentDto.OrderSn = orderSn;
paymentDto.OrderTotalPrice = orderPo.OrderTotalPrice;
paymentDto.UserId = sysUser.UserId;
paymentDto.ProductId = orderPo.ProductId;
paymentDto.ProductName = orderPo.ProductName;
return paymentDto;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/117588169
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)