package com.debug.kill.server.service.impl;/**
* Created by Administrator on 2019/6/17.
*/
import com.debug.kill.model.dto.KillSuccessUserInfo;
import com.debug.kill.model.entity.*;
import com.debug.kill.model.mapper.*;
import com.debug.kill.server.enums.SysConstant;
import com.debug.kill.server.service.IKillService;
import com.debug.kill.server.service.RabbitSenderService;
import com.debug.kill.server.utils.RandomUtil;
import com.debug.kill.server.utils.SnowFlake;
import com.google.common.collect.Maps;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.joda.time.DateTime;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @Author:debug (SteadyJack)
* @Date: 2019/6/17 22:21
**/
@Service
public class KillService implements IKillService {
private static final Logger log= LoggerFactory.getLogger(KillService.class);
private SnowFlake snowFlake=new SnowFlake(2,3);
@Autowired
private ItemKillSuccessMapper itemKillSuccessMapper;
@Autowired
private ItemKillMapper itemKillMapper;
@Autowired
private RabbitSenderService rabbitSenderService;
@Autowired
private Environment env;
@Autowired
private NewBeeMallOrderMapper mallOrderMapper;
@Autowired
private NewBeeMallOrderItemMapper orderItemMapper;
@Autowired
private UserMapper userMapper;
/**
* 商品秒杀核心业务逻辑的处理
* @param killId
* @param userId
* @return
* @throws Exception
*/
@Override
public Boolean killItem(Integer killId, Integer userId) throws Exception {
Boolean result=false;
//TODO:判断当前用户是否已经抢购了当前商品
if (itemKillSuccessMapper.countByKillUserId(killId,userId) <= 0){
//TODO:判断当前代抢购的商品库存是否充足、以及是否出在可抢的时间段内 - canKill
ItemKill itemKill=itemKillMapper.selectById(killId);
if (itemKill!=null && 1==itemKill.getCanKill()){
//TODO:扣减库存-减1
int res=itemKillMapper.updateKillItem(killId);
if (res>0){
//TODO:判断是否扣减成功了?是-生成秒杀成功的订单、同时通知用户秒杀已经成功(在一个通用的方法里面实现)
this.commonRecordKillSuccessInfo(itemKill,userId);
result=true;
}
}
}else{
throw new Exception("您已经抢购过该商品了!");
}
return result;
}
/**
* 通用的方法-记录用户秒杀成功后生成的订单-并进行异步邮件消息的通知
* @param kill
* @param userId
* @throws Exception
*/
private void commonRecordKillSuccessInfo(ItemKill kill, Integer userId) throws Exception{
//TODO:记录抢购成功后生成的秒杀订单记录
ItemKillSuccess entity=new ItemKillSuccess();
String orderNo=String.valueOf(snowFlake.nextId());
//entity.setCode(RandomUtil.generateOrderCode()); //传统时间戳+N位随机数
entity.setCode(orderNo); //雪花算法
entity.setItemId(kill.getItemId());
entity.setKillId(kill.getId());
entity.setUserId(userId.toString());
entity.setStatus(SysConstant.OrderStatus.SuccessNotPayed.getCode().byteValue());
entity.setCreateTime(DateTime.now().toDate());
//TODO:学以致用,举一反三 -> 仿照单例模式的双重检验锁写法
if (itemKillSuccessMapper.countByKillUserId(kill.getId(),userId) <= 0){
int res=itemKillSuccessMapper.insertSelective(entity);
if (res>0){
//TODO:进行异步邮件消息的通知=rabbitmq+mail
rabbitSenderService.sendKillSuccessEmailMsg(orderNo);
//TODO:入死信队列,用于 “失效” 超过指定的TTL时间时仍然未支付的订单
rabbitSenderService.sendKillSuccessOrderExpireMsg(orderNo);
//插入后台管理专属的订单表
try {
NewBeeMallOrder order = new NewBeeMallOrder();
order.setOrderNo(orderNo);
order.setUserId(Long.valueOf(userId));
User user=userMapper.selectByPrimaryKey(userId);
if (user!=null){
order.setUserAddress("用户名:"+user.getUserName()+" -- 用户邮箱:"+user.getEmail());
}
//总价
order.setTotalPrice(kill.getPrice());
if (mallOrderMapper.insertSelective(order) > 0) {
NewBeeMallOrderItem item=new NewBeeMallOrderItem();
item.setOrderId(order.getOrderId());
item.setGoodsId(Long.valueOf(kill.getItemId()));
item.setGoodsName(kill.getItemName());
item.setSellingPrice(kill.getPrice());
item.setGoodsCount(1);
item.setCreateTime(DateTime.now().toDate());
orderItemMapper.insertSelective(item);
}
}catch (Exception e){
log.error("插入后台管理专属的订单表-发生异常:",e);
}
}
}
}
/**
* 商品秒杀核心业务逻辑的处理-mysql的优化
* @param killId
* @param userId
* @return
* @throws Exception
*/
@Override
public Boolean killItemV2(Integer killId, Integer userId) throws Exception {
Boolean result=false;
//TODO:判断当前用户是否已经抢购过当前商品
if (itemKillSuccessMapper.countByKillUserId(killId,userId) <= 0){
//TODO:A.查询待秒杀商品详情
ItemKill itemKill=itemKillMapper.selectByIdV2(killId);
//TODO:判断是否可以被秒杀canKill=1?
if (itemKill!=null && 1==itemKill.getCanKill() && itemKill.getTotal()>0){
//TODO:B.扣减库存-减一
int res=itemKillMapper.updateKillItemV2(killId);
//TODO:扣减是否成功?是-生成秒杀成功的订单,同时通知用户秒杀成功的消息
if (res>0){
commonRecordKillSuccessInfo(itemKill,userId);
result=true;
}
}
}else{
throw new Exception("您已经抢购过该商品了!");
}
return result;
}
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 商品秒杀核心业务逻辑的处理-redis的分布式锁
* @param killId
* @param userId
* @return
* @throws Exception
*/
@Override
public Boolean killItemV3(Integer killId, Integer userId) throws Exception {
Boolean result=false;
if (itemKillSuccessMapper.countByKillUserId(killId,userId) <= 0){
//TODO:借助Redis的原子操作实现分布式锁-对共享操作-资源进行控制
ValueOperations valueOperations=stringRedisTemplate.opsForValue();
final String key=new StringBuffer().append(killId).append(userId).append("-RedisLock").toString();
final String value=RandomUtil.generateOrderCode();
Boolean cacheRes=valueOperations.setIfAbsent(key,value); //lua脚本提供“分布式锁服务”,就
没有合适的资源?快使用搜索试试~ 我知道了~
基于SpringBoot+Mybatis+Mysql+中间件构建的商城秒杀系统
共256个文件
xml:170个
java:48个
jsp:12个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 28 浏览量
2023-06-01
09:07:49
上传
评论
收藏 447KB ZIP 举报
温馨提示
基于SpringBoot+Mybatis+Mysql+中间件构建的商城秒杀系统;其中,中间件主要包括:缓存中间件Redis、消息中间件RabbitMQ、统一协调调度中心中间件ZooKeeper、综合中间件Redisson等等
资源推荐
资源详情
资源评论
收起资源包目录
基于SpringBoot+Mybatis+Mysql+中间件构建的商城秒杀系统 (256个子文件)
bootstrap.css 134KB
bootstrap.min.css 111KB
bootstrap-theme.css 21KB
bootstrap-theme.min.css 19KB
detail.css 31B
glyphicons-halflings-regular.eot 20KB
.gitignore 278B
server.iml 15KB
model.iml 5KB
api.iml 2KB
SpringBootSecondKill.iml 862B
KillService.java 13KB
KillController.java 9KB
RabbitmqConfig.java 7KB
RabbitSenderService.java 6KB
UserController.java 4KB
RabbitReceiverService.java 4KB
BaseController.java 4KB
SnowFlake.java 3KB
CustomRealm.java 3KB
ShiroConfig.java 2KB
MailService.java 2KB
ItemController.java 2KB
SchedulerService.java 2KB
RedisConfig.java 2KB
Item.java 1KB
NewBeeMallOrderItemMapper.java 1KB
ItemKillSuccess.java 1KB
ItemService.java 1KB
RandomUtil.java 1KB
ZooKeeperConfig.java 1KB
NewBeeMallOrder.java 1KB
MainApplication.java 1KB
NewBeeMallOrderMapper.java 1KB
BaseResponse.java 953B
RedissonConfig.java 946B
ItemKillSuccessMapper.java 928B
SysConstant.java 875B
CodeGenerateSnowThread.java 848B
NewBeeMallOrderItem.java 844B
KillSuccessUserInfo.java 777B
CodeGenerateThread.java 722B
ItemKill.java 711B
SchedulerConfig.java 688B
StatusCode.java 635B
IKillService.java 609B
GlobalExceptionHandler.java 580B
UserMapper.java 566B
MailDto.java 530B
User.java 478B
LoginException.java 463B
ItemKillMapper.java 441B
KillDto.java 404B
RandomCodeMapper.java 397B
RandomCode.java 394B
ItemMapper.java 355B
IItemService.java 305B
Main.java 164B
Main.java 162B
jquery.js 95KB
bootstrap.js 66KB
bootstrap.min.js 34KB
jquery.countdown.min.js 5KB
jquery.cookie.min.js 1KB
npm.js 496B
info.jsp 4KB
list.jsp 3KB
killRecord.jsp 2KB
register.jsp 2KB
executeMqResult.jsp 2KB
login.jsp 2KB
executeSuccess.jsp 1KB
welcome.jsp 459B
head.jsp 369B
executeFail.jsp 352B
error.jsp 203B
tag.jsp 202B
bootstrap.css.map 358KB
bootstrap-theme.css.map 41KB
application.properties 5KB
log4j.properties 2KB
application-local.properties 0B
db_second_kill.sql 5KB
glyphicons-halflings-regular.svg 61KB
glyphicons-halflings-regular.ttf 40KB
glyphicons-halflings-regular.woff 23KB
workspace.xml 144KB
server_war_exploded.xml 11KB
pom.xml 9KB
NewBeeMallOrderMapper.xml 9KB
NewBeeMallOrderItemMapper.xml 7KB
ItemKillSuccessMapper.xml 6KB
UserMapper.xml 5KB
ItemMapper.xml 4KB
mybatis-config.xml 4KB
ItemKillMapper.xml 3KB
spring-shiro.xml 2KB
spring-jdbc.xml 2KB
RandomCodeMapper.xml 2KB
pom.xml 2KB
共 256 条
- 1
- 2
- 3
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6732
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功