之前我们记录了原生java代码使用rabbitmq的方法,很简单,类似于原生jdbc代码一样,将连接对象抽离出来作为工具类,生产者和消费者通过工具类获取连接对象,进而获取通道对象,再注册交换机或者是队列等,发送消息与接收消息。
在企业开发中,我们更多的是使用spring框架来整合其它技术,springboot更是方便的提供了各种starter来快速添加依赖,完成整合,开箱即用。

  1. 添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
  1. 编写配置
    配置信息包括ip,端口,虚拟主机,用户名和密码,和原生java代码所需的配置信息一致。
spring:application:name: spirngboot-rabbitmqrabbitmq:host: 192.168.20.128port: 5672virtual-host: /vhusername: wuwlpassword: 123456
  1. 编写并测试
    本文主要针对前五种常用模型,在spirngboot框架的基础上整合rabbitmq并进行测试使用。

(1) Hello World模型

这是一种简单的直连模型,生产者将消息直接发送至消息队列,消费者绑定消息队列后直接获取,一对一。
spring-boot-starter-amqp为我们提供了一个org.springframework.amqp.rabbit.core.RabbitTemplate类来方便我们使用rabbitmq,自动注入即可。

生产者测试类:

@SpringBootTest(classes = RabbitmqDemoApplication.class)
@RunWith(SpringRunner.class)
public class RabbitmqDemoApplicationTests {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testHelloQueues(){rabbitTemplate.convertAndSend("hello","hello world");}
}

生产者向名为hello的队列发送消息,但是,在没有消费者的情况下,生产者没有任何意义。另外,convertAndSend方法的第一个参数并不是消息队列的意思,而是routingKey,我们根据源码找到最初定义的接口可以看到以下内容:

/*** Convert a Java object to an Amqp {@link Message} and send it to a default exchange* with a specific routing key.** @param routingKey the routing key* @param message a message to send* @throws AmqpException if there is a problem*/void convertAndSend(String routingKey, Object message) throws AmqpException;

第二个参数为Object类型,也就是说可以传递任意类型的对象,该方法将对象转换成一个Amqp消息并发送到一个默认的交换机,并且routingKey为第一个参数的内容,没有提到消息队列的信息,但我们可以分析到,这里的routingKeyqueues应该是同名的。

消费者类:

@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloQueuesConsumer {@RabbitHandlerpublic void consume(String msg){System.out.println("消费消息:" + msg + " " + System.currentTimeMillis());}
}

上面的代码等同于:

@Component
public class HelloQueuesConsumer {@RabbitListener(queuesToDeclare = @Queue("hello"))public void consume(String msg){System.out.println("消费消息:" + msg + " " + System.currentTimeMillis());}
}
  • @RabbitListener 可以标注在类上面,需配合 @RabbitHandler 注解一起使用
  • @RabbitListener 标注在类上面表示当有收到消息的时候,就交给 @RabbitHandler 的方法处理,具体使用哪个方法处理,根据 MessageConverter 转换后的参数类型

直接启动测试方法,也就是生产者,可以看到:
消费者有接收到消息队列中的信息并打印。

(2) work queues模型

生产者测试方法,类与第一个模型一致

@Test
public void testWorkQueues(){for (int i = 0; i < 20; i++) {rabbitTemplate.convertAndSend("work","work index " + i);}
}

消费者类:

@Component
public class WorkQueuesConsumer {@RabbitListener(queuesToDeclare = @Queue("work"))public void consume1(String msg){System.out.println("consumer1消费消息:" + msg);}@RabbitListener(queuesToDeclare = @Queue("work"))public void consume2(String msg){System.out.println("consumer2消费消息:" + msg);}
}

启动生产者测试方法:
消费者一与消费者二均匀分配了队列中的消息任务,即使两者执行效率不一致,也同样是均匀分配。

(3) Publish/Subscribe模型

生产者测试方法:

