课件
性能优化 - Nginx 动静分离

- 首先,把商品服务中静态文件夹 index 放到 nginx 下 /mydata/nginx/html/static目录;
- 给模板中所有静态资源的请求路径前都加上 /static;
修改 Nginx 配置文件 /mydata/nginx/conf/conf.d/gulimall.conf
# /static/ 下所有的请求都转给 nginxlocation /static/ {root /user/share/nginx/html;}
性能优化 - 优化查询三级分类数据
```java @Override public Map
> getCatalogJson() { System.out.println(“查询了数据库”); // 性能优化:将数据库的多次查询变为一次 List selectList = this.baseMapper.selectList(null); //1、查出所有分类 //1、1)查出所有一级分类 List
level1Categories = getParentCid(selectList, 0L); //封装数据 Map
> parentCid = level1Categories.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> { //1、每一个的一级分类,查到这个一级分类的二级分类List<CategoryEntity> categoryEntities = getParentCid(selectList, v.getCatId());//2、封装上面的结果List<Catalogs2Vo> catalogs2Vos = null;if (categoryEntities != null) {catalogs2Vos = categoryEntities.stream().map(l2 -> {Catalogs2Vo catalogs2Vo = new Catalogs2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName().toString());//1、找当前二级分类的三级分类封装成voList<CategoryEntity> level3Catelog = getParentCid(selectList, l2.getCatId());if (level3Catelog != null) {List<Catalogs2Vo.Category3Vo> category3Vos = level3Catelog.stream().map(l3 -> {//2、封装成指定格式Catalogs2Vo.Category3Vo category3Vo = new Catalogs2Vo.Category3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());return category3Vo;}).collect(Collectors.toList());catalogs2Vo.setCatalog3List(category3Vos);}return catalogs2Vo;}).collect(Collectors.toList());}return catalogs2Vos;
})); return parentCid; }
private List
性能优化 - 调整内存
对于系统的性能优化方面,我们还可以调整 Java 程序运行的堆内存空间来减少 Full GC 和 Minor GC 从而减少运行时间。
Full gc 最会影响性能,根据代码问题,避免 full gc 频率。可以适当调大年轻代容量,让大对象可以在年轻代触发 young gc,调整大对象在年轻代的回收频次,尽可能保证大对象在年轻代回收,减小老年代缩短回收时间;
