1. 程式人生 > >高並發秒殺系統方案(二)

高並發秒殺系統方案(二)

nco home null public web IT pro mage 項目

項目框架搭建:

技術分享圖片

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

高並發秒殺系統方案(二)