高并发秒杀系统方案(项目框架搭建)

项目框架搭建:

 DemoController:

package com.imooc.miaosha.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.imooc.miaosha.result.CodeMsg;
import com.imooc.miaosha.result.Result;

@Controller
@RequestMapping("/demo")
public class DemoController {
    
         @RequestMapping("/")
        @ResponseBody
        String home() {
            return "Hello World!";
        }
         //1.rest api json输出 2.页面
         @RequestMapping("/hello")
        @ResponseBody
        public Result<String> hello() {
             return Result.success("hello,imooc");
           // return new Result(0, "success", "hello,imooc");
        }
         
         @RequestMapping("/helloError")
        @ResponseBody
        public Result<String> helloError() {
             return Result.error(CodeMsg.SERVER_ERROR);
             //return new Result(500102, "XXX");
        }
         
         @RequestMapping("/thymeleaf")
        public String  thymeleaf(Model model) {
             model.addAttribute("name", "Joshua");
             return "hello";
        }
         
}

CodeMsg:

package com.imooc.miaosha.result;

public class CodeMsg {
    private int code;
    private String msg;
    
    //通用异常
    public static CodeMsg SUCCESS = new CodeMsg(0, "success");
    public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常");
    //登录模块 5002XX
    
    //商品模块 5003XX
    
    //订单模块 5004XX
    
    //秒杀模块 5005XX
    
    
    private CodeMsg(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    
    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

Result:

package com.imooc.miaosha.result;

public class Result<T> {
    private int code;
    private String msg;
    private T data;

    /**
     * 成功时候的调用
     * */
    public static <T> Result<T> success(T data){
        return new  Result<T>(data);
    }
    
    /**
     * 失败时候的调用
     * */
    public static <T> Result<T> error(CodeMsg cm){
        return new  Result<T>(cm);
    }
    
    private Result(T data) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }
    
    private Result(CodeMsg cm) {
        if(cm == null) {
            return;
        }
        this.code = cm.getCode();
        this.msg = cm.getMsg();
    }

    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
    public T getData() {
        return data;
    }
}

MainApplication:

package com.imooc.miaosha;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MainApplication.class, args);
    }
}

hello.html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'hello:'+${name}" ></p>
</body>
</html>

application.properties:

spring.thymeleaf.cache=false
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

 

posted @ 2018-03-12 11:56  虚极静笃  Views(696)  Comments(0Edit  收藏  举报