7.SpringBoot整合RabbitMQ实现微服务间的异步消息沟通
来源:互联网 发布:数据库分为哪几种类型 编辑:程序博客网 时间:2024/06/05 17:26
1.需求:
前我们已经开发了两个微服务,一个用来管理设备目录,一个用来管理技术状态。现在需要通过MQ来沟通彼此。两个微服务之间的关系如图:
左右都是一个单独的微服务,彼此之间没有直接的关联关系,各自的信息是分库保存的。也就是说在通过设备目录中存在的信息建立技术状态时,技术状态中保存的并非设备目录信息的引用,而是副本。这样可以保证即使在设备目录服务无法工作时技术状态服务不受影响。由于保存的是副本而非引用,那么当设备目录中的信息发生变化时,就需要使用消息队列发送通知如:id为1的设备名称变为‘发电机’这样的消息给技术状态服务,技术状态服务接收通知后,根据消息内容更新自身数据库中的信息,保证整个系统中数据的一致性。
2.搭建RabbitMQ服务器
依然使用Docker的方式来部署RabbitMQ服务器。
按照RabbitMQ官方Docker使用说明
先使用docker pull rabbitmq命令获取RabbitMQ的镜像。
接着使用
docker run -d --hostname my-rabbit --name some-rabbit-p 8080:15672-e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=passwordrabbitmq:3-management
测试访问控制台
部署一个指定用户名和密码并且可以通过8080端口访问控制台的RabbitMQ服务器。
3.配置SpringBoot
在项目的POM文件中加入Spring-AOP和Spring-AMQP的依赖
<!-- AOP --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency><!-- rabbitMQ --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId></dependency>
通过SpringBoot配置文件整合RabbitMQ
#rabbitMQspring.rabbitmq.host=192.168.227.101spring.rabbitmq.port=5672spring.rabbitmq.username=userspring.rabbitmq.password=password
4.发送消息
思路是这样的,当设备目录服务的设备信息变更方法正常执行完成后,我们使用AOP触发消息的发送
package an.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.alibaba.fastjson.JSON;import an.entity.Catalog;//声明切面@Component@Aspectpublic class CatalogAop { //在引入Spring-AMQP后可以直接使用AmqpTemplate @Autowired private AmqpTemplate rabbitTemplate; //使用@AfterReturning并声明切点 @AfterReturning(value="execution(* an.catalog.CatalogService.editCatalog(..))" , returning="result") public void doAccessCheck(JoinPoint joinPoint , Object result) { //组装要发送的消息内容 Object[] args = joinPoint.getArgs(); Long catalogId = (Long) args[0]; String name = (String) args[1]; String model = (String) args[2]; String unit = (String) args[3]; Boolean unique = (Boolean) args[4]; Boolean parent = (Boolean) args[5]; Catalog catalog = new Catalog(catalogId, name, model, unit, unique, parent); //由于editCatalog正常执行的返回值是void,所以如果返回为null说明成功执行,可以开始发送消息,如果失败则什么都不发送 if(result == null) { //使用已封装好的convertAndSend(String exchange , String routingKey , Object message)使用特定的routingKey发送消息到指定的exchange rabbitTemplate.convertAndSend("catalogExchange" , "editCatalogKey" , JSON.toJSONString(catalog)); } }}
5.接收消息
package an.aop;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;import com.alibaba.fastjson.JSON;import an.entity.Catalog;import an.techstatus.TechStatusRepository;@Componentpublic class Receiver { //注入技术状态数据持久层 @Autowired private TechStatusRepository repository; //使用@RabbitListener监听指定队列、指定exchange、指定routingKey的消息 //同时@RabbitListener有建立队列、exchange、routingKey的功能 @RabbitListener( bindings = @QueueBinding( value = @Queue(value = "editCatalog" , durable = "true") , exchange = @Exchange(value = "catalogExchange" , type = "topic" , durable = "true") , key = "editCatalogKey") ) public void receiveMessage(String message) throws Exception { Catalog catalog = JSON.parseObject(message , Catalog.class); //进行更新 repository.updateDeviceInfo(catalog); }}
6.启动服务后验证RabbitMQ中队列、exchange、routingKey的建立
7.验证
阅读全文
0 0
- 7.SpringBoot整合RabbitMQ实现微服务间的异步消息沟通
- SpringBoot整合RabbitMQ实现微服务间的异步消息沟通
- RabbitMQ学习之spring整合发送异步消息(注解实现)
- SpringBoot整合Rabbitmq设置消息请求头
- SpringBoot~整合mybatis+微服务注册
- SpringBoot~整合mybatis+微服务注册
- RabbitMQ学习(十二)之spring整合发送异步消息(注解实现)
- RabbitMQ入门-10-3(整合spring-发送异步消息注解实现)
- SpringBoot 整合 Websocket 实现消息推送框架的设计笔记
- springboot(十二)整合rabbitmq
- rabbitmq 整合springboot
- SpringBoot--整合RabbitMq
- 13.springboot整合rabbitmq
- Springboot整合RabbitMQ
- springboot 整合 rabbitmq
- 微服务,微架构[十三]springboot集成RabbitMQ
- RabbitMQ消息收发-SpringBoot
- RabbitMQ学习之spring整合发送异步消息
- STM32F4应用笔记(三)UCGUI裸机移植——不支持触摸屏
- 重装系统的常见方法
- 调整数组顺序使奇数位于偶数前面
- kafka+storm+hbase整合试验(Wordcount)
- 交换两个数的三种方法
- 7.SpringBoot整合RabbitMQ实现微服务间的异步消息沟通
- openssl aes 加密解密示例程序
- GreenPlum 集群 gpfdist 实战
- 链表中倒数第K个节点
- codeforces 789A
- python 对text进行读写小例子非常简单
- C# 遍历Hashtable
- 二叉树的垂直遍历
- D3.js实现折线图的方法详解