RabbitMQ 远程连接

 

默认情况下,rabbitmq使用guest来连接本地(localhost)的server,当需要远程连接时,就会失效。

"guest" user can only connect via localhost

官方文档:http://www.rabbitmq.com/access-control.html

如果必须使用guest用户来进行远程登录,需要修改配置

 

1
[{rabbit, [{loopback_users, []}]}].

 

(1)那么首先需要创建并添加一个用户test,让其具有管理员权限

  • rabbitmqctl add_user admin admin

  • rabbitmqctl set_user_tags admin administrator

  • rabbitmqctl set_permissions -p / admin "." "." ".*"

(2)修改配置文件

 

1
[{rabbit, [{loopback_users, ["root"]}]}].

 

(3)重启rabbitmq-server

 

1
/etc/init.d/rabbitmq-server restart

 

(4)修改host

修改远程客户端机器上的/etc/hosts,添加rabbit-server的IP

 

1
xx.xx.xx.xx rabbit-server

 

(5)认证

原文:https://pika.readthedocs.org/en/0.9.14/modules/parameters.html
pika提供了两种认证方式:ConnectinParameters和URLParameters。

ConnectionParameters

 

1
2
3
4
5
6
7
8
import pika

# Set the connection parameters to connect to rabbit-server1 on port 5672# on the / virtual host using the username "guest" and password "guest"
credentials = pika.PlainCredentials('root', 'root')
parameters = pika.ConnectionParameters('rabbit-server1',
                                       5672,
                                       '/',
                                       credentials)

 

URLParameters

 

1
2
3
4
import pika

# Set the connection parameters to connect to rabbit-server1 on port 5672# on the / virtual host using the username "guest" and password "guest"
parameters = pika.URLParameters('amqp://guest:guest@rabbit-server1:5672/%2F')

 

例子

import pika

i = 1def callback(ch, method, properties, body):
    global i    #print 'receive %r'%body
    print 'receive %s'%i
    i += 1
    f = open('%s'%i, 'w+')
    f.write(body)
    f.close()#第一种方法#credentials = pika.PlainCredentials('mtest', 'root')#connection = pika.BlockingConnection(pika.ConnectionParameters('rabbit-server', 5672, '/', credentials))#第二种方法parameters = pika.URLParameters('amqp://mtest:root@rabbit-server:5672/%2F')
connection = pika.BlockingConnection(parameters)

channel = connection.channel()

channel.queue_declare(queue='hello')

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

channel.start_consuming()

         

 

 

#rabbitmq                  

python 对象                      

memcache 内存管理                      

 

 

转载于:https://my.oschina.net/airship/blog/638906

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值