1.Spring AMQP

(1)简介

Spring有很多不同的项目,其中就有对AMQP的支持:

Spring AMQP的页面:http://spring.io/projects/spring-amqp

注意:Spring-amqp是对AMQP协议的抽象实现,而spring-rabbit 是对协议的具体实现,也是目前的唯一实现。底层使用的就是RabbitMQ。

(2)依赖和配置

添加AMQP的启动器:

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

application.yml中添加RabbitMQ地址:

spring:
rabbitmq:
host: 127.0.0.1
username: guest
password: guest
virtual-host: /

(3)监听者

在SpringAmqp中,对消息的消费者进行了封装和抽象,一个普通的JavaBean中的普通方法,只要通过简单的注解,就可以成为一个消费者。

@Component
public class Listener { @RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "spring.test.queue", durable = "true"),
exchange = @Exchange(
value = "spring.test.exchange",
ignoreDeclarationExceptions = "true",
type = ExchangeTypes.TOPIC
),
key = {"#.#"}))
public void listen(String msg){
System.out.println("接收到消息:" + msg);
}
}
  • @Componet:类上的注解,注册到Spring容器

  • @RabbitListener:方法上的注解,声明这个方法是一个消费者方法,需要指定下面的属性:

    • bindings:指定绑定关系,可以有多个。值是@QueueBinding的数组。@QueueBinding包含下面属性:

      • value:这个消费者关联的队列。值是@Queue,代表一个队列

      • exchange:队列所绑定的交换机,值是@Exchange类型

      • key:队列和交换机绑定的RoutingKey

类似listen这样的方法在一个类中可以写多个,就代表多个消费者。

(4)AmqpTemplate

Spring最擅长的事情就是封装,把他人的框架进行封装和整合。

Spring为AMQP提供了统一的消息处理模板:AmqpTemplate,非常方便的发送消息,其发送方法:

红框圈起来的是比较常用的3个方法,分别是:

  • 指定交换机、RoutingKey和消息体

  • 指定消息

  • 指定RoutingKey和消息,会向默认的交换机发送消息

(5)测试代码

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MqDemo { @Autowired
private AmqpTemplate amqpTemplate; @Test
public void testSend() throws InterruptedException {
String msg = "hello, Spring boot amqp";
this.amqpTemplate.convertAndSend("spring.test.exchange","a.b", msg);
// 等待10秒后再结束
Thread.sleep(10000);
}
}

运行后查看日志:

2.搜索服务、商品静态页的数据同步

(1)思路分析

<1>发送方:商品微服务

  • 什么时候发?

    当商品服务对商品进行写操作:增、删、改的时候,需要发送一条消息,通知其它服务。

  • 发送什么内容?

    对商品的增删改时其它服务可能需要新的商品数据,但是如果消息内容中包含全部商品信息,数据量太大,而且并不是每个服务都需要全部的信息。因此我们只发送商品id,其它服务可以根据id查询自己需要的信息。

<2>接收方:搜索微服务、静态页微服务

接收消息后如何处理?

  • 搜索微服务:

    • 增/改:添加新的数据到索引库

    • 删:删除索引库数据

  • 静态页微服务:

    • 增/改:创建新的静态页

    • 删:删除原来的静态页

(2)商品服务发送消息

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

<1>引入依赖

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

<2>配置文件

我们在application.yml中添加一些有关RabbitMQ的配置:

spring:
rabbitmq:
host: 127.0.0.1
username: guest
password: guest
virtual-host: /
template:
exchange: leyou.item.exchange
publisher-confirms: true
  • template:有关AmqpTemplate的配置

    • exchange:缺省的交换机名称,此处配置后,发送消息如果不指定交换机就会使用这个

  • publisher-confirms:生产者确认机制,确保消息会正确发送,如果发送失败会有错误回执,从而触发重试

<3>改造GoodsService

在GoodsService中封装一个发送消息到mq的方法:(需要注入AmqpTemplate模板)


@Autowired
private AmqpTemplate amqpTemplate;
/**
* 利用rabbitmq发送消息
* @param id
* @param type
*/
private void sendMessage(Long id, String type){
// 发送消息
try {
this.amqpTemplate.convertAndSend("item." + type, id);
} catch (Exception e) {
//logger.error("{}商品消息发送异常,商品id:{}", type, id, e);
}
}

