Java spike combat system series - Product Code spike combat

Summary:

This post is "Java spike combat system a series of articles," the sixth chapter, this post we will go into the whole spike code development system core functional modules, namely the code real "commodity spike" function module.

content:

"Commodity spike" function module is based on the "Product Details" function module above, for this function module, its main core processes that: front-end buying initiate the request, the request will carry some of the requested data: pending Id Id with spike data such as the current user; backend interface after receiving the request, will perform a series of processing logic determines the spike, will eventually return to the processing result to the front end.

Wherein the series of processing logic determines the backend interface spike was quite complex, Debug flow diagrams which become the following:

 

As can be seen from the flowchart of the service, the rear end of the front-end user interface upon receiving a request spike, the processing logic of the core:

(1) first determines whether the current user has snapped through the product, and if not, does not represent the user buy this item before, can enter the next processing logic

(2) for determining the remaining quantity of the goods can grab, i.e., the adequacy of stock (i.e., is greater than 0), and if so, processing logic proceeds to the next step

(3) deduction inventory, and update the corresponding buying records inventory database (usually minus one operation), to determine whether to update the inventory database operations successful, and if so, create a user spike successful orders, and asynchronously send text messages or e-mail notification message notification user

(4) the above logic operation step is if there is any condition is not satisfied, then the process ends the entire spike, spike i.e. failure!

Next, we are still based on the MVC pattern of development, the use of actual code that perform this function module!

(1) The first is the development of a method receiving function "front-end user requests spike" in KillController controller, wherein the front end of the process needs to receive a request over the "to be Id spike", and the current Id through the user's blog post presentation Shiro's session module session be acquired!

Its source code is shown below:

private static final String prefix = "kill";
 
@Autowired
private IKillService killService;
 
@Autowired
private ItemKillSuccessMapper itemKillSuccessMapper;
 
/***
 * 商品秒杀核心业务逻辑
 */
@RequestMapping(value = prefix+"/execute",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public BaseResponse execute(@RequestBody @Validated KillDto dto, BindingResult result, HttpSession session){
    if (result.hasErrors() || dto.getKillId()<=0){
        return new BaseResponse(StatusCode.InvalidParams);
}
//获取当前登录用户的信息
    Object uId=session.getAttribute("uid");
    if (uId==null){
        return new BaseResponse(StatusCode.UserNotLogin);
    }
    Integer userId= (Integer)uId ;
    BaseResponse response=new BaseResponse(StatusCode.Success);
    try {
        Boolean res=killService.killItem(dto.getKillId(),userId);
        if (!res){
            return new BaseResponse(StatusCode.Fail.getCode(),"哈哈~商品已抢购完毕或者不在抢购时间段哦!");
        }
    }catch (Exception e){
        response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
    }
    return response;
}
 

 

 

其中,KillDto对象主要封装了“待秒杀Id”等字段信息,其主要用于接收前端过来的用户秒杀请求信息,源代码如下所示:  

@Data
@ToString
public class KillDto implements Serializable{
    @NotNull
    private Integer killId;
 
    private Integer userId; //在整合shiro之后,userId字段可以不需要了!因为通过session进行获取了
}
 

 

(2)紧接着是开发  killService.killItem(dto.getKillId(),userId) 的功能,该功能对应的代码的编写逻辑可以参见本文刚开始介绍时的流程图!其完整源代码如下所示:  

@Autowired
private ItemKillSuccessMapper itemKillSuccessMapper;
 
@Autowired
private ItemKillMapper itemKillMapper;
 
@Autowired
private RabbitSenderService rabbitSenderService;
 
//商品秒杀核心业务逻辑的处理
@Override
public Boolean killItem(Integer killId, Integer userId) throws Exception {
    Boolean result=false;
 
    //TODO:判断当前用户是否已经抢购过当前商品
    if (itemKillSuccessMapper.countByKillUserId(killId,userId) <= 0){
        //TODO:查询待秒杀商品详情
        ItemKill itemKill=itemKillMapper.selectById(killId);
 
        //TODO:判断是否可以被秒杀canKill=1?
        if (itemKill!=null && 1==itemKill.getCanKill() ){
            //TODO:扣减库存-减一
            int res=itemKillMapper.updateKillItem(killId);
 
            //TODO:扣减是否成功?是-生成秒杀成功的订单,同时通知用户秒杀成功的消息
            if (res>0){
                commonRecordKillSuccessInfo(itemKill,userId);
 
                result=true;
            }
        }
    }else{
        throw new Exception("您已经抢购过该商品了!");
    }
    return result;
}

 

其中,itemKillMapper.selectById(killId); 表示用于获取待秒杀商品的详情信息,这在前面的篇章中已经介绍过了;而 itemKillMapper.updateKillItem(killId); 主要用于扣减库存(在这里是减1操作),其对应的动态Sql如下所示:

<!--抢购商品,剩余数量减一-->
  <update id="updateKillItem">
    UPDATE item_kill
    SET total = total - 1
    WHERE
        id = #{killId}
  </update>

 

(3)值得一提的是,在上面 KillService执行killItem功能方法时,还开发了一个通用的方法:用户秒杀成功后创建秒杀订单、并异步发送通知消息给到用户秒杀成功的信息!该方法为 commonRecordKillSuccessInfo(itemKill,userId); 其完整的源代码如下所示:

/**
 * 通用的方法-用户秒杀成功后创建订单-并进行异步邮件消息的通知
 * @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);
        }
    }
}

 

该方法涉及的功能模块稍微比较多,即主要包含了“分布式唯一ID-雪花算法的应用”、“整合RabbitMQ异步发送通知消息给用户”、“基于JavaMail开发发送邮件的功能”、“死信队列失效超时未支付的订单”等等,这些功能模块将在后面的小节一步一步展开进行介绍!

(4)最后是需要在前端页面info.jsp开发“提交用户秒杀请求”的功能,其部分核心源代码如下所示:

 

其中,提交的数据是采用application/json的格式提交的,即json的格式!并采用POST的请求方法进行交互!

(5)将整个系统、项目采用外置的tomcat运行起来,观察控制台的输出信息,如果没有报错信息,则代表整体的实战代码没有语法级别的错误!点击“详情”按钮,登录成功后,进入“待秒杀商品的的详情”,可以查看当前待秒杀商品的详情信息;点击“抢购”按钮,即可进入“秒杀”环节,后端经过一系列的逻辑处理之后,将处理的结果返回给到前端,如下图所示:

 

与此同时,当前用户的邮箱中将收到一条“秒杀成功”的邮件信息,表示当前用户已经成功秒杀抢到当前商品了,如下图所示:  

除此之外,在数据库表item_kill_success中也将会生成一笔“秒杀成功的订单记录”,如下图所示:

 

当然,对于“邮件的通知”和“秒杀成功生成的订单的订单编号”的功能,我们将在后面的篇章进行分享介绍,在本节我们主要是分享介绍了秒杀系统中用户的“秒杀/抢购请求”功能!  

 

补充: 

 

1、目前,这一秒杀系统的整体构建与代码实战已经全部完成了,完整的源代码数据库地址可以来这里下载:https://gitee.com/steadyjack/SpringBoot-SecondKill 记得Fork跟Star啊!!!

2、实战期间有任何问题都可以留言或者与Debug联系、交流;QQ技术交流群:605610429,顺便关注一下Debug的技术微信公众号呗:

Guess you like

Origin www.cnblogs.com/SteadyJack/p/11228391.html