for (int i = 0; i < 20; i++) {rabbitTemplate.convertAndSend("amq.fanout","","fanout msg " + i);
}

消费者类:

@Component
public class FanoutQueuesConsumer {@RabbitListener(bindings = {@QueueBinding(value = @Queue,exchange = @Exchange(value = "amq.fanout",type = "fanout"))})public void consume1(String msg) {System.out.println("consumer1消费消息:" + msg);}@RabbitListener(bindings = {@QueueBinding(value = @Queue,exchange = @Exchange(value = "amq.fanout",type = "fanout"))})public void consume2(String msg) {System.out.println("consumer2消费消息:" + msg);}
}

注意此处的交换机信息

启动生产者测试方法:
此处只粘贴了部分打印信息,两个消费者获得了相同的消息,生产者将消息发送至交换机,由交换机发送至已注册到交换机的所有临时消息队列,进而消费者获取队列中的消息。

(4) Routing模型

生产者测试方法:

@Test
public void testDirectQueues(){rabbitTemplate.convertAndSend("amq.direct","info","routingKey is info");rabbitTemplate.convertAndSend("amq.direct","warn","routingKey is warn");rabbitTemplate.convertAndSend("amq.direct","error","routingKey is error");
}

routing也成为fanout模型,对应的交换机类型为direct

消费者类:

@Component
public class DirectQueuesConsumer {@RabbitListener(bindings = {@QueueBinding(value = @Queue,exchange = @Exchange(value = "amq.direct",type = "direct"),key = {"info", "warn", "error"})})public void consume1(String msg) {System.out.println("consumer1消费消息:" + msg);}@RabbitListener(bindings = {@QueueBinding(value = @Queue,exchange = @Exchange(value = "amq.direct",type = "direct"),key = "error")})public void consume2(String msg) {System.out.println("consumer2消费消息:" + msg);}
}

启动生产者测试类:

消费者一配置了三种类型的routingKey,所以三种类型的消息都能够接收到,消费者二只能接受到error类型的消息。

(5) Topic模型

生产者测试方法:

@Test
public void testTopicQueues(){rabbitTemplate.convertAndSend("amq.topic","file.info","routingKey is info");rabbitTemplate.convertAndSend("amq.topic","file.warn","routingKey is warn");rabbitTemplate.convertAndSend("amq.topic","file.error","routingKey is error");
}

消费者类:

@Component
public class TopicQueuesConsumer {@RabbitListener(bindings = {@QueueBinding(value = @Queue,exchange = @Exchange(value = "amq.topic",type = "topic"),key = {"#"})})public void consume1(String msg) {System.out.println("consumer1消费消息:" + msg);}@RabbitListener(bindings = {@QueueBinding(value = @Queue,exchange = @Exchange(value = "amq.topic",type = "topic"),key = "*.error")})public void consume2(String msg) {System.out.println("consumer2消费消息:" + msg);}
}

启动生产者测试方法:
消费者一配置的routingKey#,可以接受任意类型的消息,*好代表一个单词,消费者二可以接受任意单词加上.errorroutingKey的消息。

