谷粒商城-高级-51 -检索服务-页面环境搭建

一、导入相关依赖和资源

因为我们的项目是微服务架构,所以,商城检索服务需要放在 gulimall-search 模块。
1、引入 thymeleaf 依赖 gulimall-search/pom.xml

<!-- thymeleaf -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

        <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
    </dependency>

关掉 thymeleaf 缓存
gulimall-search/src/main/resources/application.yml

spring:
  #  配置nacos注册中心
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: gulimall-search
  thymeleaf:
    cache: false   # 测试期间关掉缓存
server:
  port: 12000

2、导入静态资源
搜索页面放在templates路径下,即:

gulimall-search/src/main/resources/templates/list.html

该页面所用到的js、css都放在Nginx服务下,即动态分离模式。

二、修改服务配置

1、修改hosts配置文件

192.168.10.10 gulimall.com
192.168.10.10 search.gulimall.com  # 新增搜索域名

2、修改Nginx配置文件

给原来的域名gulimall.com 后边添加子域名 *.gulimall.com

# server_name  gulimall.com ;
server_name  gulimall.com *.gulimall.com;

gulimall.conf 完整文件:

[root@localhost conf.d]# cat gulimall.conf
server {
    listen       80;
    server_name  gulimall.com  *.gulimall.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    location /static/ {
       root /usr/share/nginx/html;
    }

    location / {
       #proxy_pass http://192.168.10.1:10000;
        proxy_set_header Host $host;
        proxy_pass http://gulimall; #use nginx.conf upstream load balancing
    }

    #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;
    #}
}

重启Nginx:

docker restart nginx

3、添加搜索网关服务

网关服务修改配置文件,添加搜索网关:
gulimall-gateway/src/main/resources/application.yml

spring:
  cloud:
    gateway:
      routes:
        - id: product_route
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: third_party_route
          uri: lb://gulimall-third-party
          predicates:
            - Path=/api/thirdparty/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: member_route
          uri: lb://gulimall-member
          predicates:
            - Path=/api/member/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: ware_route
          uri: lb://gulimall-ware
          predicates:
            - Path=/api/ware/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}

        - id: gulimall_host_route
          uri: lb://gulimall-product
          predicates:
            - Host=gulimall.com

        - id: gulimall_search_route
          uri: lb://gulimall-search
          predicates:
            - Host=search.gulimall.com

三、搜索页控制器

com/atguigu/gulimall/search/controller/SearchController.java

package com.atguigu.gulimall.search.controller;

import com.atguigu.gulimall.search.service.MallSearchService;
import com.atguigu.gulimall.search.vo.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author: kaiyi
 * @create: 2020-09-03 00:37
 */
@Controller
public class SearchController {

  @Autowired
  MallSearchService mallSearchService;

  @GetMapping("/list.html")
  public String listPage(SearchParam param){

    Object result = mallSearchService.search(param);
    return "list";
  }

}

搜索页面展示:
file

为者常成,行者常至