设为首页 收藏本站
查看: 593|回复: 0

[经验分享] RabbitMQ之Exchange的4种类型详解

[复制链接]

尚未签到

发表于 2017-7-2 09:25:20 | 显示全部楼层 |阅读模式
  上一篇介绍了如何在Windows下搭配RabbitMQ环境,现在就来写一下简单版 队列的生产和消费。
  通用记日志方法:



        /// <summary>
/// 记日志
/// </summary>
/// <param name="content"></param>
private static void WriteLog(string content)
{
string path = @"E:\rabbitmqlog";
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path + "/log.txt", true, Encoding.UTF8))
{
sw.WriteLine(content);
}
}
  简单版生产者:



private static void SendMessageByPersist()
{
ConnectionFactory factory = new ConnectionFactory()
{
Uri = "amqp://localhost:15672/",
UserName = "guest",
Password = "guest",
Port = AmqpTcpEndpoint.UseDefaultPort
};
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("test", true, false, false, null); 持久化
var properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;  持久化
string message = string.Empty;
for (int i = 0; i < 20; i++)
{
message = "first_" + i.ToString();
byte[] byteMes = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", "test", properties, byteMes);
WriteLog("第" + (i + 1).ToString() + "次发送信息" + message + "到队列first,发送时间为   " + DateTime.Now.ToString("yyyyMMddHHmmss"));
}
WriteLog("=====================================================================================");
}
}
}
  通用版消费者:



private static void ReceiveMessageByPersist()
{
ConnectionFactory factory = new ConnectionFactory()
{
Uri = "amqp://localhost:15672/",
UserName = "guest",
Password = "guest",
Port = AmqpTcpEndpoint.UseDefaultPort
};
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
//channel.QueueDeclare("test", true, false, false, null);
channel.BasicQos(0, 1, false);
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume("test", false, consumer);  test 对列名
int i = 1;
while (true)
{
var item = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
byte[] body = item.Body;
string message = Encoding.UTF8.GetString(body);
WriteLog("接收到队列中的第" + i + "条数据内容    " + message + "     接受时间为    " + DateTime.Now.ToString("yyyyMMddHHmmss"));
i++;
channel.BasicAck(item.DeliveryTag, false);  手动应答
}
}
}
}
  1、Direct类型
  生产者:需要注意的是,Direct类型可以自己绑定自定义的路由key,一旦绑定了路由key,那么在添加数据的时候,第二个参数就必须是自定义的路由key,不可以是对列名,如果绑定的时候路由key为空,那么添加数据的时候,第二个参数必须为对列名,大多数情况下,我们都使用前者,切记一定要注意这个流程。



   private static void SendMessageByDirectPersist()
{
ConnectionFactory factory = new ConnectionFactory()
{
Uri = "amqp://localhost:15672/",
UserName = "guest",
Password = "guest",
Port = AmqpTcpEndpoint.UseDefaultPort
};
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare("directpub", "direct", true, false, null);
channel.QueueDeclare("dir_a", true, false, false, null);
channel.QueueBind("dir_a", "directpub", "dir_rout_a");
channel.QueueDeclare("dir_b", true, false, false, null);
channel.QueueBind("dir_b", "directpub", "dir_rout_b");
channel.QueueDeclare("dir_c", true, false, false, null);
channel.QueueBind("dir_c", "directpub", "dir_rout_c");
var properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;
string message = string.Empty;
for (int i = 0; i < 20; i++)
{
message = "first_" + i.ToString();
byte[] byteMes = Encoding.UTF8.GetBytes(message);
      channel.BasicPublish("directpub", "dir_rout_b", properties, byteMes);
//channel.BasicPublish("directpub", "dir_b", properties, byteMes);//没用,如果已经定义了routekey,那么再使用对列名来添加数据则会无用
WriteLog("广播发送方式中 第" + (i + 1).ToString() + "次发送信息" + message + "到队列first,发送时间为   " + DateTime.Now.ToString("yyyyMMddHHmmss"));
}
WriteLog("=====================================================================================");
}
}
}
  2、Fanout类型
  生产者:需要注意的是,广播类型没有路由key,因为它的原理是把生产者要发送到队列里的数据给存在当前信道里的每一个队列都发一份,一模一样的复制到每个队列。切记一定要注意这个流程。



private static void SendMessageByFanoutPersist()
{
ConnectionFactory factory = new ConnectionFactory()
{
Uri = "amqp://localhost:15672/",
UserName = "guest",
Password = "guest",
Port = AmqpTcpEndpoint.UseDefaultPort
};
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare("fanoutpub", "fanout", true, false, null);
   channel.QueueDeclare("fan_a", true, false, false, null);
channel.QueueBind("fan_a", "fanoutpub", "");
channel.QueueDeclare("fan_b", true, false, false, null);
channel.QueueBind("fan_b", "fanoutpub", "");
channel.QueueDeclare("fan_c", true, false, false, null);
channel.QueueBind("fan_c", "fanoutpub", "");
var properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;
string message = string.Empty;
for (int i = 0; i < 20; i++)
{
message = "first_" + i.ToString();
byte[] byteMes = Encoding.UTF8.GetBytes(message);
   channel.BasicPublish("fanoutpub", "", properties, byteMes);
WriteLog("广播发送方式中 第" + (i + 1).ToString() + "次发送信息" + message + "到队列first,发送时间为   " + DateTime.Now.ToString("yyyyMMddHHmmss"));
}
WriteLog("=====================================================================================");
}
}
}
  3、Topic类型
  生产者:需要注意的是,对应队列的路由key是可以匹配的,相比Direct类型来说就放宽了命中的队列,其中 *代表:0个1个    #代表:0个1个多个,等到真正添加对列的时候输入指定的路由,然后去各个声明并且绑定了routingKey的对列名利进行匹配,命中的话 就填充到该对列。切记一定要注意这个流程。



