关键词:
springboot学习笔记-6 springboot整合RabbitMQ
一 RabbitMQ的介绍
RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿里巴巴公司的,现已经转让给apache).
消息中间件的工作过程可以用生产者消费者模型来表示.即,生产者不断的向消息队列发送信息,而消费者从消息队列中消费信息.具体过程如下:
从上图可看出,对于消息队列来说,生产者,消息队列,消费者是最重要的三个概念,生产者发消息到消息队列中去,消费者监听指定的消息队列,并且当消息队列收到消息之后,接收消息队列传来的消息,并且给予相应的处理.消息队列常用于分布式系统之间互相信息的传递.
对于RabbitMQ来说,除了这三个基本模块以外,还添加了一个模块,即交换机(Exchange).它使得生产者和消息队列之间产生了隔离,生产者将消息发送给交换机,而交换机则根据调度策略把相应的消息转发给对应的消息队列.那么RabitMQ的工作流程如下所示:
紧接着说一下交换机.交换机的主要作用是接收相应的消息并且绑定到指定的队列.交换机有四种类型,分别为Direct,topic,headers,Fanout.
Direct是RabbitMQ默认的交换机模式,也是最简单的模式.即创建消息队列的时候,指定一个BindingKey.当发送者发送消息的时候,指定对应的Key.当Key和消息队列的BindingKey一致的时候,消息将会被发送到该消息队列中.
topic转发信息主要是依据通配符,队列和交换机的绑定主要是依据一种模式(通配符+字符串),而当发送消息的时候,只有指定的Key和该模式相匹配的时候,消息才会被发送到该消息队列中.
headers也是根据一个规则进行匹配,在消息队列和交换机绑定的时候会指定一组键值对规则,而发送消息的时候也会指定一组键值对规则,当两组键值对规则相匹配的时候,消息会被发送到匹配的消息队列中.
Fanout是路由广播的形式,将会把消息发给绑定它的全部队列,即便设置了key,也会被忽略.
二.SpringBoot整合RabbitMQ(Direct模式)
SpringBoot整合RabbitMQ非常简单!感觉SpringBoot真的极大简化了开发的搭建环境的时间..这样我们程序员就可以把更多的时间用在业务上了,下面开始搭建环境:
首先创建两个maven工程,这是为了模拟分布式应用系统中,两个应用之间互相交流的过程,一个发送者(Sender),一个接收者(Receiver)
紧接着,配置pom.xml文件,注意其中用到了springboot对于AMQP(高级消息队列协议,即面向消息的中间件的设计)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <properties> <java.version>1.7</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 添加springboot对amqp的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies>
紧接着,我们编写发送者相关的代码.首先毫无疑问,要书写启动类:
@SpringBootApplication public class App public static void main(String[] args) SpringApplication.run(App.class, args);
接着在application.properties中,去编辑和RabbitMQ相关的配置信息,配置信息的代表什么内容根据键就能很直观的看出了.这里端口是5672,不是15672...15672是管理端的端口!
spring.application.name=spirng-boot-rabbitmq-sender spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
随后,配置Queue(消息队列).那注意由于采用的是Direct模式,需要在配置Queue的时候,指定一个键,使其和交换机绑定.
@Configuration public class SenderConf @Bean public Queue queue() return new Queue("queue");
接着就可以发送消息啦!在SpringBoot中,我们使用AmqpTemplate去发送消息!代码如下:
@Component public class HelloSender @Autowired private AmqpTemplate template; public void send() template.convertAndSend("queue","hello,rabbit~");
编写测试类!这样我们的发送端代码就编写完了~
@SpringBootTest(classes=App.class) @RunWith(SpringJUnit4ClassRunner.class) public class TestRabbitMQ @Autowired private HelloSender helloSender; @Test public void testRabbit() helloSender.send();
接着我们编写接收端.接收端的pom文件,application.properties(修改spring.application.name),Queue配置类,App启动类都是一致的!这里省略不计.主要在于我们需要配置监听器去监听绑定到的消息队列,当消息队列有消息的时候,予以接收,代码如下:
@Component public class HelloReceive @RabbitListener(queues="queue") //监听器监听指定的Queue public void processC(String str) System.out.println("Receive:"+str);
接下来就可以测试啦,首先启动接收端的应用,紧接着运行发送端的单元测试,接收端应用打印出来接收到的消息,测试即成功!
需要注意的地方,Direct模式相当于一对一模式,一个消息被发送者发送后,会被转发到一个绑定的消息队列中,然后被一个接收者接收!
实际上RabbitMQ还可以支持发送对象:当然由于涉及到序列化和反序列化,该对象要实现Serilizable接口.HelloSender做出如下改写:
public void send() User user=new User(); //实现Serializable接口 user.setUsername("hlhdidi"); user.setPassword("123"); template.convertAndSend("queue",user);
HelloReceiver做出如下改写:
@RabbitListener(queues="queue") //监听器监听指定的Queue public void process1(User user) //用User作为参数 System.out.println("Receive1:"+user);
三.SpringBoot整合RabbitMQ(Topic转发模式)
首先我们看发送端,我们需要配置队列Queue,再配置交换机(Exchange),再把队列按照相应的规则绑定到交换机上:
@Configuration public class SenderConf @Bean(name="message") public Queue queueMessage() return new Queue("topic.message"); @Bean(name="messages") public Queue queueMessages() return new Queue("topic.messages"); @Bean public TopicExchange exchange() return new TopicExchange("exchange"); @Bean Binding bindingExchangeMessage(@Qualifier("message") Queue queueMessage, TopicExchange exchange) return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message"); @Bean Binding bindingExchangeMessages(@Qualifier("messages") Queue queueMessages, TopicExchange exchange) return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");//*表示一个词,#表示零个或多个词
而在接收端,我们配置两个监听器,分别监听不同的队列:
@RabbitListener(queues="topic.message") //监听器监听指定的Queue public void process1(String str) System.out.println("message:"+str); @RabbitListener(queues="topic.messages") //监听器监听指定的Queue public void process2(String str) System.out.println("messages:"+str);
好啦!接着我们可以进行测试了!首先我们发送如下内容:
方法的第一个参数是交换机名称,第二个参数是发送的key,第三个参数是内容,RabbitMQ将会根据第二个参数去寻找有没有匹配此规则的队列,如果有,则把消息给它,如果有不止一个,则把消息分发给匹配的队列(每个队列都有消息!),显然在我们的测试中,参数2匹配了两个队列,因此消息将会被发放到这两个队列中,而监听这两个队列的监听器都将收到消息!那么如果把参数2改为topic.messages呢?显然只会匹配到一个队列,那么process2方法对应的监听器收到消息!
四.SpringBoot整合RabbitMQ(Fanout Exchange形式)
那前面已经介绍过了,Fanout Exchange形式又叫广播形式,因此我们发送到路由器的消息会使得绑定到该路由器的每一个Queue接收到消息,这个时候就算指定了Key,或者规则(即上文中convertAndSend方法的参数2),也会被忽略!那么直接上代码,发送端配置如下:
@Configuration public class SenderConf @Bean(name="Amessage") public Queue AMessage() return new Queue("fanout.A"); @Bean(name="Bmessage") public Queue BMessage() return new Queue("fanout.B"); @Bean(name="Cmessage") public Queue CMessage() return new Queue("fanout.C"); @Bean FanoutExchange fanoutExchange() return new FanoutExchange("fanoutExchange");//配置广播路由器 @Bean Binding bindingExchangeA(@Qualifier("Amessage") Queue AMessage,FanoutExchange fanoutExchange) return BindingBuilder.bind(AMessage).to(fanoutExchange); @Bean Binding bindingExchangeB(@Qualifier("Bmessage") Queue BMessage, FanoutExchange fanoutExchange) return BindingBuilder.bind(BMessage).to(fanoutExchange); @Bean Binding bindingExchangeC(@Qualifier("Cmessage") Queue CMessage, FanoutExchange fanoutExchange) return BindingBuilder.bind(CMessage).to(fanoutExchange);
发送端使用如下代码发送:
接收端监听器配置如下:
@Component public class HelloReceive @RabbitListener(queues="fanout.A") public void processA(String str1) System.out.println("ReceiveA:"+str1); @RabbitListener(queues="fanout.B") public void processB(String str) System.out.println("ReceiveB:"+str); @RabbitListener(queues="fanout.C") public void processC(String str) System.out.println("ReceiveC:"+str);
运行测试代码,发现三个监听器都接收到了数据,测试成功!
rabbitmq——springboot整合rabbitmq(代码片段)
springboot整合RabbitMQ环境搭建一、创建一个Springboot项目二、导入相关依赖或者不勾选SpringforRabbitMQ,自己导入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</a 查看详情
springboot整合rabbitmq报错
org.springframework.amqp.AmqpAuthenticationException:com.rabbitmq.client.AuthenticationFailureException:ACCESS_REFUSED-LoginwasrefusedusingauthenticationmechanismPLAIN.Fordetailsseethebrokerlogfile. 查看详情
springboot2.x-springboot整合amqp之rabbitmq
文章目录SpringBoot2.X-SpringBoot整合AMQP之RabbitMQRabbitMQ简介引入依赖编写配置编写接口启用Rabbit注解消息监听消息测试SpringBoot2.X-SpringBoot整合AMQP之RabbitMQSpringBoot2整合RabbitMQ案例。RabbitMQ简介简介RabbitMQ是一个由erlang开发的AMQP(AdvanvedMess... 查看详情
springboot整合rabbitmq
1整合RabbitMQ1.1RabbitMQ的相关概念组成部分队列(Queue)声明队列```java@BeanpublicQueueaddUserQueue(){returnnewQueue("demo-user-add");}```交换机(Exchange)用于转发消息,但是它不会做存储,如果没有Queuebind到Exchange的话,它会直接丢弃掉Producer发... 查看详情
springboot整合rabbitmq
记录学习过程从我做起。搞个备份有备无患加入依赖配置yml文件注入RabbitTemplate测试生产者功能消费端实现消费端结果 查看详情
springboot整合rabbitmq(代码片段)
SpringBoot整合RabbitMQ搭建环境创建测试项目:test_rabbitmq_boot添加依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi& 查看详情
spring和springboot整合rabbitmq(代码片段)
Spring和SpringBoot整合RabbitMQ一、Spring整合RabbitMQ1.Producer1.1Config1.1Producer2.Consumer拉取推送消息2.1Config2.2Consumer3.Consumer消息监听(用于推消息)3.1Config3.2MessageListener3.3Consumer二、SpringBoot整合Ra 查看详情
springboot整合rabbitmq(新手整合请勿喷)
整合前先在springboot引入rabbitMqJAR包,版本号可以为自己自定义,本项目是跟随springboot的版本<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></ 查看详情
企业级springboot教程(十五)springboot整合rabbitmq
...务器,并且通过它怎么去发送和接收消息。我将构建一个springboot工程,通过RabbitTemplate去通过MessageListenerAdapter去订阅一个POJO类型的消息。准备工作15minIDEAmaven3.0在开始构建项目之前,机器需要安装rabbitmq,你可以去官网下载,htt... 查看详情
springboot整合rabbitmq入门~~
SpringBoot整合RabbitMQ 入门2020-01-12 创建生产者类,并且在yml配置文件中配置5要素连接MQyml配置文件 spring: & 查看详情
springboot整合rabbitmq
当前社区活跃度最好的消息中间件就是kafka和rabbitmq了,前面对kafaka的基础使用做了一些总结,最近开始研究rabbitmq,查看了很多资料,自己仿着写了一些demo,在博客园记录一下。rabbitmq基础知识 关于rabbitmq基础知识,可... 查看详情
[rabbitmq]整合springboot(代码片段)
整合SpringBoot创建项目引入依赖<dependencies><!--RabbitMQ依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>< 查看详情
springboot整合rabbitmq
SpringBoot整合RabbitMQ公司最近在开发CRM系统的时候,需要将ERP的订单数据实时的传输到CRM系统中,但是由于每天的订单量特别大,采用实时获取后并存储到数据库中,接口的相应速度较慢,性能较差。经过经过多方位评估采用在数... 查看详情
springboot学习——springboot快速整合rabbitmq
RabbitMQ消息队列@[toc]简介优点erlang开发,并发能力强。社区活跃,使用的人多,稳定性较强。延时低缺点erlang语言开发的,国内精通的不多,日后定制开发困难。RabbitMQ工作模式1,"HelloWorld!"模式简单模式是RabbitMQ最简单入... 查看详情
springboot整合消息队列——rabbitmq
...息路由到bindingkey与routingkey模式匹配的队列中。这里基于springboot整合消息队列,测试这 查看详情
springboot整合rabbitmq之发送接收消息实战
实战前言前几篇文章中,我们介绍了SpringBoot整合RabbitMQ的配置以及实战了Spring的事件驱动模型,这两篇文章对于我们后续实战RabbitMQ其他知识要点将起到奠基的作用的。特别是Spring的事件驱动模型,当我们全篇实战完毕RabbitMQ并... 查看详情
springboot整合rabbitmq
本文序列化和添加package参考:https://www.jianshu.com/p/13fd9ff0648dRabbitMq安装[root@topcheer~]#dockerimagesREPOSITORYTAGIMAGEIDCREATEDSIZEelasticsearchlatest874179f1960311daysago771MBsprin 查看详情
消息队列mq——springboot整合rabbitmq(代码片段)
...bitMQ的介绍、安装以及管理页面的使用消息队列MQ(二)——SpringBoot整合RabbitMQ文章目录系列文章目录前言一、简介二、搭建工程1.创建P系统(生产者)1.1yml配置RabbitMQ属性2.创建C系统(消费者)2.1yml配置RabbitMQ属性三、实操RabbitMQ五种工... 查看详情