当前位置: 首页 > wzjs >正文

网站设计策划案雄安网建 网站建设

网站设计策划案,雄安网建 网站建设,在线做ppt的网站有哪些,wordpress防注册一、场景简介 现有酒店管理与酒店搜索预定两个分离的微服务模块,为了数据的安全性我们在就带你管理模块通过Mysql进行crud,为了搜索的高效和质量在搜索预定模块我们采用了ElasticSearch搜索引擎(视作一种NoSQL 数据库)&#xff0c…

一、场景简介

        现有酒店管理与酒店搜索预定两个分离的微服务模块,为了数据的安全性我们在就带你管理模块通过Mysql进行crud,为了搜索的高效和质量在搜索预定模块我们采用了ElasticSearch搜索引擎(视作一种NoSQL 数据库),这样一来如何同步数据就是我们必须要解决的问题。

 

        而上图这样的调用暴露接口的方式无疑又违背了分模块单一职责的初衷,降低了效率又增加了耦合,为了解决这一问题,我们将采用消息中间件Rabbit MQ来承担通讯的角色。

 

 二、问题解决

        有关MQ的内容大家可以自行查找或查看官方文档RabbitMQ Tutorials | RabbitMQ

1.引入依赖

因为两个模块一个发布,一个监听都需要用到MQ,所以两者都要引入依赖

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

 2.配置.yml

spring:rabbitmq:port: 5672host: 192.168.56.128 #主机地址username: #账号password: #密码virtual-host: / #虚拟主机

2.常量定义

        一些关于MQ中间件定义和使用时需要用到的常量,当然也可以直接在用的时候再声明,这里选择做统一管理。

public class MqConstant {/*** 交换机*/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.key";/*** 删除的RoutingKey*/public final static String HOTEL_DELETE_KEY="hotel.delete.key";}

 3.组件定义

可以直接在@RabbitListener注解中定义绑定关系等属性,这里为了看起来更清晰,我采用@Bean注解定义在配置类,放在需要进行监听的用户搜索模块

import cn.itcast.hotel.constant.MqConstant;
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 {//定义交换机,关于fanout,direct,topic三种不同交换机的区别可查看官方文档@Beanpublic TopicExchange topicExchange(){return new TopicExchange(MqConstant.HOTEL_EXCHANGE,true,false);}//定义增改队列@Beanpublic Queue insertQueue(){return new Queue(MqConstant.HOTEL_INSERT_QUEUE,true);}//定义删除队列@Beanpublic Queue deleteQueue(){return new Queue(MqConstant.HOTEL_DELETE_QUEUE,true);}//定义增改绑定关系@Beanpublic Binding insertQueueBinding(){return new Binding(MqConstant.HOTEL_INSERT_QUEUE,Binding.DestinationType.QUEUE,MqConstant.HOTEL_EXCHANGE,MqConstant.HOTEL_INSERT_KEY,null);}//定义删除绑定关系@Beanpublic Binding deleteQueueBinding(){return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(MqConstant.HOTEL_DELETE_KEY);}
}

