乐优商城(九)数据同步

1. 项目问题分析

现在项目中有三个独立的微服务:

  • 商品微服务:原始数据保存在 MySQL 中,从 MySQL 中增删改查商品数据。
  • 搜索微服务:原始数据保存在 ES 的索引库中,从 ES 中查询商品数据。
  • 商品详情微服务:做了页面静态化,静态页面的商品数据不会随着数据库发生变化。

假如我在商品微服务中,修改了商品的数据,也就是 MySQL 中数据发生了改变。但搜索微服务查询到的数据还是原来的,商品详情微服务的生成的静态页面也没有发生改变,这样显然不对。我们需要实现数据的同步,让搜索微服务和商品详情微服务的商品也发生修改。

我们使用消息队列技术(RabbitMQ)来实现数据同步:

2. 项目改造

接下来,我们就改造项目,实现搜索微服务和商品详情微服务的数据同步。

2.1 改造思路

生产者:商品微服务

  • 什么时候发送消息?

    当商品微服务对商品进行增、删、改的操作时候,就发送一条消息。

  • 发送什么内容?

    我们发送商品 id,其它微服务可以根据 id 查询自己需要的信息。

消费者:搜索微服务和商品详情微服务

接收消息后如何处理?

  • 搜索微服务:
    • 增/改:添加新的数据到索引库
    • 删:删除索引库数据
  • 商品详情微服务:
    • 增/改:创建新的静态页
    • 删:删除原来的静态页

2.2 商品微服务发送消息

在 leyou-item-service 中实现商品微服务发送消息。

2.2.1 引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.2.2 配置文件

在 application.yaml 中添加 RabbitMQ 的配置:

spring:
  rabbitmq:
    host: 127.0.0.1
    username: leyou
    password: leyou
    virtual-host: /leyou
    template:
      exchange: leyou.item.exchange
    publisher-confirms: true
  • template:有关 AmqpTemplate 的配置
    • exchange:缺省的交换机名称,此处配置后,发送消息如果不指定交换机就会使用这个
  • publisher-confirms:生产者确认机制,确保消息会正确发送,如果发送失败会有错误回执,从而触发重试
2.2.3 改造 SpuService
  1. 注入 AmpqTemplate 模板

@Autowired
private AmqpTemplate amqpTemplate;

编写发送消息方法

/**
 * 发送消息
 * @param id 商品 id
 * @param type
 */
