乐优商城(09)–商品详情
一、商品详情
1.1、商品详情页服务
商品详情浏览量比较大,并发高,所以独立开发一个微服务,用来展示商品详情
创建module
商品的详情页服务,命名为:leyou-goods-web
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>myLeyou</artifactId>
<groupId>com.leyou.parent</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.leyou.goods</groupId>
<artifactId>leyou-goods-web</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.leyou.item</groupId>
<artifactId>leyou-item-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
启动类
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class LeyouGoodsWebApplication {
public static void main(String[] args) {
SpringApplication.run(LeyouGoodsWebApplication.class,args);
}
}
application.yaml
server:
port: 8084
spring:
application:
name: goods-web
thymeleaf:
cache: false #关闭缓存,利于开发
cloud:
nacos:
discovery:
server-addr: ip地址:8848
username: nacos
password: nacos
页面模板
从leyou-portal中复制item.html模板到当前项目resource目录下的templates中:
1.2、页面跳转
1.2.1、修改页面跳转路径
首先需要修改搜索结果页的商品地址,目前所有商品的地址都是:http://www.leyou.com/item.html
应该跳转到对应的商品的详情页才对。
那么问题来了:商品详情页是一个SKU?还是多个SKU的集合?
通过详情页的预览,知道它是多个SKU的集合,即SPU。
所以,页面跳转时,应该携带SPU的id信息。
例如:http://www.leyou.com/item/2314123.html
这里就采用了路径占位符的方式来传递spu的id,打开search.html
,修改其中的商品路径:
1.2.2、nginx反向代理
接下来,要把这个地址指向刚刚创建的服务:leyou-goods-web
,其端口为8084
需要在nginx.conf中添加一段配置:
把以/item开头的请求,代理到机器的8084端口。
1.2.3、编写跳转controller
在leyou-goods-web
中编写controller,接收请求,并跳转到商品详情页:
@RestController
@RequestMapping("