1.本机地址(也就是nginx,因为nginx监听本机的80端口)域名映射 用upstream软件 192.18.1.1 gulimall.com 意思就是将gulimall.com这个域名映射到本机地址
2.nginx配置
- upstream: 定义一组服务器,这里就是收到请求转到本地88端口(网关)
- listen 80
- server_name gulimall.com
监听gulimall.com这个域名 - location /
将请求转给gulimall 也就是upstream配置的服务器
nginx带来给网关的时候会丢失请求的host信息 所以加上 proxy_set_header - location /static/ 带static前缀的映射到nginx本地的html文件夹,这个文件夹存放了静态资源,实现动静分离的效果
3.gateway配置
- id: gulimall_host_route
uri: lb://service-product
predicates:
- Host=gulimall.com,item.gulimall.com
首页查询一级目录(父id为0的记录)
后端接收:
@GetMapping(value = {"/","index.html"})
private String indexPage(Model model) {
//1、查出所有的一级分类
List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
model.addAttribute("categories",categoryEntities);
return "index";
}
首页就是访问gulimall.com,然后nginx会代理到本地的88端口,也就是网关,网关将这个域名转到具体的服务gulimall-product
鼠标放到一级目录查询二三级目录
后端接收
//catalogLoader.js定义了路径,鼠标放到以及菜单,会发送这个请求
//index/json/catalog.json
@GetMapping(value = "/index/catalog.json")
@ResponseBody
public Map<String, List<Catelog2Vo>> getCatalogJson() {
Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
return catalogJson;
}
getCatlogJson方法 也就是获取二三级菜单方法
@Override
public Map<String, List<Catelog2Vo>> getCatalogJson() {
// System.out.println("查询了数据库");
//将数据库的多次查询变为一次
List<CategoryEntity> selectList = this.baseMapper.selectList(null);
//1、查出所有分类
//1、1)查出所有一级分类
List<CategoryEntity> level1Categorys = getParent_cid(selectList, 0L);
//封装数据
Map<String, List<Catelog2Vo>> parentCid = level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
//1、每一个的一级分类,查到这个一级分类的二级分类
List<CategoryEntity> categoryEntities = getParent_cid(selectList, v.getCatId());
//2、封装上面的结果
List<Catelog2Vo> catelog2Vos = null;
if (categoryEntities != null) {
catelog2Vos = categoryEntities.stream().map(l2 -> {
Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName().toString());
//1、找当前二级分类的三级分类封装成vo
List<CategoryEntity> level3Catelog = getParent_cid(selectList, l2.getCatId());
if (level3Catelog != null) {
List<Catelog2Vo.Category3Vo> category3Vos = level3Catelog.stream().map(l3 -> {
//2、封装成指定格式
Catelog2Vo.Category3Vo category3Vo = new Catelog2Vo.Category3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());
return category3Vo;
}).collect(Collectors.toList());
catelog2Vo.setCatalog3List(category3Vos);
}
return catelog2Vo;
}).collect(Collectors.toList());
}
return catelog2Vos;
}));
return parentCid;
}
版权声明:本文为weixin_46666822原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。