JAVA高併發秒殺API專案的學習筆記
阿新 • • 發佈:2019-01-09
-
設計Restful介面
-
SpringMVC整合Spring
- 在web.xml中配置DispatcherServlet
- 建立web包
- 建立spring-web.xml配置檔案
-
在spring-web.xml進行SpringMVC的配置
- 開啟SpringMVC註解模式
- servlet-mapping對映路徑
- 配置jsp顯示viewResolver
-
掃描web相關的bean
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
實現秒殺相關的Restful介面
-
建立控制類SecKillController,實現獲取列表,獲取單條資料,獲取系統時間,獲取秒殺地址,秒殺的方法
@Controller @RequestMapping("/seckill/")//模組/資源 public class SecKillController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private SecKillService secKillService; @RequestMapping(name="/list",method= RequestMethod.GET) public String list(Model model){ List<SecKill> list = secKillService.getSecKillList(); model.addAttribute("list",list); return "list"; } @RequestMapping(value="/{secKillId}/detail",method=RequestMethod.GET) public String detail(@PathVariable("secKillId") Long secKillId,Model model){ if(secKillId == null){ return "redirect:/seckill/list"; } SecKill secKill = secKillService.getById(secKillId); if(secKill == null){ return "redirect:/seckill/list"; } model.addAttribute("secKill",secKill); return "detail"; } @RequestMapping(value="/{secKillId}/exposer",method = RequestMethod.POST, produces = {"application/json;charset=utf-8"}) @ResponseBody public SecKillResult<Exposer> exposer(@PathVariable("secKillId") Long secKillId){ SecKillResult<Exposer> result = null; try{ Exposer exposer = secKillService.exportSecKillUrl(secKillId); result = new SecKillResult<Exposer>(true,exposer); }catch(Exception e){ logger.error(e.getMessage(),e); result = new SecKillResult<Exposer>(false,e.getMessage()); } return result; } @RequestMapping(value="/{secKillId}/{md5}/execution", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"}) public SecKillResult<SecKillExecution> excute(@PathVariable("secKillId") Long secKillId, @PathVariable("md5") String md5, @CookieValue(value="killPhone",required = false) Long userPhone){ //springmvc valid if(userPhone == null){ return new SecKillResult<SecKillExecution>(false,"未註冊"); } SecKillResult<SecKillExecution> result = null; try{ SecKillExecution secKillExecution = secKillService.executeSecKill(secKillId,userPhone,md5); result = new SecKillResult<SecKillExecution>(true,secKillExecution); }catch(RepeatKillException e){ SecKillExecution secKillExecution = new SecKillExecution(secKillId, SecKillStatEnum.REPEAT); result = new SecKillResult<SecKillExecution>(false,secKillExecution); }catch(SecKillCloseException e){ SecKillExecution secKillExecution = new SecKillExecution(secKillId, SecKillStatEnum.END); result = new SecKillResult<SecKillExecution>(false,secKillExecution); }catch(Exception e){ logger.error(e.getMessage(),e); SecKillExecution secKillExecution = new SecKillExecution(secKillId, SecKillStatEnum.INNER_ERROR); result = new SecKillResult<SecKillExecution>(false,secKillExecution); } return result; } @RequestMapping(value="/time/now",method=RequestMethod.GET) public SecKillResult<Long> time(){ Date now = new Date(); return new SecKillResult<Long>(true,now.getTime()); } }
-