关键词:
文章目录
视频指路👉B站黑马微服务超级推荐!
MQ实现elasticsearch与mysql的数据同步
elasticsearch中的酒店数据来自于mysql数据库,因此mysql数据发生改变时,elasticsearch也必须跟着改变,这个就是elasticsearch与mysql之间的数据同步。
1.思路分析
常见的数据同步方案有三种:
- 同步调用
- 异步通知
- 监听binlog
1.1 同步调用
方案一:同步调用
基本步骤如下:
hotel-demo
对外提供接口,用来修改elasticsearch
中的数据- 酒店管理服务在完成数据库操作后,直接调用
hotel-demo
提供的接口
1.2 异步通知
方案二:异步通知
流程如下:
hotel-admin
对mysql
数据库数据完成增、删、改后,发送MQ
消息hotel-demo
监听MQ
,接收到消息后完成elasticsearch
数据修改
1.3 监听binlog
方案三:监听binlog
流程如下:
- 给
mysql
开启binlog
功能 mysql
完成增、删、改操作都会记录在binlog
中hotel-demo
基于canal
监听binlog
变化,实时更新elasticsearch
中的内容
1.4 选择
方式一:同步调用
- 优点:实现简单,粗暴
- 缺点:业务耦合度高
方式二:异步通知
- 优点:低耦合,实现难度一般
- 缺点:依赖mq的可靠性
方式三:监听binlog
- 优点:完全解除服务间耦合
- 缺点:开启binlog增加数据库负担、实现复杂度高
2.MQ实现数据同步
2.1 思路
利用课前资料提供的hotel-admin
项目作为酒店管理的微服务。当酒店数据发生增、删、改时,要求对elasticsearch
中数据也要完成相同操作。
步骤:
-
导入课前资料提供的
hotel-admin
项目,启动并测试酒店数据的CRUD -
声明
exchange
、queue
、RoutingKey
-
在
hotel-admin
中的增、删、改业务中完成消息发送 -
在
hotel-demo
中完成消息监听,并更新elasticsearch
中数据 -
启动并测试数据同步功能
2.2 声明交换机、队列
MQ结构如图:
(1)引入依赖
在hotel-admin
、hotel-demo
中引入rabbitmq
的依赖:
<!--amqp-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
(2)声明队列交换机名称
在hotel-admin
和hotel-demo
中的cn.itcast.hotel.constatnts
包下新建一个类MqConstants
:
package cn.itcast.hotel.constatnts;
public class MqConstants
/**
* 交换机
*/
public final static String HOTEL_EXCHANGE = "hotel.topic";
/**
* 监听新增和修改的队列
*/
public final static String HOTEL_INSERT_QUEUE = "hotel.insert.queue";
/**
* 监听删除的队列
*/
public final static String HOTEL_DELETE_QUEUE = "hotel.delete.queue";
/**
* 新增或修改的RoutingKey
*/
public final static String HOTEL_INSERT_KEY = "hotel.insert";
/**
* 删除的RoutingKey
*/
public final static String HOTEL_DELETE_KEY = "hotel.delete";
(3)声明队列交换机
在hotel-demo
中,定义配置类MqConfig
,声明队列、交换机、绑定队列和交换机:
@Configuration
public class MqConfig
/**
* 定义交换机
* @return
*/
@Bean
public TopicExchange topicExchange()
return new TopicExchange(MqConstants.HOTEL_EXCHANGE, true, false);
/**
* 定义新增或修改的队列
* @return
*/
@Bean
public Queue insertQueue()
return new Queue(MqConstants.HOTEL_INSERT_QUEUE, true);
/**
* 定义删除的队列
* @return
*/
@Bean
public Queue deleteQueue()
return new Queue(MqConstants.HOTEL_DELETE_QUEUE, true);
/**
* 绑定修改队列和交换机
* @return
*/
@Bean
public Binding insertQueueBinding()
return BindingBuilder.bind(insertQueue())
.to(topicExchange())
.with(MqConstants.HOTEL_INSERT_KEY);
/**
* 绑定删除队列和交换机
* @return
*/
@Bean
public Binding deleteQueueBinding()
return BindingBuilder.bind(deleteQueue())
.to(topicExchange())
.with(MqConstants.HOTEL_DELETE_KEY);
2.4 发送MQ消息
当在hotel-admin
中发生增、删、改业务中分别发送MQ
消息:
要传入酒店的id给hotel-demo
用于修改
@PostMapping
public void saveHotel(@RequestBody Hotel hotel)
// 新增酒店
hotelService.save(hotel);
// 发送MQ消息
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消息
rabbitTemplate.convertAndSend(HotelMqConstants.EXCHANGE_NAME, HotelMqConstants.INSERT_KEY, hotel.getId());
@DeleteMapping("/id")
public void deleteById(@PathVariable("id") Long id)
hotelService.removeById(id);
// 发送MQ消息
rabbitTemplate.convertAndSend(HotelMqConstants.EXCHANGE_NAME, HotelMqConstants.DELETE_KEY, id);
2.5 接受MQ消息
hotel-demo
接收到MQ
消息要做的事情包括:
- 新增消息:根据传递的hotel的id查询hotel信息,然后新增一条数据到索引库
- 删除消息:根据传递的hotel的id删除索引库中的一条数据
(1)首先在hotel-demo
的cn.itcast.hotel.service
包下的IHotelService
中新增新增、删除业务
void deleteById(Long id);
void insertById(Long id);
(2)给hotel-demo
中的cn.itcast.hotel.service.impl
包下的HotelService
中实现业务:
@Override
public void deleteById(Long id)
try
// 1.准备Request
DeleteRequest request = new DeleteRequest("hotel", id.toString());
// 2.发送请求
client.delete(request, RequestOptions.DEFAULT);
catch (IOException e)
throw new RuntimeException(e);
@Override
public void insertById(Long id)
try
// 0.根据id查询酒店数据
Hotel hotel = getById(id);
// 转换为文档类型
HotelDoc hotelDoc = new HotelDoc(hotel);
// 1.准备Request对象
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
// 2.准备Json文档
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
// 3.发送请求
client.index(request, RequestOptions.DEFAULT);
catch (IOException e)
throw new RuntimeException(e);
(3)编写监听器
在hotel-demo
中的cn.itcast.hotel.mq
包新增一个类:
@Component
public class Hotelistener
@Autowired
private IHotelService iHotelServicel;
/**
* 监听酒店新增或者修改的业务
* @param id 酒店id
*/
@RabbitListener(queues = MqConstants.HOTEL_INSERT_QUEUE)
public void listenHotelInsertOrupdate(Long id)
iHotelServicel.saveById(id);
/**
* 监听酒店删除的业务
* @param id 酒店id
*/
@RabbitListener(queues = MqConstants.HOTEL_DELETE_QUEUE)
public void listenHotelDelete(Long id)
iHotelServicel.deleteById(id);
最后喜欢的小伙伴,记得三连哦!😏🍭😘
elasticsearch搜学习笔记
Elasticsearch学习笔记B站【尚硅谷】ElasticSearch入门到精通2021最新教程学习文档下载链接常用APIforpostman 查看详情
elasticsearch学习笔记1:elasticsearch是什么?就是做搜索的!
一、Elasticsearch是什么?Elasticsearch:弹性搜索 Elaticsearch,简称为es,es是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本身扩展性很好可以扩展到上百台服务器,处理PB级别(大数据时... 查看详情
elasticsearch系列之索引机制学习笔记(代码片段)
前言在上一章的学习,我们对ElasticSearch有了比较清晰的理解,然后本博客继续学习ES中比较重要的核心原理和具体实现。相对于MySQL的索引机制,大部分是基于B+树的,需要我们进行手动创建索引,但是ES的... 查看详情
elasticsearch学习笔记
在学习elasticsearch之前,我们先要弄清楚几个问题,就是what,why和how1.what-------elasticsearch是什么 官方概念:elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTfulweb接口。Elasticsear... 查看详情
elasticsearch学习笔记一
一.Elasticsearch简介 Elasticsearch是基于ApacheLucene的开源的搜索引擎。支持时间检索和全文检索。二.安装下载来自http://www.elasticsearch.org/download/的ElasticSearch。 w 查看详情
elasticsearch-尚硅谷(9.面试题)学习笔记
...:(8.优化)学习笔记文章目录1为什么要使用Elasticsearch?2Elasticsearch的master选举流程?3Elasticsearch集群脑裂问题?4Elasticsearch索引文档的流程?5Elasticsearch更新和删除文档的流程?6Elasticsearch搜索的流 查看详情
elasticsearch学习笔记-03探索集群
...据官方文档的翻译,能力有限、水平一般,如果对想学习Elasticsearch的朋友有帮助,将是本人的莫大荣幸。原文出处:https://www.elastic.co/guide/en/elasticsearch/reference/current/_exploring_your_cluster.htmlRESTAPI现在咱们已经成功让Elasticsearch的节... 查看详情
elasticsearch学习笔记-02安装
...据官方文档的翻译,能力有限、水平一般,如果对想学习Elasticsearch的朋友有帮助,将是本人的莫大荣幸。原文出处:https://www.elastic.co/guide/en/elasticsearch/reference/current/_installation.htmlElasticsearch要求Java最低版本为8.截止本文撰写的时... 查看详情
elasticsearch学习笔记--安装
1、安装ElasticSearchhttps://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html这个页面有详细安装步骤2、安装Head插件head插件可以管理elasticsearch集群,管理索引等信息,使用起来比较方便,就是界面有点丑,不过丑就丑吧... 查看详情
elasticsearch学习笔记elasticsearch及elasticsearchhead安装配置(代码片段)
一、安装与配置1、到官网下载Elasticsearch,https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.3.zip2、解压成三分份3、下载Elasticsearch的管理工具 https://codeload.github.com/mobz/elasticsearch-head/zip/mast 查看详情
elasticsearch学习笔记——安装和数据导入
到elasticsearch网站下载最新版本的elasticsearch6.2.1https://www.elastic.co/downloads/elasticsearch下载tar包,然后解压到/usr/local目录下,修改一下用户和组之后可以使用非root用户启动,启动命令./bin/elasticsearch然后访问http://127.0.0.1:9200/接下来导... 查看详情
elasticsearch学习笔记-kibana(代码片段)
一、了解ELKELK是Elasticsearch、Logstash、Kibana三大开源框架首字母大写简称。市面上也被称为ElasticStack。其中Elasticsearch是一个基于Lucene、分布式、通过Restful方式进行交互的近实时搜索平台框架。像类似百度、谷歌这种大数据全文搜... 查看详情
elasticsearch学习笔记elasticsearch分布式机制
一、Elasticsearch对复杂分布式机制透明的隐藏特性 1、分片机制: (1)index包含多个shard,每个shard都是一个最小工作单元,承载部分数据,lucene实例,完整的... 查看详情
el&jstl学习笔记
一、EL表达式(形式:${}) 1.EL运算符 算术运算符: +、-、*、/、% 示例 结果 ${1+1} 2 ${1-1} 0 ${1*3} 3 ${3/2} 1.5 ... 查看详情
elasticsearch学习笔记
1.搜索所有结果,并按照balance倒序排序,返回条数设置为最大1000条1{2"size":1000,3"sort":{4"balance":{5"order":"desc"6}7}8}结果:2.模糊匹配关键字keyword,搜索address中含有"Court"字符串的记录 1{2"size":1000,3"query":{4"match":{5"address":"Court"6 查看详情
elasticsearch学习笔记-03.1集群健康
...据官方文档的翻译,能力有限、水平一般,如果对想学习Elasticsearch的朋友有帮助,将是本人的莫大荣幸。原文出处:https://www.elastic.co/guide/en/elasticsearch/reference/current/_cluster_health.html 让我们以一个基础的健康检查开始,... 查看详情
elasticsearch学习笔记2集群和数据
集群术语-节点:一个elasticsearch实例(一个elasticsearch进程)就是一个节点-集群:由一个或者多个elasticsearch节点组成-主节点:临时管理集群级别变更:新建/删除索引,新建/移除节点,不参与文档级别变更或者搜索,当数据量增长... 查看详情
vue——学习笔记
1、vue需要在dom加载完成之后实现实例化eg:window.onload=function(){newVue({el:‘#editor‘,data:{input:‘#hello‘}})} 查看详情