• vczz
    了解作者
  • Java
    开发工具
  • 13.9MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2019-01-08 15:02
    上传日期
基于spring boot的小秒杀项目,完全手写
project.zip
内容介绍
package com.imooc.miaosha.controller; import java.awt.image.BufferedImage; import java.io.OutputStream; import java.util.HashMap; import java.util.List; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.imooc.miaosha.access.AccessLimit; import com.imooc.miaosha.domain.MiaoshaOrder; import com.imooc.miaosha.domain.MiaoshaUser; import com.imooc.miaosha.rabbitmq.MQSender; import com.imooc.miaosha.rabbitmq.MiaoshaMessage; import com.imooc.miaosha.redis.GoodsKey; import com.imooc.miaosha.redis.MiaoshaKey; import com.imooc.miaosha.redis.OrderKey; import com.imooc.miaosha.redis.RedisService; import com.imooc.miaosha.result.CodeMsg; import com.imooc.miaosha.result.Result; import com.imooc.miaosha.service.GoodsService; import com.imooc.miaosha.service.MiaoshaService; import com.imooc.miaosha.service.MiaoshaUserService; import com.imooc.miaosha.service.OrderService; import com.imooc.miaosha.vo.GoodsVo; @Controller @RequestMapping("/miaosha") public class MiaoshaController implements InitializingBean { @Autowired MiaoshaUserService userService; @Autowired RedisService redisService; @Autowired GoodsService goodsService; @Autowired OrderService orderService; @Autowired MiaoshaService miaoshaService; @Autowired MQSender sender; private HashMap<Long, Boolean> localOverMap = new HashMap<Long, Boolean>(); /** * 系统初始化 * */ public void afterPropertiesSet() throws Exception { List<GoodsVo> goodsList = goodsService.listGoodsVo(); if(goodsList == null) { return; } for(GoodsVo goods : goodsList) { redisService.set(GoodsKey.getMiaoshaGoodsStock, ""+goods.getId(), goods.getStockCount()); localOverMap.put(goods.getId(), false); } } @RequestMapping(value="/reset", method=RequestMethod.GET) @ResponseBody public Result<Boolean> reset(Model model) { List<GoodsVo> goodsList = goodsService.listGoodsVo(); for(GoodsVo goods : goodsList) { goods.setStockCount(10); redisService.set(GoodsKey.getMiaoshaGoodsStock, ""+goods.getId(), 10); localOverMap.put(goods.getId(), false); } redisService.delete(OrderKey.getMiaoshaOrderByUidGid); redisService.delete(MiaoshaKey.isGoodsOver); miaoshaService.reset(goodsList); return Result.success(true); } /** * QPS:1306 * 5000 * 10 * QPS: 2114 * */ @RequestMapping(value="/{path}/do_miaosha", method=RequestMethod.POST) @ResponseBody public Result<Integer> miaosha(Model model,MiaoshaUser user, @RequestParam("goodsId")long goodsId, @PathVariable("path") String path) { model.addAttribute("user", user); if(user == null) { return Result.error(CodeMsg.SESSION_ERROR); } //验证path boolean check = miaoshaService.checkPath(user, goodsId, path); if(!check){ return Result.error(CodeMsg.REQUEST_ILLEGAL); } //内存标记,减少redis访问 boolean over = localOverMap.get(goodsId); if(over) { return Result.error(CodeMsg.MIAO_SHA_OVER); } //预减库存 long stock = redisService.decr(GoodsKey.getMiaoshaGoodsStock, ""+goodsId);//10 if(stock < 0) { localOverMap.put(goodsId, true); return Result.error(CodeMsg.MIAO_SHA_OVER); } //判断是否已经秒杀到了 MiaoshaOrder order = orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(), goodsId); if(order != null) { return Result.error(CodeMsg.REPEATE_MIAOSHA); } //入队 MiaoshaMessage mm = new MiaoshaMessage(); mm.setUser(user); mm.setGoodsId(goodsId); sender.sendMiaoshaMessage(mm); return Result.success(0);//排队中 /* //判断库存 GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);//10个商品,req1 req2 int stock = goods.getStockCount(); if(stock <= 0) { return Result.error(CodeMsg.MIAO_SHA_OVER); } //判断是否已经秒杀到了 MiaoshaOrder order = orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(), goodsId); if(order != null) { return Result.error(CodeMsg.REPEATE_MIAOSHA); } //减库存 下订单 写入秒杀订单 OrderInfo orderInfo = miaoshaService.miaosha(user, goods); return Result.success(orderInfo); */ } /** * orderId:成功 * -1:秒杀失败 * 0: 排队中 * */ @RequestMapping(value="/result", method=RequestMethod.GET) @ResponseBody public Result<Long> miaoshaResult(Model model,MiaoshaUser user, @RequestParam("goodsId")long goodsId) { model.addAttribute("user", user); if(user == null) { return Result.error(CodeMsg.SESSION_ERROR); } long result =miaoshaService.getMiaoshaResult(user.getId(), goodsId); return Result.success(result); } @AccessLimit(seconds=5, maxCount=5, needLogin=true) @RequestMapping(value="/path", method=RequestMethod.GET) @ResponseBody public Result<String> getMiaoshaPath(HttpServletRequest request, MiaoshaUser user, @RequestParam("goodsId")long goodsId, @RequestParam(value="verifyCode", defaultValue="0")int verifyCode ) { if(user == null) { return Result.error(CodeMsg.SESSION_ERROR); } boolean check = miaoshaService.checkVerifyCode(user, goodsId, verifyCode); if(!check) { return Result.error(CodeMsg.REQUEST_ILLEGAL); } String path =miaoshaService.createMiaoshaPath(user, goodsId); return Result.success(path); } @RequestMapping(value="/verifyCode", method=RequestMethod.GET) @ResponseBody public Result<String> getMiaoshaVerifyCod(HttpServletResponse response,MiaoshaUser user, @RequestParam("goodsId")long goodsId) { if(user == null) { return Result.error(CodeMsg.SESSION_ERROR); } try { BufferedImage image = miaoshaService.createVerifyCode(user, goodsId); OutputStream out = response.getOutputStream(); ImageIO.write(image, "JPEG", out); out.flush(); out.close(); return null; }catch(Exception e) { e.printStackTrace(); return Result.error(CodeMsg.MIAOSHA_FAIL); } } }
评论
    相关推荐