一、下載RabbitMQ http://www.rabbitmq.com/install-windows.html 二、下載OTP http://www.erlang.org/downloads 三、安裝OTP、RabbitMQ 四、配置RabbitMQ 找到bat的目錄 執行相關命令 1.添加用戶 ...
一、下載RabbitMQ
http://www.rabbitmq.com/install-windows.html
二、下載OTP
http://www.erlang.org/downloads
三、安裝OTP、RabbitMQ
四、配置RabbitMQ
找到bat的目錄
執行相關命令
1.添加用戶密碼 rabbitmqctl add_user wenli wenli
2.設置wenli為管理員 rabbitmqctl set_user_tags wenli administrator
3.啟動RabbitMQ的web管理 rabbitmq-plugins enable rabbitmq_management
4.創建virtual host
5.設置用戶許可權
點擊用戶名進行設置
將virtual hosts 許可權賦給用戶wenli
6.創建Exchanges
五.創建C# console
1.下載RabbitMQ驅動 https://github.com/yswenli/Wenli.Data.RabbitMQ/releases/tag/Release1.0.0
2.添加引用
3.添加配置
4.測試代碼:
1 using System; 2 using System.Text; 3 using System.Threading; 4 using System.Threading.Tasks; 5 6 namespace Wenli.Data.RabbitMQ.Console 7 { 8 using Console = System.Console; 9 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 Console.Title = "Wenli.Data.RabbitMQ.Console"; 15 Console.WriteLine("正連接到mq"); 16 17 try 18 { 19 Test(); 20 } 21 catch (Exception ex) 22 { 23 Console.WriteLine("err:" + ex.Message + ex.Source + ex.StackTrace); 24 } 25 26 Console.Read(); 27 } 28 29 30 static void Test() 31 { 32 33 var topic = "testtopic"; 34 35 var cnn = RabbitMQBuilder.Get(MQConfig.Default).GetConnection(); 36 37 var operation = cnn.GetOperation(topic); 38 39 Console.WriteLine("正連接到訂閱【" + topic + "】"); 40 41 operation.Subscribe(); 42 43 Console.WriteLine("正在入隊"); 44 45 Task.Factory.StartNew(() => 46 { 47 while (true) 48 { 49 operation.Enqueue(Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " hello!")); 50 Thread.Sleep(1); 51 } 52 }); 53 54 55 56 57 Console.WriteLine("正在出隊"); 58 59 60 61 Task.Factory.StartNew(() => 62 { 63 while (true) 64 { 65 var result = operation.Dnqueue(); 66 67 if (result == null) 68 { 69 Thread.Sleep(1); 70 } 71 else 72 { 73 Console.WriteLine(Encoding.UTF8.GetString(result)); 74 } 75 } 76 }); 77 78 Console.ReadLine(); 79 80 Console.WriteLine("正在取消訂閱"); 81 82 operation.UnSubscribe(); 83 84 Console.WriteLine("測試完成"); 85 } 86 } 87 }
5.運行結果:
至此C# 成功操作Rabbitmq完成
轉載請標明本文來源:http://www.cnblogs.com/yswenli/p/7446919.html
更多內容歡迎star作者的github:https://github.com/yswenli/Wenli.Data.RabbitMQ
如果發現本文有什麼問題和任何建議,也隨時歡迎交流~