这里没有指定交换机,因此默认发送到了配置中的:leyou.item.exchange

注意:这里要把所有异常都try起来,不能让消息的发送影响到正常的业务逻辑

然后在新增的时候调用:

/**
* 新增商品
* @param spuBo
*/
@Override
@Transactional //添加事务
public void saveGoods(SpuBo spuBo) {
// 01 新增spu
// 设置默认字段
spuBo.setId(null);
spuBo.setSaleable(true); //设置是否可售
spuBo.setValid(true);
spuBo.setCreateTime(new Date()); //设置创建时间
spuBo.setLastUpdateTime(spuBo.getCreateTime()); //设置更新时间
this.spuMapper.insertSelective(spuBo); // 02 新增spuDetail
SpuDetail spuDetail = spuBo.getSpuDetail();
spuDetail.setSpuId(spuBo.getId());
this.spuDetailMapper.insertSelective(spuDetail); saveSkuAndStock(spuBo); //发送rabbitmq消息
sendMessage(spuBo.getId(),"insert");
}

修改的时候调用:

/**
* 更新商品
* @param spu
*/
@Override
@Transactional
public void updateGoods(SpuBo spu) {
// 查询以前sku
Sku sku=new Sku();
sku.setSpuId(spu.getId());
List<Sku> skus = this.skuMapper.select(sku); // 如果以前存在,则删除
if(!CollectionUtils.isEmpty(skus)) {
List<Long> ids = skus.stream().map(s -> s.getId()).collect(Collectors.toList());
// 删除以前库存
Example example = new Example(Stock.class);
example.createCriteria().andIn("skuId", ids);
this.stockMapper.deleteByExample(example); // 删除以前的sku
Sku record = new Sku();
record.setSpuId(spu.getId());
this.skuMapper.delete(record); }
// 新增sku和库存
saveSkuAndStock(spu); // 更新spu
spu.setLastUpdateTime(new Date());
spu.setCreateTime(null); //不能更新的内容,设置为null
spu.setValid(null);
spu.setSaleable(null);
this.spuMapper.updateByPrimaryKeySelective(spu); // 更新spu详情
this.spuDetailMapper.updateByPrimaryKeySelective(spu.getSpuDetail()); //发送rabbitmq消息
sendMessage(spu.getId(),"insert");

}

(3)搜索服务接收消息

搜索服务接收到消息后要做的事情:

  • 增:添加新的数据到索引库

  • 删:删除索引库数据

  • 改:修改索引库数据

因为索引库的新增和修改方法是合二为一的,因此我们可以将这两类消息一同处理,删除另外处理。

<1>引入依赖

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

<2>添加配置

spring:
rabbitmq:
host: 127.0.0.1
username: guest
password: guest
virtual-host: /

这里只是接收消息而不发送,所以不用配置template相关内容。

<3>编写监听器

package lucky.leyou.listener;

import lucky.leyou.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @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);
}
}

<4>编写创建和删除索引方法

这里因为要创建和删除索引,我们需要在SearchService中拓展两个方法,创建和删除索引:

public void createIndex(Long id) throws IOException {

    Spu spu = this.goodsClient.querySpuById(id);
// 构建商品
Goods goods = this.buildGoods(spu); // 保存数据到索引库
this.goodsRepository.save(goods);
} public void deleteIndex(Long id) {
this.goodsRepository.deleteById(id);
}

创建索引的方法可以从之前导入数据的测试类中拷贝和改造。

