补充:es与mysql之间的数据同步 2
创始人
2024-04-21 15:54:57
0

本片文章只是对之前写的文章的补充,

es与mysql之间的数据同步

http://t.csdn.cn/npHt4

补充一:

之前的文章对于交换机、队列、绑定,使用的是@bean,

而这里使用的是纯注解版

在消费方,声明交换机:

package com.hmall.search.mq;import com.hmall.search.service.IsearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
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;
import org.yaml.snakeyaml.events.Event;/*** @author ning* @since 2022/12/12 0:16*/@Slf4j
@Component
public class ItemListener {@Autowiredprivate IsearchService isearchService;@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "up.queue"),//声明队列exchange = @Exchange(name = "item.topic",type = ExchangeTypes.TOPIC),//声明交换机key = "item.up"//声明绑定关系))private void listenItemUp(Long id){log.info("监听到上架消息:"+ id);isearchService.saveitById(id);}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "down.queue"),exchange = @Exchange(name = "item.topic",type = ExchangeTypes.TOPIC),key = "item.down"))private void listenItemDown(Long id){log.info("监听到下架消息:"+ id);isearchService.deleteItemById(id);}}

补充二:

之前的文章是直接使用es操作数据,新增和修改,这样做不是很合适,而且没有遵守单一原则,所以这里使用feign远程调用其他模块的接口方法,

1、新建一个feign模块(如果没有的话)

可以参考

http://t.csdn.cn/GqMVN

2、在模块中新建一个接口ItemClient(使用哪个模块就用哪个模块名+Client),在模块中定义你要在es中调用的方法,(也就是es需要操作数据库,但是其他模块已经写完的方法,就不需要再写一遍了)

例如:需要根据id查询数据库,就把其他模块写完的根据id查询数据库的方法写到接口里

package com.hmall.client;import com.hmall.common.dto.Item;
import com.hmall.common.dto.PageDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;/*** 商品模块的远程调用** @author ning* @since 2022/12/9 18:39*/
//表示对应的是itemservice服务器
@FeignClient("itemservice")
public interface ItemClient {//分页查询@GetMapping("/item/list")public PageDTO list(@RequestParam("page") Integer page, @RequestParam("size") Integer size);//根据id查询数据@GetMapping("/item/{id}")public Item selectById(@PathVariable("id") Long id);
}

以上接口中还有分页查询的内容,详情可以参考

使用分页导入的方式把大量数据从mysql导入es

http://t.csdn.cn/XECXD

3、生产方和消费方的代码,只有消费方新增的代码有一点不同,其他的都一样

    //注入es的工具类@Autowiredprivate RestHighLevelClient client;//注入feign远程调用的接口@Autowiredprivate ItemClient itemClient;@Overridepublic void saveitById(Long id) {try {//使用feign的远程调用接口,查询数据库//查询一条商品数据,并转为jsonItem item = itemClient.selectById(id);ItemDoc itemDoc = new ItemDoc(item);String jsonString = JSON.toJSONString(itemDoc);//创建请求IndexRequest request = new IndexRequest("item").id(id.toString());//设置参数request.source(jsonString, XContentType.JSON);//执行请求client.index(request, RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...