136、商城業務-首頁-整合thymeleaf渲染首頁 - 140、商城業務-nginx-搭建域名訪問環境二(負載均衡到網關)
坑主要出現在我服務器的虛擬機訪問不到我的本地訪問,所以nginx暫時不能在上面,自己在本地測試的話,就先安裝在本地虛擬機上,到后面帶上都部署到k8s上,在切換到服務器虛擬機上。
渲染代碼:
@GetMapping({"/", "index.html"}) public String indexPage(Model model) { List<CategoryEntity> categoryEntities = categoryservice.getLevel1Categorys(); model.addAttribute("categorys", categoryEntities); return "index"; } @ResponseBody @GetMapping("/index/catalog.json") public Map<String, List<Catalog2Vo>> getCatalogJson() { Map<String, List<Catalog2Vo>> map = categoryservice.getCatalogJson(); return map; }
/** * 查詢所有一級分類 * * @return */ @Override public List<CategoryEntity> getLevel1Categorys() { List<CategoryEntity> categoryEntities = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0)); return categoryEntities; } @Override public Map<String, List<Catalog2Vo>> getCatalogJson() { //查詢出所有分類 List<CategoryEntity> selectList = baseMapper.selectList(null); //先查出所有一級分類 List<CategoryEntity> level1Categorys = getCategorys(selectList, 0L); //封裝數據 map k,v 結構 Map<String, List<Catalog2Vo>> map = level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> { //每一個的一級分類,查到這個一級分類的二級分類 List<CategoryEntity> category2Entities = getCategorys(selectList, v.getCatId()); List<Catalog2Vo> catelog2Vos = null; if (category2Entities != null) { catelog2Vos = category2Entities.stream().map(level2 -> { //封裝catalog2Vo Catalog2Vo catalog2Vo = new Catalog2Vo(v.getCatId().toString(), null, level2.getCatId().toString(), level2.getName()); //每一個二級分類,查到三級分類 List<CategoryEntity> category3Entities = getCategorys(selectList, level2.getCatId()); if (category3Entities != null) { List<Object> catalog3List = category3Entities.stream().map(level3 -> { //封裝catalog3Vo Catalog2Vo.Catalog3Vo catalog3Vo = new Catalog2Vo.Catalog3Vo(level2.getCatId().toString(), level3.getCatId().toString(), level3.getName()); return catalog3Vo; }).collect(Collectors.toList()); //封裝catalog3Vo到catalog2Vo catalog2Vo.setCatalog3List(catalog3List); } return catalog2Vo; }).collect(Collectors.toList()); } //返回v=catalog2Vo return catelog2Vos; })); return map; } /** * 根據傳進分類篩選出對應級別 * * @param list * @param parent_cid * @return */ public List<CategoryEntity> getCategorys(List<CategoryEntity> list, Long parent_cid) { List<CategoryEntity> collect = list.stream().filter(l -> parent_cid.equals(l.getParentCid())).collect(Collectors.toList()); return collect; }
<ul>
<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>
</ul>
nginx
[root@192 conf.d]# cat gulimall.conf server { listen 80; server_name gulimall.com; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { proxy_set_header Host $host; proxy_pass http://gulimall; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } [root@192 conf.d]#
注:收獲最多的就是 proxy_set_header Host $host;如果只配置網關的話,后面加api能路由到商品訪問,但是訪問不到gulimall。
修改網關:
- id: gulimall_host uri: lb://gulimall-product predicates: - Host=**.gulimall.com,gulimall.com
要把這個放到最后面,如果放到前面當請求接口的時候,會優先匹配到這個路由,而真正的訪問商品訪問是要去掉api的。
測試如下: