我们已经把环境搭建好了,我们就来编写Provider和Consumer,我们首先 来编写Consumer,然后回到我们的代码当中,我们看一下配置文件,在这个配置文件当中呢,配置了交换器的名称mq.config.exchange=order.fanout
mq.config.queue.sms=order.sms
mq.config.queue.push=order.push配置了短信队列名称,短信平台服务队列名称,这个是push服务队列名称,我们把它更新到自己的服务平台当中,那么这两个名称大家应该知道表示什么了,是我们看到的这两个Queue的名称,我们打算给这两个Queue取这两个名字,然后交换器起的名字,现在我们-直接拷贝direct项目下的,还是日志记录的Receiver,那我们是不是要把这两个Receiver给改一下,首先名称我们不能叫InfoReceiver,比如我们先去做短信服务,SmsReceiver,然后ErrorReceiver,就是Push服务的,PushReceiver,首先名字改好以后呢,接下来我们再看,在@RabbitListener我们该怎么没去做呢,首先第一个value这一块,绑定了队列的名称,现在还是info这里是不是得改了,那这里绑谁呢,看我们的配置文件,这里是不是要绑sms所对应的value,因为我们给队列起名叫sms吗,所以这块是sms,这是要改动的第一个地方,第二个地方是exchange,这个值不用动吧,这个key值不就是他吗,所以这个名字就不用动,我们已经改好了mq.config.exchange=order.fanout关键是type这里,type我们这里用的还是direct,得把它改成fanout,然后再往后看,这里还有一个key的属性,这个key表示的是什么,我们之前也说过,这个key就是标示路由键,其实我们通过写前两个案例,direct和topic的,应该也能体会到路由键的特点,其实路由键的特点就是告诉交换器,现在我要将哪个信息交换到哪个队列当中,那我们现在用的fanout,它是广播模式,所有的队列都要有,既然所有队列都要有了,还有区分这个概念吗,就没有了,没有区分这个概念,那么路由键就不需要存在了,是的,这块大家需要注意,在我们的fanout交换器当中,不需要路由键的,如果你要给他加路由键,不是广播模式了,那就是根据你特定的路由键,将信息发送到指定的队列当中了,就是特定的队列当中了,并不是将消息传递到所有的消息队列当中了,所以一定要注意,我们一定要把路由键给删掉,@RabbitListener(bindings=@QueueBinding(value=@Queue(value="${mq.config.queue.sms}",autoDelete="true"),exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)))这样一个sms的服务就修改完了,然后我们在这里再改一改这个名字,@RabbitHandler
public void process(String msg){System.out.println("Sms........receiver: "+msg);
}然后PushReceiver,这个也是同样的道理,首先需要把消息队列的名称给改一下,这个叫pushvalue="${mq.config.queue.push}"应该是叫push,然后这儿,type=ExchangeTypes.FANOUT,这个应该是fanout,然后他也是没有key,没有路由键,有路由键的反而收不到消息的,我们现在把这个代码粘到笔记当中@RabbitListener(bindings=@QueueBinding(value=@Queue(value="${mq.config.queue.push}",autoDelete="true"),exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT))
)这个是smsReceiver,然后接下来是PushReceiver,这样我们的Consumer就编写完了,我们建立了两个模拟服务的类,一个是SmsReceiver,一个是PushReceiver,然后把它标记一下,在这里,这一块${mq.config.queue.sms}是需要改变的,第二个就是这个type=ExchangeTypes.FANOUT,交换器的类型,这儿也是,value="${mq.config.queue.push}",这块需要改动一下,然后还有交换器的类型type=ExchangeTypes.FANOUT
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>rabbitmq-fanout-consumer</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version><relativePath/> </parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><!-- 这个插件,可以将应用打包成一个可执行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
spring.application.name=rabbitmq-fanout-consumerspring.rabbitmq.host=59.110.158.145
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guestmq.config.exchange=order.fanout
mq.config.queue.sms=order.sms
mq.config.queue.push=order.push
package com.learn;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.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** 消息接收者* @author Administrator* @RabbitListener bindings:绑定队列* @QueueBinding  value:绑定队列的名称*                exchange:配置交换器*                key:路由键* * @Queue value:配置队列名称*        autoDelete:是否是一个可删除的临时队列* * @Exchange value:为交换器起个名称*           type:指定具体的交换器类型*/
@Component
@RabbitListener(bindings=@QueueBinding(value=@Queue(value="${mq.config.queue.sms}",autoDelete="true"),exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)))
public class SmsReceiver {/*** 接收消息的方法。采用消息队列监听机制* @param msg*/@RabbitHandlerpublic void process(String msg){System.out.println("Sms........receiver: "+msg);}
}
package com.learn;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.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** 消息接收者* @author Administrator* @RabbitListener bindings:绑定队列* @QueueBinding  value:绑定队列的名称*                exchange:配置交换器* * @Queue value:配置队列名称*        autoDelete:是否是一个可删除的临时队列* * @Exchange value:为交换器起个名称*           type:指定具体的交换器类型*/
@Component
@RabbitListener(bindings=@QueueBinding(value=@Queue(value="${mq.config.queue.push}",autoDelete="true"),exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)))
public class PushReceiver {/*** 接收消息的方法。采用消息队列监听机制* @param msg*/@RabbitHandlerpublic void process(String msg){System.out.println("Push..........receiver: "+msg);}
}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class RabbitFanoutConsumerApplication {public static void main(String[] args) {SpringApplication.run(RabbitFanoutConsumerApplication.class, args);}
}

