@xtccc
2018-03-16T15:15:51.000000Z
字数 4259
阅读 2827
RabbitMQ
目录
要点:
exchange,queue等组件都是存在于某个vhost上的。
当client连接到RabbitMQ server时,必须要指定1个vhost。RabbitMQ本身存在一个默认的名为
/
的vhost,这个vhost可以直接用guest
的身份来使用(密码也为guest
)。
guest
只能连接到本地的broker,不能连接到远程的broker。不同的vhosts之间彼此是绝对隔离的,例如不能将
vhost_1
上的exchange绑定到vhost_2
上的queue
创建vhost:
$ rabbitmqctl add_vhost vhost_tao
查看vhost:
$ rabbitmqctl list_vhosts
Listing vhosts ...
/
vhost_tao
创建/删除/列出user:
$ rabbitmqctl add_user <username> <password>
$ rabbitmqctl change_password <username> <newpassword>
$ rabbitmqctl delete_user <username>
$ rabbitmqctl list_users
Note that rabbitmqctl manages the RabbitMQ internal user database. Permissions for users from any alternative authorisation backend will not be visible to rabbitmqctl.
$ rabbitmqctl list_permissions -p <vhost_name>
$ rabbotmqctl list_user_permissions <user_name>
$ rabbitmqctl set_permissions -p <vhost_name> <username> <conf> <write> <read>
在ubuntu上的安装过程,参考 HOW-TO INSTALL ERLANG ON UBUNTU 14.04
在CentOS 7上面安装的过程如下。
根据 RabbitMQ Erlang Version Requirements,我决定安装RabbitMQ 3.7.x的最新版本,在此之前要先安装Erlang的20.x最新版本。
下载
wget --content-disposition https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/7/rabbitmq-server-3.7.2-1.el7.noarch.rpm/download.rpm
我们选择安装rabbitmy自己发布的精简版的erlang,在/etc/yum.repos.d/目录下创建一个名为rabbitmq-erlang.repo的文件,其内容为:
[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/20/el/7
gpgcheck=1
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
然后运行
$ yum clean all # 刷新yum的仓库配置缓存
$ yum install erlang
此时,Erlang已经装好。
从官网下载安装包: rabbitmq-server-3.7.2-1.el7.noarch.rpm
然后运行
$ yum install rabbitmq-server-3.7.2-1.el7.noarch.rpm
到这里,rabbitmq已装好。
如果想开机自动运行,执行命令:
$ chkconfig rabbitmq-server on
启动RabbitMQ Server:
$ /sbin/service rabbitmq-server start
注意:RabbitMQ Server是以 rabbitmq
这个user来运行的,因此,如果改变log/db等文件的路径,必须保证rabbitmq是这些文件的owner。
有一些配置很重要,比如 max number of open files ,对于生产环境推荐至少设置为65536。
There are two limits in play: the maximum number of open files the OS kernel allows (fs.file-max) and the per-user limit (ulimit -n). The former must be higher than the latter.
配置方法参考 Linux Increase The Maximum Number Of Open Files / File Descriptors (FD)
推荐看这篇:CENTOS/RHEL 7 系统中设置SYSTEMD SERVICE的ULIMIT资源限制
首先,打开web插件:
$ rabbitmq-plugins enable rabbitmq_management
然后,可以在 http://host:15672 打开web。
如果要以已经创建的某个user来登录的话,需要为其设置user_tag:
$ rabbitmqctl set_user_tags tao administrator
现在就可以登录了。
参考
RabbitMQ Management HTTP API
$ curl -i -u <username>:<passwd> http://ecs1:15672/api/vhosts
$ curl -i -u <username>:<passwd> http://ecs1:15672/api/vhosts/<vhost_name>
$ curl -i -u guest:guest -H "content-type:application/json" \
-XPUT http://localhost:15672/api/vhosts/foo
$ curl -i -u guest:guest -H "content-type:application/json" \
-XDELETE http://localhost:15672/api/vhosts/bar
$ curl -i -u guest:guest -H "content-type:application/json" \
-XPUT http://localhost:15672/api/permissions/tao_debug/tao \
-d '{"configure":"^123","write":".*","read":"^abc"}'
$ curl -i -u guest:guest -H "content-type:application/json" \
-XGET http://localhost:15672/api/queues/<vhost>
$ curl -i -u guest:guest -H "content-type:application/json" \
-XDELETE http://localhost:15672/api/queues/<vhost>/<queue>
成功的话,返回:
HTTP/1.1 204 No Content
server: Cowboy
date: Thu, 17 Aug 2017 12:23:08 GMT
content-length: 0
content-type: application/json
vary: accept, accept-encoding, origin
失败的话,返回:
HTTP/1.1 404 Not Found
server: Cowboy
date: Thu, 17 Aug 2017 12:23:24 GMT
content-length: 49
content-type: application/json
vary: accept, accept-encoding, origin
{"error":"Object Not Found","reason":"Not Found"}