Java高並發秒殺API之web層


 第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的配置,如下:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">
<!--用maven創建的web-app需要修改servlet的版本為3.0-->

<!--配置DispatcherServlet-->
    <servlet>
        <servlet-name>seckill-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置SpringMVC 需要配置的文件 spring-dao.xml,spring-service.xml,spring-web.xml 整合順序 Mybites -> spring -> springMvc spring和springMVC不需要整合,兩者都出自spring -->
        <init-param>
            <param-name>contextConfigLocation</param-name>    <!-- config配置文件的location -->
            <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>seckill-dispatcher</servlet-name>
            <!-- 默認匹配所有的請求 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
</web-app>

然后在spring容器中進行web層相關bean(即Controller)的配置,在spring包下創建一個spring-web.xml,內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <!-- 配置springMVC -->
    <!--1.開啟springmvc注解模式 簡化配置: (1)自動注冊DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter (2)默認提供一系列的功能:數據綁定,數字和日期的format@NumberFormat,@DateTimeFormat xml,json的默認讀寫支持 -->
    <mvc:annotation-driven />

    <!-- servlet-mapping 映射路徑:“/” -->
    <!--2.靜態資源默認servlet配置 -->
    <!-- 1).加入對靜態資源處理:js,gif,png 2).允許使用 "/" 做整體映射 -->
    <mvc:default-servlet-handler />

    <!--3:配置JSP 顯示ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!--4:掃描web相關的bean -->
    <context:component-scan base-package="org.myseckill.web" />



</beans>

