当前位置:网站首页>谷粒商城-day14-商城业务与压力测试
谷粒商城-day14-商城业务与压力测试
2022-08-02 21:33:00 【周周写不完的代码】
整合 thymeleaf
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
访问 首页

这里跟雷神讲的 springboot2 也互动了下,哈哈哈哈,前端时间也复习了下
整合 dev-tools
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
yml 关闭 thymeleaf缓存
这里先取出以及分类
Controller
@Controller
public class IndexController {
@Resource
CategoryService categoryService;
@GetMapping({
"/", "/index.html"})
public String indexPage(Model model) {
// TODO 查出所有的分类数据
List<CategoryEntity> categorys = categoryService.getLevel1Categorys();
// 这里提供过 默认前缀和后缀 thymeleaf
// 视图解析器
// 如果之前搞过 springMVC 整合 会对这里很熟悉
model.addAttribute("categorys", categorys);
return "index";
}
}
service 比较简单就是 parent_id 为 0 的分类数据
下面把前端渲染的代码简单写下,就是 thymeleaf 的遍历还有属性绑定,熟悉 vue 的这块就会发现就是换了个底层实现
<li th:each="category : ${categorys}">
<a href="#" class="header_main_left_a" th:attr="ctg-data=${category.catId}"><b th:text="${category.name}"></b></a>
</li>
渲染二三级目录
这个之前做过一次,主要就是要构造符合要求的 json
controller 代码我贴一下
@Override
public Map<String, List<Catelog2Vo>> getCatalogJson() {
// 1. 查出所有1级分类
List<CategoryEntity> level1Categorys = getLevel1Categorys();
// 2. 封装数据
// 1.每一个分类,查到这个一级分类的二级分类
Map<String, List<Catelog2Vo>> parentCid = level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
// 1.每一个分类,查到这个一级分类的二级分类
List<CategoryEntity> categoryEntityList = baseMapper.selectList(
new QueryWrapper<CategoryEntity>().eq("parent_cid", v.getCatId()));
// 2. 封装上面的结果
List<Catelog2Vo> catelog2Vos = null;
if (categoryEntityList != null) {
catelog2Vos = categoryEntityList.stream().map(l2 -> {
Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName());
// 1. 找到当前二级分类的三级分类分装成 vo
List<CategoryEntity> level3Catalog = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", l2.getCatId()));
if (level3Catalog!=null) {
List<Catelog2Vo.Catelog3Vo> collect = level3Catalog.stream().map(l3 -> {
// 2.封装成指定格式
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());
return catelog3Vo;
}).collect(Collectors.toList());
catelog2Vo.setCatalog3List(collect);
}
return catelog2Vo;
}).collect(Collectors.toList());
}
return catelog2Vos;
}));
return parentCid;
}
搭建域名访问环境

上面是需要实现的效果,简单说下我这边环境,我这边 nginx 没用 docker,本地安装的nginx,然后java服务也在本机
# /etc/hosts
127.0.0.1 gulimall.com
# nginx.conf
upstream gulimall {
server localhost:88;
}
# 详细配置
server {
listen 80;
server_name gulimall.com;
location / {
proxy_set_header Host $host;
proxy_pass http://gulimall;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
gateway
- id: gulimall_host_route
uri: lb://gulimall-product
predicates:
- Host=**.gulimall.com,gulimall.com
访问 gulimall.com 测试即可
性能压测
这里通过修改 简单测试下吞吐量大小
jvisualvm
我这边 jdk 自带的里面用不了,去官网重新下载了个 VisualVM: Download
装下 visual gc 这个插件
nginx 动静分离

mac 上 Nginx 文件目录是这个 /opt/homebrew/opt/nginx/html
我们把 thymeleaf 里面的 static 文件夹放到 nginx 下面
nginx 添加配置
server {
listen 80;
server_name gulimall.com;
location /static/ {
root /opt/homebrew/opt/nginx/html;
}
location / {
proxy_set_header Host $host;
proxy_pass http://gulimall;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
nginx -s reload
重启即可
优化三级分类代码
总的思路就是减少搜索次数,一次扫表,后续从这个集合 中取,这个我有个项目当中也是这样做的,没缓存情况下很多次搜索io开销很高
@Override
public Map<String, List<Catelog2Vo>> getCatalogJson() {
// TODO 将数据库的查询变成一次
List<CategoryEntity> selectList = baseMapper.selectList(null);
// 1. 查出所有1级分类
List<CategoryEntity> level1Categorys = getParentCid(selectList, 0L);
// 2. 封装数据
// 1.每一个分类,查到这个一级分类的二级分类
Map<String, List<Catelog2Vo>> parentCid = level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
// 1.每一个分类,查到这个一级分类的二级分类
List<CategoryEntity> categoryEntityList = getParentCid(selectList, v.getCatId());
// 2. 封装上面的结果
List<Catelog2Vo> catelog2Vos = null;
if (categoryEntityList != null) {
catelog2Vos = categoryEntityList.stream().map(l2 -> {
Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName());
// 1. 找到当前二级分类的三级分类分装成 vo
List<CategoryEntity> level3Catalog = getParentCid(selectList, l2.getCatId());
if (level3Catalog != null) {
List<Catelog2Vo.Catelog3Vo> collect = level3Catalog.stream().map(l3 -> {
// 2.封装成指定格式
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());
return catelog3Vo;
}).collect(Collectors.toList());
catelog2Vo.setCatalog3List(collect);
}
return catelog2Vo;
}).collect(Collectors.toList());
}
return catelog2Vos;
}));
return parentCid;
}
private List<CategoryEntity> getParentCid(List<CategoryEntity> selectList, Long parentCid) {
return selectList.stream().filter(item -> {
return item.getParentCid().equals(parentCid);
}).collect(Collectors.toList());
}
再次用 jmeter 测试
边栏推荐
猜你喜欢
随机推荐
30天啃透这份Framework 源码手册直接面进大厂
用户之声 | 我与GBase的缘分
一群搞社区的人
js function anti-shake and function throttling and other usage scenarios
面试了个985毕业的,回答“性能调优”题时表情令我毕生难忘
二叉搜索树的实现
面试官:可以谈谈乐观锁和悲观锁吗
y85.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶、pushgateway和prometheus存储(十六)
最近火爆朋友圈的“广告电商”,核心商业模式是什么,广告收入真实靠谱吗?
RuoYi-App启动教程
JumpServer开源堡垒机完成龙芯架构兼容性认证
IP协议(网际协议)
golang 刷leetcode:将字符串翻转到单调递增
2018HBCPC个人题解
ML之PDP:基于titanic泰坦尼克是否获救二分类预测数据集利用PDP部分依赖图对RF随机森林和LightGBM模型实现可解释性案例
JumpServer open source bastion machine completes Loongson architecture compatibility certification
成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
Bee 事务注解 @Tran 使用实例
源码构建LAMP环境-3
牛客每日刷题之链表