RabbitMQ概念篇 vhost
what is virtual_host#
vhost是rabbitmq分配权限的最小细粒度。比如我们可以为一个用户分配一个可以访问哪个或者哪一些vhost的权限。
但是不能为用户分配一个可以访问哪一些exchange,或者queue的权限,因为rabbitmq的权限细粒度没有细化到交换器和队列,他的最小细粒度是vhost(vhost中包含许多的exchanges,queues,bingdings)。
所以如果exchangeA 和queueA 只能让用户A访问,exchangeB 和queueB 只能让用户B访问,要达到这种需求,只能为exchangeA 和queueA创建一个vhostA,为exchangeB 和queueB 创建vhostB,这样就隔离开来了。
补充:一个broker可以开设多个vhost,用于不同用户的权限分离
virtual host只是起到一个命名空间的作用,所以可以多个user共同使用一个virtual host,文章开头写的vritual_host = '/',这个是系统默认的,
就是说当我们创建一个到rabbitmq的connection时候,它的命名空间是'/',需要注意的是不同的命名空间之间的资源是不能访问的,比如 exchang,queue ,bingding等
How does it work#
查看rabbitmqctl工具相关命令
Copy
rabbitmqctl
查看所有虚拟主机
Copy
rabbitmqctl list_vhosts
添加vhost#
Copy
rabbitmqctl add_vhost test_vhost rabbitmqctl list_vhosts
可以看到有两个,一个是系统默认的 '/', 还有一个就是我们新建的 test_host
add_user 添加用户#
Copy
rabbitmqctl add_user test1 123456 rabbitmqctl add_user test2 123456 rabbitmqctl list_users
用户的管理命令#
Copy
add_user <username> <password> delete_user <username> change_password <username> <newpassword> clear_password <username> authenticate_user <username> <password> set_user_tags <username> <tag> ... list_users
set_permissions 分配访问权限#
Copy
set_permissions [-p <vhost>] <user> <conf> <write> <read>
其中,权限控制(配置,读,写) 的位置分别用正则表达式来匹配特定的资源,如'^(amq.gen.*|amq.default)'可以匹配server生成的和默认的exchange,'^'可以匹配server生成的和默认的exchange,'^'不匹配任何资源
需要注意的是RabbitMQ会缓存每个connection或channel的权限验证结果、因此权限发生变化后需要重连才能生效。
Copy
rabbitmqctl set_permissions -p test_host test1 ".*" ".*" ".*" rabbitmqctl set_permissions -p test_host test2 ".*" ".*" ".*"
角色#
Comma-separated list of tags to apply to the user. Currently supported by the management plugin:
management#
User can access the management plugin
policymaker#
User can access the management plugin and manage policies and parameters for the vhosts they have access to.
monitoring#
User can access the management plugin and see all connections and channels as well as node-related information.
administrator#
User can do everything monitoring can do, manage users, vhosts and permissions, close other user's connections, and manage policies and parameters for all vhosts.
Note that you can set any tag here; the links for the above four tags are just for convenience.
RabbitMQ的用户角色分类:
none、management、policymaker、monitoring、administrator
RabbitMQ各类角色描述:
none#
不能访问 management plugin
management#
用户可以通过AMQP做的任何事外加:
列出自己可以通过AMQP登入的virtual hosts
查看自己的virtual hosts中的queues, exchanges 和 bindings
查看和关闭自己的channels 和 connections
查看有关自己的virtual hosts的“全局”的统计信息,包含其他用户在这些virtual hosts中的活动。
policymaker#
management可以做的任何事外加:
查看、创建和删除自己的virtual hosts所属的policies和parameters
monitoring#
management可以做的任何事外加:
列出所有virtual hosts,包括他们不能登录的virtual hosts
查看其他用户的connections和channels
查看节点级别的数据如clustering和memory使用情况
查看真正的关于所有virtual hosts的全局的统计信息
administrator#
policymaker和monitoring可以做的任何事外加:
创建和删除virtual hosts
查看、创建和删除users
查看创建和删除permissions
关闭其他用户的connections
创建用户并设置角色:
可以创建管理员用户,负责整个MQ的运维,例如:
Copy
rabbitmqctl add_user user_admin passwd_admin
赋予其administrator角色:
Copy
rabbitmqctl set_user_tags user_admin administrator
可以创建RabbitMQ监控用户,负责整个MQ的监控,例如:
Copy
rabbitmqctl add_user user_monitoring passwd_monitor
赋予其monitoring角色:
Copy
rabbitmqctl set_user_tags user_monitoring monitoring
可以创建某个项目的专用用户,只能访问项目自己的virtual hosts
Copy
rabbitmqctl add_user user_proj passwd_proj
赋予其monitoring角色:
Copy
rabbitmqctl set_user_tags user_proj management
创建和赋角色完成后查看并确认:
Copy
rabbitmqctl list_users
参考资料
参考资料