rabbitmq 命令&& rabbitmq教程(一)

先來個官方教程html

http://www.rabbitmq.comwindows

 

在windows 下 命名 去掉sudo 我是在windows下測試 用net調用app

經常使用命令

控制檯命令:sudo rabbitmqctl#只能在root權限下使用測試

本地節點默認被命名爲」rabbit」。能夠經過這個命令前使 用」-n」標誌明確的指定節點名稱, 例如:  spa

sudo rabbitmqctl -n rabbit@hostname **** 控制檯命令regexp

啓動:sudo rabbitmq-server startserver

啓動應用:sudo  rabbitmqctl start_apphtm

中止應用:sudo rabbitmqctl stop_app中間件

中止rabbitmq:sudo rabbitmqctl stop教程

顯示RabbitMQ中間件各類信息:sudo rabbitmqctl status

重置RabbitMQ節點:
sudo rabbitmqctl reset

sudo rabbitmqctl force_reset

 

用戶管理
添加用戶
sudo rabbitmqctl add_user username password
刪除用戶
sudo rabbitmqctl delete_user username
修改密碼
sudo rabbitmqctl change_password username newpassword

列出全部用戶

 

sudo rabbitmqctl list_users  
建立虛擬主機
sudo rabbitmqctl add_vhost vhostpath

刪除虛擬主機

sudo rabbitmqctl delete_vhost vhostpath

列出全部虛擬主機
sudo rabbitmqctl list_vhosts
設置用戶權限

sudo rabbitmqctl set_permissions [-p vhostpath] username regexp regexp regexp

example:sudo rabbitmqctl set_permissions -p vhostname testuser ".*" ".*" ".*"

清除用戶權限
sudo rabbitmqctl clear_permissions [-p vhostpath] username
列出虛擬主機上的全部權限
sudo rabbitmqctl list_permissions [-p vhostpath]
列出用戶權限

sudo rabbitmqctl list_user_permissions username

列出所有隊列,exchanger,binder

sudo rabbitmqctl  list_queues [-p <vhostpath>] [<queueinfoitem> ...]
   list_exchanges [-p <vhostpath>] [<exchangeinfoitem> ...]
   list_bindings [-p <vhostpath>] [<bindinginfoitem> ...]
   list_connections [<connectioninfoitem> ...]
   list_channels [<channelinfoitem> ...]
   list_consumers [-p <vhostpath>]

 

環境搭建

環境仍是簡單的說下

一、從官方下載http://www.rabbitmq.com/download.html 服務版本

二、沒有erlang須要下載http://www.erlang.org/download.html

三、啓動服務

四、驗證是否成功

sudo rabbitmqctl status

簡單消息(Hello World)

發送消息

static void Main(string[] args)
        {
            var factory = new ConnectionFactory();
            factory.HostName = "Localhost";
            factory.UserName = "shuzhengyi";
            factory.Password = "shuzhengyi";

            while (true)
            {
                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        channel.QueueDeclare("hello", false, false, false, null);
                        string message = "Hello World";
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish("", "hello", null, body);
                        Console.WriteLine(" set {0}", message);
                        Console.ReadLine();
                    }
                }
            }           
        }

 

接受消息

public static void Main()
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            factory.UserName = "shuzhengyi";
            factory.Password = "shuzhengyi";
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    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);
                    }
                }
            }
        }

 

運行結果

相關文章
相關標籤/搜索