1. 程式人生 > >JAVA高併發秒殺API專案的學習筆記

JAVA高併發秒殺API專案的學習筆記

  • 設計Restful介面

  • SpringMVC整合Spring

    1. 在web.xml中配置DispatcherServlet
    2. 建立web包
    3. 建立spring-web.xml配置檔案
    4. 在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"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:conext="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
        >
        <!--配置Spring MVC--> <!--開啟SpringMVC註解模式--> <!--簡化配置 1、自動註冊DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter 2、提供一系列功能:資料繫結,數字和日期的轉化@NumberFormat,@DataTimeFormat xml,json預設讀寫支援 --> <mvc:annotation-driven/> <!--servlet-mapping對映路徑-->
        <!--靜態資源預設servlet配置 1、加入對靜態資源的處理:js,css,img 2、允許使用/做整體對映 --> <mvc:default-servlet-handler/> <!--配置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> <!--掃描web相關的bean--> <conext:component-scan base-package="org.seckill.web"/> </beans>
  • 實現秒殺相關的Restful介面

    1. 建立控制類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());
      
        }
      }