[轉]Rabbitmq的使用及Web監控工具使用
阿新 • • 發佈:2018-12-20
本文轉自:https://blog.csdn.net/xingxing513234072/article/details/51014850
一、文件資料
1、官方網站: http://www.rabbitmq.com/ 2、安裝教程: http://www.rabbitmq.com/install-windows.html 二、安裝指南 1、下載並按照OTP(必須在安裝RabbitMQ Server前安裝):32位:OTP 17.5 Windows 32-bit Binary File (91.0 MB) 64位:OTP 17.5 Windows 64-bit Binary File (91.1 MB) 2、下載並安裝RabbitMQ Server: http://www.rabbitmq.com/download.html
3、下載並安裝.Net客戶端: http://www.rabbitmq.com/dotnet.html
http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v3.5.1/rabbitmq-dotnet-client-3.5.1-dotnet-4.0.zip
1、服務端程式碼
- namespace Server
- {
- class Program
- {
- static void Main(string[] args)
- {
- var factory = new ConnectionFactory() { HostName = "localhost" };
- using (var connection = factory.CreateConnection())
- {
- using (var channel = connection.CreateModel())
- {
- //定義佇列(hello為佇列名)
- channel.QueueDeclare( "hello", false, false, false, null);
- var consumer = new QueueingBasicConsumer(channel);
- channel.BasicConsume( "hello", true, consumer);
- Console.WriteLine( " [*] Waiting for messages." +
- "To exit press CTRL+C");
- while (true)
- {
- //接受客戶端傳送的訊息並打印出來
- var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
- var body = ea.Body;
- var message = Encoding.UTF8.GetString(body);
- Console.WriteLine( " [x] Received {0}", message);
- }
- }
- }
- }
- }
- }
2、客戶端程式碼
- namespace Client
- {
- class Program
- {
- static void Main(string[] args)
- {
- var factory = new ConnectionFactory() { HostName = "localhost" };
- using (var connection = factory.CreateConnection())
- {
- using (var channel = connection.CreateModel())
- {
- //定義佇列(hello為佇列名)
- channel.QueueDeclare( "hello", false, false, false, null);
- //傳送到佇列的訊息,包含時間戳
- string message = "Hello World!" + "_" + DateTime.Now.ToString();
- var body = Encoding.UTF8.GetBytes(message);
- channel.BasicPublish( "", "hello", null, body);
- Console.WriteLine( " [x] Sent {0}", message);
- }
- }
- }
- }
- }
如果啟動服務端前啟動了客戶端,訊息就會存放到佇列中,da 五、RabbitMQ GUID使用 1、官方提供的一個web管理工具(rabbitmq_management) http://www.rabbitmq.com/management.html 2、安裝了Rabbitmq後,預設也安裝了該管理工具,執行命令即可啟動 rabbitmq-plugins enable rabbitmq_management(先定位到rabbitmq安裝目錄) 3、啟動後,直接在瀏覽器地址輸入: http://localhost:15672/ 賬號密碼都是:guest
六、異常問題
1、None of the specified endpoints were reachable
生產端和消費端的factory引數要統一
var factory = new ConnectionFactory();
factory.UserName = QueueSetttiong.UserName; //使用者名稱,對應Management工具的admin-->user
factory.Password = QueueSetttiong.Password; //密碼,對應Management工具的admin-->密碼
factory.HostName = QueueSetttiong.HostName; //本地部署服務直接用hostname即可
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
factory.VirtualHost = QueueSetttiong.VirtualHost; //使用預設值: "/"
factory.Protocol = Protocols.DefaultProtocol;