Java高并发秒杀API之web层 您所在的位置:网站首页 经期怎么减肥最快最有效的方法 Java高并发秒杀API之web层

Java高并发秒杀API之web层

2022-05-24 13:22| 来源: 网络整理| 查看: 265

 第1章 设计Restful接口

1.1前端交互流程设计

 

 1.2 学习Restful接口设计

 什么是Restful?它就是一种优雅的URI表述方式,用来设计我们资源的访问URL。通过这个URL的设计,我们就可以很自然的感知到这个URL代表的是哪种业务场景或者什么样的数据或资源。基于Restful设计的URL,对于我们接口的使用者、前端、web系统或者搜索引擎甚至是我们的用户,都是非常友好的。

 

 

 

 

 第2章 SpringMVC整合spring

2.1 SpringMvc理论

 

 

蓝色部分是需要我们自己开发的

 

 ?表一个字符

*表任意个字符

**表任意路径

{}中的字符以参数形式传入

 

 2.2 整合配置springMVC框架

首先在WEB-INF的web.xml中进行我们前端控制器DispatcherServlet的配置,如下:

seckill-dispatcher org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring/spring-*.xml seckill-dispatcher /

然后在spring容器中进行web层相关bean(即Controller)的配置,在spring包下创建一个spring-web.xml,内容如下:

这样我们便完成了Spring MVC的相关配置(即将Spring MVC框架整合到了我们的项目中)

 

第3章 实现秒杀相关的Restful接口

在org.myseckill下新建一个web文件夹,用于存放我们的controller

在该包下创建一个SeckillController.java,内容如下:

controller即MVC中的C控制层,职责是接收参数,做跳转的控制

/** * controller即MVC中的C控制层,职责是接收参数,做跳转的控制 * Controller开发中的方法完全是对照Service接口方法进行开发的,第一个方法用于访问我们商品的列表页, * 第二个方法访问商品的详情页,第三个方法用于返回一个json数据,数据中封装了我们商品的秒杀地址, * 第四个方法用于封装用户是否秒杀成功的信息,第五个方法用于返回系统当前时间。 * @author TwoHeads * */ @Controller @RequestMapping("/seckill") //一级映射,相当于二级映射前面都有"/seckill" url:模块/资源/{}/细分 public class SeckillController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private SeckillService seckillService; @RequestMapping(value="/list",method=RequestMethod.GET) //二级映射 public String list(Model model) { //model用于存放渲染list的数据 list.jsp是页面的模板,model是数据 //list.jsp+mode=ModelAndView //获取秒杀的列表页 List list = seckillService.getSeckillList(); model.addAttribute("list",list); return "list"; //即WEB-INF/jsp/"list".jsp } @RequestMapping(value="/{seckillId}/detail",method=RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId,Model model) { //判断seckillId有没有传 if(seckillId == null) { //之前把Long seckillId写为了long seckillId,不是一个对象,导致无法判断null return "redirect:/seckill/list"; } Seckill seckill = seckillService.getById(seckillId); //如果传的id不存在 if(seckill == null) { return "forward:/seckill/list"; } model.addAttribute("seckill",seckill); return "detail"; } //ajax接口 返回类型是json 暴露秒杀接口的方法 //只接受post方式,即直接在浏览器中输入这个地址是无效的,地址栏回车属于get方式。post方式只能设计一个表单,一个提交按钮,才可以。 //produces告诉浏览器我们的contentType是json @RequestMapping(value="/{seckillId}/exposer", method=RequestMethod.POST, produces= {"application/json;"}) //@ResponseBody这个注解告诉springMVC返回的是一个json类型的数据 @ResponseBody public SeckillResult exposer(Long seckillId) { SeckillResult result; try { Exposer exposer = seckillService.exportSeckillUrl(seckillId); result = new SeckillResult(true,exposer); } catch (Exception e) { logger.error(e.getMessage(),e); //出现异常则调用SeckillResult的另一个构造方法 result = new SeckillResult(false,e.getMessage()); } return result; } //所有的ajax请求都统一的返回SeckillResult,dto用于web层和service层的数据传递,SeckillResult和Exposer,SeckillExecution全都是dto包下的类 @RequestMapping(value="/{seckillId}/{md5}/execution", method=RequestMethod.POST, produces= {"application/json;charset=UTF-8"}) @ResponseBody //参数seckillId和md5都可以从url映射的请求参数中{seckillId},{md5}取得,而用户标识killPhone在url中并没有,从用户浏览器request请求的cookie中取得 //required = false使当cookie没有killPhone参数时springMVC不报错,把killphone的验证逻辑放到程序中来 public SeckillResult execute(@PathVariable("seckillId") Long seckillId, @PathVariable("md5")String md5, @CookieValue(value = "killPhone",required = false)Long phone){ if(phone == null) { return new SeckillResult(false,"未注册"); } SeckillResult result; try { SeckillExecution execution = seckillService.executeSeckill(seckillId, phone, md5); return new SeckillResult(true, execution); } catch (SeckillCloseException e) { //SeckillCloseException和RepeatKillException是允许的异常 SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.END); return new SeckillResult(false, execution); } catch (RepeatKillException e) { SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL); return new SeckillResult(false, execution); } catch (Exception e) { logger.error(e.getMessage(), e); //其他所有未知异常算作INNER_ERROR SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR); return new SeckillResult(false, execution); } } @RequestMapping(value="/time/now",method=RequestMethod.GET) public SeckillResult time(){ Date now = new Date(); return new SeckillResult(true, now.getTime()); } }

 

 