private static void SendMessageByTopicPersist()
{
ConnectionFactory factory = new ConnectionFactory()
{
Uri = "amqp://localhost:15672/",
UserName = "guest",
Password = "guest",
Port = AmqpTcpEndpoint.UseDefaultPort
};
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare("topicpub", "topic", true, false, null);
channel.QueueDeclare("a", true, false, false, null);
channel.QueueBind("a", "topicpub", "*.test");
channel.QueueDeclare("b", true, false, false, null);
channel.QueueBind("b", "topicpub", "*.test.*");
channel.QueueDeclare("c", true, false, false, null);
channel.QueueBind("c", "topicpub", "test.#");
channel.QueueDeclare("d", true, false, false, null);
channel.QueueBind("d", "topicpub", "#.test");
var properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;
string message = string.Empty;
for (int i = 0; i < 20; i++)
{
message = "first_" + i.ToString();
byte[] byteMes = Encoding.UTF8.GetBytes(message);
//channel.BasicPublish("topicpub", "test.1", properties, byteMes);   c增加
//channel.BasicPublish("topicpub", ".test.1", properties, byteMes);  b增加
//channel.BasicPublish("topicpub", ".test.", properties, byteMes);   b增加
//channel.BasicPublish("topicpub", ".test", properties, byteMes);    a,d增加
//channel.BasicPublish("topicpub", "1.2.test", properties, byteMes); d增加
WriteLog("广播发送方式中 第" + (i + 1).ToString() + "次发送信息" + message + "到队列first,发送时间为   " + DateTime.Now.ToString("yyyyMMddHHmmss"));
}
WriteLog("=====================================================================================");
}
}
}
  Headers类型:
  生产者:需要注意的是,Headers方式是在绑定对列的时候将匹配的条件以字典型数据当参数传入,然后在生产的时候再将要匹配的条件以字典型数据当参数传入来进行匹配。切记一定要注意这个流程。



private static void SendMessageByHeaders()
{
ConnectionFactory factory = new ConnectionFactory()
{
Uri = "amqp://localhost:15672/",
UserName = "guest",
Password = "guest",
Port = AmqpTcpEndpoint.UseDefaultPort
};
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare("headerspub", "headers", true, false, null);
channel.QueueDeclare("headers_a", true, false, false, null);
   Dictionary<string, object> aHeader = new Dictionary<string, object>();
aHeader.Add("format", "pdf");
aHeader.Add("type", "report");
aHeader.Add("x-match", "all");
channel.QueueBind("headers_a", "headerspub", "", aHeader);

channel.QueueDeclare("headers_b", true, false, false, null);
  Dictionary<string, object> bHeader = new Dictionary<string, object>();
bHeader.Add("format", "pdf");
bHeader.Add("type", "log");
bHeader.Add("x-match", "any");
channel.QueueBind("headers_b", "headerspub", "", bHeader);

channel.QueueDeclare("headers_c", true, false, false, null);
   Dictionary<string, object> cHeader = new Dictionary<string, object>();
cHeader.Add("format", "zip");
cHeader.Add("type", "report");
cHeader.Add("x-match", "all");
channel.QueueBind("headers_c", "headerspub", "", cHeader);
var properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;
string message = string.Empty;
for (int i = 0; i < 20; i++)
{
message = "first_" + i.ToString();
byte[] byteMes = Encoding.UTF8.GetBytes(message);
Dictionary<string, object> mHeader1 = new Dictionary<string, object>();
//mHeader1.Add("format", "zip");
//mHeader1.Add("type", "report");
//properties.Headers = mHeader1;
//此消息路由到 headers_c
//headers_a 的binding (format=pdf, type=report, x-match=all)
//headers_b 的binding (format = pdf, type = log, x - match = any)
//headers_c 的binding (format = zip, type = report, x - match = all)
//mHeader1.Add("format", "pdf");
//mHeader1.Add("type", "report");
//properties.Headers = mHeader1;
//此消息路由到 headers_a和headers_b
//headers_a 的binding (format=pdf, type=report, x-match=all)
//headers_b 的binding (format = pdf, type = log, x - match = any)
//headers_c 的binding (format = zip, type = report, x - match = all)

mHeader1.Add("format", "pdf");
mHeader1.Add("type", "log");
properties.Headers = mHeader1;
//此消息路由到 headers_b
//headers_a 的binding (format=pdf, type=report, x-match=all)
//headers_b 的binding (format = pdf, type = log, x - match = any)
//headers_c 的binding (format = zip, type = report, x - match = all)
channel.BasicPublish("headerspub", "", properties, byteMes);
}
}
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-390248-1-1.html 上篇帖子: Exchange: How to get Mailbox size in Exchange Shell? 下篇帖子: C#使用PowerShell 操作Exchange
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表