源码:https://github.com/joshul/seckill
本篇文章总结自己开发秒杀系统Web层的过程,主要介绍前端交互设计、Restful:url满足Restful设计规范、Spring MVC、bootstrap+jquery这四个方面的开发。
1.前端交互流程设计
对于一个系统,需要产品经理、前端工程师和后端工程师的参数,产品经理将用户的需求做成一个开发文档交给前端工程师和后端工程师,前端工程师为系统完成页面的开发,后端工程师为系统完成业务逻辑的开发。对于我们这个秒杀系统,它的前端交互流程设计如下图:
这个流程图就告诉了我们详情页的流程逻辑,前端工程师根据这个流程图设计页面,而我们后端工程师根据这个流程图开发我们对应的代码。前端交互流程是系统开发中很重要的一部分,接下来进行Restful接口设计的学习。
设计学习)
2.Restful接口设计学习
什么是Restful?它就是一种优雅的URI表述方式,用来设计我们资源的访问URL。通过这个URL的设计,我们就可以很自然的感知到这个URL代表的是哪种业务场景或者什么样的数据或资源。基于Restful设计的URL,对于我们接口的使用者、前端、web系统或者搜索引擎甚至是我们的用户,都是非常友好的。关于Restful的了解大家去网上一搜一大把,我这里就不再做介绍了。下面看看我们这个秒杀系统的URL设计:
接下来基于上述资源接口来开始我们对Spring MVC框架的使用。
3.整合配置Spring MVC框架
首先在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框架整合到了我们的项目中),接下来就要基于Restful接口进行我们项目的Controller开发工作了。
4.Controller开发
Controller中的每一个方法都对应我们系统中的一个资源URL,其设计应该遵循Restful接口的设计风格。在org.seckill包下创建一个web包用于放web层Controller开发的代码,在该包下创建一个SeckillController.java,内容如下:
@Component
@RequestMapping("/seckill")//url:模块/资源/{}/细分
public class SeckillController
{
@Autowired
private SeckillService seckillService;
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String list(Model model)
{
//list.jsp+mode=ModelAndView
//获取列表页
List 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 "forward:/seckill/list";
}
model.addAttribute("seckill",seckill);
return "detail";
}
//ajax ,json暴露秒杀接口的方法
@RequestMapping(value = "/{seckillId}/exposer",
method = RequestMethod.POST,
produces = {"application/json;charset=UTF-8"})
@ResponseBody
public SeckillResult exposer(Long seckillId)
{
SeckillResult result;
try{
Exposer exposer=seckillService.exportSeckillUrl(seckillId);
result=new SeckillResult(true,exposer);
}catch (Exception e)
{
e.printStackTrace();
result=new SeckillResult(false,e.getMessage());
}
return result;
}
@RequestMapping(value = "/{seckillId}/{md5}/execution",
method = RequestMethod.POST,
produces = {"application/json;charset=UTF-8"})
@ResponseBody
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 (RepeatKillException e1)
{
SeckillExecution execution=new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL);
return new SeckillResult(false,execution);
}catch (SeckillCloseException e2)
{
SeckillExecution execution=new SeckillExecution(seckillId, SeckillStatEnum.END);
return new SeckillResult(false,execution);
}
catch (Exception e)
{
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());
}
}
Controller开发中的方法完全是对照Service接口方法进行开发的,第一个方法用于访问我们商品的列表页,第二个方法访问商品的详情页,第三个方法用于返回一个json数据,数据中封装了我们商品的秒杀地址,第四个方法用于封装用户是否秒杀成功的信息,第五个方法用于返回系统当前时间。代码中涉及到一个将返回秒杀商品地址封装为json数据的一个Vo类,即SeckillResult.java,在dto包中创建它,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//将所有的ajax请求返回类型,全部封装成json数据
public class SeckillResult {
private boolean success;
private T data;
private String error;
public SeckillResult(boolean success, T data) {
this.success = success;
this.data = data;
}
public SeckillResult(boolean success, String error) {
this.success = success;
this.error = error;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
到此,Controller的开发任务完成,接下来进行我们的页面开发。
5.页面开发
页面由前端工程师完成,这里直接拷贝我github上源代码中jsp的代码(webapp包下的所有资源)即可。
点击相应商品后面的详情页链接即可查看该商品是否开启秒杀、秒杀该商品等活动。到此,web层的开发也结束,我们的系统开发便告一段落。但往往这样一个秒杀系统,往往是会有成千上万的人进行参与,我们目前的系统是抗不起多少高并发操作的,所以后面我们会对本系统进行高并发的优化。请查看我的下篇文章Java高并发秒杀API之高并发优化(待更新)