springBoot整合rabbitmq并测试五种常用模型相关推荐

  1. rabbit和mysql事务_分布式事务原理及SpringBoot整合RabbitMQ实现可靠事件,TCC事务模型及接口幂等性...

    分布式事务 我们知道在单数据库系统中,实现数据的一致性,通过数据库的事务来处理比较简单.在微服务或分布式系统中,各个独立的服务都会有自己的数据库,而不是在同一个数据库中,所以当一组事务(如商品交易中, ...

  2. Springboot整合RabbitMQ,包含direct,topic,fanout三种模式的整合

    一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...

  3. RabbitMQ,RabbitMQ 的工作模式,Spring 整合 RabbitMQ,Springboot 整合RabbitMQ

    什么是RabbitMQ 1.1 MQ概述 MQ全称 Message Queue(消息队列),是在消息的传输过程中保存消息的容器.多用于分布式系统之间进行通信. ⚫ MQ,消息队列,存储消息的中间件 ⚫ ...

  4. RabbitMq详解+SpringBoot整合RabbitMq快速入门

    1概述: 1.1.什么是MQ 消息队列(Message Queue,简称MQ),从字面意思上看,本质是个队列,FIFO先入先出,只不过队列中存放的内容是message而已. 其主要用途:不同进程Pro ...

  5. Springboot 整合RabbitMq ,用心看完这一篇就够了

    该篇文章内容较多,包括有rabbitMq相关的一些简单理论介绍,provider消息推送实例,consumer消息消费实例,Direct.Topic.Fanout的使用,消息回调.手动确认等. (但是 ...

  6. 九、springboot整合rabbitMQ

    springboot整合rabbitMQ 简介 rabbitMQ是部署最广泛的开源消息代理. rabbitMQ轻量级,易于在内部和云中部署. 它支持多种消息传递协议. RabbitMQ可以部署在分布式 ...

  7. Springboot整合一之Springboot整合RabbitMQ

    前言 目前,springboot已然成为了最热的java开发整合框架,主要是因其简单的配置,并且本身提供了很多与第三方框架的整合,甚至可以让我们在短短的几分钟里就可以搭建一个完整的项目架构.所以,博主 ...

  8. SpringBoot 整合RabbitMq

    SpringBoot 整合RabbitMq (黑马讲义) SpringAMQP是基于RabbitMQ封装的一套模板,并且还利用SpringBoot对其实现了自动装配,使用起来非常方便. SpringA ...

  9. Springboot——整合Rabbitmq之Confirm和Return详解

    文章目录 前言 为什么会有Confirm Springboot 整合 Mq 实现 Confirm 监听机制 依赖引入 增加配置文件,设定连接信息 配置队列.交换机,以及对其进行绑定 编写mq消息发送服 ...

最新文章

  1. 报告:AI岗年薪下降8.9%,收入不及2018年
  2. luogu P4258 [WC2016]挑战NPC(一般图的最大匹配,带花树,建图、拆点技巧)
  3. wordpress给后台文章列表增加自定义排序栏
  4. PHp批量推送数据太慢,PHP非阻塞批量推送数据-php教程
  5. 37.拷贝控制和资源管理
  6. 点站点链接出现短时间白屏或闪屏现象
  7. jar包 热加载/卸载 的初步实现
  8. centos给用户添加sudo权限
  9. appinventor离线版下载_Chrome 离线安装包下载
  10. Javascript原生之用cssText批量修改样式
  11. Spring MVC-学习笔记(3)参数绑定注解、HttpMessageConverterT信息转换、jackson、fastjson、XML...
  12. 需求分析报告应该包含哪些部分_一份数据分析报告所需要的内容以及注意事项...
  13. rpc无法启动计算机,RPC服务器不可用,无法进入系统,怎么处理?
  14. python数据生成pdf_利用Python的Django框架生成PDF文件的教程
  15. LED恒流驱动芯片H6119
  16. python查询水果价格_C语言查询水果价格
  17. js获取并设置lt;pgt;lt;/pgt;的显示的值。
  18. 软件测试理论与经验--阅读笔记
  19. 身份证号码验证C语言函数
  20. 计算机网络实验三(基于packet tracer)

热门文章

  1. 蚂蚁警告:“‘includeantruntime‘未设置”
  2. 什么是Git最好的视觉合并工具? [关闭]
  3. match_parent和fill_parent有什么区别?
  4. Django可扩展吗? [关闭]
  5. win11正式版iso镜像如何安装 windows11正式版iso镜像安装方法
  6. win固定ip无效,自动配成169.254.xx.xx
  7. python获取网页标题_Python2获取网页标题
  8. c语言3%10等于多少,[编程入门]数字的处理与判断-题解(C语言代码)
  9. 液晶面板里面有些什么配件_液晶电视核心部件液晶面板有哪些类型?
  10. 力扣——搜索插入位置