Rabbitmq新增用戶角色

首先,查看一下當前有哪些用戶:python

$ /usr/local/sbin/rabbitmqctl list_users
Listing users ...
patty    [administrator]
guest    [administrator]

而後,咱們新增一個用戶wgm並再次查看新增後的用戶列表:安全

$ /usr/local/sbin/rabbitmqctl add_user wgm 123456
$ /usr/local/sbin/rabbitmqctl list_users
Listing users ...
patty    [administrator]
wgm    []
guest    [administrator]

因爲此時還未對wgm授予角色, wgm的角色一欄結果仍是[],如今, 咱們授予wgm administrator角色函數

$ /usr/local/sbin/rabbitmqctl set_user_tags wgm administrator
Setting tags for user "wgm" to [administrator] ...

$
/usr/local/sbin/rabbitmqctl list_users Listing users ... patty [administrator] wgm [administrator] guest [administrator]

接下來, 查看咱們當前在哪一個virtual host上,以及該virtual host上全部用戶所擁有的權限:spa

$ /usr/local/sbin/rabbitmqctl list_vhosts
Listing vhosts ...
/

$
/usr/local/sbin/rabbitmqctl list_permissions -p / Listing permissions in vhost "/" ... guest .* .* .* patty .* .* .*

可見當前vhost上有默認用戶guest,以及以前經過rabbitmqadmin管理界面添加的用戶patty, 他們擁有該vhost全部的配置、讀寫權限。此時並無顯示wgm,是由於咱們還未對他進行賦權,固然,咱們也能夠單獨查看一個用戶的權限信息:code

$ /usr/local/sbin/rabbitmqctl list_user_permissions wgm
Listing permissions for user "wgm" ...

如今,對wgm這個用戶進行受權,賦予其在"/"這個vhost上全部的配置、讀寫權限。blog

$ /usr/local/sbin/rabbitmqctl set_permissions -p / wgm ".*" ".*" ".*" 
Setting permissions for user "wgm" in vhost "/" ...

$ /usr/local/sbin/rabbitmqctl list_user_permissions wgm
Listing permissions for user "wgm" ...
/    .*    .*    .*

set_permissions命令格式爲:rabbitmq

set_permissions [-p vhost] {user} {conf} {write} {read}, conf對應配置權限, write寫權限, read讀權限utf-8

如今,再查看「/」這個vhost的權限信息,會發現多了一個「wgm」it

$ /usr/local/sbin/rabbitmqctl list_permissions -p / 
Listing permissions in vhost "/" ...
guest    .*    .*    .*
wgm    .*    .*    .*
patty    .*    .*    .*

 接下來, 咱們經過Python程序驗證,使用用戶wgm來發送消息信息,而後guest用戶去接收消息。io

#!/usr/bin/env python
# encoding: utf-8

"""
@version: 1.0
@author: Gaomin Wu
@contact: pattywgm@163.com
@file: send.py
@time: 17/7/11  下午3:47
@desc: 消息發送方
涉及基本的消息發送,不對消息進行確認
"""

import pika

# 遠程訪問,用戶認證
credential = pika.PlainCredentials(username="wgm", password="123456")
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost", credentials=credential))

channel = connection.channel()

# 消息的發送者和消費者均可以聲明queue, 屢次聲明只有一次會被建立
# 若是消息發送到一個不存在的queue,將被丟棄,所以,安全起見,最好是在雙方都聲明一次
channel.queue_declare("hello")

# routing_key指定exchange關聯到hello這個queue
channel.basic_publish(exchange="", routing_key="hello", body="Hello Patty!!!")

print(" [x] Sent 'Hello World!'")

connection.close()
#!/usr/bin/env python
# encoding: utf-8

"""
@version: 1.0
@author: Gaomin Wu
@contact: pattywgm@163.com
@file: receive.py
@time: 17/7/11  下午3:47
@desc: 消息接收方
"""
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))

channel = connection.channel()

channel.queue_declare("hello")

# 消息回調處理函數
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)


channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
相關文章
相關標籤/搜索