private void sendMessage(Long id, String type){
    try {
        this.amqpTemplate.convertAndSend("item." + type, id);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

在新增方法中,调用发送消息方法

在修改方法中,调用发送消息方法

在这里插入图片描述

2.3 搜索微服务接收消息

在 leyou-search 中实现搜索微服务接收消息。

2.3.1 引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.3.2 配置文件
spring:
  rabbitmq:
    host: 127.0.0.1
    username: leyou
    password: leyou
    virtual-host: /leyou
2.3.3 编写监听器

@Component
public class GoodsListener {

    @Autowired
    private SearchService searchService;

    /**
     * 处理 insert 和 update 的消息
     *
     * @param id
     * @throws Exception
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "leyou.create.index.queue", durable = "true"),
            exchange = @Exchange(
                    value = "leyou.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = {"item.insert", "item.update"}))
    public void listenCreate(Long id) throws Exception {
        if (id == null) {
            return;
        }
        // 创建或更新索引
        this.searchService.createIndex(id);
    }

    /**
     * 处理 delete 的消息
     *
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "leyou.delete.index.queue", durable = "true"),
            exchange = @Exchange(
                    value = "leyou.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = "item.delete"))
    public void listenDelete(Long id) {
        if (id == null) {
            return;
        }
        // 删除索引
        this.searchService.deleteIndex(id);
    }
}
2.3.4 编写创建和删除索引方法

在 SearchService 中添加创建和删除索引方法:

/**
 * 创建索引
 *
 * @param id
 * @throws IOException
 */
public void createIndex(Long id) throws IOException {
    Spu spu = this.spuClient.querySpuById(id);
    // 构建商品
    Goods goods = this.buildGoods(spu);

    // 保存数据到索引库
    this.goodsRepository.save(goods);
}
/**
 * 删除索引
 *
 * @param id
 */
public void deleteIndex(Long id) {
    this.goodsRepository.deleteById(id);
}

2.4 商品详情微服务接收消息

在 leyou-goods-web 中实现商品详情微服务接收消息。

2.4.1 引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.4.2 配置文件
spring:
  rabbitmq:
    host: 127.0.0.1
    username: leyou
    password: leyou
    virtual-host: /leyou
2.4.3 编写监听器

在这里插入图片描述

@Component
public class GoodsListener {

    @Autowired
    private GoodsHtmlService goodsHtmlService;

    /**
     * 处理 insert 和 update 的消息
     *
     * @param id
     * @throws Exception
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "leyou.create.web.queue", durable = "true"),
            exchange = @Exchange(
                    value = "leyou.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = {"item.insert", "item.update"}))
    public void listenCreate(Long id) throws Exception {
        if (id == null) {
            return;
        }
        // 创建页面
        goodsHtmlService.createHtml(id);
    }

    /**
     * 处理 delete 的消息
     *
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "leyou.delete.web.queue", durable = "true"),
            exchange = @Exchange(
                    value = "leyou.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = "item.delete"))
    public void listenDelete(Long id) {
        if (id == null) {
            return;
        }
        // 删除页面
        goodsHtmlService.deleteHtml(id);
    }
}
2.4.4 添加删除页面方法

在 GoodsHtmlService 中 添加删除页面方法

/**
 * 删除 HTML 页面
 * @param id
 */
public void deleteHtml(Long id) {
    File file = new File("D:\\nginx-1.14.0\\html\\item\\" + id + ".html");
    file.deleteOnExit();
}

3. 测试

  1. 启动微服务

打开 RabbitMQ 管理界面,交换机和队列都已将创建好了

启动后台管理系统和门户系统

查看搜索商品页和商品详情页

进入后台管理系统,修改商品价格为 4999

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/443984.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

2024 年 AI 辅助研发发展与趋势研究

引言 这几年&#xff0c;人工智能&#xff08;AI&#xff09;技术火得不行&#xff0c;它渗透到了我们生活的方方面面。从帮助我们识别图片、理解语音&#xff0c;到推荐我们喜欢的内容&#xff0c;甚至自动驾驶汽车&#xff0c;AI都在大显身手。特别是在研发领域&#xff0c;…

代码随想录训练营第41天 | 动态规划:01背包理论基础、动态规划:01背包理论基础(滚动数组)、LeetCode 416.分割等和子集

动态规划&#xff1a;01背包理论基础 文章讲解&#xff1a;代码随想录(programmercarl.com) 视频讲解&#xff1a;带你学透0-1背包问题&#xff01;_哔哩哔哩_bilibili 动态规划&#xff1a;01背包理论基础&#xff08;滚动数组&#xff09; 文章讲解&#xff1a;代码随想录(…

好好好,这样玩是吧。

python聊天机器人 //1.安装方法 pip install requests //2.实例 import requestsdef chat_bot():url http://api.qingyunke.com/api.phpparams {key: free,appid: 0}print("你好&#xff01;开始对话吧&#xff08;输入exit退出&#xff09;")while True:user_in…

Navicat Premium:掌控数据库的强大工具

在当今数字化的时代&#xff0c;数据管理变得越来越重要。无论您是数据库管理员、开发人员还是普通用户&#xff0c;找到一个高效、易用的数据库管理工具是至关重要的。Navicat Premium for Mac/Win 就是这样一款强大的多协议数据库管理工具&#xff0c;它将为您的数据库管理体…

指纹加密U盘/指纹KEY方案——采用金融级安全芯片 ACH512

方案概述 指纹加密U盘解决方案可实现指纹算法处理、数据安全加密、数据高速存取&#xff08;EMMC/TF卡/NandFlash&#xff09;&#xff0c;可有效保护用户数据安全。 方案特点 • 采用金融级安全芯片 ACH512 • 存储介质&#xff1a;EMMC、TF卡、NandFlash • 支持全系列国密…

docker 使用官方镜像搭建 PHP 环境

一、所需环境&#xff1a; 1、PHP&#xff1a;7.4.33-fpm 的版本 2、Nginx&#xff1a;1.25.1 的版本 3、MySQL&#xff1a; 5.7 的版本 4、Redis&#xff1a;7.0 的版本 1.1、拉取官方的镜像 docker pull php:7.4.33-fpm docker pull nginx:1.25.1 docker pull mysql:5.7 do…

Java编程实战:构建校园二手物品交易系统

✍✍计算机编程指导师 ⭐⭐个人介绍&#xff1a;自己非常喜欢研究技术问题&#xff01;专业做Java、Python、微信小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。 ⛽⛽实战项目&#xff1a;有源码或者技术上的问题欢迎在评论区一起讨论交流&#xff01; ⚡⚡ Java实战 |…

Toyota Programming Contest 2024#3(AtCoder Beginner Contest 344)(A~C)

A - Spoiler 竖线里面的不要输出&#xff0c;竖线只有一对&#xff0c;且出现一次。 #include <bits/stdc.h> //#define int long long #define per(i,j,k) for(int (i)(j);(i)<(k);(i)) #define rep(i,j,k) for(int (i)(j);(i)>(k);--(i)) #define debug(a) cou…

Qt教程3-Ubuntu(x86_64)上配置arm64(aarch64)交叉编译环境及QT编译arm64架构工程

汇创慧玩 写在前面1. 查看系统架构相关指令2. ARM64交叉编译器环境搭建3. Qt编译arm64环境搭建4. 配置 Qt的本地aarch64交叉编译器5. 工程建立及编译验证 写在前面 苦辣酸甜时光八载&#xff0c;春夏秋冬志此一生 Qt简介&#xff1a; Qt&#xff08;官方发音 [kju:t]&#xff…

配置oh-my-posh

在windows上的powershell上配置oh-my-posh&#xff0c;使其更像在linux用oh-my-zsh。 首先打开powershell&#xff0c;输入&#xff1a; winget install JanDeDobbeleer.OhMyPosh -s winget安装on-my-posh.exe和oh-my-posh上最新的主题。 之后重启powershell。 打开配置文件…

flink重温笔记(十三): flink 高级特性和新特性(2)——ProcessFunction API 和 双流 join

Flink学习笔记 前言&#xff1a;今天是学习 flink 的第 13 天啦&#xff01;学习了 flink 高级特性和新特性之ProcessFunction API 和 双流 join&#xff0c;主要是解决大数据领域数据从数据增量聚合的问题&#xff0c;以及快速变化中的流数据拉宽问题&#xff0c;即变化中多个…

【简写Mybatis】03-Mapper xml的注册和使用

前言 在学习MyBatis源码文章中&#xff0c;斗胆想将其讲明白&#xff1b;故有此文章&#xff0c;如有问题&#xff0c;不吝指教&#xff01; 注意&#xff1a; 学习源码一定一定不要太关注代码的编写&#xff0c;而是注意代码实现思想&#xff1b; 通过设问方式来体现代码中的…

vSphere 8考试认证题库 2024最新(VCP 8.0版本)

VMware VCP-DCV&#xff08;2V0-21.23&#xff09;认证考试题库&#xff0c;已全部更新&#xff0c;答案已经完成校对&#xff0c;完整题库请扫描上方二维码访问。正常考可以考到450分以上&#xff08;满分500分&#xff0c;300分通过&#xff09; An administrator is tasked …

HPE ProLiant MicroServer Gen8驱动程序下载(windows)

记录下&#xff0c;以方便需要重装系统时将驱动更新到最后版本。 共有下面设备有适用的驱动可用&#xff1a; 1、系统管理&#xff1a; iLO 4 Channel Interface Driver for Windows Server 2016 下面这个驱动&#xff0c;安装后不知道有什么用 iLO 3/4 Management Control…

基于springboot+vue实现物资仓储物流管理系统项目【项目源码+论文说明】计算机毕业设计

基于springbootvue实现物资仓储物流管理系统演示 摘要 随着我国经济及产业化结构的持续升级&#xff0c;越来越多的企业借助信息化及互联网平台实现了技术的创新以及竞争力的提升&#xff0c;在电子经济的影响下仓储物流业务也获得了更多的关注度&#xff0c;利用系统平台实现…

【软考】设计模式之享元模式

目录 1. 说明2. 应用场景3. 结构图4. 构成5. 适用性6. java示例 1. 说明 1.享元设计模式&#xff08;Flyweight Design Pattern&#xff09;是一种常见的软件设计模式2.属于结构型设计模式&#xff0c;对象结构型模式3.目的&#xff1a;运用共享技术有效地支持大量细粒度的对象…

【Wireshark傻瓜式安装,Wireshark使用过滤条件】

Wireshark傻瓜式安装&#xff0c;Wireshark使用过滤条件 安装使用wireshark过滤器表达式的规则1.抓包过滤器语法和实例&#xff08;1&#xff09;协议过滤&#xff08;2&#xff09;IP过滤&#xff08;3&#xff09;端口过滤&#xff08;4&#xff09;逻辑运算符&&与、…

后端传给前端的时间字段前端显示不正确

具体问题是什么呢&#xff0c;就比如我后段有一个字段是TimeStamp类型&#xff0c;从数据库中查出数据是下面的样式&#xff1a; 但是前端显示的是下面的格式&#xff1a; 这个的解决方法还是挺多的&#xff0c;那接下来具体来看看吧~ 第一种&#xff1a; 在application.prop…

【数据结构】数组、双链表代码实现

&#x1f497;&#x1f497;&#x1f497;欢迎来到我的博客&#xff0c;你将找到有关如何使用技术解决问题的文章&#xff0c;也会找到某个技术的学习路线。无论你是何种职业&#xff0c;我都希望我的博客对你有所帮助。最后不要忘记订阅我的博客以获取最新文章&#xff0c;也欢…

迁移篇 | MatrixOne与MySQL全面对比

Part 1 迁移背景 Skyable 自研了物联网私有云平台用于 IoT 设备的数据上报和协议解析&#xff0c;由于管理设备数量的增加导致设备上报的数据量越来越大&#xff0c;架构中原使用的 MySQL 数据库&#xff08;分库分表&#xff09;的部分业务在对设备上报信息进行相关的查询时&…