.net使用RabbitMQ
前面的两篇博文算是把RabbitMQ的基础了解了下,今天学习.Net 中RabbitMQ的使用。原本这篇博文是应该上周写的,可在自己使用的过程中出现了一个问题bug:就是在连接服务端时,一直报下面的错误:None of the specified endpoints were reachable.自己也上网查了好久,又问了网友,这里要感谢博客园的园有的帮助。一般使用这些技术的使用都是有一定的步骤,有固有的套路,熟悉了套路,用起来就会很方便。像ADO.Net的五大对象也是,操作数据库先进行连接connection,然后使用command过滤出要选择的数据,使用DataReader或DataSet、DataAdapter,在RabbitMQ的使用当中也有基本固定的步骤。
一、生产者
1.创建连接connection:不管是生产者还是消费者都需要先于RabbitMQ服务器连接,才能进行数据交换
2.创建通道 Channel:生产者、消费者的消息传递是在通道下传递的
3.声明交换器、队列
4.交换器与队列进行绑定
5.通过交换器BasicPublish数据到队列
二、消费者
1.创建连接 :和生产者一样
2.创建通道:和生产者一样
3.声明交换器、队列
4.交换器与队列进行绑定
5.通过BasicGet方法获取队列中的数据
上面一、二是大致的基本步骤,按照大致的步骤来基本不会出现大的问题,其实生产者和消费者的前4个步骤基本一样,主要是第5个步骤,一个是生产BasicPublish发布,一个是获取Get。
三、出现的错误
在ConnectionFactory创建连接对象时出现上面提到的bug:None of the specified endpoints were reachable.这个问了下博客园博友的,博友的回复如下:
这个显然是你的服务端网络不通或者服务没有启用、端口配置不正确导致的,建议你先检查先端口、服务是否启动成功。可以用telnet或者netstat -a等命令,排除本地服务是否有误等问题。排除服务端,再调试客户端,之前自己也有检查了,RabbitMQ也是启动的,用浏览器登录http://localhost:15672也能登录,感觉是相当的纳闷,自己重启了下RabbitMQ之后又试了一下没想到成功了,感觉到好纳闷,现在还不知道是什么原因导致的。好神奇的样子。
四、demo
1.首先在生产者端和消费者端引入RabbitMQ
2.生产者端
using RabbitMQ.Client; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RMQProducter { class Program { /// <summary> /// 连接配置 /// </summary> private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory(){ UserName = "cywadmin", Password = "123456", Port = 5672, VirtualHost= "cywVirtualHost" }; /// <summary> /// 路由名称 /// </summary> const string ExchangeName = "cyw.exchange"; //队列名称 const string QueueName = "cyw.queue"; /// <summary> /// 路由名称 /// </summary> const string TopExchangeName = "topic.cyw.exchange"; //队列名称 const string TopQueueName = "topic.cyw.queue"; static void Main(string[] args) { DirectExchangeSendMsg(); // TopicExchangeSendMsg(); Console.WriteLine("按任意值,退出程序"); Console.ReadKey(); } /// <summary> /// 单点精确路由模式 /// </summary> public static void DirectExchangeSendMsg() { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null); channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); var props = channel.CreateBasicProperties(); props.Persistent = true; string vadata = Console.ReadLine(); while (vadata != "exit") { var msgBody = Encoding.UTF8.GetBytes(vadata); channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, basicProperties: props, body: msgBody); Console.WriteLine(string.Format("***发送时间:{0},发送完成,输入exit退出消息发送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); vadata = Console.ReadLine(); } } } } public static void TopicExchangeSendMsg() { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(TopExchangeName, "topic", durable: false, autoDelete: false, arguments: null); channel.QueueDeclare(TopQueueName, durable: false, autoDelete: false, exclusive: false, arguments: null); channel.QueueBind(TopQueueName, TopExchangeName, routingKey: TopQueueName); //var props = channel.CreateBasicProperties(); //props.Persistent = true; string vadata = Console.ReadLine(); while (vadata != "exit") { var msgBody = Encoding.UTF8.GetBytes(vadata); channel.BasicPublish(exchange: TopExchangeName, routingKey: TopQueueName, basicProperties: null, body: msgBody); Console.WriteLine(string.Format("***发送时间:{0},发送完成,输入exit退出消息发送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); vadata = Console.ReadLine(); } } } } } }
上面的代码分别创建了两个路由和两个队列,一种是DirectExchange,一种是TopicExchange,验证时需要生产者和消费者使用同一种的ExChange。
3.消费者端
using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RMQCustomer { class Program { /// <summary> /// 连接配置 /// </summary> private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName = "127.0.0.1", UserName = "cywadmin", Password = "123456", Port = 15672, VirtualHost = "cywVirtualHost" }; /// <summary> /// 路由名称 /// </summary> const string ExchangeName = "cyw.exchange"; //队列名称 const string QueueName = "cyw.queue"; /// <summary> /// 路由名称 /// </summary> const string TopExchangeName = "topic.cyw.exchange"; //队列名称 const string TopQueueName = "topic.cyw.queue"; static void Main(string[] args) { DirectAcceptExchange(); //DirectAcceptExchangeEvent(); //DirectAcceptExchangeTask(); //TopicAcceptExchange(); Console.WriteLine("按任意值,退出程序"); Console.ReadKey(); } public static void DirectAcceptExchange() { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null); channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); while (true) { BasicGetResult msgResponse = channel.BasicGet(QueueName, noAck: true); if (msgResponse != null) { var msgBody = Encoding.UTF8.GetString(msgResponse.Body); Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),msgBody)); } //BasicGetResult msgResponse2 = channel.BasicGet(QueueName, noAck: false); ////process message ... //channel.BasicAck(msgResponse2.DeliveryTag, multiple: false); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); } } } } public static void DirectAcceptExchangeEvent() { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { //channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null); //channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var msgBody = Encoding.UTF8.GetString(ea.Body); Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody)); }; channel.BasicConsume(QueueName, noAck: true, consumer: consumer); //已过时用EventingBasicConsumer代替 //var consumer2 = new QueueingBasicConsumer(channel); //channel.BasicConsume(QueueName, noAck: true, consumer: consumer); //var msgResponse = consumer2.Queue.Dequeue(); //blocking //var msgBody2 = Encoding.UTF8.GetString(msgResponse.Body); Console.WriteLine("按任意值,退出程序"); Console.ReadKey(); } } } public static void DirectAcceptExchangeTask() { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { //channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null); channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);//告诉broker同一时间只处理一个消息 //channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var msgBody = Encoding.UTF8.GetString(ea.Body); Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody)); int dots = msgBody.Split('.').Length - 1; System.Threading.Thread.Sleep(dots * 1000); Console.WriteLine(" [x] Done"); //处理完成,告诉Broker可以服务端可以删除消息,分配新的消息过来 channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); }; //noAck设置false,告诉broker,发送消息之后,消息暂时不要删除,等消费者处理完成再说 channel.BasicConsume(QueueName, noAck: false, consumer: consumer); Console.WriteLine("按任意值,退出程序"); Console.ReadKey(); } } } public static void TopicAcceptExchange() { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(TopExchangeName, "topic", durable: false, autoDelete: false, arguments: null); channel.QueueDeclare(TopQueueName, durable: false, autoDelete: false, exclusive: false, arguments: null); channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); channel.QueueBind(TopQueueName, TopExchangeName, routingKey: TopQueueName); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var msgBody = Encoding.UTF8.GetString(ea.Body); Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody)); int dots = msgBody.Split('.').Length - 1; System.Threading.Thread.Sleep(dots * 1000); Console.WriteLine(" [x] Done"); channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); }; channel.BasicConsume(TopQueueName, noAck: false, consumer: consumer); Console.WriteLine("按任意值,退出程序"); Console.ReadKey(); } } } } }
消费者端也是两个路由两个队列,在实现DirectExchange时使用了三种方式,DirectAcceptExchange是基于时间轮询的,每隔一段时间获取一次,DirectAcceptExchangeEvent、DirectAcceptExchangeTask是基于事件的,当消息到达时触发事件,获取数据。
4.实验截图
参考:http://www.cnblogs.com/xibei666/p/5931267.html#3680686 感谢园主,我这篇博文用的都是他的例子。
作者:社会主义接班人
出处:http://www.cnblogs.com/5ishare/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。