?引言
本文參考黑馬 分布式Elastic search
Elasticsearch是一款非常強(qiáng)大的開源搜索引擎,具備非常多強(qiáng)大功能,可以幫助我們從海量數(shù)據(jù)中快速找到需要的內(nèi)容
一、思路分析
?實(shí)現(xiàn)方式
同步調(diào)用
方案一:同步調(diào)用
基本步驟如下:
- hotel-demo對(duì)外提供接口,用來(lái)修改elasticsearch中的數(shù)據(jù)
- 酒店管理服務(wù)在完成數(shù)據(jù)庫(kù)操作后,直接調(diào)用hotel-demo提供的接口,
異步通知
方案二:異步通知
流程如下:
- hotel-admin對(duì)mysql數(shù)據(jù)庫(kù)數(shù)據(jù)完成增、刪、改后,發(fā)送MQ消息
- hotel-demo監(jiān)聽MQ,接收到消息后完成elasticsearch數(shù)據(jù)修改
監(jiān)聽binlong
方案三:監(jiān)聽binlog
流程如下:
- 給mysql開啟binlog功能
- mysql完成增、刪、改操作都會(huì)記錄在binlog中
- hotel-demo基于canal監(jiān)聽binlog變化,實(shí)時(shí)更新elasticsearch中的內(nèi)容
?框架選擇
方式一:同步調(diào)用
- 優(yōu)點(diǎn):實(shí)現(xiàn)簡(jiǎn)單,粗暴
- 缺點(diǎn):業(yè)務(wù)耦合度高
方式二:異步通知
- 優(yōu)點(diǎn):低耦合,實(shí)現(xiàn)難度一般
- 缺點(diǎn):依賴mq的可靠性
方式三:監(jiān)聽binlog
- 優(yōu)點(diǎn):完全解除服務(wù)間耦合
- 缺點(diǎn):開啟binlog增加數(shù)據(jù)庫(kù)負(fù)擔(dān)、實(shí)現(xiàn)復(fù)雜度高
本次實(shí)現(xiàn)方式我們選擇 以RabbitMQ 異步方式 搭載 SpringCloud Alibaba + Feign 實(shí)現(xiàn)。
二、實(shí)現(xiàn)數(shù)據(jù)同步
?需求分析
需求
實(shí)現(xiàn)酒店管理增刪改查業(yè)務(wù),已提供頁(yè)面。 完成其數(shù)據(jù)發(fā)生增刪改查操作時(shí) 同步 ElasticSearch
分析
我們采用分布式技術(shù)的方式來(lái)實(shí)現(xiàn)
框架采用 SpringCloud Alibaba、Nacos 、OpenFeign 遠(yuǎn)程調(diào)用、RabbitMQ 作為消息承載體承載數(shù)據(jù)、 Elastic Search 搜索引擎
?搭建環(huán)境
以下為模塊概覽
主要分為兩大模塊
- 完成酒店模塊增刪改查業(yè)務(wù),引入MQ依賴,完成其向MQ的發(fā)送消息 此模塊作為生產(chǎn)者
- 完成ES-MQ模塊,引入MQ、ES依賴,完成
接受MQ的消息以及完成對(duì)ES的更新
此模塊作為消費(fèi)者
注意:Nacos需要自行下載,本項(xiàng)目依賴于Nacos注冊(cè)中心, 運(yùn)行起來(lái)后不影響后面的服務(wù)注冊(cè)進(jìn)nacos
本次所用到的 RabbitMQ、 ElasticSearch 均部署在 云服務(wù)器
MQ結(jié)構(gòu)如圖:
?核心源碼
hotel-service 業(yè)務(wù)模塊
導(dǎo)入hotel-service 核心代碼,已完成基礎(chǔ)的增刪改查工作。 具體源碼公眾號(hào)搜索 程序員Bug終結(jié)者
回復(fù) es 獲取
ES模塊引入依賴
<!--amqp-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- ES -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
聲明隊(duì)列交換機(jī)名稱
public class MqConstants {
/**
* 交換機(jī)
*/
public static final String HOTEL_EXCHANGE = "hotel.topic";
/**
* 新增或修改的routing_key
*/
public static final String HOTEL_INSERT_KEY = "hotel.insert";
/**
* 刪除的 routing_key
*/
public static final String HOTEL_DELETE_KEY = "hotel.delete";
}
hotel-service模塊 發(fā)送消息
@RestController
@RequestMapping("hotel")
public class HotelController {
@Autowired
private HotelService hotelService;
@Resource
private RabbitTemplate rabbitTemplate;
@GetMapping("/{id}")
public Hotel queryById(@PathVariable("id") Long id){
return hotelService.getById(id);
}
@GetMapping("/list")
public PageResult hotelList(
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "size", defaultValue = "1") Integer size
){
Page<Hotel> result = hotelService.page(new Page<>(page, size));
return new PageResult(result.getTotal(), result.getRecords());
}
@PostMapping
public void saveHotel(@RequestBody Hotel hotel){
hotelService.save(hotel);
rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.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(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_INSERT_KEY, hotel.getId());
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") Long id) {
hotelService.removeById(id);
rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_DELETE_KEY, id);
}
}
ES模塊接受消息
@Component
@Slf4j
public class MqConsumerListener {
@Resource
private HotelService hotelService;
/**
* 監(jiān)聽酒店新增或修改的業(yè)務(wù)
* @param id
*/
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.HOTEL_INSERT_KEY, durable = "true"),
exchange = @Exchange(name = MqConstants.HOTEL_EXCHANGE, type = ExchangeTypes.DIRECT), key = MqConstants.HOTEL_INSERT_KEY))
public void listenHotelInsertOrUpdate(String id) throws IOException {
hotelService.insertById(id);
}
/**
* 監(jiān)聽酒店刪除的業(yè)務(wù)
* @param id
*/
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.HOTEL_DELETE_KEY, durable = "true"),
exchange = @Exchange(name = MqConstants.HOTEL_EXCHANGE, type = ExchangeTypes.DIRECT), key = MqConstants.HOTEL_DELETE_KEY))
public void listenHotelDelete(String id) throws IOException {
hotelService.deleteById(id);
}
}
核心方法實(shí)現(xiàn)
@Service
public class HotelService {
@Resource
private RestHighLevelClient client;
@Resource
private HotelClient hotelClient;
public void insertById(String id) {
try {
//1. 根據(jù)id查詢酒店數(shù)據(jù)
Hotel hotel = hotelClient.findById(id);
HotelDoc hotelDoc = new HotelDoc(hotel);
//2. 準(zhǔn)備Request
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
//3. 準(zhǔn)備DSL
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
//4. 發(fā)送請(qǐng)求
client.index(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
public void deleteById(String id) {
try {
//1. 準(zhǔn)備 Request
DeleteRequest request = new DeleteRequest("hotel", id);
// 2.發(fā)送請(qǐng)求
client.delete(request, RequestOptions.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
}
}
三、測(cè)試
運(yùn)行nacos
startup.cmd -m standalone
將hotel-service模塊注冊(cè)到nacos
訪問(wèn)頁(yè)面,對(duì)酒店數(shù)據(jù)進(jìn)行增刪改查操作
將第一條信息價(jià)格修改為399
查看es中數(shù)據(jù)的變化
成功完成數(shù)據(jù)同步
四、源碼獲取
請(qǐng)聯(lián)系 公眾號(hào) 程序員Bug終結(jié)者 回復(fù) es同步 獲取源碼及數(shù)據(jù)庫(kù)文件
?小結(jié)
以上就是【Bug 終結(jié)者】對(duì) 分布式 SpringCloudAlibaba、Feign與RabbitMQ實(shí)現(xiàn)MySQL到ES數(shù)據(jù)同步 的簡(jiǎn)單介紹,ES搜索引擎無(wú)疑是最優(yōu)秀的分布式搜索引擎,使用它,可大大提高項(xiàng)目的靈活、高效性! 通過(guò)本文已了解 MySQL數(shù)據(jù)同步ES基本過(guò)程以及核心實(shí)現(xiàn) 技術(shù)改變世界?。?!
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-847432.html
如果這篇【文章】有幫助到你,希望可以給【Bug 終結(jié)者】點(diǎn)個(gè)贊??,創(chuàng)作不易,如果有對(duì)【后端技術(shù)】、【前端領(lǐng)域】感興趣的小可愛,也歡迎關(guān)注?????? 【Bug 終結(jié)者】??????,我將會(huì)給你帶來(lái)巨大的【收獲與驚喜】??????!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-847432.html
到了這里,關(guān)于分布式 SpringCloudAlibaba、Feign與RabbitMQ實(shí)現(xiàn)MySQL到ES數(shù)據(jù)同步的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!