[关闭]
@xtccc 2018-03-16T15:15:51.000000Z 字数 4259 阅读 2827

基础入门知识

给我写信
GitHub

此处输入图片的描述

RabbitMQ


目录


1 概念


1.1 Virtual Host

要点:

exchange,queue等组件都是存在于某个vhost上的。

当client连接到RabbitMQ server时,必须要指定1个vhost。RabbitMQ本身存在一个默认的名为/的vhost,这个vhost可以直接用guest的身份来使用(密码也为guest)。

guest只能连接到本地的broker,不能连接到远程的broker。

不同的vhosts之间彼此是绝对隔离的,例如不能将vhost_1上的exchange绑定到vhost_2上的queue

创建vhost:

  1. $ rabbitmqctl add_vhost vhost_tao

查看vhost:

  1. $ rabbitmqctl list_vhosts
  2. Listing vhosts ...
  3. /
  4. vhost_tao


1.2 User

创建/删除/列出user:

  1. $ rabbitmqctl add_user <username> <password>
  2. $ rabbitmqctl change_password <username> <newpassword>
  3. $ rabbitmqctl delete_user <username>
  4. $ rabbitmqctl list_users


1.3 Permission

两级权限检查

  1. 当client连接到server时,必须指定一个vhost,client将在这个vhost的空间内进行活动。RabbitMQ会检查这个user是否有权限来访问该vhost,没有权限的话就拒绝连接。
  2. Resource是vhost中的有名实体(named entities),例如queue和exchange。当client对这些resources进行操作时,就会存在第二层权限检查。RabbitMQ distinguishes between configure, write and read operations on a resource. The configure operations create or destroy resources, or alter their behaviour. The write operations inject messages into a resource. And the read operations retrieve messages from a resource.


设置权限

参考 Access control

Note that rabbitmqctl manages the RabbitMQ internal user database. Permissions for users from any alternative authorisation backend will not be visible to rabbitmqctl.


  1. $ rabbitmqctl list_permissions -p <vhost_name>
  2. $ rabbotmqctl list_user_permissions <user_name>
  3. $ rabbitmqctl set_permissions -p <vhost_name> <username> <conf> <write> <read>




2. Operations


2.1 安装RabbitMQ

在ubuntu上的安装过程,参考 HOW-TO INSTALL ERLANG ON UBUNTU 14.04


在CentOS 7上面安装的过程如下。

2.1.1 确定版本

根据 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


2.1.2 安装Erlang

我们选择安装rabbitmy自己发布的精简版的erlang,在/etc/yum.repos.d/目录下创建一个名为rabbitmq-erlang.repo的文件,其内容为:

  1. [rabbitmq-erlang]
  2. name=rabbitmq-erlang
  3. baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/20/el/7
  4. gpgcheck=1
  5. gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
  6. repo_gpgcheck=0
  7. enabled=1

然后运行

  1. $ yum clean all # 刷新yum的仓库配置缓存
  2. $ yum install erlang

此时,Erlang已经装好。

2.1.3 安装RabbitMQ

从官网下载安装包: rabbitmq-server-3.7.2-1.el7.noarch.rpm
然后运行

  1. $ yum install rabbitmq-server-3.7.2-1.el7.noarch.rpm

到这里,rabbitmq已装好。

2.1.4 运行RabbitMQ

如果想开机自动运行,执行命令:

  1. $ chkconfig rabbitmq-server on

启动RabbitMQ Server:

  1. $ /sbin/service rabbitmq-server start

注意:RabbitMQ Server是以 rabbitmq 这个user来运行的,因此,如果改变log/db等文件的路径,必须保证rabbitmq是这些文件的owner。


2.2 配置

有一些配置很重要,比如 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资源限制


2.3 Web UI

首先,打开web插件:

  1. $ rabbitmq-plugins enable rabbitmq_management

然后,可以在 http://host:15672 打开web。

如果要以已经创建的某个user来登录的话,需要为其设置user_tag:

  1. $ rabbitmqctl set_user_tags tao administrator

现在就可以登录了。



3. HTTP APIs


参考
RabbitMQ Management HTTP API


3.1 查看所有的virtual hosts

  1. $ curl -i -u <username>:<passwd> http://ecs1:15672/api/vhosts


3.2 查看某个virtual host

  1. $ curl -i -u <username>:<passwd> http://ecs1:15672/api/vhosts/<vhost_name>


3.3 新建virtual host

  1. $ curl -i -u guest:guest -H "content-type:application/json" \
  2. -XPUT http://localhost:15672/api/vhosts/foo


3.4 删除virtual host

  1. $ curl -i -u guest:guest -H "content-type:application/json" \
  2. -XDELETE http://localhost:15672/api/vhosts/bar


3.5 设置permission

  1. $ curl -i -u guest:guest -H "content-type:application/json" \
  2. -XPUT http://localhost:15672/api/permissions/tao_debug/tao \
  3. -d '{"configure":"^123","write":".*","read":"^abc"}'


3.6 查看某个vhost里面的queues

  1. $ curl -i -u guest:guest -H "content-type:application/json" \
  2. -XGET http://localhost:15672/api/queues/<vhost>

3.7 删除queue

  1. $ curl -i -u guest:guest -H "content-type:application/json" \
  2. -XDELETE http://localhost:15672/api/queues/<vhost>/<queue>

成功的话,返回:

  1. HTTP/1.1 204 No Content
  2. server: Cowboy
  3. date: Thu, 17 Aug 2017 12:23:08 GMT
  4. content-length: 0
  5. content-type: application/json
  6. vary: accept, accept-encoding, origin

失败的话,返回:

  1. HTTP/1.1 404 Not Found
  2. server: Cowboy
  3. date: Thu, 17 Aug 2017 12:23:24 GMT
  4. content-length: 49
  5. content-type: application/json
  6. vary: accept, accept-encoding, origin
  7. {"error":"Object Not Found","reason":"Not Found"}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注