当前位置: 首页 > news >正文

【业务功能篇84】微服务SpringCloud-ElasticSearch-Kibanan-电商实例应用

一、商品上架功能

ElasticSearch实现商城系统中全文检索的流程。

image.png

1.商品ES模型

商品的映射关系

PUT product 
{"mappings": {"properties": {"skuId": {"type": "long"},"spuId": {"type": "keyword"},"skuTitle": {"type": "text","analyzer": "ik_smart"},"skuPrice": {"type": "keyword"},"skuImg": {"type": "keyword","index": "false","doc_values": "false"},"saleCount": {"type": "long"},"hasStock": {"type": "boolean"},"hotScore": {"type": "long"},"brandId": {"type": "long"},"catalogId": {"type": "long"},"brandName": {"type": "keyword","index": "false","doc_values": "false"},"brandImg": {"type": "keyword","index": "false","doc_values": "false"},"catalogName": {"type": "keyword","index": "false","doc_values": "false"},"attrs": {"type": "nested","properties": {"attrId": {"type": "long"},"attrName": {"type": "keyword","index": "false","doc_values": "false"},"attrValue": {"type": "keyword"}}}}}
}

2.netsted数据类型

参考官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/7.4/nested.html

3.实现上架功能

3.1 创建ESModel

点击上架功能传递spuId到后台,我们需要根据SpuID查询对应的信息,然后封装到自定义的Model对象中,然后将该对象传递给mall-search服务,所以我们需要先定义这样一个Model对象

@Data
public class SkuESModel {private Long skuId;private Long spuId;private String subTitle;private BigDecimal skuPrice;private String skuImg;private Long saleCount;private Boolean hasStock;private Long hotScore;private Long brandId;private Long catalogId;private String brandName;private String brandImg;private String catalogName;private List<Attrs> attrs;@Datapublic static class Attrs{private Long attrId;private String attrName;private String attrValue;}}

3.2 上架逻辑实现

controller层

/*** spu信息**/
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {@Autowiredprivate SpuInfoService spuInfoService;/*** app/product/spuinfo/6/up* 商品的上架功能* 传递过来一个spuID* 我们就需要根据SPUID查询出需要存储在ElasticSearch中的数据* 然后把数据存储到ELasticSearch中,并修改该SPU的状态为上架*/@PostMapping("/{spuId}/up")public R spuUp(@PathVariable("spuId") Long spuId){spuInfoService.up(spuId);return R.ok();}
}

核心service层方法实现

调用了一些同moudle下的其他实现类方法忽略