 4.消息发布

酒店管理模块中在增删改业务成功完成后应该向MQ发布消息通知,我们来补全Service中的逻辑

import cn.itcast.hotel.constant.MqConstant;
import cn.itcast.hotel.mapper.HotelMapper;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.service.IHotelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {@Autowiredprivate RabbitTemplate rabbitTemplate;/*** 保存酒店信息并发送消息到 RabbitMQ* @param hotel 酒店对象*/public void saveHotelWithMessage(Hotel hotel) {// 使用 MyBatis-Plus 保存酒店信息到数据库boolean saveSuccess = this.save(hotel);if (saveSuccess) {// 发送消息到 RabbitMQrabbitTemplate.convertAndSend(MqConstant.HOTEL_EXCHANGE, MqConstant.HOTEL_INSERT_KEY, hotel.getId());} else {throw new RuntimeException("保存酒店信息失败");}}/*** 更新酒店信息并发送消息到 RabbitMQ* @param hotel 酒店对象*/public void updateHotelWithMessage(Hotel hotel) {// 检查 ID 是否存在if (hotel.getId() == null) {throw new IllegalArgumentException("ID 不能为空");}// 使用 MyBatis-Plus 更新酒店信息boolean updateSuccess = this.updateById(hotel);if (updateSuccess) {// 发送消息到 RabbitMQrabbitTemplate.convertAndSend(MqConstant.HOTEL_EXCHANGE, MqConstant.HOTEL_INSERT_KEY, hotel.getId());} else {throw new RuntimeException("更新酒店信息失败");}}/*** 删除酒店信息并发送消息到 RabbitMQ* @param id*/@Overridepublic void removeByIdWithMessage(Long id) {if (id == null) {throw new IllegalArgumentException("ID 不能为空");}// 使用 MyBatis-Plus 更新酒店信息boolean removeSuccess = this.removeById(id);if (removeSuccess) {// 发送消息到 RabbitMQrabbitTemplate.convertAndSend(MqConstant.HOTEL_EXCHANGE, MqConstant.HOTEL_DELETE_KEY, id);} else {throw new RuntimeException("删除酒店信息失败");}}
}

5.监听消息

在用户搜索模块,需要监听MQ发来的消息,根据key判断并执行ES的更新操作

  • 监听
    import cn.itcast.hotel.constant.MqConstant;
    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 {@Autowiredprivate IHotelService hotelService;/*** 监听酒店新增或修改* @param id*/@RabbitListener(queues = MqConstant.HOTEL_INSERT_QUEUE)public void listenHotelInsert(Long id){hotelService.insertDocById(id);}/*** 监听酒店删除* @param id*/@RabbitListener(queues = MqConstant.HOTEL_DELETE_QUEUE)public void listenHotelDelete(Long id){hotelService.deleteDocById(id);}
    }
    
  •  更新操作
    @Overridepublic void deleteDocById(Long id) {//request对象DeleteRequest deleteRequest = new DeleteRequest("hotel",id.toString());//发送请求try {restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}}@Overridepublic void insertDocById(Long id) {//获取数据库数据Hotel hotel = getById(id);//转换esHotelDoc hotelDoc = new HotelDoc(hotel);//request对象IndexRequest indexRequest =new IndexRequest("hotel").id(hotel.getId().toString());//jsonindexRequest.source(JSON.toJSONString(hotelDoc), XContentType.JSON);//发送请求try {restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}}

三、功能测试 

         在酒店管理模块中更改MySQL数据库内容,观察是否同步到利用ES数据库回显的用户查询模块,以下是价格1500以上酒店的间隔升序回显:

可以看到深圳大中华最低,以1599排在第一页首位

 我们在管理模块将其价格上升十倍

再回到用户搜索模块,可以看到深圳大中华价格已经变为15990并且出现在最后一页末尾,可见ES中数据也被同步修改,这里就不在通过DSL查询向大家展示了。

通过以上更改,我们实现了更合理高效的数据同步,那么本次的分享到这里就结束了,感谢大家阅读。

http://www.dtcms.com/wzjs/595535.html

相关文章:

  • 上海网站设计制作公司室内设计专业作品集
  • 登陆江西建设厅三类人员的网站下载网站开发
  • 学校门户网站怎么做网络优化的三个方法
  • 推广产品网站建设做一个游戏小程序需要多少钱
  • 网站设计开发的销售主要工作调价智能关键词软件
  • 公司建立网站的意义免费发帖网站大全
  • 照片视频制作网站做服装最好的网站有哪些
  • 顺德大良哪家做网站好子夜免费观看
  • 湖南衡五建设公司网站那里有专业注册网站建设的
  • wordpress怎么弄网站合肥关键词排名提升
  • 网站建设技能考试试题三网站开发设计中的收获
  • 网站建设的步骤目标规划做微信的网站叫什么
  • 天津地区网站建设公司注册网上申报流程
  • 网站建设的感想用vs2010做的网站的源码
  • 网站建设中js控制什么steam交易链接怎么获取
  • android 网站开发专业医院网站建设
  • 做网站需要哪方面的编程做网站书
  • 生鲜网站建设背景网站开发分类
  • 网站开发转型哪里做网站最好网站
  • 开封网站建设流程与开发步骤wordpress 突然加速
  • 摄影网站的需求分析怎么注册公司需要什么材料
  • 仿业务网站源码凡科建站网站
  • 网站建设网页设计网站301和302
  • 滕州网站建设优化有没有推广app的平台
  • 眉山网站定制上海抖音seo
  • 石家庄建站模板厂家学院网站设计案例
  • 做网站哪家便宜jfinal网站开发
  • 可视化网站后台wordpress主题 超级
  • 网站页面建设营销网站制作比较好的
  • 网站推广代运营多少钱app产品网站模板