Rabbitmq的使用及Web監控工具使用

本文轉載自:https://www.cnblogs.com/gossip/p/4475978.htmlhtml

windows安裝手冊請參考:http://www.rabbitmq.com/install-windows-manual.htmlgit

 

 
 

 

1、文檔資料github

       一、官方網站: http://www.rabbitmq.com/
       二、安裝教程: http://www.rabbitmq.com/install-windows.html
 
2、安裝指南
      一、下載並按照OTP(必須在安裝RabbitMQ Server前安裝): http://www.erlang.org/download.html
            32位:OTP 17.5 Windows 32-bit Binary File (91.0 MB)
            64位:OTP 17.5 Windows 64-bit Binary File (91.1 MB)
      二、下載並安裝RabbitMQ Server: http://www.rabbitmq.com/download.html
            選擇windows平臺,直接從官方連接下載,同時也能夠從github上下載, 安裝完成後,服務自動啓動(services.msc)
      三、下載並安裝.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
              (選擇的是.net 4.0版本, 同時也能夠經過nuget下載
 
3、命令行工具
      一、定位到rabbitmq安裝目錄:
          windows 7   cd C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-3.5.1\sbin
          Widows Serve  cd /d C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-3.5.1\sbin
      二、執行rabbitmq命令行工具(rabbitmqctl ):
         rabbitmqctl -q status       //打印了一些rabbitmq服務狀態信息,包括內存,硬盤,和使用erlong的版本信息
         rabbitmqctl list_queues     //查看全部隊列消息
 
4、C#客戶端使用
         一、服務端代碼
       
複製代碼
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);
                    }
                }
            }
        }
    }
}
複製代碼

 

 
          二、客戶端代碼
複製代碼
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
 
5、RabbitMQ GUID使用
       一、官方提供的一個web管理工具(rabbitmq_management)
       二、安裝了Rabbitmq後,默認也安裝了該管理工具,執行命令便可啓動
            rabbitmq-plugins enable rabbitmq_management(先定位到rabbitmq安裝目錄)  
            
        三、啓動後,直接在瀏覽器地址輸入: http://localhost:15672/   帳號密碼都是:guest    
 
 



6、異常問題web

一、None of the specified endpoints were reachablewindows

      生產端和消費端的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;工具

相關文章
相關標籤/搜索