RabbitMQ之Exchange的4种类型详解
上一篇介绍了如何在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); } } } }
浙公网安备 33010602011771号