前言
最近項目要使用RabbitMQ,網上已經有很多優秀的文章了,百度百科對RabbitMQ闡述也非常明確,建議去看下,還有amqp協議。必須一提的是rabbitmq是由LShift提供的一個消息隊列協議(AMQP)的開源實現,由以高性能、健壯以及可伸縮性出名的Erlang寫成(因此也是繼承了這些優點)。
最近參考大神們的博客,自己做了一個RabbitMQ即時發消息的Demo。下面話不多說了,來一起看看詳細的介紹吧。
步驟如下:
1.使用VS的NuGet安裝包管理工具安裝RabbitMQ.Client:
2.生產者端代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using RabbitMQ.Client; namespace RabbitMQ.Producter { class Program { /// <summary> /// 連接配置 /// </summary> private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName="localhost", UserName = "guest", Password = "guest", Port = 5672, //VirtualHost = "JentVirtualHost" }; /// <summary> /// 路由名稱 /// </summary> const string ExchangeName = "Jent.Exchange"; /// <summary> /// 隊列名稱 /// </summary> const string QueueName = "Jent.Queue"; static void Main(string[] args) { DirectExchangeSendMsg(); Console.WriteLine("按任意鍵退出程序!"); Console.ReadKey(); } /// <summary> /// 單點精確路由模式 /// </summary> private 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, exclusive: false, autoDelete: false, arguments: null); channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); var props = channel.CreateBasicProperties(); props.Persistent = true; Console.WriteLine("請輸入需要發送的消息:"); 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(); } } } } } }
3.消費者端代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using RabbitMQ.Client; namespace RabbitMQ.Consumer { class Program { /// <summary> /// 連接配置 /// </summary> private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName = "127.0.0.1", UserName = "guest", Password = "guest", Port = 5672, //VirtualHost = "JentVirtualHost" }; /// <summary> /// 路由名稱 /// </summary> const string ExchangeName = "Jent.Exchange"; /// <summary> /// 隊列名稱 /// </summary> const string QueueName = "Jent.Queue"; static void Main(string[] args) { DirectAcceptExchange(); Console.WriteLine("輸入任意值退出程序!"); Console.ReadKey(); } private 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, exclusive: false, autoDelete: false, arguments: null); channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); while (true) { BasicGetResult msgResponse = channel.BasicGet(QueueName, autoAck: false); 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)); } //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); } } } } } }
4.程序結果:
注:在第一步之前,你需要安裝RabbitMQ客戶端,可從http://www.rabbitmq.com/download.html下載,
但是RabbitMQ又是依賴于Erlang OTP平臺,所以,安裝RabbitMQ之前,需要先從http://www.erlang.org/download.html下載安裝erlang
關于這部分的內容,推薦閱讀:https://www.gxlcms.com/article/143499.htm
總結
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com