怎么做根优酷差不多的网站,怎么访问被禁止的网站,新手学做网站从哪里开始,沛县建设局网站方式一:同步调用 优点:实现简单#xff0c;粗暴 缺点:业务耦合度高
方式二:异步通知 优点:低耦含#xff0c;实现难度一般 缺点:依赖mq的可靠性 方式三:监听binlog 优点:完全解除服务间耦合 缺点:开启binlog增加数据库负担、实现复杂度高 利用MQ实现mysql与elastics…方式一:同步调用 优点:实现简单粗暴 缺点:业务耦合度高
方式二:异步通知 优点:低耦含实现难度一般 缺点:依赖mq的可靠性 方式三:监听binlog 优点:完全解除服务间耦合 缺点:开启binlog增加数据库负担、实现复杂度高 利用MQ实现mysql与elasticsearch数据同步
利用课前资料提供的hotel-admin项目作为酒店管理的微服务。当酒店数据发生增、删、改时要求对elasticsearch中数据也要完成相同操作。
步骤:
导入课前资料提供的hotel-admin项目启动并测试酒店数据的CRUD
1、声明exchange、queue、RoutingKey
Configuration
public class MQConfig {Beanpublic TopicExchange topicExchange(){return new TopicExchange(MqConstants.HOTEL_EXCHANGE,true,false);}Beanpublic Queue insertQueue(){return new Queue(MqConstants.HOTEL_INSERT_QUEUE);}Beanpublic Queue deleteQueue(){return new Queue(MqConstants.HOTEL_DELETE_QUEUE);}Beanpublic Binding insertBinding(Queue insertQueue, TopicExchange topicExchange){return BindingBuilder.bind(insertQueue).to(topicExchange).with(MqConstants.HOTEL_INSERT_KEY);}Beanpublic Binding deleteBinding(Queue deleteQueue, TopicExchange topicExchange){return BindingBuilder.bind(deleteQueue).to(topicExchange).with(MqConstants.HOTEL_DELETE_KEY);}
}
2、在hotel-admin中的增、删、改业务中完成消息发送
PostMappingpublic 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);}
3、在hotel-demo中完成消息监听并更新elasticsearch中数据
Component
public class HotelListener {Autowiredprivate IHotelService iHotelService;/*** 监听酒店新增或修改* param id*/RabbitListener(queues MqConstants.HOTEL_INSERT_QUEUE)public void listenHotelInsertOrUpdate(Long id){iHotelService.insertById(id);}/*** 监听酒店删除* param id*/RabbitListener(queues MqConstants.HOTEL_DELETE_QUEUE)public void listenHotelDelete(Long id){iHotelService.deleteById(id);}
}
Overridepublic void insertById(Long id) {try {//根据id查酒店数据Hotel hotel getById(id);HotelDoc hotelDoc new HotelDoc(hotel);//转换成文档接收的格式//1、准备Request对象IndexRequest request new IndexRequest(hotel).id(hotelDoc.getId().toString());//2、准备Json文档request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);//转换成JSON并发送//3、发送client.index(request,RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}}Overridepublic void deleteById(Long id) {try {DeleteRequest request new DeleteRequest(hotel);request.id(id.toString());log.info(删除id);client.delete(request,RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}}
4、启动并测试数据同步功能 该项目分成两个部分一个hotel-demo一个hotel-admin分别代表管理端和客户端。具体同步流程即 客户端 修改/新增/删除 某酒店时发送id到mq对应操作的队列中即上文的第二步 客户端监听到消息后便同步操作es即上文的第三步 过程收获es和mysql一样都是数据库管理系统只是在存储方式、数据类型等方面不一样。把这个过程理解为操作两个不同语言的数据库的同步操作用mq作为中间件就很好理解了。 其中插入操作是相对有点麻烦的
IndexRequest request new IndexRequest(hotel).id(hotelDoc.getId().toString());
这段代码是用于创建一个索引请求IndexRequest用于将数据存储到名为hotel的Elasticsearch索引中。具体解释如下 1. IndexRequest(hotel)表示要将数据索引到名为hotel的索引中。在Elasticsearch中索引类似于数据库中的表用于存储和组织数据。 2. id(hotelDoc.getId().toString())设置要存储的文档的唯一标识符即文档的ID。这里使用hotelDoc对象的ID属性并将其转换为字符串形式。在Elasticsearch中每个文档都有一个唯一的ID用于区分不同的文档。 综合起来这段代码的作用是创建一个索引请求将hotelDoc对象可能是一个酒店文档存储到名为hotel的Elasticsearch索引中并指定文档的唯一ID。 此时如果对应id已存在数据则覆盖