def __init__(self,
host='localhost',
userid='guest',
password='guest',
login_method='AMQPLAIN',
login_response=None,
virtual_host='/',
locale='en_US',
client_properties=None,
ssl=False,
insist=False,
connect_timeout=None,
**kwargs):
这里面有个参数叫virtual_host,系统默认是'/', 类似于root。
question 1: what is virtual_host
2:How does it work
一. what is it
There are four building blocks you really care about in AMQP: virtual hosts, exchanges, queues and bindings. A virtual host holds a bundle of exchanges, queues and bindings. Why would you want multiple virtual hosts? E asy. A username in RabbitMQ grants you access to a virtual host…in its entirety. So the only way to keep group A from accessing group B’s exchanges/queues/bindings/etc. is to create a virtual host for A and one for B. Every RabbitMQ server has a default virtual host named “/”. If that’s all you need, you’re ready to roll.
virtualHost is used as a namespace for AMQP resources (default is \"/\"), so different applications could use multiple virtual hosts on the same AMQP server
virtual host只是起到一个命名空间的作用,所以可以多个user共同使用一个virtual host,文章开头写的vritual_host = '/',这个是系统默认的,就是说当我们创建一个到rabbitmq的connection时候,它的命名空间是'/',需要注意的是不同的命名空间之间的资源是不能访问的,比如 exchang,queue ,bingding等
二.How does it work
既然vitrual host 只是一个命名空间,那么我们就可以自己创建一个喽,怎么去干呢?这里我们需要用到工具 :rabbitmqctl,安装rabbitMQ的时候已经安装好了。这真是一个强大的工具阿。输入 ./rabbitmqctl,我们可以看到
首先我们可以建立一个测试用户(如果你想用系统默认的guest也可以)
rabbitmqctl add_user test 123456 ,这样我们就新建了一个可以连到rabbitmq的用户,用户名时test,密码是123456
我们可以用 rabbitmqctl list_users 看看有多少个用户了,可以看到有guest和test了吧
然后 我们通过 rabbitmqctl add_vhosts命令新建一个virtual host : rabbitmqctl add_vhost test_host
我们通过 rabbitmqctl list_vhosts命令看看现在系统有几个vhost了。可以看到有两个,一个是系统默认的 '/', 还有一个就 是我们新建的 test_host。
但是到这里是不够的,我们只是声明了一个vhost,我们还要给它分配访问权限。
rabbitmqctl set_permissions -p test_host test "test-*" ".*" ".*",如此用户名为test的用户就可以访问vitrual host为test_host的资源了,并且具备读写的权限。
[set_permissions命令的格式:
set_permissions [-p <VHostPath>] <UserName> <Regexp> <Regexp> <Regexp>
示例:rabbitmqctl set_permissions -p "test_host" test_user ".*" ".*" ".*"
]
下面是我自己写的一个demo,分为conmuser 和 producer
conmuser :
#author yi
#time 2010-08-17 15:30
import sys
import amqplib.client_0_8 as amqp
conn = amqp.Connection(host='localhost',userid='test',password='123456',ssl=False,virtual_host='test_host')
ch = conn.channel()
ch.access_request('/data',active=True,read=True)
ch.queue_declare(queue='x',durable=False,auto_delete=False,exclusive=False)
ch.exchange_declare('y','fanout',durable=False,auto_delete=False)
ch.queue_bind(queue='x',exchange='y',routing_key='love')
def need_callback(msg):
print 'Hello,your message1 is',msg.body
ch.basic_consume('x',consumer_tag='xiaorao',callback=need_callback,no_ack=True)
while ch.callbacks:
ch.wait()
ch.close()
conn.close()
producer:
#author yi
#time 2010-08-17 15:37
import sys
import amqplib.client_0_8 as amqp
conn = amqp.Connection(host='localhost',userid='test',password='123456',virtual_host='test')
chan = conn.channel()
chan.access_request('/data',active=True,read=True)
chan.exchange_declare('y','fanout',durable=False,auto_delete=False)
print '<<'
meg_body = raw_input()
meg = amqp.Message(meg_body,content_encoding='UTF-8')
meg.properties['delivery_mode'] = 2
chan.basic_publish(meg,'y',routing_key='love')
结果如下:
producer:
consumer: