使用RabbitMQ实现带权限的Routing
2014-09-18 22:46
549 查看
一、RabbitMQ的安装(官网安装教程)
检查rabbitmq默认服务端口5672是否已监听
二、创建用户、虚拟主机、配置权限
三、简单Routing程序
rabbitmq.py
consume1.py
consume2.py
运行代码
#ubuntu安装方式,如果出错,按照终端提示方式进行安装 sudo dpkg -i rabbitmq-server_3.3.5-1_all.deb
检查rabbitmq默认服务端口5672是否已监听
netstat -ntlp | grep LISTEN
二、创建用户、虚拟主机、配置权限
sudo rabbitmqctl add_vhost test sudo rabbitmqctl add_user test 123456 sudo rabbitmqctl set_permissions -p test test ".*" ".*" ".*"
三、简单Routing程序
rabbitmq.py
#-*-coding:utf-8-*- #!/usr/bin/env python #rabbitmq.py import pika import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) mq = { 'host': '127.0.0.1', 'port': 5672, 'user': 'test', 'pwd': '123456', 'virt': 'test', 'exchange': 'securityte', 'exchange_type': 'direct', } class MQRouting(): """rabbitmq操作类,广播消息到所有队列,各队列进行消息筛选""" def __init__(self): credentials = pika.PlainCredentials(mq['user'], mq['pwd']) self.conn = pika.BlockingConnection(pika.ConnectionParameters(mq['host'], mq['port'], mq['virt'], credentials)) self.channel = self.conn.channel() self.channel.exchange_declare(exchange=mq['exchange'], type=mq['exchange_type']) logger.info('Rabbitmq连接已建立,host={}, port={}'.format(mq['host'], mq['port'])) def public(self, routing_key, msg): """发布消息""" self.channel.basic_publish( exchange = mq['exchange'], routing_key = routing_key, body=msg, properties=pika.BasicProperties( delivery_mode = 2, # make message persistent ) ) logger.info('消息已发布:{}:{}'.format(routing_key,msg)) def consume(self, routing_key): """接收消息""" def callback(ch, method, properties, body): print " [x] Received %r" % (body,) self.channel.queue_declare(queue=routing_key) self.channel.queue_bind(exchange=mq['exchange'], queue=routing_key, routing_key=routing_key) self.channel.basic_consume(callback, queue=routing_key, no_ack=True) self.channel.start_consuming() def close(self): if self.channel: self.channel.close() if self.conn: self.conn.close() if __name__ == '__main__': routing = MQRouting() routing.public('first','hello first!') routing.public('second','hello second!')
consume1.py
from rabbitmq import MQRouting routing = MQRouting() routing.consume('first')
consume2.py
from rabbitmq import MQRouting routing = MQRouting() routing.consume('second')
运行代码
python rabbitmq.py python consume1.py python consume2.py
相关文章推荐
- 使用『与非』运算实现权限判断
- 使用product_user_profile来实现用户权限的设定(轉)
- cakephp学习:大部分动作使用Auth组件来控制权限,但少部分不用,如何实现?
- ——使用『与非』运算实现权限判断
- 使用Struts2的拦截器实现权限控制
- 使用product_user_profile来实现用户权限的设定
- 使用AOP实现日志和权限认证组件
- 使用HttpMoudle和IPrincipal实现自定义身份及权限认证
- C#中使用位运算来实现权限管理
- struts2 使用拦截器 实现用户权限的验证
- 使用HttpMoudle和IPrincipal实现自定义身份及权限认证
- 使用product_user_profile来实现用户权限的设定
- 使用product_user_profile来实现用户权限的设定
- C#中使用位运算来实现权限管理
- 使用HttpMoudle和IPrincipal实现自定义身份及权限认证
- 使用filter过滤器实现登陆权限验证
- 使用HttpMoudle和IPrincipal实现自定义身份及权限认证
- 使用filter过滤器实现登陆权限验证
- 使用Struts2的拦截器实现权限控制
- 在yii中使用Filter实现RBAC权限自动判断