/*** 实现商品上架--》商品相关数据存储到ElasticSearch中* 1.根据SpuID查询出相关的信息*   封装到对应的对象中* 2.将封装的数据存储到ElasticSearch中--》调用mall-search的远程接口* 3.更新SpuID对应的状态--》上架** @param spuId*/@Overridepublic void up(Long spuId) {// 1.根据spuId查询相关的信息 封装到SkuESModel对象中List<SkuESModel> skuEs = new ArrayList<>();// 根据spuID找到对应的SKU信息List<SkuInfoEntity> skus = skuInfoService.getSkusBySpuId(spuId);// 对应的规格参数  根据spuId来查询规格参数信息List<SkuESModel.Attrs> attrsModel = getAttrsModel(spuId);// 需要根据所有的skuId获取对应的库存信息---》远程调用List<Long> skuIds = skus.stream().map(sku -> {return sku.getSkuId();}).collect(Collectors.toList());Map<Long, Boolean> skusHasStockMap = getSkusHasStock(skuIds);// 2.远程调用mall-search的服务,将SukESModel中的数据存储到ES中List<SkuESModel> skuESModels = skus.stream().map(item -> {SkuESModel model = new SkuESModel();// 先实现属性的复制BeanUtils.copyProperties(item,model);model.setSubTitle(item.getSkuTitle());model.setSkuPrice(item.getPrice());// hasStock 是否有库存 --》 库存系统查询  一次远程调用获取所有的skuId对应的库存信息if(skusHasStockMap == null){model.setHasStock(true);}else{model.setHasStock(skusHasStockMap.get(item.getSkuId()));}// hotScore 热度分 --> 默认给0即可model.setHotScore(0l);// 品牌和类型的名称BrandEntity brand = brandService.getById(item.getBrandId());CategoryEntity category = categoryService.getById(item.getCatalogId());model.setBrandName(brand.getName());model.setBrandImg(brand.getLogo());model.setCatalogName(category.getName());// 需要存储的规格数据model.setAttrs(attrsModel);return model;}).collect(Collectors.toList());// 将SkuESModel中的数据存储到ES中R r = searchFeginService.productStatusUp(skuESModels);// 3.更新SPUID对应的状态// 根据对应的状态更新商品的状态log.info("----->ES操作完成:{}" ,r.getCode());System.out.println("-------------->"+r.getCode());if(r.getCode() == 0){// 远程调用成功  更新商品的状态为 上架baseMapper.updateSpuStatusUp(spuId, ProductConstant.StatusEnum.SPU_UP.getCode());}else{// 远程调用失败}}

Feign远程调用了其他微服务module

在当前需要调用远程微服务的模块中创建接口,通过注解@FeignClient(“mall-search”)找到对应被调用的微服务,接着将被调用微服务的controller层的所需方法名,形参路径等都全部复制过来,就能完成远程调用

@FeignClient("mall-search")
public interface SearchFeginService {@PostMapping("/search/save/product")public R productStatusUp(@RequestBody List<SkuESModel> skuESModels);
}

被调用的微服务模块Controller层

/**** 存储商城数据到ElasticSearch的服务*/
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSearchSaveController {@Autowiredprivate ElasticSearchSaveService service;/*** 存储商品上架信息到ElasticSearch服务的接口* @return*/@PostMapping("/product")public R productStatusUp(@RequestBody List<SkuESModel> skuESModels){Boolean b = false;try {b = service.productStatusUp(skuESModels);} catch (IOException e) {// e.printStackTrace();log.error("ElasticSearch商品上架错误:{}",e);return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());}if(b){return R.ok();}return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());}
http://www.lryc.cn/news/141112.html

相关文章:

  • 图像检索,目标检测map的实现
  • Docker容器学习:Dockerfile制作Web应用系统nginx镜像
  • 【vue3.0 引入Element Plus步骤与使用】
  • 金融客户敏感信息的“精细化管控”新范式
  • Starrocks--数据插入方式
  • Java学数据结构(3)——树Tree B树 红黑树 Java标准库中的集合Set与映射Map 使用多个映射Map的案例
  • Vue3.0极速入门 - 环境安装新建项目
  • android 使用libyuv 图像转换
  • 奥比中光:进击具身智能,打造机器人之眼
  • 微信小程序报错: SyntaxError: Cannot use import statement outside a module
  • Ruoyi微服务启动流程
  • Android scrollTo、scrollBy、以及scroller详解 自定义ViewPager
  • Aidex 移动端快速开发框架# RuoYi-Uniapp项目,uniapp vue app项目跨域问题
  • JVM7:垃圾回收是什么?从运行时数据区看垃圾回收到底回收哪块区域?垃圾回收如何去回收?垃圾回收策略,引用计数算法及循环引用问题,可达性分析算法
  • NFT Insider #104:The Sandbox:全新土地销售活动 Turkishverse 来袭
  • Docker架构及原理
  • VScode使用SSH连接linux
  • 多线程的创建与同步
  • LLMs 缩放指令模型Scaling instruct models FLAN(Fine-tuned LAnguage Net,微调语言网络)
  • 政务大厅人员睡岗离岗玩手机识别算法
  • 认识Mybatis的关联关系映射,灵活关联表对象之间的关系
  • 如何开发一款唯一艺术平台 区块链 /数字藏品
  • 【UniApp开发小程序】私聊功能后端实现 (买家、卖家 沟通商品信息)【后端基于若依管理系统开发】
  • 运维高级学习--Kubernetes(K8s 1.28.x)部署
  • Apache zookeeper kafka 开启SASL安全认证 —— 筑梦之路
  • lintcode 1017 · 相似的RGB颜色【进制计算】
  • 全国首台!浙江机器人产业集团发布垂起固定翼无人机-机器人自动换电机巢
  • 采用 UML 对软件系统进行建模的基本框架
  • 编译tiny4412 Linux 内核
  • Ubuntu22.04安装中文输入法►由踩坑到上岸版◄