elasticsearch实战三 elasticsearch与mysql数据实时同步

一 介绍

elasticsearch数据不是一直不变的,需要与mysql、oracle等数据库的数据做同步。

本博客里涉及到的项目地址:https://www.aliyundrive.com/s/7bRWpTYsxWV

方案一: 同步调用,即操作mysql数据后,接着操作elasticsearch的数据

  • 优点:实现简单,粗暴
  • 缺点:业务耦合度高

方案二: 引入mq中间件,操作完mysql后,发消息给mq,然后更新elasticsearch。

  • 优点:低耦合,实现难度一般
  • 缺点:依赖mq的可靠性

方案三: 监听mysql的binlog日志,操作mysql时,监听到binlog后,接着操作elasticsearch数据

  • 优点:完全解除服务间耦合
  • 缺点:开启binlog增加数据库负担、实现复杂度高(且目前只有mysql支持binlog)

本文介绍比较通用的方案,即方案二,使用的mq消息队列是rabbitmq

二 消费端搭建服务

资料里的hotel-admin项目,是用来操作mysql、产生mq消息的。hotel-demo项目,是用来操作es、消费mq消息的。

2.1 hotel-demo项目搭建rabbitmq

声明消息队列里的exchage、queue、RoutingKey

声明交换机、队列等,一般都是在消费者里操作。由于对于ES来说,新增与修改是一样的(修改时,找不到id,就会新增),所以队列只声明新增、删除两种队列即可。

hotel-demo:引入依赖

xml 复制代码
<!--amqp-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
 </dependency>

添加rabbitmq配置信息

声明exchange、queue、RoutingKey

java 复制代码
//常量类定义交换机、队列、路由key等,消息的消费者和发送者都要定义这个类
public class HotelMqConstants {

    /**
     * 交换机名称
     */
    public static final String EXCHANGE_NAME = "hotel.topic";

    /**
     * 新增、修改队列
     */
    public static final String INSERT_QUEUE_NAME = "hotel.insert.queue";

    /**
     * 删除队列
     */
    public static final String DELETE_QUEUE_NAME = "hotel.delete.queue";

    /**
     * 新增或修改的RoutingKey
     */
    public static final String INSERT_KEY = "hotel.insert";
    /**
     * 删除的RoutingKey
     */
    public static final String DELETE_KEY = "hotel.delete";
}

定义队列、主题等的绑定关系时,有两种方式

  1. 基于注解(较简单)
  2. 基于bean

这里使用基于bean的方式,MqConfig.java配置类

java 复制代码
public class HotelMqConstants {
    //交换机名称
    public static final String EXCHANGE_NAME = "hotel.topic";
    //插入、更新数据时的队列名
    public static final String INSERT_QUEUE_NAME = "hotel.insert.queue";
    //删除数据时的队列名
    public static final String DELETE_QUEUE_NAME = "hotel.delete.queue";
    //插入、更新数据时的RoutingKey
    public static final String INSERT_KEY = "hotel.insert";
    //删除数据时的RoutingKey
    public static final String DELETE_KEY = "hotel.delete";
}
java 复制代码
import cn.itcast.hotel.constants.HotelMqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfig {
    /**
     * 定义交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange(){
        //参数一:交换机名字
        //参数二:持久化
        return new TopicExchange(HotelMqConstants.EXCHANGE_NAME,true,false);
    }
    /**
     * 插入、更新数据的队列
     */
    @Bean
    public Queue insertQueue(){
        return new Queue(HotelMqConstants.INSERT_QUEUE_NAME,true);
    }
    /**
     * 删除数据的队列
     */
    @Bean
    public Queue deleteQueue(){
        return new Queue(HotelMqConstants.DELETE_QUEUE_NAME,true);
    }
    /**
     * 定义插入、更新数据时,队列、交换机、路由key的绑定关系
     */
    public Binding insertQueueBinding(){
        //队列绑定交换机、绑定RoutingKey
        return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(HotelMqConstants.INSERT_KEY);
    }

    /**
     * 定义删除数据时,队列、交换机、路由key的绑定关系
     */
    public Binding deleteQueueBinding(){
        //队列绑定交换机、绑定RoutingKey
        return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(HotelMqConstants.DELETE_KEY);
    }
}

2.2 hotel-admin项目搭建rabbitmq

启动访问8099端口

引入amqp依赖

xml 复制代码
<!--amqp-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

定义常量类

java 复制代码
public class HotelMqConstants {
    //交换机名称
    public static final String EXCHANGE_NAME = "hotel.topic";
    //插入、更新数据时的队列名
    public static final String INSERT_QUEUE_NAME = "hotel.insert.queue";
    //删除数据时的队列名
    public static final String DELETE_QUEUE_NAME = "hotel.delete.queue";
    //插入、更新数据时的RoutingKey
    public static final String INSERT_KEY = "hotel.insert";
    //删除数据时的RoutingKey
    public static final String DELETE_KEY = "hotel.delete";
}

