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

ElasticSearch:项目实战(2)

ElasticSearch: 项目实战 (1)

需求:

新增文章审核通过后同步数据到es索引库

 1、文章服务中添加消息发送方法

               在service层文章新增成功后,将数据通过kafka消息同步发送到搜索服务

    @Autowiredprivate KafkaTemplate<String,String> kafkaTemplate;/*** 送消息,创建索引* @param apArticle* @param content* @param path*/private void createArticleESIndex(ApArticle apArticle, String content, String path) {SearchArticleVo vo = new SearchArticleVo();BeanUtils.copyProperties(apArticle,vo);vo.setContent(content);vo.setStaticUrl(path);kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(vo));}

2、搜索服务中实现消息接收

        创建SyncArticleListener普通类来接收文章服务发送过来的数据

package com.heima.search.listener;import com.alibaba.fastjson.JSON;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.common.search.SearchArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;import java.io.IOException;@Component
@Slf4j
public class SyncArticleListener {@Autowiredprivate RestHighLevelClient restHighLevelClient;@KafkaListener(topics = ArticleConstants.ARTICLE_ES_SYNC_TOPIC)public void onMessage(String message){if(StringUtils.isNotBlank(message)){log.info("SyncArticleListener,message={}",message);/*添加数据到索引库*/SearchArticleVo searchArticleVo = JSON.parseObject(message, SearchArticleVo.class);IndexRequest indexRequest = new IndexRequest("app_info_article");indexRequest.id(searchArticleVo.getId().toString());indexRequest.source(message, XContentType.JSON);try {restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();log.error("sync es error={}",e);}}}
}

3、分别配置文章服务和搜索服务的nacos

spring:kafka:bootstrap-servers: 192.168.200.130:9092consumer:group-id: ${spring.application.name}key-deserializer: org.apache.kafka.common.serialization.StringDeserializervalue-deserializer: org.apache.kafka.common.serialization.StringDeserializer

http://www.lryc.cn/news/120128.html

相关文章:

  • Eleastisearch5.2.2利用镜像迁移构建实例后ES非健康状态
  • sealos安装k8s
  • 经典贪吃蛇游戏 - 用 C 语言实现控制台版
  • 安灯Andon系统的应用与优势
  • 2023年的C++基础笔记
  • 综合能源系统(6)——综合能源综合评估技术
  • 华为OD机试真题 Java 实现【寻找相同子串】【2023 B卷 100分】,附详细解题思路
  • 软件外包开发的桌面客户端开发
  • PAT(Advanced Level) Practice(with python)——1058 A+B in Hogwarts
  • 【ES】笔记-ES6的函数rest参数用法
  • 【MOOC】北京理工大学Python网络爬虫与信息提取慕课答案-综合挑出了一些很难评的慕课测验题
  • 【论文阅读】基于深度学习的时序预测——Crossformer
  • 谷粒商城第十一天-完善商品分组(主要添上关联属性)
  • C++笔记之函数参数列表中设置默认值
  • Verilog求log10和log2近似
  • 二叉树小结
  • vue二进制下载
  • c++QT文件操作
  • Jmeter —— jmeter设置HTTP信息头管理器模拟请求头
  • vue 图片转pdf
  • 20.5 HTML 媒体
  • 科大讯飞分类算法挑战赛2023的一些经验总结
  • 2023年京东按摩仪行业数据分析(京东销售数据分析)
  • 【C语言】进阶指针,超详解,含丰富代码示例
  • wireshark入门指北
  • 18、SQL注入之堆叠及WAF绕过注入
  • nodejs+vue+elementui+express旅游出行指南网站_655ms
  • 【心电图信号压缩】ECG信号压缩与通过三次样条近似重建的ECG信号压缩研究(Matlab代码实现)
  • matlab使用教程(11)—创建随机数
  • 一、安全世界观