前言
最近項(xiàng)目要使用RabbitMQ,網(wǎng)上已經(jīng)有很多優(yōu)秀的文章了,百度百科對(duì)RabbitMQ闡述也非常明確,建議去看下,還有amqp協(xié)議。必須一提的是rabbitmq是由LShift提供的一個(gè)消息隊(duì)列協(xié)議(AMQP)的開(kāi)源實(shí)現(xiàn),由以高性能、健壯以及可伸縮性出名的Erlang寫(xiě)成(因此也是繼承了這些優(yōu)點(diǎn))。
最近參考大神們的博客,自己做了一個(gè)RabbitMQ即時(shí)發(fā)消息的Demo。下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
步驟如下:
1.使用VS的NuGet安裝包管理工具安裝RabbitMQ.Client:
2.生產(chǎn)者端代碼:
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> /// 隊(duì)列名稱 /// </summary> const string QueueName = "Jent.Queue"; static void Main(string[] args) { DirectExchangeSendMsg(); Console.WriteLine("按任意鍵退出程序!"); Console.ReadKey(); } /// <summary> /// 單點(diǎn)精確路由模式 /// </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("請(qǐng)輸入需要發(fā)送的消息:"); 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("發(fā)送時(shí)間:{0},發(fā)送完畢,輸入exit退出消息發(fā)送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); vadata = Console.ReadLine(); } } } } } }
3.消費(fèi)者端代碼:
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> /// 隊(duì)列名稱 /// </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("接收時(shí)間:{0},消息內(nèi)容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody)); } //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); } } } } } }
4.程序結(jié)果:
注:在第一步之前,你需要安裝RabbitMQ客戶端,可從http://www.rabbitmq.com/download.html下載,
但是RabbitMQ又是依賴于Erlang OTP平臺(tái),所以,安裝RabbitMQ之前,需要先從http://www.erlang.org/download.html下載安裝erlang
關(guān)于這部分的內(nèi)容,推薦閱讀:https://www.gxlcms.com/article/143499.htm
總結(jié)
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com