040 RabbitMq及数据同步02的更多相关文章

  1. 039 RabbitMq及数据同步01

    1.RabbitMq (1)问题引出 目前我们已经完成了商品详情和搜索系统的开发.我们思考一下,是否存在问题? 商品的原始数据保存在数据库中,增删改查都在数据库中完成. 搜索服务数据来源是索引库,如果 ...

  2. RabbitMQ数据同步一致性解决方案

    1.概述 我们知道在使用RabbitMQ时,生产者将消息发布出去之后,消息是否顺利到达broker代理服务器呢?默认情况下发布操作没有任何信息返回给生产者,也就是生产者是不知道消息有没有顺利到达bro ...

  3. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 大型软件系统客户端数据同步的问题解决

    作为一个完整的整体信息化解决方案需要有足够强大的各种功能,这些功能相对独立,又互相依存.当有需要这样的功能时可以随时拿出来用,适当修改一下就可以满足要求.只有这样才能快速开发各种信息化系统,才能满足各 ...

  4. SqlServer调用外部程序实现数据同步

    首先创建两个数据库:SyncA是数据源,SyncB是对SyncA进行同步的数据库. 在SyncA和SyncB中分别创建Source表和Target表,实际业务中,两张表的结构大多不相同.     然后 ...

  5. Kettle ETL 来进行mysql 数据同步——试验环境搭建(表中无索引,无约束,无外键连接的情况)

    今天试验了如何在Kettle的图形界面(Spoon)下面来整合来mysql 数据库中位于不同数据库中的数据表中的数据. 试验用的数据表是customers: 第三方的数据集下载地址是:http://w ...

  6. OGG &quot;Loading data from file to Replicat&quot;table静态数据同步配置过程

    OGG "Loading data from file to Replicat"table静态数据同步配置过程 一个.mgr过程 GGSCI (lei1) 3> view p ...

  7. Linux实战教学笔记21:Rsync数据同步工具

    第二十一节 Rsync数据同步工具 标签(空格分隔): Linux实战教学笔记-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品,允许转载,转载 ...

  8. 搭建中小规模集群之rsync数据同步备份

    NFS重要问题 1.有关NFS客户端普通用户写NFS的问题. 1)为什么要普通用户写NFS. 2)exports加all_squash. Rsync介绍 什么是Rsync? Rsync是一款开源的.快 ...

  9. 转载:MySQL和Redis 数据同步解决方案整理

    from: http://blog.csdn.net/langzi7758521/article/details/52611910 最近在做一个Redis箱格信息数据同步到数据库Mysql的功能. 自 ...

随机推荐

  1. Maven学习 一 (下载安装)

    Maven简介 Apache Maven 是一个软件项目管理工具.基于项目对象模型(project Object Model ,POM)的概念,Maven可用来管理项目的依赖,编译,文档等信息 使用M ...

  2. Linux文本编辑器的常用命令

    Vi文本编辑器 首先介绍命令模式,命令模式是在进入文本编辑器的时候的当前状态:输入模式(插入模式)是在插入文本时候按下的键,比如 大小 i  键,a键.o键:底行模式是在命令模式的状态直接输入冒号 & ...

  3. 获取Object对象属性的方法,Reflect.ownKeys, Object.getOwnPropertyNames,Object.getOwnPropertySymbols,Object.keys,for in

    let triangle={ a:1, b:2, c:3 } function coloTriangle(){ this.color='red'; } coloTriangle.prototype=t ...

  4. c# 根据域名的到对应的IP

    今天做了域名解析,由于项目里要用到域名对应的ip 所以做了这个小程序 private void button_Net_Click(object sender, EventArgs e)        ...

  5. 【spring data jpa】带有条件的查询后分页和不带条件查询后分页实现

    一.不带有动态条件的查询 分页的实现 实例代码: controller:返回的是Page<>对象 @Controller @RequestMapping(value = "/eg ...

  6. 5种处理Vue异常的方法

    原文: Handling Errors in Vue.js 译者: Fundebug 本文采用意译,版权归原作者所有 去年一整年,我都在使用最爱的-Vue.js- 来做项目.最近突然意识到,我竟然从来 ...

  7. JS数据打印的几种方式

    /** * 在页面输出内容! */ document.write("This's Great!"); /** * 控制台打印: */ console.log("This ...

  8. Window 2003 IIS + MySQL + PHP + Zend 环境配置

    图文详解 下载 Windows 2003 Zend, PHP, PHPMyadmin 与 MySQL Windows 2003 安装包中包含了 Zend,PHP 5.2.17,PHPWind8.7 和 ...

  9. TRACE32 Simulator License

    链接:https://www.lauterbach.com/frames.html?sim_license.html Which Features of the TRACE32 Instruction ...

  10. Jupyter Notebook 更换主题(背景、字体)

    通过命令行窗口或 Anaconda Prompt 窗口 1.安装 Jupyter 主题 pip install jupyterthemes 2.更新 Jupyter 主题 (可选) pip insta ...