RabbitMQ消息队列(七)-通过fanout模式将消息推送到多个Queue中(.Net Core版)
前面第六章我们使用的是direct直连模式来进行消息投递和分发。本章将介绍如何使用fanout模式将消息推送到多个队列。
有时我们会遇到这样的情况,多个功能模块都希望得到完整的消息数据。例如一个log的消息,一个我们希望输出在屏幕上实时监控,另外一个用户持久化日志。这时就可以使用fanout模式。fanout模式模式不像direct模式通过routingkey来进行匹配,而是会把消息发送到所以的已经绑定的队列中。

新建FanoutProduct用来发布消息。FanoutCustomerA和FanoutCustomerB用来订阅不同队列消费消息。
FanoutProduct代码:
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events; namespace FanoutProduct
{
class Program
{
static void Main(string[] args)
{
String exchangeName = "wytExchange";
String message = "Hello World!"; ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "192.168.63.129";
factory.Port = ;
factory.VirtualHost = "/wyt";
factory.UserName = "wyt";
factory.Password = "wyt"; using (IConnection connection=factory.CreateConnection())
{
using (IModel channel=connection.CreateModel())
{
channel.ExchangeDeclare(exchange: exchangeName, type: "fanout", durable: true, autoDelete: false, arguments: null); IBasicProperties properties = channel.CreateBasicProperties();
properties.Persistent = true; Task.Run(() =>
{
while (true)
{
for (int i = ; i < ; i++)
{
Byte[] body = Encoding.UTF8.GetBytes(message + i);
channel.BasicPublish(exchange: exchangeName, routingKey: "", basicProperties: properties, body: body);
}
Thread.Sleep();
}
}).Wait(); Console.WriteLine(" [x] Sent {0}", message);
}
} Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine(); }
}
}
FanoutCustomerA与FanoutCustomerB(代码相同):
using System;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events; namespace FanoutCustomerA
{
class Program
{
static void Main(string[] args)
{
String exchangeName = "wytExchange"; ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "192.168.63.129";
factory.Port = ;
factory.VirtualHost = "/wyt";
factory.UserName = "wyt";
factory.Password = "wyt"; using (IConnection connection=factory.CreateConnection())
{
using (IModel channel=connection.CreateModel())
{
channel.ExchangeDeclare(exchange: exchangeName, type: "fanout", durable: true, autoDelete: false, arguments: null); String queueName = channel.QueueDeclare().QueueName; channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: "", arguments: null); EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
Byte[] body = ea.Body;
String message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] {0}", message);
}; channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine(); }
}
}
}
}

可以看到FanoutCustomerA和FanoutCustomerB收到的消息完全一致。注意以上代码FanoutProduct中并没有新建队列,所以先运行FanoutCustomerA和FanoutCustomerB,如果先运行FanoutProduct因为找不到绑定的队列数据就会丢失。
还有一种情况我们有可能随时增加一项处理机制,如果在声明queue时不指定名字,那么RabbitMQ会随机为我们生成一个名字,如果不指定queue为持久化队列那在消息为空并且订阅者为0时自动删除该队列。这样Queue挥之即来呼之即去。
String queueName = channel.QueueDeclare().QueueName;
RabbitMQ消息队列(七)-通过fanout模式将消息推送到多个Queue中(.Net Core版)的更多相关文章
- (七)RabbitMQ消息队列-通过fanout模式将消息推送到多个Queue中
原文:(七)RabbitMQ消息队列-通过fanout模式将消息推送到多个Queue中 前面第六章我们使用的是direct直连模式来进行消息投递和分发.本章将介绍如何使用fanout模式将消息推送到多 ...
- (八)RabbitMQ消息队列-通过Topic主题模式分发消息
原文:(八)RabbitMQ消息队列-通过Topic主题模式分发消息 前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过 ...
- Spring Boot+RabbitMQ 通过fanout模式实现消息接收(支持消费者多实例部署)
本文章适用的场景:同一条消息可以被多个消费者同时消费.注意:当消费者多实例部署时,会轮询消费消息.网上有大量的的案例展示:P生产一条消息,消费者服务C中建立Q1和Q2两个队列共同消费.但极少的材料展示 ...
- rabbitmq 延时队列 插件方式实现 每条消息都延时自己时间
上篇文章的延时是加到队列上的 通过死信过时推送 ,缺点就是不能每条消息定义自己的过时时间而且每次有新的过时时间,要新建一个交换机和队列 https://www.cnblogs.com/brady-wa ...
- RabbitMQ消息队列(五):Routing 消息路由
上篇文章中,我们构建了一个简单的日志系统.接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log.比如我们希望只有error的log才保存到磁盘上. 1. Bindings ...
- redis实现消息队列(七)
1. 介绍 redis有一个数据类型叫list(列表),它的每个子元素都是 string 类型的双向链表.我们可以通过 push,pop 操作从链表的头部或者尾部添加删除元素.这使得 list 既可以 ...
- RabbitMQ消息队列(五):Routing 消息路由 2[原]
上一篇文章使用的是Direct的Exchange,但是没有指定Queue的名字,这样只能是先运行Consumer之后,Producer在运行发消息Consumer才能收到,否则先运行Producer发 ...
- RabbitMQ消息队列(五):Routing 消息路由[转]
上篇文章中,我们构建了一个简单的日志系统.接下来,我们将丰富它:能够使用不同的severity(严重程度)来监听不同等级的log.比如我们希望只有error的log才保存到磁盘上. 1. Bindin ...
- (转)RabbitMQ消息队列(五):Routing 消息路由
上篇文章中,我们构建了一个简单的日志系统.接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log.比如我们希望只有error的log才保存到磁盘上. 1. Bindings绑定 上 ...
随机推荐
- 雕刻机制作 PCB 指南
之前使用过感光蓝油制作过 PCB,虽然感光法精度高,但个人制作耗时耗力,发给厂家周期又很长.看到国外的网友使用雕刻机制作 PCB 视频之后.几番周折之后还是成功了.有感于网上几乎没有一份完整的雕刻机 ...
- nginx 静态目录配置规则
1.子目录匹配 如下配置 location / { root /data/www; } 访问http://127.0.0.1/时,配匹配/data/www 访问http://127.0.0.1/ima ...
- node01
---恢复内容开始--- 1.node初体验 安装完成node,写好相应的js代码后,在cmd中node 文件名即可完成编译执行过程. 2.尝试使用node搭建一个简单服务器 //引入http模块 c ...
- weex 学习: 添加图标(使用阿里吧吧-icon)
添加图标(使用阿里吧吧-icon) <text slot="left" class="header-left"></text> i ...
- python错误和异常
一:语法错误syntax errors 熟悉语法! 二:异常 ①打印错误信息时,异常的类型作为异常的内置名显示,并以调用栈的形式显示具体信息 ②常见的异常: ...
- ES 6 proimse &&iterator &&Generator函数 &&async
1.proimse 异步调用function getData(){ let promise =new Promise((resolve,reject)); let xmlHttp =new XMLHt ...
- HTML标签 按功能排序
按功能类别排列 New : HTML5 中的新标签. 基础 标签 描述 <!DOCTYPE> 定义文档类型. <html> 定义 HTML 文档. <title> ...
- JSP的执行原理
在一个JSP文件第一次被请求时,JSP引擎把该JSP文件转换成为一个Servlet.而这个引擎本身也是一个Servlet.JSP的运行过程如下所示: (1)JSP引擎先把该JSP文件转换成一个Java ...
- [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [Swift]LeetCode349. 两个数组的交集 | Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...