這樣我們便完成了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<Seckill> 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;charset=UTF-8"}) //@ResponseBody這個注解告訴springMVC返回的是一個json類型的數據
 @ResponseBody public SeckillResult<Exposer> exposer(Long seckillId) { SeckillResult<Exposer> result; try { Exposer exposer = seckillService.exportSeckillUrl(seckillId); result = new SeckillResult<Exposer>(true,exposer); } catch (Exception e) { logger.error(e.getMessage(),e); //出現異常則調用SeckillResult的另一個構造方法
            result = new SeckillResult<Exposer>(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<SeckillExecution> execute(@PathVariable("seckillId") Long seckillId, @PathVariable("md5")String md5, @CookieValue(value = "killPhone",required = false)Long phone){ if(phone == null) { return new SeckillResult<SeckillExecution>(false,"未注冊"); } SeckillResult<SeckillExecution> result; try { SeckillExecution execution = seckillService.executeSeckill(seckillId, phone, md5); return new SeckillResult<SeckillExecution>(true, execution); } catch (SeckillCloseException e) { //SeckillCloseException和RepeatKillException是允許的異常
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.END); return new SeckillResult<SeckillExecution>(false, execution); } catch (RepeatKillException e) { SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL); return new SeckillResult<SeckillExecution>(false, execution); } catch (Exception e) { logger.error(e.getMessage(), e); //其他所有未知異常算作INNER_ERROR
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR); return new SeckillResult<SeckillExecution>(false, execution); } } @RequestMapping(value="/time/now",method=RequestMethod.GET) public SeckillResult<Long> time(){ Date now = new Date(); return new SeckillResult<Long>(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<T>可以為SeckillResult<Exposer>也可以為SeckillResult<SeckillExecution>
public class SeckillResult<T> { 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如下:

      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <!-- 引入 Bootstrap -->
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
 
      <!-- HTML5 Shiv 和 Respond.js 用於讓 IE8 支持 HTML5元素和媒體查詢 -->
      <!-- 注意: 如果通過 file:// 引入 Respond.js 文件,則該文件無法起效果 -->
      <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script> <![endif]-->

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

剩下前端的內容暫時先復制了代碼

list.jsp:

<%@page contentType="text/html; charset=UTF-8" language="java" %>
<%@include file="common/tag.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <title>秒殺商品列表</title>
    <%@include file="common/head.jsp" %>
</head>
<body>
<div class="container">
    <div class="panel panel-default">
        <div class="panel-heading text-center">
            <h2>秒殺列表</h2>
        </div>
        <div class="panel-body">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>名稱</th>
                    <th>庫存</th>
                    <th>開始時間</th>
                    <th>結束時間</th>
                    <th>創建時間</th>
                    <th>詳情頁</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${list}" var="sk">
                    <tr>
                        <td>${sk.name}</td>
                        <td>${sk.number}</td>
                        <td>
                            <fmt:formatDate value="${sk.startTime}" pattern="yyyy-MM-dd HH:mm:ss" />
                        </td>
                        <td>
                            <fmt:formatDate value="${sk.endTime}" pattern="yyyy-MM-dd HH:mm:ss" />
                        </td>
                        <td>
                            <fmt:formatDate value="${sk.createTime}" pattern="yyyy-MM-dd HH:mm:ss" />
                        </td>
                        <td><a class="btn btn-info" href="/seckill/${sk.seckillId}/detail" target="_blank">詳情</a></td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>

        </div>
    </div>
</div>



<!-- jQuery文件。務必在bootstrap.min.js 之前引入 -->
<script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</body>
</html>

 

detail.jsp:

<%@page contentType="text/html; charset=UTF-8" language="java" %>
<%@include file="common/tag.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <title>秒殺詳情頁</title>
    <%@include file="common/head.jsp" %>
</head>
<body>
<div class="container">
    <div class="panel panel-default text-center">
        <div class="pannel-heading">
            <h1>${seckill.name}</h1>
        </div>

        <div class="panel-body">
            <h2 class="text-danger">
                <%--顯示time圖標--%>
                <span class="glyphicon glyphicon-time"></span>
                <%--展示倒計時--%>
                <span class="glyphicon" id="seckill-box"></span>
            </h2>
        </div>
    </div>
</div>
<%--登錄彈出層 輸入電話--%>
<div id="killPhoneModal" class="modal fade">

    <div class="modal-dialog">

        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title text-center">
                    <span class="glyphicon glyphicon-phone"> </span>秒殺電話: </h3>
            </div>

            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-8 col-xs-offset-2">
                        <input type="text" name="killPhone" id="killPhoneKey" placeholder="填寫手機號^o^" class="form-control">
                    </div>
                </div>
            </div>

            <div class="modal-footer">
                <%--驗證信息--%>
                <span id="killPhoneMessage" class="glyphicon"> </span>
                <button type="button" id="killPhoneBtn" class="btn btn-success">
                    <span class="glyphicon glyphicon-phone"></span> Submit </button>
            </div>

        </div>
    </div>

</div>

</body>
<%--jQery文件,務必在bootstrap.min.js之前引入--%>
<script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<%--使用CDN 獲取公共js http://www.bootcdn.cn/--%>
<%--jQuery Cookie操作插件--%>
<script src="http://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<%--jQuery countDown倒計時插件--%>
<script src="http://cdn.bootcss.com/jquery.countdown/2.1.0/jquery.countdown.min.js"></script>

<script src="/resource/script/seckill.js" type="text/javascript"></script>

<script type="text/javascript"> $(function () { //使用EL表達式傳入參數 seckill.detail.init({ seckillId:${seckill.seckillId}, startTime:${seckill.startTime.time},//毫秒 endTime:${seckill.endTime.time} }); }) </script>
</html>

 

common文件夾下head.jsp:

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
<!-- 可选的Bootstrap主题文件(一般不使用) -->
<link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap-theme.min.css" rel="stylesheet">

<!-- HTML5 Shim 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
<!-- 注意: 如果通过 file:// 引入 Respond.js 文件,则该文件无法起效果 -->
<!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script> <![endif]-->

 

common文件夾下tag.jsp:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

 

運行時出現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('<label class="label label-danger">手機號錯誤!</label>').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('<button class="btn btn-primary btn-lg" id="killBtn">開始秒殺</button>'); $.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');//,<-$(this)===('#killBtn')->
                        //2.發送秒殺請求執行秒殺
                        $.post(killUrl, {}, function (result) { if (result && result['success']) { var killResult = result['data']; var state = killResult['state']; var stateInfo = killResult['stateInfo']; //顯示秒殺結果
                                node.html('<span class="label label-success">' + stateInfo + '</span>'); } }); }); 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 < startTime) { //秒殺未開始,計時事件綁定
            var killTime = new Date(startTime + 1000);//todo 防止時間偏移
            seckillBox.countdown(killTime, function (event) { //時間格式
                var format = event.strftime('秒殺倒計時: %D天 %H時 %M分 %S秒 '); seckillBox.html(format); }).on('finish.countdown', function () { //時間完成后回調事件
                //獲取秒殺地址,控制現實邏輯,執行秒殺
                console.log('______fininsh.countdown'); seckill.handlerSeckill(seckillId, seckillBox); }); } else { //秒殺開始
 seckill.handlerSeckill(seckillId, seckillBox); } } }

 

 