所有的ajax请求都统一的返回SeckillResult,

dto用于web层和service层的数据传递,SeckillResult和Exposer,SeckillExecution全都是dto包下的类

 

@ResposeBody注解的使用

1、

  @responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML

  数据,需要注意的呢,在使用此注解之后不会再走试图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。

2、  

  @RequestMapping("/login")  @ResponseBody  public User login(User user){    return user;  }  User字段:userName pwd  那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}'

  效果等同于如下代码:  @RequestMapping("/login")  public void login(User user, HttpServletResponse response){    response.getWriter.write(JSONObject.fromObject(user).toString());  }

 

 

Controller开发中的方法完全是对照Service接口方法进行开发的,第一个方法用于访问我们商品的列表页,第二个方法访问商品的详情页,第三个方法用于返回一个json数据,数据中封装了我们商品的秒杀地址,第四个方法用于封装用户是否秒杀成功的信息,第五个方法用于返回系统当前时间。代码中涉及到一个将返回秒杀商品地址封装为json数据的一个Vo类,即SeckillResult.java,在dto包中创建它,内容如下:

//封装json数据结果,将所有的ajax请求返回类型,全部封装成json数据//泛型SeckillResult可以为SeckillResult也可以为SeckillResult public class SeckillResult { private boolean success; //标识,判断请求是否成功 private T data; //泛型类型的数据 private String error; //错误信息 //如果success是true则有数据 public SeckillResult(boolean success, T data) { super(); this.success = success; this.data = data; } //如果success是false则传递错误信息 public SeckillResult(boolean success, String error) { super(); this.success = success; this.error = error; } //getter和setter }

 

 

 

第4章 基于bootstrap开发页面结构

在WEB-INF下新建jsp文件夹和list.jsp和detail.jsp

 直接在http://www.runoob.com/bootstrap/bootstrap-environment-setup.html中找到bootstrap模板,拷贝到list.jsp和detail.jsp中并进行修改

在jsp文件夹下创建common文件夹用于存放公用的的jsp,在其下创建head.jsp如下:

list.jsp和detail.jsp中便不需要以上内容了

剩下前端的内容暂时先复制了代码

list.jsp:

DOCTYPE html> 秒杀商品列表 秒杀列表 名称 库存 开始时间 结束时间 创建时间 详情页 ${sk.name} ${sk.number} 详情

 

detail.jsp:

