二、商城系统首页
不使用前后端分离开发了,管理后台用vue nginx发给网关集群,网关再路由到微服务
静态资源放到nginx中
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-q6oWW2aH-1633360430231)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004141235769.png)]](https://img-blog.csdnimg.cn/22f648e3b269406a85bdabd02aaf536b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
导入thymeleaf依赖、热部署依赖devtools使页面实时生效
achangmall-product/pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
html\首页资源\index放到achangmall-product下的static文件夹
把index.html放到templates中
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DzhvIwVW-1633360430233)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004141636988.png)]](https://img-blog.csdnimg.cn/82d976c7b62d471aa8fbad7809a938e9.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_8,color_FFFFFF,t_70,g_se,x_16)
thymeleaf:
cache: false
suffix: .html
prefix: classpath:/templates/
- web开发放到web包下,原来的controller是前后分离对接手机等访问的,所以可以改成app,对接app应用
com.achang.achangmall.product.web【存放专门用于页面跳转的controller】
com.achang.achangmall.product.app【存放前后端奶粉里的rest风格接口controller】
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tPsk2HYj-1633360430235)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004142800547.png)]](https://img-blog.csdnimg.cn/267291def9eb47af8dc059507a532f35.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_16,color_FFFFFF,t_70,g_se,x_16)
- 渲染一级分类菜单 刚导入index.html时,里面的分类菜单都是写死的,我们要访问数据库拿到放到model中,然后在页面foreach填入
@Controller
public class IndexController {
@Autowired
private CategoryService categoryService;
@GetMapping({"/","/index.html"})
public String indexPage(Model model){
List<CategoryEntity> categoryEntityList = categoryService.getLevel1Categorys();
model.addAttribute("categorys",categoryEntityList);
return "index";
}
}
@Override
public List<CategoryEntity> getLevel1Categorys() {
return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("cat_level",1));
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aBQnhfHR-1633360430238)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004144731515.png)]](https://img-blog.csdnimg.cn/86eb37d28ad44642a7d36940a38e6966.png)
xmlns:th="http://www.thymeleaf.org"
更新页面后,在当前页面按Ctrl+Shift+F9 ,进行刷新热部署页面
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XSOh43W6-1633360430242)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004145424575.png)]](https://img-blog.csdnimg.cn/43a18490232d4d429ff63d23d817bd92.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_12,color_FFFFFF,t_70,g_se,x_16)
<li th:each="category : ${categorys}">
<a href="#" class="header_main_left_a" th:attr="ctg-data=${category.catId}"><b th:text="${category.name}">家用电器111</b></a>
</li>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-05kYVe1v-1633360430244)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004150003768.png)]](https://img-blog.csdnimg.cn/6de4f81791ea4013a5bbfc29dc6c3dc8.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
com.achang.achangmall.product.vo.Catelog2Vo
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Catelog2Vo {
private String catalog1Id;
private List<Catelog3Vo> catalog3List;
private String id;
private String name;
@NoArgsConstructor
@AllArgsConstructor
@Data
public static class Catelog3Vo{
private String catalog2Id;
private String id;
private String name;
}
}
com.achang.achangmall.product.web.IndexController
@ResponseBody
@GetMapping("/index/catalog.json")
public Map<Long, List<Catelog2Vo>> getCatalogJson(){
Map<Long, List<Catelog2Vo>> resultMap = categoryService.getCatelogJson();
return resultMap;
}
com.achang.achangmall.product.service.impl.CategoryServiceImpl
@Override
public Map<Long, List<Catelog2Vo>> getCatelogJson() {
List<CategoryEntity> level1Categorys = getLevel1Categorys();
Map<Long, List<Catelog2Vo>> resultMap = level1Categorys.stream().collect(Collectors.toMap(CategoryEntity::getCatId, v -> {
List<CategoryEntity> list = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", v.getCatId()));
List<Catelog2Vo> catelog2VoList = null;
if (!StringUtils.isEmpty(list)) {
catelog2VoList = list.stream().map(item -> {
Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, item.getCatId().toString(), item.getName());
List<CategoryEntity> entityList = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", item.getCatId()));
if (!StringUtils.isEmpty(entityList)){
List<Catelog2Vo.Catelog3Vo> catelog3Vos = entityList.stream().map(m -> {
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(item.getCatId().toString(),m.getCatId().toString(),m.getName());
return catelog3Vo;
}).collect(Collectors.toList());
catelog2Vo.setCatalog3List(catelog3Vos);
}
return catelog2Vo;
}).collect(Collectors.toList());
return catelog2VoList;
}
return catelog2VoList;
}));
return resultMap;
}
1、Nginx配置文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g0qtVtoX-1633360430245)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004215328514.png)]](https://img-blog.csdnimg.cn/754c612576574557a2bda9595910c261.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
C:\Windows\System32\drivers\etc\hosts
192.168.109.101 achangmall.com
测试访问之前的9200端口
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3nxKHKGr-1633360430247)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004220252978.png)]](https://img-blog.csdnimg.cn/213c07af10b14e7a83cc1dc3bb78a0b0.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_18,color_FFFFFF,t_70,g_se,x_16)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-42kIYxX1-1633360430249)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004222126644.png)]](https://img-blog.csdnimg.cn/f3cacc33f62c46899bce7f897b03ddab.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
server {
listen 80;
server_name achangmall.com;
location / {
proxy_pass http://192.168.153.1:10000;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
- 测试访问http://achangmall.com/
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fDSzgkRR-1633360430251)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004222946343.png)]](https://img-blog.csdnimg.cn/7301407347ad4acb9ff86c0e4f99f624.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_20,color_FFFFFF,t_70,g_se,x_16)
2、Nginx+网关
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qHdTCuEE-1633360430252)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004223900250.png)]](https://img-blog.csdnimg.cn/2e29492193194a02adf00daa601a5b02.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
upstream achangmall{
server 192.168.153.1:88;
}
location / {
proxy_pass http://achangmall;
}
- 但是我们测试访问http://achangmall.com/
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OfNPN1iB-1633360430254)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004224743696.png)]](https://img-blog.csdnimg.cn/9bb74186f17146a397a11c33ae247428.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
nginx转发会丢失掉host中的信息
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CknwcXiQ-1633360430255)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004224955016.png)]](https://img-blog.csdnimg.cn/0feb0cc7b9d448fd8f2309d5e6842c9c.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_17,color_FFFFFF,t_70,g_se,x_16)
proxy_set_header Host $host;
- 再次测试访问,http://achangmall.com,测试成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jS14NIga-1633360430257)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004225038215.png)]](https://img-blog.csdnimg.cn/ebd46a63d21b46bda0b6e149420f80ad.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-163I3Utr-1633360430259)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004225334138.png)]](https://img-blog.csdnimg.cn/116952d498ef468585e8c70a8c2112fb.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_17,color_FFFFFF,t_70,g_se,x_16)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cSAwEuiH-1633360430260)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004231101010.png)]](https://img-blog.csdnimg.cn/e1e771b023e14560a42942d2b462cc3b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_20,color_FFFFFF,t_70,g_se,x_16)
3、 压力测试
- Jmeter下载:https://jmeter.apache.org/download_jmeter.cgi
创建测试计划,添加线程组
线程数==用户
ramp-up 多长时间内发送完
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z3Oa150X-1633427287147)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211005132510370.png)]](https://img-blog.csdnimg.cn/80d1b1291359492b8ac660170d0b8a5b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_17,color_FFFFFF,t_70,g_se,x_16)
添加-取样器-HTTP请求
添加-监听器-查看结果树
添加-监听器-汇总报告
SQL耗时越小越好,一般情况下微秒级别
命中率越高越好,一般情况下不能低于95%
锁等待次数越低越好,等待时间越短越好
中间件越多,性能损失越大,大多都损失在网络交互
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ugk8WPO9-1633427287150)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211005132551401.png)]](https://img-blog.csdnimg.cn/3c0a6a74c1284b4db6c71ef90d8ad6d1.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_16,color_FFFFFF,t_70,g_se,x_16)
4、Nginx动静分离
由于动态资源和静态资源目前都处于服务端,所以为了减轻服务器压力,我们将 js、css、img等静态资源放置在Nginx端,以减轻服务器压力
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6l3uixHC-1633427287153)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211005150759814.png)]](https://img-blog.csdnimg.cn/94e2b6b9d71e46f1a71443f09d869133.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
静态文件上传到 mydata/nginx/html/static/index/css,这种格式
mkdir static
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AJAH9Krr-1633427287155)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211005151412797.png)]](https://img-blog.csdnimg.cn/5be54f19c5be4c11b754c80fd33d6cfb.png)
修改index.html的静态资源路径,加上static前缀src="/static/index/img/img_09.png"
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h4dibUij-1633427287157)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211005152809889.png)]](https://img-blog.csdnimg.cn/483ae3207d784442b790f082ae0d4a96.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PGPco1Qb-1633427287159)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211005152853535.png)]](https://img-blog.csdnimg.cn/797c5623dfee48f9923f0ebb0ac5b798.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_15,color_FFFFFF,t_70,g_se,x_16)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MXktd7OB-1633427287162)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211005152936333.png)]](https://img-blog.csdnimg.cn/7593bb44e7f1465b921ceb368de1370b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_15,color_FFFFFF,t_70,g_se,x_16)
等等等,给index.html页面中的路径都加上/static/
修改/mydata/nginx/conf/conf.d/gulimall.conf
如果遇到有/static为前缀的请求,转发至html文件夹
location /static/ {
root /usr/share/nginx/html;
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ogVzG5mE-1633427287164)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211005164343874.png)]](https://img-blog.csdnimg.cn/881ffe6809eb42bdbe530816347f7a0f.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_17,color_FFFFFF,t_70,g_se,x_16)
再次测试访问achangmall.com ,成功将静态资源由nginx返回
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R1Q45Hbb-1633427287166)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211005164413288.png)]](https://img-blog.csdnimg.cn/7bf2416e33214ca8a0415d86db3775e7.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_5piM5Zac5qyi5ZCD6buE5qGD,size_19,color_FFFFFF,t_70,g_se,x_16)
5、优化三级分类查询
之前是循环查询数据库,导致有多少跟数据库进行io交互;
这里我们减少数据库的交互,一次性查询所有的数据 ,减少io资源消耗
com.achang.achangmall.product.service.impl.CategoryServiceImpl
@Override
public Map<Long, List<Catelog2Vo>> getCatelogJson() {
List<CategoryEntity> allList = baseMapper.selectList(null);
List<CategoryEntity> level1Categorys = getParent_cid(allList,0L);
Map<Long, List<Catelog2Vo>> resultMap = level1Categorys.stream().collect(Collectors.toMap(CategoryEntity::getCatId, v -> {
List<CategoryEntity> list = getParent_cid(allList,v.getCatId());
List<Catelog2Vo> catelog2VoList = null;
if (!StringUtils.isEmpty(list)) {
catelog2VoList = list.stream().map(item -> {
Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, item.getCatId().toString(), item.getName());
List<CategoryEntity> entityList = getParent_cid(allList,item.getCatId());
if (!StringUtils.isEmpty(entityList)){
List<Catelog2Vo.Catelog3Vo> catelog3Vos = entityList.stream().map(m -> {
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(item.getCatId().toString(),m.getCatId().toString(),m.getName());
return catelog3Vo;
}).collect(Collectors.toList());
catelog2Vo.setCatalog3List(catelog3Vos);
}
return catelog2Vo;
}).collect(Collectors.toList());
return catelog2VoList;
}
return catelog2VoList;
}));
return resultMap;
}
private List<CategoryEntity> getParent_cid(List<CategoryEntity> allList,Long parent_cid) {
List<CategoryEntity> collect = allList.stream().filter(item -> {
return item.getParentCid().equals(parent_cid);
}).collect(Collectors.toList());
return collect;
}
|