遇到的問題:

1.無法彈出手機號輸入框

發現seckill.js加載失敗,但程序並未報錯

 

把<script src="/resource/script/seckill.js" type="text/javascript"></script>修改為

<script src="/myseckill/resource/script/seckill.js" type="text/javascript"></script>

老師使用的IDEA與eclipse的路徑不同

2.輸入手機號后還是一直彈出手機號輸入框

seckill.js邏輯中登入成功會刷新頁面,猜想應該是cookie寫入失敗,刷新后認為是新用戶

將seckill.js中$.cookie('userPhone', inputPhone, {expires: 7, path: '/seckill'});修改為

 $.cookie('userPhone', inputPhone, {expires: 7, path: '/myseckill/seckill'});
path中加入項目名,不然寫不進去cookie

 

 cookie寫入成功,不再重復要求登錄(輸入手機號)

 

3./time/now 404錯誤

 

修改路徑無效

發現原因是web層SeckillController中沒有加入@ResponseBody注解

@RequestMapping(value="/time/now",method=RequestMethod.GET) @ResponseBody public SeckillResult<Long> time(){ Date now = new Date(); return new SeckillResult<Long>(true, now.getTime()); }

 

 測試成功:

但不能顯示重復秒殺結果

 

將SeckillController中execute方法中的false都改為true,因為seckill.js中當success的值為true時才輸出結果

public class SeckillResult<T> { private boolean success;   //標識,判斷請求是否成功而不是秒殺是否成功
if (result && result['success']) { var killResult = result['data']; var state = killResult['state']; var stateInfo = killResult['stateInfo']; //顯示秒殺結果
                                node.html('<span class="label label-success">' + stateInfo + '</span>');

 

這樣,當出現異常(包括我們允許的異常)時,也算請求成功

public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId") Long seckillId, @PathVariable("md5")String md5, @CookieValue(value = "killPhone",required = false)Long phone){ if(phone == null) { return new SeckillResult<SeckillExecution>(false,"未注冊"); } SeckillResult<SeckillExecution> result; try { SeckillExecution execution = seckillService.executeSeckill(seckillId, phone, md5); return new SeckillResult<SeckillExecution>(true, execution); } catch (SeckillCloseException e) { //SeckillCloseException和RepeatKillException是允許的異常
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.END); return new SeckillResult<SeckillExecution>(true, execution); } catch (RepeatKillException e) { SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL); return new SeckillResult<SeckillExecution>(true, execution); } catch (Exception e) { logger.error(e.getMessage(), e); //其他所有未知異常算作INNER_ERROR
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR); return new SeckillResult<SeckillExecution>(true, execution); } }

成功顯示結果

 mysql中也相應進行了減庫存操作

 

 

 

success_killed成功秒殺明細:

 

至此秒殺的業務基本完成

接下來針對高性能高並發進行優化


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM