小工具知识网

当前位置: 首页 > 行业知识 > 谷粒商城【成神路】-【9】——商城页面

行业知识

谷粒商城【成神路】-【9】——商城页面

2026-11-08 06:39:26 来源:互联网转载

���录

1.项目服务部署架构

2.Thymealf

3.请求接口

4.使用nginx转发

5.nginx动静分离 

6.优化 


1.项目服务部署架构

使用nginx动静分离,使图片、js等静态资源和服务器请求分开

2.Thymealf

1.导入依赖 

                            org.springframework.boot            spring-boot-starter-thymeleaf        

 2.导入资源

3.关闭缓存

spring:    thymeleaf:        cache: false #关闭缓存
  •  静态资源全部放在static文件夹下就可以按照路径直接访问
  • 页面放在templates,直接访问
  • springboot访问项目时,默认找index

    3.请求接口

    根据页面请求数据编写controller

    @Controllerpublic class IndexController {    @Autowired    CategoryService categoryService;    @GetMapping({"/", "/index.html"})    public String indexPage(Model model) {        //TODO 查询所有的一级分类        List categoryEntities = categoryService.getLeve1Categorys();        //视图解析器,加上前缀,加上后缀        model.addAttribute("categorys",categoryEntities);        return "index";    }    @ResponseBody    @GetMapping("/index/catalog.json")    public  Map getCatalogJson(){        Map catalogJson=categoryService.getCatalogJson();        return catalogJson;    }}

    具体的业务逻辑实现 

    /**     * 查询所有一级分类     *     * @return     */    @Override    public List getLeve1Categorys() {        List categoryEntities = baseMapper.selectList(new QueryWrapper().eq("parent_cid", 0));        return categoryEntities;    }    /**     * 查出所有分类     *     * @return     */    @Override    public Map getCatalogJson() {        /**         * 优化:将数据库查询的多次变为一次         */        List selectList = baseMapper.selectList(null);        //1.查出所有1级分类        List leve1Categorys = getParent_cid(selectList, 0L);        //2.封装数据        Map parentCid = leve1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {            //1.每一个一级分类            List categoryEntities = getParent_cid(selectList, v.getCatId());            List catalog2Vos = null;            if (categoryEntities != null) {                catalog2Vos = categoryEntities.stream().map(l2 -> {                    Catalog2Vo vo = new Catalog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName());                    //找二级分类的三级分类                    List categoryEntities3 = getParent_cid(selectList, l2.getCatId());                    if (categoryEntities3 != null) {                        List collect = categoryEntities3.stream().map(l3 -> {                            Catalog2Vo.Catalog3Vo catalog3Vo = new Catalog2Vo.Catalog3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());                            return catalog3Vo;                        }).collect(Collectors.toList());                        vo.setCatalog3List(collect);                    }                    return vo;                }).collect(Collectors.toList());            }            return catalog2Vos;        }));        return parentCid;    }

    4.使用nginx转发

    • 1.在nginx配置,路由到网关的负载均衡

       nginx代理给网关时,会丢失请求的host信息

      • 2.在网关中配置陆游规则 

        域名路由,一定要放到最后!!

        转发思路:

        在host文件配置gulimall映射的虚拟机地址,虚拟机中安装了nginx,默认访问80端口,这个端口专门监听gulimall域名,然后nginx会代理到网关,(在代理到网关的过程中会丢失Host头),网关根据陆游规则,匹配到具体的服务

        5.nginx动静分离 

        1.所有的静态资源全都放到nginx下

        2.所有:/static/**请求,都有nginx返回

        5.1.文件上传到nginx 

        在nginx的html目录下创建static目录,并将index文件全部上传到inginx的static目录下,并删除本地的index,并在前端页面都添加/static/

        温馨提示:catalogLoader.js 文件中也要添加/static/不然 catalog.json渲染不上

        5.2修改nginx配置 

        6.优化 

        在没有话之间我们每一次都要查数据库,导致性能不高,我们可以直接一次性查出所有,然后从缓存中获取指定的数据

        /**     * 查出所有分类     *     * @return     */    @Override    public Map getCatalogJson() {        /**         * 优化:将数据库查询的多次变为一次         */        List selectList = baseMapper.selectList(null);        //1.查出所有1级分类        List leve1Categorys = getParent_cid(selectList, 0L);        //2.封装数据        Map parentCid = leve1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {            //1.每一个一级分类            List categoryEntities = getParent_cid(selectList, v.getCatId());            List catalog2Vos = null;            if (categoryEntities != null) {                catalog2Vos = categoryEntities.stream().map(l2 -> {                    Catalog2Vo vo = new Catalog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName());                    //找二级分类的三级分类                    List categoryEntities3 = getParent_cid(selectList, l2.getCatId());                    if (categoryEntities3 != null) {                        List collect = categoryEntities3.stream().map(l3 -> {                            Catalog2Vo.Catalog3Vo catalog3Vo = new Catalog2Vo.Catalog3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());                            return catalog3Vo;                        }).collect(Collectors.toList());                        vo.setCatalog3List(collect);                    }                    return vo;                }).collect(Collectors.toList());            }            return catalog2Vos;        }));        return parentCid;    }

        这样就大大提高系统的吞吐量~ 

上一篇:ps怎么画q版人物?

下一篇:前端excel带样式导出 exceljs 插件的使用