前言
最近項(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)者端代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
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)者端代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:https://www.cnblogs.com/JentZhang/archive/2018/07/09/9283964.html