第五次课程提交 es实体化
This commit is contained in:
parent
51d1d45850
commit
fa0f80b30a
|
@ -14,7 +14,8 @@ public class AuthContextHolder {
|
|||
}
|
||||
|
||||
public static Long getUserId() {
|
||||
return userId.get();
|
||||
|
||||
return userId.get();
|
||||
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,70 @@
|
|||
package com.atguigu.tingshu.common.config.pool;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* @author: atguigu
|
||||
* @create: 2024-08-09 11:28
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class ThreadPoolConfig {
|
||||
|
||||
/**
|
||||
* 基于JDK(JUC)提供线程池Class
|
||||
*/
|
||||
@Bean
|
||||
public Executor threadPoolExecutor() {
|
||||
//1.获取当前服务器核心数确定核心线程数
|
||||
int cpuCoreCount = Runtime.getRuntime().availableProcessors();
|
||||
int threadCount = cpuCoreCount * 2;
|
||||
//2.通过构造方法创建线程池对象
|
||||
ThreadPoolExecutor threadPoolExecutor =
|
||||
new ThreadPoolExecutor(
|
||||
threadCount,
|
||||
threadCount,
|
||||
0,
|
||||
TimeUnit.SECONDS,
|
||||
new ArrayBlockingQueue<>(200),
|
||||
Executors.defaultThreadFactory(),
|
||||
new ThreadPoolExecutor.CallerRunsPolicy()
|
||||
);
|
||||
//3.可选:提交创建核心线程
|
||||
threadPoolExecutor.prestartCoreThread();
|
||||
return threadPoolExecutor;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 基于Spring提供线程池Class-threadPoolTaskExecutor 功能更强
|
||||
*/
|
||||
@Bean
|
||||
public Executor threadPoolTaskExecutor() {
|
||||
int count = Runtime.getRuntime().availableProcessors();
|
||||
int threadCount = count*2+1;
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
// 核心池大小
|
||||
taskExecutor.setCorePoolSize(threadCount);
|
||||
// 最大线程数
|
||||
taskExecutor.setMaxPoolSize(threadCount);
|
||||
// 队列程度
|
||||
taskExecutor.setQueueCapacity(300);
|
||||
// 线程空闲时间
|
||||
taskExecutor.setKeepAliveSeconds(0);
|
||||
// 线程前缀名称
|
||||
taskExecutor.setThreadNamePrefix("async-tingshu-Executor--");
|
||||
// 该方法用来设置 线程池关闭 的时候 等待 所有任务都完成后,再继续 销毁 其他的 Bean,
|
||||
// 这样这些 异步任务 的 销毁 就会先于 数据库连接池对象 的销毁。
|
||||
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
|
||||
// 任务的等待时间 如果超过这个时间还没有销毁就 强制销毁,以确保应用最后能够被关闭,而不是阻塞住。
|
||||
taskExecutor.setAwaitTerminationSeconds(300);
|
||||
// 线程不够用时由调用的线程处理该任务
|
||||
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
return taskExecutor;
|
||||
}
|
||||
}
|
|
@ -13,7 +13,8 @@ public class SystemConstant {
|
|||
|
||||
//专辑状态 0301-审核通过 0302-审核不通过
|
||||
public static final String ALBUM_STATUS_PASS="0301"; // 审核通过
|
||||
public static final String ALBUM_STATUS_NO_PASS="0302"; // 审核不通过
|
||||
public static final String ALBUM_STATUS_NO_PASS="0302";
|
||||
public static final String ALBUM_STATUS_ARTIFICIAL = "0303";// 审核不通过
|
||||
|
||||
//专辑统计 0401-播放量 0402-订阅量 0403-购买量 0403-评论数
|
||||
public static final String ALBUM_STAT_PLAY="0401"; // 播放量
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,7 +1,12 @@
|
|||
package com.atguigu.tingshu.album;
|
||||
|
||||
import com.atguigu.tingshu.album.impl.AlbumDegradeFeignClient;
|
||||
import com.atguigu.tingshu.common.result.Result;
|
||||
import com.atguigu.tingshu.model.album.AlbumInfo;
|
||||
import com.atguigu.tingshu.model.album.BaseCategoryView;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -10,7 +15,23 @@ import org.springframework.cloud.openfeign.FeignClient;
|
|||
*
|
||||
* @author atguigu
|
||||
*/
|
||||
@FeignClient(value = "service-album", fallback = AlbumDegradeFeignClient.class)
|
||||
@FeignClient(value = "service-album", fallback = AlbumDegradeFeignClient.class, path = "/api/album")
|
||||
public interface AlbumFeignClient {
|
||||
/**
|
||||
* 查询专辑信息,包含专辑标签列表
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/albumInfo/getAlbumInfo/{id}")
|
||||
public Result<AlbumInfo> getAlbumInfo(@PathVariable Long id);
|
||||
/**
|
||||
* 后续改为Redis缓存
|
||||
* 根据三级分类ID查询分类视图
|
||||
* @param category3Id
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/category/getCategoryView/{category3Id}")
|
||||
public Result<BaseCategoryView> getCategoryView(@PathVariable Long category3Id);
|
||||
|
||||
}
|
||||
|
|
|
@ -2,10 +2,26 @@ package com.atguigu.tingshu.album.impl;
|
|||
|
||||
|
||||
import com.atguigu.tingshu.album.AlbumFeignClient;
|
||||
import com.atguigu.tingshu.common.result.Result;
|
||||
import com.atguigu.tingshu.model.album.AlbumInfo;
|
||||
import com.atguigu.tingshu.model.album.BaseCategoryView;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AlbumDegradeFeignClient implements AlbumFeignClient {
|
||||
|
||||
|
||||
@Override
|
||||
public Result<AlbumInfo> getAlbumInfo(Long id) {
|
||||
log.error("[专辑服务]提供远程调用方法getAlbumInfo执行服务降级");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<BaseCategoryView> getCategoryView(Long category3Id) {
|
||||
log.error("[专辑服务]提供远程调用方法getCategoryView执行服务降级");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,7 +1,11 @@
|
|||
package com.atguigu.tingshu.user.client;
|
||||
|
||||
import com.atguigu.tingshu.common.result.Result;
|
||||
import com.atguigu.tingshu.user.client.impl.UserDegradeFeignClient;
|
||||
import com.atguigu.tingshu.vo.user.UserInfoVo;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -10,7 +14,14 @@ import org.springframework.cloud.openfeign.FeignClient;
|
|||
*
|
||||
* @author atguigu
|
||||
*/
|
||||
@FeignClient(value = "service-user", fallback = UserDegradeFeignClient.class)
|
||||
@FeignClient(value = "service-user",path = "/api/user",fallback = UserDegradeFeignClient.class)
|
||||
public interface UserFeignClient {
|
||||
/**
|
||||
* 根据用户ID查询用户信息
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/userInfo/getUserInfoVo/{userId}")
|
||||
public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
package com.atguigu.tingshu.user.client.impl;
|
||||
|
||||
|
||||
import com.atguigu.tingshu.common.result.Result;
|
||||
import com.atguigu.tingshu.user.client.UserFeignClient;
|
||||
import com.atguigu.tingshu.vo.user.UserInfoVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class UserDegradeFeignClient implements UserFeignClient {
|
||||
|
||||
@Override
|
||||
public Result<UserInfoVo> getUserInfoVo(Long userId) {
|
||||
log.error("[用户服务]提供远程调用方法getUserInfoVo执行服务降级");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
|
|||
import com.atguigu.tingshu.album.service.BaseCategoryService;
|
||||
import com.atguigu.tingshu.common.result.Result;
|
||||
import com.atguigu.tingshu.model.album.BaseAttribute;
|
||||
import com.atguigu.tingshu.model.album.BaseCategoryView;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -34,6 +35,18 @@ public class BaseCategoryApiController {
|
|||
List<BaseAttribute> list=baseCategoryService.findAttribute(category1Id);
|
||||
return Result.ok(list);
|
||||
}
|
||||
/**
|
||||
* 后续改为Redis缓存
|
||||
* 根据三级分类ID查询分类视图
|
||||
* @param category3Id
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "根据三级分类ID查询分类视图")
|
||||
@GetMapping("/category/getCategoryView/{category3Id}")
|
||||
public Result<BaseCategoryView> getCategoryView(@PathVariable Long category3Id) {
|
||||
BaseCategoryView baseCategoryView = baseCategoryService.getCategoryView(category3Id);
|
||||
return Result.ok(baseCategoryView);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -14,4 +14,6 @@ public interface BaseCategoryService extends IService<BaseCategory1> {
|
|||
List<JSONObject> getBaseCategoryList();
|
||||
|
||||
List<BaseAttribute> findAttribute(Long category1Id);
|
||||
|
||||
BaseCategoryView getCategoryView(Long category3Id);
|
||||
}
|
||||
|
|
|
@ -7,8 +7,11 @@ import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
|
|||
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
|
||||
import com.atguigu.tingshu.album.service.AlbumAttributeValueService;
|
||||
import com.atguigu.tingshu.album.service.AlbumInfoService;
|
||||
import com.atguigu.tingshu.album.service.AuditService;
|
||||
import com.atguigu.tingshu.common.constant.SystemConstant;
|
||||
import com.atguigu.tingshu.common.execption.GuiguException;
|
||||
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
|
||||
import com.atguigu.tingshu.common.rabbit.service.RabbitService;
|
||||
import com.atguigu.tingshu.model.album.AlbumAttributeValue;
|
||||
import com.atguigu.tingshu.model.album.AlbumInfo;
|
||||
import com.atguigu.tingshu.model.album.AlbumStat;
|
||||
|
@ -43,6 +46,10 @@ public class AlbumInfoServiceImpl extends ServiceImpl<AlbumInfoMapper, AlbumInfo
|
|||
private AlbumStatMapper albumStatMapper;
|
||||
@Autowired
|
||||
private TrackInfoMapper trackInfoMapper;
|
||||
@Autowired
|
||||
private AuditService auditService;
|
||||
@Autowired
|
||||
private RabbitService rabbitService;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
|
@ -80,11 +87,24 @@ public class AlbumInfoServiceImpl extends ServiceImpl<AlbumInfoMapper, AlbumInfo
|
|||
//2.3 批量保存
|
||||
albumAttributeValueService.saveBatch(attributeValueList);
|
||||
}
|
||||
//3.保存专辑统计信息
|
||||
this.saveAlbumInfoStat(albumId, SystemConstant.ALBUM_STAT_PLAY, 0);
|
||||
this.saveAlbumInfoStat(albumId, SystemConstant.ALBUM_STAT_SUBSCRIBE, 0);
|
||||
this.saveAlbumInfoStat(albumId, SystemConstant.ALBUM_STAT_BUY, 0);
|
||||
this.saveAlbumInfoStat(albumId, SystemConstant.ALBUM_STAT_COMMENT, 0);
|
||||
|
||||
//对专辑内容(标题、简介等)进行审核
|
||||
String text = albumInfo.getAlbumTitle() + albumInfo.getAlbumIntro();
|
||||
String suggestion = auditService.auditText(text);
|
||||
if ("pass".equals(suggestion)) {
|
||||
albumInfo.setStatus(SystemConstant.ALBUM_STATUS_PASS);
|
||||
//4.采用RabbitMQ可靠性消息将过审专辑存入ES索引库
|
||||
rabbitService.sendMessage(MqConst.EXCHANGE_ALBUM, MqConst.ROUTING_ALBUM_UPPER, albumId);
|
||||
} else if ("block".equals(suggestion)) {
|
||||
albumInfo.setStatus(SystemConstant.ALBUM_STATUS_NO_PASS);
|
||||
} else if ("review".equals(suggestion)) {
|
||||
albumInfo.setStatus(SystemConstant.ALBUM_STATUS_ARTIFICIAL);
|
||||
}
|
||||
albumInfoMapper.updateById(albumInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -171,6 +191,18 @@ public class AlbumInfoServiceImpl extends ServiceImpl<AlbumInfoMapper, AlbumInfo
|
|||
//2.3 批量保存
|
||||
albumAttributeValueService.saveBatch(attributeValueList);
|
||||
|
||||
String text = albumInfo.getAlbumTitle() + albumInfo.getAlbumIntro();
|
||||
String suggestion = auditService.auditText(text);
|
||||
if ("pass".equals(suggestion)) {
|
||||
albumInfo.setStatus(SystemConstant.ALBUM_STATUS_PASS);
|
||||
//3.采用RabbitMQ可靠性消息将过审专辑存入ES索引库
|
||||
rabbitService.sendMessage(MqConst.EXCHANGE_ALBUM, MqConst.ROUTING_ALBUM_UPPER, id);
|
||||
} else if ("block".equals(suggestion)) {
|
||||
albumInfo.setStatus(SystemConstant.ALBUM_STATUS_NO_PASS);
|
||||
} else if ("review".equals(suggestion)) {
|
||||
albumInfo.setStatus(SystemConstant.ALBUM_STATUS_ARTIFICIAL);
|
||||
}
|
||||
albumInfoMapper.updateById(albumInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -187,8 +219,8 @@ public class AlbumInfoServiceImpl extends ServiceImpl<AlbumInfoMapper, AlbumInfo
|
|||
queryWrapper.last("limit 200");
|
||||
//5.按照专辑ID降序
|
||||
queryWrapper.orderByDesc(AlbumInfo::getId);
|
||||
return albumInfoMapper.selectList(queryWrapper);
|
||||
|
||||
List<AlbumInfo> albumInfos = albumInfoMapper.selectList(queryWrapper);
|
||||
return albumInfos;
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -56,6 +56,7 @@ public class AuditServiceImpl implements AuditService {
|
|||
}
|
||||
} catch (TencentCloudSDKException e) {
|
||||
log.error("文本审核失败");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -82,6 +83,7 @@ public class AuditServiceImpl implements AuditService {
|
|||
}
|
||||
} catch (Exception e) {
|
||||
log.error("图片审核失败");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -103,4 +103,13 @@ public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, Ba
|
|||
return list;
|
||||
|
||||
}
|
||||
/**
|
||||
* 根据三级分类ID查询分类视图
|
||||
* @param category3Id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public BaseCategoryView getCategoryView(Long category3Id) {
|
||||
return baseCategoryViewMapper.selectById(category3Id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,7 @@ public class TrackInfoServiceImpl extends ServiceImpl<TrackInfoMapper, TrackInfo
|
|||
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void saveTrackInfo(TrackInfoVo trackInfoVo, Long userId) {
|
||||
//1. 根据专辑id查询专辑信息 用于更新专辑声音数量
|
||||
AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfoVo.getAlbumId());
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,8 +1,12 @@
|
|||
package com.atguigu.tingshu.search.api;
|
||||
|
||||
import com.atguigu.tingshu.common.result.Result;
|
||||
import com.atguigu.tingshu.search.service.SearchService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
@ -14,6 +18,28 @@ public class SearchApiController {
|
|||
|
||||
@Autowired
|
||||
private SearchService searchService;
|
||||
/**
|
||||
* 手动将指定专辑保存到索引库
|
||||
* @param albumId
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "手动将指定专辑保存到专辑索引库")
|
||||
@GetMapping("/albumInfo/upperAlbum/{albumId}")
|
||||
public Result upperAlbum(@PathVariable Long albumId){
|
||||
searchService.upperAlbum(albumId);
|
||||
return Result.ok();
|
||||
}
|
||||
/**
|
||||
* 手动删除专辑索引库中文档
|
||||
* @param albumId
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "手动删除专辑索引库中文档")
|
||||
@GetMapping("/albumInfo/lowerAlbum/{albumId}")
|
||||
public Result lowerAlbum(@PathVariable Long albumId){
|
||||
searchService.lowerAlbum(albumId);
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package com.atguigu.tingshu.search.receiver;
|
||||
|
||||
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
|
||||
import com.atguigu.tingshu.search.service.SearchService;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.Exchange;
|
||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author: atguigu
|
||||
* @create: 2025-07-22 15:22
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SearchReciever {
|
||||
|
||||
@Autowired
|
||||
private SearchService searchService;
|
||||
|
||||
/**
|
||||
* 监听专辑上架消息
|
||||
*
|
||||
* @param albumId
|
||||
*/
|
||||
@SneakyThrows
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM, durable = "true"),
|
||||
value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER, durable = "true"),
|
||||
key = MqConst.ROUTING_ALBUM_UPPER
|
||||
))
|
||||
public void upperAlbum(Long albumId, Channel channel, Message message) {
|
||||
if (albumId != null) {
|
||||
log.info("监听到上架专辑消息:{}", albumId);
|
||||
searchService.upperAlbum(albumId);
|
||||
}
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 监听专辑下消息
|
||||
*
|
||||
* @param albumId
|
||||
*/
|
||||
|
||||
@SneakyThrows
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM, durable = "true"),
|
||||
value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER, durable = "true"),
|
||||
key = MqConst.ROUTING_ALBUM_LOWER
|
||||
))
|
||||
public void lowerAlbum(Long albumId, Channel channel, Message message) {
|
||||
if (albumId != null) {
|
||||
log.info("监听到下架专辑消息:{}", albumId);
|
||||
searchService.lowerAlbum(albumId);
|
||||
}
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.atguigu.tingshu.search.repository;
|
||||
|
||||
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
|
||||
public interface AlbumInfoIndexRepository extends ElasticsearchRepository<AlbumInfoIndex, Long> {
|
||||
}
|
|
@ -3,5 +3,7 @@ package com.atguigu.tingshu.search.service;
|
|||
public interface SearchService {
|
||||
|
||||
|
||||
void upperAlbum(Long albumId);
|
||||
|
||||
void lowerAlbum(Long albumId);
|
||||
}
|
||||
|
|
|
@ -1,14 +1,125 @@
|
|||
package com.atguigu.tingshu.search.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.atguigu.tingshu.album.AlbumFeignClient;
|
||||
import com.atguigu.tingshu.model.album.AlbumAttributeValue;
|
||||
import com.atguigu.tingshu.model.album.AlbumInfo;
|
||||
import com.atguigu.tingshu.model.album.BaseCategoryView;
|
||||
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
|
||||
import com.atguigu.tingshu.model.search.AttributeValueIndex;
|
||||
import com.atguigu.tingshu.search.repository.AlbumInfoIndexRepository;
|
||||
import com.atguigu.tingshu.search.service.SearchService;
|
||||
import com.atguigu.tingshu.user.client.UserFeignClient;
|
||||
import com.atguigu.tingshu.vo.user.UserInfoVo;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@SuppressWarnings({"all"})
|
||||
public class SearchServiceImpl implements SearchService {
|
||||
|
||||
@Autowired
|
||||
private AlbumInfoIndexRepository albumInfoIndexRepository;
|
||||
@Resource(name = "threadPoolTaskExecutor")
|
||||
private Executor threadPoolTaskExecutor;
|
||||
@Autowired
|
||||
private AlbumFeignClient albumFeignClient;
|
||||
@Autowired
|
||||
private UserFeignClient userFeignClient;
|
||||
|
||||
|
||||
@Override
|
||||
public void upperAlbum(Long albumId) {
|
||||
//1.创建专辑索引对象
|
||||
AlbumInfoIndex albumInfoIndex = new AlbumInfoIndex();
|
||||
//2.远程调用专辑服务查询专辑信息,封装专辑以及专辑标签列表
|
||||
CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
|
||||
Assert.notNull(albumInfo, "专辑{}不存在", albumId);
|
||||
//2.1 封装专辑信息
|
||||
BeanUtil.copyProperties(albumInfo, albumInfoIndex);
|
||||
|
||||
//2.2 封装专辑标签列表
|
||||
List<AlbumAttributeValue> albumAttributeValueVoList = albumInfo.getAlbumAttributeValueVoList();
|
||||
if (CollUtil.isNotEmpty(albumAttributeValueVoList)) {
|
||||
List<AttributeValueIndex> attributeValueIndexList = albumAttributeValueVoList
|
||||
.stream()
|
||||
.map(a -> BeanUtil.copyProperties(a, AttributeValueIndex.class))
|
||||
.collect(Collectors.toList());
|
||||
albumInfoIndex.setAttributeValueIndexList(attributeValueIndexList);
|
||||
}
|
||||
return albumInfo;
|
||||
}, threadPoolTaskExecutor);
|
||||
|
||||
//3.远程调用专辑服务查询分类信息,封装1,2级分类ID
|
||||
CompletableFuture<Void> BaseCategoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
|
||||
BaseCategoryView baseCategoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
|
||||
Assert.notNull(baseCategoryView, "分类{}不存在", albumInfo.getCategory3Id());
|
||||
albumInfoIndex.setCategory1Id(baseCategoryView.getCategory1Id());
|
||||
albumInfoIndex.setCategory2Id(baseCategoryView.getCategory2Id());
|
||||
}, threadPoolTaskExecutor);
|
||||
|
||||
//4.远程调用用户服务查询用户信息,封装主播名称
|
||||
CompletableFuture<Void> userInfoCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
|
||||
UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
|
||||
Assert.notNull(userInfoVo, "用户{}不存在", albumInfo.getUserId());
|
||||
albumInfoIndex.setAnnouncerName(userInfoVo.getNickname());
|
||||
}, threadPoolTaskExecutor);
|
||||
|
||||
//5.暂时随机为当前专辑生成四项统计数值,基于四项统计数值计算得出热度
|
||||
CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {
|
||||
int num1 = RandomUtil.randomInt(2000, 3000);
|
||||
int num2 = RandomUtil.randomInt(1000, 2000);
|
||||
int num3 = RandomUtil.randomInt(500, 1000);
|
||||
int num4 = RandomUtil.randomInt(200, 500);
|
||||
albumInfoIndex.setPlayStatNum(num1);
|
||||
albumInfoIndex.setSubscribeStatNum(num2);
|
||||
albumInfoIndex.setBuyStatNum(num3);
|
||||
albumInfoIndex.setCommentStatNum(num4);
|
||||
//基于四项统计数值计算得出热度 规则:播放量*0.1 + 订阅量*0.2 + 购买量*0.3 + 评论量*0.4
|
||||
BigDecimal hotScore = new BigDecimal(num1).multiply(new BigDecimal("0.1"))
|
||||
.add(new BigDecimal(num2).multiply(new BigDecimal("0.2")))
|
||||
.add(new BigDecimal(num3).multiply(new BigDecimal("0.3")))
|
||||
.add(new BigDecimal(num4).multiply(new BigDecimal("0.4")));
|
||||
albumInfoIndex.setHotScore(hotScore.doubleValue());
|
||||
}, threadPoolTaskExecutor);
|
||||
|
||||
//6.等待所有任务执行完成
|
||||
CompletableFuture.allOf(
|
||||
albumInfoCompletableFuture,
|
||||
BaseCategoryViewCompletableFuture,
|
||||
userInfoCompletableFuture,
|
||||
statCompletableFuture
|
||||
).orTimeout(5, TimeUnit.SECONDS)
|
||||
.join();
|
||||
|
||||
//7.保存专辑到索引库
|
||||
albumInfoIndexRepository.save(albumInfoIndex);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除专辑索引库中文档
|
||||
*
|
||||
* @param albumId
|
||||
*/
|
||||
@Override
|
||||
public void lowerAlbum(Long albumId) {
|
||||
albumInfoIndexRepository.deleteById(albumId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package com.atguigu.tingshu.search;
|
||||
|
||||
import com.atguigu.tingshu.search.service.SearchService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
|
||||
public class test01 {
|
||||
@Autowired
|
||||
private SearchService searchService;
|
||||
|
||||
@Test
|
||||
public void test01() {
|
||||
for (long i = 1; i <= 1629; i++) {
|
||||
try {
|
||||
searchService.upperAlbum(i);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,8 +1,13 @@
|
|||
package com.atguigu.tingshu.user.api;
|
||||
|
||||
import com.atguigu.tingshu.common.result.Result;
|
||||
import com.atguigu.tingshu.user.service.UserInfoService;
|
||||
import com.atguigu.tingshu.vo.user.UserInfoVo;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
@ -14,6 +19,17 @@ public class UserInfoApiController {
|
|||
|
||||
@Autowired
|
||||
private UserInfoService userInfoService;
|
||||
/**
|
||||
* 根据用户ID查询用户信息
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "根据用户ID查询用户信息")
|
||||
@GetMapping("/userInfo/getUserInfoVo/{userId}")
|
||||
public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId){
|
||||
UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
|
||||
return Result.ok(userInfoVo);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue