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


    项目框架搭建:

     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
  • 相关阅读:
    无刷电机控制学习笔记
    "程序宅男"从改善皮肤开始——不再长痘
    跨平台国际化测试——Switch本体测试
    自动驾驶技术了解
    互联网加班狗:零碎时间学英语的方法
    ASCII,Unicode,GBK和UTF-8字符编码的区别和联系
    领域驱动设计的必要性和模型标准——《领域驱动设计-精简版》
    异步、非阻塞和IO多路复用总结
    Debian 8 安装Nginx最新版本
    字节、字、bit、Byte、byte的关系区分
  • 原文地址:https://www.cnblogs.com/XJJD/p/8548533.html
Copyright © 2020-2023  润新知