分布式搜索--ElasticSearch 数据同步&集群搭建(服务器内存不足待完结)(六)

68 阅读3分钟

一、数据同步

1.思路分析

es中的数据来自于mysql数据库,因此mysql数据发生改变时,es也必须跟着改变,这个就是elasticsearch与mysql之间的数据同步

常见的数据同步方案有三种:

  • 同步调用
  • 异步通知
  • 监听binlog

(1)同步调用

image.png

基本步骤如下:

  • hotel-demo服务对外提供接口,用来修改elasticsearch中的数据
  • 酒店管理服务在完成数据库操作后,直接调用hotel-demo服务提供的修改接口

(2)异步通知

image.png

流程如下:

  • hotel-admin服务对mysql数据库数据完成增、删、改后,发送MQ消息
  • hotel-demo服务监听MQ,接收到消息后完成elasticsearch数据修改

(3)监听binlog

image.png

流程如下:

  • 给mysql开启binlog功能
  • mysql完成增、删、改操作都会记录在binlog中
  • hotel-demo基于canal监听binlog变化,实时更新elasticsearch中的内容

(4)优缺点

方式一:同步调用

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

方式二:异步通知

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

方式三:监听binlog

  • 优点:完全解除服务间耦合
  • 缺点:开启binlog增加数据库负担、实现复杂度高
2.搭建环境

我们使用第二种 利用mq异步通知的方法实现es与mysql双写一致

步骤:

  • 声明exchange、queue、RoutingKey
  • 在hotel-admin中的增、删、改业务中完成消息发送
  • 在hotel-demo中完成消息监听,并更新elasticsearch中数据
  • 启动并测试数据同步功能

1.在hotel-admin、hotel-demo中引入rabbitmq的依赖:

<!--amqp-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

思路: 数据库有增删改操作,但增删可当成一个操作,当修改一个不存在的文档id时,可当做新增,因此只需要定义两个队列即可,一个操作增改,一个操作删除

2.hotel-admin和hotel-demo中声明队列和交换机名称和RoutingKey

public class MqConstans {
    //交换机名称
    private final static String HOTEL_EXCHAGE = "hotel.topic";
    //增改队列名称
    private final static String HOTEL_INAERT_QUEUE = "hotel.insert.queue";

    //删除队列名称
    private final static String HOTEL_DELETE_QUEUE = "hotel.delete.queue";

    //增改队列的RoutingKey
    private final static String HOTEL_INSERT_KEY = "hotel.insert";

    //删除队列的RoutingKey
    private final static String HOTEL_DELETE_KEY = "hotel.delete";
}

3.接收者hotem-demo中声明队列和交换机和RoutingKey

@Configuration
public class MqConfig {

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(MqConstans.HOTEL_EXCHAGE, true, false); 
        //第二个参数为持久化durable,第三个参数为autoDelete
    }

    @Bean
    public Queue insertQueue() {
        return new Queue(MqConstans.HOTEL_INAERT_QUEUE, true); //第二个参数为durable持久化
    }

    @Bean
    public Queue deleteQueue() {
        return new Queue(MqConstans.HOTEL_DELETE_QUEUE, true);
    }

    @Bean
    public Binding insertQueueBinding() {
        return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(MqConstans.HOTEL_INSERT_KEY);
    }

    @Bean
    public Binding deleteQueueBinding() {
        return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(MqConstans.HOTEL_DELETE_KEY);
    }
}

参数解释

  • durable:是否持久化,RabbitMQ关闭后,没有持久化的Exchange将被清除
  • autoDelete:是否自动删除,如果没有与之绑定的Queue,直接删除
3.发送mq消息

为了方便起见,直接在Controller层中修改

@PostMapping
public void saveHotel(@RequestBody Hotel hotel){
    hotelService.save(hotel);

    rabbitTemplate.convertAndSend(MqConstans.HOTEL_EXCHAGE, MqConstans.HOTEL_INSERT_KEY, hotel.getId());
}

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

    rabbitTemplate.convertAndSend(MqConstans.HOTEL_EXCHAGE, MqConstans.HOTEL_INSERT_KEY, hotel.getId());
}

@DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") Long id) {
    hotelService.removeById(id);
    rabbitTemplate.convertAndSend(MqConstans.HOTEL_EXCHAGE, MqConstans.HOTEL_DELETE_KEY, id);
}
4.接收mq消息

listener层

package cn.itcast.hotel.mq;

import cn.itcast.hotel.constans.MqConstans;
import cn.itcast.hotel.service.IHotelService;
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;

    @RabbitListener(queues = MqConstans.HOTEL_INAERT_QUEUE)
    public void listenerHoterInsertOrUpdate(Long id) {
        hotelService.insertById(id);

    }

    @RabbitListener(queues = MqConstans.HOTEL_DELETE_QUEUE)
    public void listenerHoterDelete(Long id) {
        hotelService.deleteById(id);
    }
}

service层

@Override
public void deleteById(Long id) {
    try {
        DeleteRequest request = new DeleteRequest("hotel", id.toString());
        client.delete(request, RequestOptions.DEFAULT);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

@Override
public void insertById(Long id) {
    try {
        Hotel hotel = getById(id);
        HotelDoc hotelDoc = new HotelDoc(hotel);

        IndexRequest request = new IndexRequest("hotel").id(id.toString());
        request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
        client.index(request, RequestOptions.DEFAULT);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

二、集群搭建