RabbitMQ Exchange类型详解
前言
在上一篇文章中,我们知道了RabbitMQ的消息流程如下:

但在具体的使用中,我们还需知道exchange的类型,因为不同的类型对应不同的队列和路由规则。
在rabbitmq中,exchange有4个类型:direct,topic,fanout,header。
direct exchange
此类型的exchange路由规则很简单:
exchange在和queue进行binding时会设置routingkey
channel.QueueBind(queue: "create_pdf_queue",
exchange: "pdf_events",
routingKey: "pdf_create",
arguments: null);
然后我们在将消息发送到exchange时会设置对应的routingkey:
channel.BasicPublish(exchange: "pdf_events",
routingKey: "pdf_create",
basicProperties: properties,
body: body);
在direct类型的exchange中,只有这两个routingkey完全相同,exchange才会选择对应的binging进行消息路由。
具体的流程如下:

通过代码可以会理解好一点:
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
// Direct类型的exchange, 名称 pdf_events
channel.ExchangeDeclare(exchange: "pdf_events",
type: ExchangeType.Direct,
durable: true,
autoDelete: false,
arguments: null);
// 创建create_pdf_queue队列
channel.QueueDeclare(queue: "create_pdf_queue",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
//创建 pdf_log_queue队列
channel.QueueDeclare(queue: "pdf_log_queue",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
//绑定 pdf_events --> create_pdf_queue 使用routingkey:pdf_create
channel.QueueBind(queue: "create_pdf_queue",
exchange: "pdf_events",
routingKey: "pdf_create",
arguments: null);
//绑定 pdf_events --> pdf_log_queue 使用routingkey:pdf_log
channel.QueueBind(queue: "pdf_log_queue",
exchange: "pdf_events",
routingKey: "pdf_log",
arguments: null);
var message = "Demo some pdf creating...";
var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
//发送消息到exchange :pdf_events ,使用routingkey: pdf_create
//通过binding routinekey的比较,次消息会路由到队列 create_pdf_queue
channel.BasicPublish(exchange: "pdf_events",
routingKey: "pdf_create",
basicProperties: properties,
body: body);
message = "pdf loging ...";
body = Encoding.UTF8.GetBytes(message);
properties = channel.CreateBasicProperties();
properties.Persistent = true;
//发送消息到exchange :pdf_events ,使用routingkey: pdf_log
//通过binding routinekey的比较,次消息会路由到队列 pdf_log_queue
channel.BasicPublish(exchange: "pdf_events",
routingKey: "pdf_log",
basicProperties: properties,
body: body);
}
topic exchange
此类型exchange和上面的direct类型差不多,但direct类型要求routingkey完全相等,这里的routingkey可以有通配符:'*','#'.
其中'*'表示匹配一个单词, '#'则表示匹配没有或者多个单词

如上图第一个binding:
- exchange: agreements
- queue A: berlin_agreements
- binding routingkey: agreements.eu.berlin.#
第二个binding:
- exchange: agreements
- queue B: all_agreements
- binding routingkey: agreements.#
第三个binding:
- exchange: agreements
- queue c: headstore_agreements
- binding routingkey: agreements.eu.*.headstore
所以如果我们消息的routingkey为agreements.eu.berlin那么符合第一和第二个binding,但最后一个不符合,具体的代码如下:
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
// Topic类型的exchange, 名称 agreements
channel.ExchangeDeclare(exchange: "agreements",
type: ExchangeType.Topic,
durable: true,
autoDelete: false,
arguments: null);
// 创建berlin_agreements队列
channel.QueueDeclare(queue: "berlin_agreements",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
//创建 all_agreements 队列
channel.QueueDeclare(queue: "all_agreements",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
//创建 headstore_agreements 队列
channel.QueueDeclare(queue: "headstore_agreements",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
//绑定 agreements --> berlin_agreements 使用routingkey:agreements.eu.berlin.#
channel.QueueBind(queue: "berlin_agreements",
exchange: "agreements",
routingKey: "agreements.eu.berlin.#",
arguments: null);
//绑定 agreements --> all_agreements 使用routingkey:agreements.#
channel.QueueBind(queue: "all_agreements",
exchange: "agreements",
routingKey: "agreements.#",
arguments: null);
//绑定 agreements --> headstore_agreements 使用routingkey:agreements.eu.*.headstore
channel.QueueBind(queue: "headstore_agreements",
exchange: "agreements",
routingKey: "agreements.eu.*.headstore",
arguments: null);
var message = "hello world";
var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
//发送消息到exchange :agreements ,使用routingkey: agreements.eu.berlin
//agreements.eu.berlin 匹配 agreements.eu.berlin.# 和agreements.#
//agreements.eu.berlin 不匹配 agreements.eu.*.headstore
//最终次消息会路由到队里:berlin_agreements(agreements.eu.berlin.#) 和 all_agreements(agreements.#)
channel.BasicPublish(exchange: "agreements",
routingKey: "agreements.eu.berlin",
basicProperties: properties,
body: body);
}
fanout exchange
此exchange的路由规则很简单直接将消息路由到所有绑定的队列中,无须对消息的routingkey进行匹配操作。

header exchange
此类型的exchange和以上三个都不一样,其路由的规则是根据header来判断,其中的header就是以下方法的arguments参数:
Dictionary<string, object> aHeader = new Dictionary<string, object>();
aHeader.Add("format", "pdf");
aHeader.Add("type", "report");
aHeader.Add("x-match", "all");
channel.QueueBind(queue: "queue.A",
exchange: "agreements",
routingKey: string.Empty,
arguments: aHeader);
其中的x-match为特殊的header,可以为all则表示要匹配所有的header,如果为any则表示只要匹配其中的一个header即可。
在发布消息的时候就需要传入header值:
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
Dictionary<string, object> mHeader1 = new Dictionary<string, object>();
mHeader1.Add("format", "pdf");
mHeader1.Add("type", "report");
properties.Headers = mHeader1;
具体的规则可以看以下代码:
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
// Headers类型的exchange, 名称 agreements
channel.ExchangeDeclare(exchange: "agreements",
type: ExchangeType.Headers,
durable: true,
autoDelete: false,
arguments: null);
// 创建queue.A队列
channel.QueueDeclare(queue: "queue.A", durable: true, exclusive: false, autoDelete: false, arguments: null);
//创建 queue.B 队列
channel.QueueDeclare(queue: "queue.B", durable: true, exclusive: false, autoDelete: false, arguments: null);
//创建 queue.C 队列
channel.QueueDeclare(queue: "queue.C", durable: true, exclusive: false, autoDelete: false, arguments: null);
//绑定 agreements --> queue.A 使用arguments (format=pdf, type=report, x-match=all)
Dictionary<string, object> aHeader = new Dictionary<string, object>();
aHeader.Add("format", "pdf");
aHeader.Add("type", "report");
aHeader.Add("x-match", "all");
channel.QueueBind(queue: "queue.A",
exchange: "agreements",
routingKey: string.Empty,
arguments: aHeader);
//绑定 agreements --> queue.B 使用arguments (format=pdf, type=log, x-match=any)
Dictionary<string, object> bHeader = new Dictionary<string, object>();
bHeader.Add("format", "pdf");
bHeader.Add("type", "log");
bHeader.Add("x-match", "any");
channel.QueueBind(queue: "queue.B",
exchange: "agreements",
routingKey: string.Empty,
arguments: bHeader);
//绑定 agreements --> queue.C 使用arguments (format=zip, type=report, x-match=all)
Dictionary<string, object> cHeader = new Dictionary<string, object>();
cHeader.Add("format", "zip");
cHeader.Add("type", "report");
cHeader.Add("x-match", "all");
channel.QueueBind(queue: "queue.C",
exchange: "agreements",
routingKey: string.Empty,
arguments: cHeader);
string message1 = "hello world";
var body = Encoding.UTF8.GetBytes(message1);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
Dictionary<string, object> mHeader1 = new Dictionary<string, object>();
mHeader1.Add("format", "pdf");
mHeader1.Add("type", "report");
properties.Headers = mHeader1;
//此消息路由到 queue.A 和 queue.B
//queue.A 的binding (format=pdf, type=report, x-match=all)
//queue.B 的binding (format = pdf, type = log, x - match = any)
channel.BasicPublish(exchange: "agreements",
routingKey: string.Empty,
basicProperties: properties,
body: body);
string message2 = "hello world";
body = Encoding.UTF8.GetBytes(message2);
properties = channel.CreateBasicProperties();
properties.Persistent = true;
Dictionary<string, object> mHeader2 = new Dictionary<string, object>();
mHeader2.Add("type", "log");
properties.Headers = mHeader2;
//x-match 配置queue.B
//queue.B 的binding (format = pdf, type = log, x-match = any)
channel.BasicPublish(exchange: "agreements",
routingKey: string.Empty,
basicProperties: properties,
body: body);
string message3= "hello world";
body = Encoding.UTF8.GetBytes(message3);
properties = channel.CreateBasicProperties();
properties.Persistent = true;
Dictionary<string, object> mHeader3 = new Dictionary<string, object>();
mHeader3.Add("format", "zip");
properties.Headers = mHeader3;
//配置失败,不会被路由
channel.BasicPublish(exchange: "agreements",
routingKey: string.Empty,
basicProperties: properties,
body: body);
}
总计
以上就是exchange 类型的总结,一般来说direct和topic用来具体的路由消息,如果要用广播的消息一般用fanout的exchange。
header类型用的比较少,但还是知道一点好。
RabbitMQ Exchange类型详解的更多相关文章
- RabbitMQ基础知识详解
什么是MQ? MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.MQ是消费-生产者模型的一个典型的代表,一端往消息队列中不断写入消息,而另一端则可以读取队列中 ...
- RabbitMQ,Apache的ActiveMQ,阿里RocketMQ,Kafka,ZeroMQ,MetaMQ,Redis也可实现消息队列,RabbitMQ的应用场景以及基本原理介绍,RabbitMQ基础知识详解,RabbitMQ布曙
消息队列及常见消息队列介绍 2017-10-10 09:35操作系统/客户端/人脸识别 一.消息队列(MQ)概述 消息队列(Message Queue),是分布式系统中重要的组件,其通用的使用场景可以 ...
- C++11 并发指南六(atomic 类型详解四 C 风格原子操作介绍)
前面三篇文章<C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)>.<C++11 并发指南六( <atomic> 类型详解二 std::at ...
- C++11 并发指南六( <atomic> 类型详解二 std::atomic )
C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍) 一文介绍了 C++11 中最简单的原子类型 std::atomic_flag,但是 std::atomic_flag ...
- C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解 ...
- C++11 并发指南六(atomic 类型详解三 std::atomic (续))
C++11 并发指南六( <atomic> 类型详解二 std::atomic ) 介绍了基本的原子类型 std::atomic 的用法,本节我会给大家介绍C++11 标准库中的 std: ...
- 服务启动项 Start类型详解
注册表的服务启动项 Start类型详解 HKLM\SYSTEM\CurrentControlSet\services\ 下的服务项.不论有没有在services.msc服务管理控制台中显示,在注册表中 ...
- c# WebApi之接口返回类型详解
c# WebApi之接口返回类型详解 https://blog.csdn.net/lwpoor123/article/details/78644998
- C++之string类型详解
C++之string类型详解 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个泛型类出现,他集成的操作函 ...
随机推荐
- 【POJ3037】Skiing 最短路
题意: 有个n*m的滑雪场,bessie要从(1,1)滑到(n,m),问最小时间. 起始有一个速度v,然后每从一个点A到一个点B(仅仅能上下左右走,每次一格),速度就会乘上2^(权值A-权值B). 然 ...
- CI框架 .htaccess 隐藏url在index.php解决方案
CodeIgniter(下面简称"CI")是一款国外优秀的PHP轻量级MVC框架,它支持PHP4和PHP5.是开发中小型可拓展性需求高的Web应用程序的利器.眼下你所见到的这个博客 ...
- 安装uBuntu操作系统 - 初学者系列 - 学习者系列文章
uBuntu是一款不错的Linux操作系统,在上面的应用软件不少,就是说它的支持率挺高.下面就对这款操作系统的安装做下介绍. 1. 下载uBuntu安装文件 打开中文页面.http://www.ub ...
- web开发中的多线程死锁问题,避免死锁
1.什么是死锁,产生死锁的原因,和产生死锁的必要条件 所谓死锁(DeadLock),是指多个进程或线程在运行过程中因争夺资源而造成的一种僵局,当进程或线程处于僵局时,若无外力作用,它们将无法再向前推进 ...
- 在希望的田野上--生物柴油(Biodiesel)光明的未来
请看下图: 这是科学家Bernie Tao教授给美国Purdue大学的学生们出的题目"有关大豆.谷物产品的创新竞赛",实质上,就是鼓舞研究.开发及应用生物柴油(Biodiesel) ...
- knockout笔记
根据汤姆大叔博客总结-笔记: =============<script type="text/javascript"> $(function () { var View ...
- C#:vs2010无法打开vs2012创建的项目
vs低版本打开高版本创建的项目时会提示"选择的文件是解决方案文件,但是用此应用程序的较新版本创建的,无法打开" 解决办法: 写字板打开解决方案sln文件 将其改成你现在用的vs版本 ...
- cefsharp实现javascript回调C#方法
在构建完WebView webView = new WebView(url)后,即可调用RegisterJsObject方法来注册一个js对象,从而前端的javascript就可以访问这个对象,调用定 ...
- C#编程总结
C#编程总结--总目录 多年的C#实战经历,希望通过一个系列课程对C#编程做系统总结. 总结过去,展望未来.新的一年,新的征程,新的开始! 希望我们在2014梦想成真,马到成功! 1.C#编程总结(一 ...
- 【ios开发】iOS App测试方案
之前IOS测试一半都是采用的Testflight,但是2014.2.19日以后,testflight已经不提供新注册的用户下载SDK了. 但是不用担心我们还可以采用其他几种方案. 1)Ubertest ...