Fanout交换器-编写消费者相关推荐

  1. Fanout交换器-编写生产者

    我们再来编写生产者,Provider,这里我们已经把消费者给写好了,一个SmsReceiver,一个PushReceiver,先改一下,这是push receiver@RabbitHandler pu ...

  2. Direct交换器-编写消费者

    我们已经把环境搭建好了,我们先从Consumer开始写起,看一下如何编写Consumer,回到我们的代码当中,然后我们来看一下我们的代码,这个代码是我们copy过来的,QueueConfig我们当时是 ...

  3. Topic交换器-编写消费者

    我们接着来编写Consumer,回到我们的代码当中,打开我们的topic-consumer,配置文件我们已经改完了,先放到这里,看我们的代码,我们先改第一个InfoReceiver,InfoRecei ...

  4. Fanout交换器-搭建环境

    我们讲一下RabbitMQ中的第三种交换器,Fanout交换器,相比上两种我们讲过的交换器,最大的特点它是以广播的模式,来做消息的传递,我们来看一下这样的需求,我这里有一个订单服务,然后还有一个短信服 ...

  5. 使用idea编写消费者,接收生产者的持续日志输出【小案例】(三)

    在   用idea编写代码作为生产者,Kafka接收其[持续]发来的广告日志信息[小案例](二) https://georgedage.blog.csdn.net/article/details/10 ...

  6. Direct交换器-编写生产者

    我们来编写消息的生产者,Provider,回到我们的代码当中,Provider配置文件已经配置好了,在这里Receiver就不要了,只要Sender就可以了,然后我们来看一下Sender的代码,这是我 ...

  7. Topic交换器-编写生产者

    我们已经把环境搭建好了,我们就来编写具体代码,我们首先来编写Provider,消息提供者的代码,编写Provider,我们打开我们的项目,配置文件先关掉,看我们的Provider,Provider这里 ...

  8. RabbitMQ fanout交换机(消费者)

    /** 消息接收* */public class ReceiveLogs01 {//交换机名称public static final String EXCHANGE_NAME = "logs ...

  9. rabbitmq配置文件_RabbitMQ学习

    学习主题:RabbitMQ RabbitMQ安装 什么是RabbitMQ? 是一种消息中间件 什么是Erlang? 他是Erlang的环境依赖 如何在Linux环境中安装RabbitMQ? https ...

最新文章

  1. .NET中做多语言版本的心得 1
  2. layui 下拉选择框可以选择但不显示数据值_你想做的quot;基因药物疾病quot;网络数据在这里!...
  3. Python学习日记(六) 浅深copy
  4. Pytorch:GAN生成对抗网络实现MNIST手写数字的生成
  5. 使用XmlReader读Xml
  6. Mysql(10)——聚合函数的用法
  7. c语言n次方怎么输入_C语言实现斐波拉契数列
  8. Properties 类的使用
  9. python函数和方法的编写原则_跟老齐学Python之传说中的函数编写条规
  10. IoT边缘,你究竟是何方神圣?
  11. 用 js判断 一个数是否是素数(质数)_小学五年级下册数学公式打印版,孩子寒假预习用的上!...
  12. 一台交换机可以有多个ip段吗_网络设备:中继器、集线器、网桥、交换机、路由器、网关的总结!...
  13. Python实战入门到精通第一讲——函数
  14. 最新Android ADT, SDK, SDK_tool等官方下载说明(及时更新)
  15. ideal pom文件安装到maven库中_不装 maven 直接使用 IntelliJ 的插件来把本地 jar 包加入到 maven 仓库...
  16. 微信小程序在手机上预览时出现白屏
  17. 高纬度思考法读书笔记
  18. Programming in lua 中文版
  19. px rpx pt em rem单位
  20. vmware mac安装教程 | 不能全屏的终极原因

热门文章

  1. 【struts2】struts2配置文件—struts.properties
  2. [ZOJ 4024] Peak
  3. config对象的使用及常用方法
  4. SQL Server 2016 行级别权限控制
  5. Micropython教程之TPYBoard制作蓝牙+红外循迹小车
  6. openssl pem 生成公钥和私钥及文件
  7. 微软自夸Edge浏览器的电源效率
  8. 各种版本的ST-LINK仿真器
  9. 汽车之家购买价格PC真正的原因阿拉丁
  10. 马哥教育第二十二天IO模型理论、数据库基础理论、LAMP平台理论基础及mysql部署...