RabbitMQ的主页在http://www.rabbitmq.com/ 。
1. 安装Erlang
RabbitMQ是用Erlang编写的,所以需要先安装Erlang,如果有的话跳过这步即可(我是跳过了,系统已有)。
# brew install erlang
2. 安装rabbitMq
确保系统有Erlang环境后,即可使用brew安装RabbitMQ了,安装很简单,只需如下即可:
# brew update
# brew install rabbitmq
安装后需要将/usr/local/sbin添加到$PATH里去,我的环境下编辑~/.profile就行了。
具体的官方操作文档在这里:
http://www.rabbitmq.com/install-homebrew.html
3. 一些简单常用的命令
命令都在/usr/sbin目录下。
3.1. rabbitmq-server
rabbitmq-server用来启动RabbitMQ服务器进程:
# rabbitmq-server -detached
如果不加上面的-datached,即非守护启动方式,启动后控制台被挂起,ctrl+c信号会发给其进程,从而可以直接kill掉进程。
3.2. rabbitmqctl
rabbitmqctl 是RabbitMQ中间件的一个用来管理节点(Node)的命令行工具。它针对各个节点的实例进行各种配置和控制。
停止RabbitMQ应用并关闭节点(即同时停止rabbitmq的进程及Erlang虚拟机)
# rabbitmqctl stop
用上面方法停止RabbitMQ后,只能再通过rabbitmq-server来启动RabbitMQ服务。
只停止RabbitMQ应用 (erl还在运行中,可通过start_app再启动服务)
# rabbitmqctl stop_app
启动RabbitMQ应用 (和停止RabbitMQ应用对应)
# rabbitmqctl start_app
显示RabbitMQ中间件各种信息
# rabbitmqctl -q status
[{pid,1021}, {running_applications, [{rabbitmq_management,"RabbitMQ Management Console","3.5.3"}, {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.5.3"}, {webmachine,"webmachine","1.10.3-rmq3.5.3-gite9359c7"}, {mochiweb,"MochiMedia Web Server","2.7.0-rmq3.5.3-git680dba8"}, {rabbitmq_mqtt,"RabbitMQ MQTT Adapter","3.5.3"}, {rabbitmq_stomp,"Embedded Rabbit Stomp Adapter","3.5.3"}, {rabbitmq_management_agent,"RabbitMQ Management Agent","3.5.3"}, {rabbitmq_amqp1_0,"AMQP 1.0 support for RabbitMQ","3.5.3"}, {rabbit,"RabbitMQ","3.5.3"}, {os_mon,"CPO CXC 138 46","2.3.1"}, {mnesia,"MNESIA CXC 138 12","4.12.5"}, {amqp_client,"RabbitMQ AMQP Client","3.5.3"}, {inets,"INETS CXC 138 49","5.10.6"}, {xmerl,"XML parser","1.3.7"}, {sasl,"SASL CXC 138 11","2.4.1"}, {stdlib,"ERTS CXC 138 10","2.4"}, {kernel,"ERTS CXC 138 10","3.2"}]}, {os,{unix,darwin}}, {erlang_version, "Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:30] [hipe] [kernel-poll:true] "}, {memory, [{total,44296616}, {connection_readers,0}, {connection_writers,0}, {connection_channels,0}, {connection_other,5616}, {queue_procs,68848}, {queue_slave_procs,0}, {plugins,543352}, {other_proc,14341880}, {mnesia,80856}, {mgmt_db,134344}, {msg_index,41448}, {other_ets,1295928}, {binary,166520}, {code,21567638}, {atom,744345}, {other_system,5305841}]}, {alarms,[]}, {listeners, [{clustering,25678,"::"}, {amqp,5678,"127.0.0.1"}, {stomp,61613,"::"}, {mqtt,1883,"::"}]}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,2972144435}, {disk_free_limit,50000000}, {disk_free,42164834304}, {file_descriptors, [{total_limit,156},{total_used,5},{sockets_limit,138},{sockets_used,3}]}, {processes,[{limit,1048576},{used,212}]}, {run_queue,0}, {uptime,2123}]
启动插件
user@localhost:/usr/local/Cellar/rabbitmq/3.5.3/sbin> ./rabbitmq-plugins enable rabbitmq_management
启动插件成功:
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@localhost... started 5 plugins.
默认可是可以再本地登陆,使用guest用户,密码也是guest. http://localhost:15672
注意:guest用户不能远程登陆。
进入/usr/sbin目录 可以使用./rabbitmqctl add_user 用户名 密码
例如:./rabbitmqctl add_user peter 123456
并设置新添加的用户状态 使用./rabbitmqctl set_user_tags wuge administrator
更多参考:https://www.rabbitmq.com/management.html#configuration
关闭插件
./rabbitmq-plugins disable rabbitmq_management
关闭插件成功
The following plugins have been disabled:
mochiweb
webmachine
rabbitmq_web_dispatch
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@localhost... stopped 5 plugins.
查看已经安装的插件
./rabbitmq-plugins list
结果
Configured: E = explicitly enabled; e = implicitly enabled | Status: * = running on rabbit@localhost |/ [e*] amqp_client 3.5.3 [ ] cowboy 0.5.0-rmq3.5.3-git4b93c2d [e*] mochiweb 2.7.0-rmq3.5.3-git680dba8 [E*] rabbitmq_amqp1_0 3.5.3 [ ] rabbitmq_auth_backend_ldap 3.5.3 [ ] rabbitmq_auth_mechanism_ssl 3.5.3 [ ] rabbitmq_consistent_hash_exchange 3.5.3 [ ] rabbitmq_federation 3.5.3 [ ] rabbitmq_federation_management 3.5.3 [E*] rabbitmq_management 3.5.3 [e*] rabbitmq_management_agent 3.5.3 [ ] rabbitmq_management_visualiser 3.5.3 [E*] rabbitmq_mqtt 3.5.3 [ ] rabbitmq_shovel 3.5.3 [ ] rabbitmq_shovel_management 3.5.3 [E*] rabbitmq_stomp 3.5.3 [ ] rabbitmq_test 3.5.3 [ ] rabbitmq_tracing 3.5.3 [e*] rabbitmq_web_dispatch 3.5.3 [ ] rabbitmq_web_stomp 3.5.3 [ ] rabbitmq_web_stomp_examples 3.5.3 [ ] sockjs 0.3.4-rmq3.5.3-git3132eb9 [e*] webmachine 1.10.3-rmq3.5.3-gite9359c7
列出当前Rabbit的插件列表:
./rabbitmq-plugins list -v 显示所有可以用的插件
./rabbitmq-plugins list -m 仅显示插件名称
./rabbitmq-plugins list -E 显示所有使用的插件
/rabbitmq-plugins list -E
Configured: E = explicitly enabled; e = implicitly enabled | Status: * = running on rabbit@localhost |/ [E*] rabbitmq_amqp1_0 3.5.3 [E*] rabbitmq_management 3.5.3 [E*] rabbitmq_mqtt 3.5.3 [E*] rabbitmq_stomp 3.5.3
查看exchange信息:
./rabbitmqctl list_exchanges name type durable auto_delete arguments
查看队列信息:
./rabbitmqctl list_queues name durable auto_delete messages consumers memory
查看绑定信息:
访问http://localhost:15672进入rabbitMq的后台管理:
新建 type 为topic 的exchange:
新建 Durable的Queues
把Exchange: IhotelOrderExchange 通过Routing key (*.P1)bind到Queue IhotelOrderQueue
查看IhotelOrderQueue绑定的Exchange
3.3. rabbitmqadmin
Management Command Line Tool 是RabbitMQ管理插件提供的工具,提供几乎和管理界面一样的功能。
它能做的事情很多,比如:
- 查看exchanges, queues, bindings, vhosts, users, permissions, connections 和 channels
- 查看node概要信息
- 创建或删除exchanges, queues, bindings, vhosts, users 和 permissions.
- 生产和消费消息
- 备份、恢复配置信息等。
创建queue
# rabbitmqadmin declare queue name=test
queue declared
发布消息
# rabbitmqadmin publish exchange=amq.default routing_key=test payload=”hello, world”
Message published
消费消息
# rabbitmqadmin get queue=test requeue=false
requeue=false表示取得消息后即从队列里删除。如果我们只是确认队列里的消息状态而不想真的消费,可以不指定此参数。
4. 更改配置文件
像上面那样我们简单的就可以运行其RabbitMQ的服务程序并进行消息的收发,并不需要做什么设置。
不错,默认的RabbitMQ已经为我们做了很多默认的设置,通过brew安装的话默认一些文件路径如下:
- Logs: /usr/local/var/log/rabbitmq
- Config: /usr/local/etc/rabbitmq
- Mnesia Database: /usr/local/var/lib/rabbitmq/mnesia
然后,有时候默认设置是不能满足我们的需求的,更细化、个性的设置,
我们都可以通过修改RabbitMQ的配置文件来完成。
在RabbitMQ中,配置文件有两个(注意后缀名都不一样):
- rabbitmq-env.conf
- rabbitmq.config
rabbitmq-env.conf
其中,rabbitmq-env.conf的位置是固定的,不能改变的。里主要进行环境变量的设置,比如系统安装后默认的设置内容如下:
# cat rabbitmq-env.conf
CONFIG_FILE=/usr/local/etc/rabbitmq/rabbitmq
NODE_IP_ADDRESS=127.0.0.1
NODENAME=rabbit@localhost
通过变量CONFIG_FILE指定了rabbitmq.config的位置(注意没有后缀名,RabbitMQ会自动天剑的)
默认的服务器监听IP地址是127.0.0.1,如果想在其它机器访问,需要修改一下这个值。
rabbitmq.config
rabbitmq.config的文件位置是可以配置的,且文件内容是已Erlang的语法来记述的。
关于两个配置文件的具体说明,请参考官方文档:http://www.rabbitmq.com/configure.html