目录:
(1)商品热度排名
(2)在service-list-client封装远程接口
(3)在service-item模块调用接口
(1)商品热度排名
搜索商品时,后面我们会根据热点排序,何时更新热点?我们在获取商品详情时调用更新
当我们进入商品详情页的时候,我们从Reids取缓存数据时,这个取时候就把热点+1,改ES中的hotScore字段,然后在搜索商品的时候就根据那个字段进行排序
在service-list模块添加:
封装接口与实现类与控制器
SearchService
/**
* 更新热点
* @param skuId
*/
void incrHotScore(Long skuId);
实现类
Redis:使用zincrby hotScore 1 skuId:21
@Autowired
private RedisTemplate redisTemplate;
@Override
public void incrHotScore(Long skuId) {
// 定义key
String hotKey = "hotScore";
// 保存数据
Double hotScore = redisTemplate.opsForZSet().incrementScore(hotKey, "skuId:" + skuId, 1);
if (hotScore%10==0){
// 更新es
Optional<Goods> optional = goodsRepository.findById(skuId);
Goods goods = optional.get();
goods.setHotScore(Math.round(hotScore));
goodsRepository.save(goods);
}
}
控制器ListApiController:
/**
* 更新商品incrHotScore
*
* @param skuId
* @return
*/
@GetMapping("inner/incrHotScore/{skuId}")
public Result incrHotScore(@PathVariable("skuId") Long skuId) {
// 调用服务层
searchService.incrHotScore(skuId);
return Result.ok();
}
(2)在service-list-client封装远程接口
搭建service-list-client
搭建方式如service-item-client
修改pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>service-client</artifactId>
<version>1.0</version>
</parent>
<artifactId>service-list-client</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>service-list-client</name>
<description>service-list-client</description>
</project>
添加接口
package com.atguigu.gmall.list.client;
@FeignClient(value = "service-list", fallback = ListDegradeFeignClient.class)
public interface ListFeignClient {
/**
* 更新商品incrHotScore
* @param skuId
* @return
*/
@GetMapping("/api/list/inner/incrHotScore/{skuId}")
Result incrHotScore(@PathVariable("skuId") Long skuId);
}
降级返回:
package com.atguigu.gmall.list.client.impl;
@Component
public class ListDegradeFeignClient implements ListFeignClient {
@Override
public Result incrHotScore(Long skuId) {
return null;
}
}
(3)在service-item模块调用接口
引入依赖
<dependency> <groupId>com.atguigu.gmall</groupId> <artifactId>service-list-client</artifactId> <version>1.0</version> </dependency>
接口调用
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ProductFeignClient productFeignClient;
@Autowired
private ListFeignClient listFeignClient;
@Autowired
private ThreadPoolExecutor threadPoolExecutor;
@Override
public Map<String, Object> getBySkuId(Long skuId) {
Map<String, Object> result = new HashMap<>();
...
//获取分类信息
CompletableFuture<Void> categoryViewCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
//分类信息
result.put("categoryView", categoryView);
}, threadPoolExecutor);
//更新商品incrHotScore
CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
listFeignClient.incrHotScore(skuId);
}, threadPoolExecutor);
CompletableFuture.allOf(skuCompletableFuture, spuSaleAttrCompletableFuture, skuValueIdsMapCompletableFuture,skuPriceCompletableFuture, categoryViewCompletableFuture, incrHotScoreCompletableFuture).join();
return result;
}
}
继续添加下面代码
访问页面到19次,他是不会增加10的,每访问10才进行增加