配置rabbitmq地址

发送mq消息代码

java 复制代码
public class HotelController {
   @Autowired
    private IHotelService hotelService;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    @PostMapping
    public void saveHotel(@RequestBody Hotel hotel){
        // 新增酒店
        hotelService.save(hotel);
        // 发送MQ消息(第三个参数是消息内容:hotel.getId())
        rabbitTemplate.convertAndSend(HotelMqConstants.EXCHANGE_NAME, HotelMqConstants.INSERT_KEY, hotel.getId());
    }

    @PutMapping()
    public void updateById(@RequestBody Hotel hotel){
        if (hotel.getId() == null) {
            throw new InvalidParameterException("id不能为空");
        }
        hotelService.updateById(hotel);

        // 发送MQ消息第三个参数是消息内容:hotel.getId())
        rabbitTemplate.convertAndSend(HotelMqConstants.EXCHANGE_NAME, HotelMqConstants.INSERT_KEY, hotel.getId());
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Long id) {
        hotelService.removeById(id);

        // 发送MQ消息(第三个参数是消息内容:hotel.getId())
        rabbitTemplate.convertAndSend(HotelMqConstants.EXCHANGE_NAME, HotelMqConstants.DELETE_KEY, id);
    }
 }

2.3 hotel-demo监听消息

hotel-demo是消费者,负责监听消息

添加es依赖

xml 复制代码
<!--elasticsearch-->
<dependency>
     <groupId>org.elasticsearch.client</groupId>
     <artifactId>elasticsearch-rest-high-level-client</artifactId>
     <version>7.12.1</version>
</dependency>
java 复制代码
import cn.itcast.hotel.constants.HotelMqConstants;
import cn.itcast.hotel.service.IHotelService;
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;

@Component
public class HotelListener {

    @Autowired
    private IHotelService hotelService;

    /**
     * 监听新增、修改的消息
     * @param hotelId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HotelMqConstants.INSERT_QUEUE_NAME),
            exchange = @Exchange(name = HotelMqConstants.EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = HotelMqConstants.INSERT_KEY
    ))
    public void listenHotelInsert(Long hotelId){
        // 新增
        hotelService.saveById(hotelId);
    }

    /**
     * 监听删除的消息
     * @param hotelId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HotelMqConstants.DELETE_QUEUE_NAME),
            exchange = @Exchange(name = HotelMqConstants.EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = HotelMqConstants.DELETE_KEY
    ))
    public void listenHotelDelete(Long hotelId){
        // 删除
        hotelService.deleteById(hotelId);
    }
}
java 复制代码
import cn.itcast.hotel.mapper.HotelMapper;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.pojo.PageResult;
import cn.itcast.hotel.pojo.RequestParams;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    
    @Override
    public void saveById(Long hotelId) {
        try {
            // 查询酒店数据,应该基于Feign远程调用hotel-admin,根据id查询酒店数据(现在直接去数据库查)
            Hotel hotel = getById(hotelId);
            // 把hotel对象转换为hotel的Doc
            HotelDoc hotelDoc = new HotelDoc(hotel);

            // 1.创建Request
            IndexRequest request = new IndexRequest("hotel").id(hotelId.toString());
            // 2.准备参数
            request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
            // 3.发送请求
            restHighLevelClient.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException("新增酒店数据失败", e);
        }
    }
@Override
    public void deleteById(Long hotelId) {
        try {
            // 1.创建request
            DeleteRequest request = new DeleteRequest("hotel", hotelId.toString());
            // 2.发送请求
            restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException("删除酒店数据失败", e);
        }
    }
}
相关推荐
吐泡泡_3 小时前
MySQL(事物上)
数据库·mysql
申耀的科技观察4 小时前
【观察】拓展大模型应用交付领域“新赛道”,亚信科技为高质量发展“加速度”...
大数据·人工智能·科技
lboyj5 小时前
新能源汽车电控系统的大尺寸PCB需求:猎板PCB的技术突围
大数据·网络·人工智能
遇见火星5 小时前
OpenEuler-22.03-LTS上利用Ansible轻松部署MySQL 5.7
mysql·ansible·openeuler
CL_IN6 小时前
高效集成销售订单数据到MySQL的方法
android·数据库·mysql
人类群星闪耀时7 小时前
数据分析入门:从数据探索到洞察真相
大数据·hadoop·sql
Elastic 中国社区官方博客7 小时前
Elasticsearch:语义文本 - 更简单、更好、更精炼、更强大 8.18
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
Code额8 小时前
MySQL的事务机制
android·mysql·adb
元气满满的热码式8 小时前
MySQL启动报错解决
运维·数据库·mysql
猿小喵8 小时前
MySQL异常SQL排查
数据库·sql·mysql