DOCTYPE html> 秒杀详情页 ${seckill.name} 秒杀电话: Submit $(function () { //使用EL表达式传入参数 seckill.detail.init({ seckillId:${seckill.seckillId}, startTime:${seckill.startTime.time},//毫秒 endTime:${seckill.endTime.time} }); })

 

common文件夹下head.jsp:

 

common文件夹下tag.jsp:

 

运行时出现404错误,原因如下:

我用的Eclipse,和老师用的IDEA不一样,我的项目跑起来的路径是:http://localhost:8080/myseckill/

http://localhost:8080/myseckill/seckill/list      就是列表页。

http://localhost:8080/myseckill/seckill/1000/detail  就是详情页。

老师的http://localhost:8080/seckill/list   才是详情页,不包含项目名称

将详情页超链接href="/seckill/${sk.seckillId}/detail"改为href="/myseckill/seckill/${sk.seckillId}/detail"

 

运行成功页面如下

 

 

第5章 交互逻辑编程

在webapp下新建/resource/script/seckill.js

 

41//存放主要交互逻辑的js代码 // javascript 模块化(package.类.方法) var seckill = { //封装秒杀相关ajax的url URL: { now: function () { return '/myseckill/seckill/time/now'; }, exposer: function (seckillId) { return '/myseckill/seckill/' + seckillId + '/exposer'; }, execution: function (seckillId, md5) { return '/myseckill/seckill/' + seckillId + '/' + md5 + '/execution'; } }, //验证手机号 validatePhone: function (phone) { if (phone && phone.length == 11 && !isNaN(phone)) { return true;//直接判断对象会看对象是否为空,空就是undefine就是false; isNaN 非数字返回true } else { return false; } }, //详情页秒杀逻辑 detail: { //详情页初始化 init: function (params) { //手机验证和登录,计时交互 //规划我们的交互流程 //在cookie中查找手机号 var userPhone = $.cookie('killPhone'); //验证手机号 if (!seckill.validatePhone(userPhone)) { //绑定手机 控制输出 var killPhoneModal = $('#killPhoneModal'); killPhoneModal.modal({ show: true,//显示弹出层 backdrop: 'static',//禁止位置关闭 keyboard: false//关闭键盘事件 }); $('#killPhoneBtn').click(function () { var inputPhone = $('#killPhoneKey').val(); console.log("inputPhone: " + inputPhone); if (seckill.validatePhone(inputPhone)) { //电话写入cookie(7天过期) $.cookie('killPhone', inputPhone, {expires: 7, path: '/myseckill/seckill'}); //验证通过  刷新页面 window.location.reload(); } else { //todo 错误文案信息抽取到前端字典里 $('#killPhoneMessage').hide().html('手机号错误!').show(300); } }); } //已经登录 //计时交互 var startTime = params['startTime']; var endTime = params['endTime']; var seckillId = params['seckillId']; $.get(seckill.URL.now(), {}, function (result) { if (result && result['success']) { var nowTime = result['data']; //时间判断 计时交互 seckill.countDown(seckillId, nowTime, startTime, endTime); } else { console.log('result: ' + result); alert('result: ' + result); } }); } }, handlerSeckill: function (seckillId, node) { //获取秒杀地址,控制显示器,执行秒杀 node.hide().html('开始秒杀'); $.get(seckill.URL.exposer(seckillId), {}, function (result) { //在回调函数种执行交互流程 if (result && result['success']) { var exposer = result['data']; if (exposer['exposed']) { //开启秒杀 //获取秒杀地址 var md5 = exposer['md5']; var killUrl = seckill.URL.execution(seckillId, md5); console.log("killUrl: " + killUrl); //绑定一次点击事件 $('#killBtn').one('click', function () { //执行秒杀请求 //1.先禁用按钮 $(this).addClass('disabled');//, //2.发送秒杀请求执行秒杀 $.post(killUrl, {}, function (result) { if (result && result['success']) { var killResult = result['data']; var state = killResult['state']; var stateInfo = killResult['stateInfo']; //显示秒杀结果 node.html('' + stateInfo + ''); } }); }); node.show(); } else { //未开启秒杀(浏览器计时偏差) var now = exposer['now']; var start = exposer['start']; var end = exposer['end']; seckill.countDown(seckillId, now, start, end); } } else { console.log('result: ' + result); } }); }, countDown: function (seckillId, nowTime, startTime, endTime) { console.log(seckillId + '_' + nowTime + '_' + startTime + '_' + endTime); var seckillBox = $('#seckill-box'); if (nowTime > endTime) { //秒杀结束 seckillBox.html('秒杀结束!'); } else if (nowTime


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有