RabbitMQ使用

一、基本使用demo

#producer.py
import pika
#1、连接 RabbitMq服务器
connection = pika.BlockingConnection ( pika.ConnectionParameters(
                                      host = 'localhost') )
#2、创建一个新的频道                                     
channel = connection.channel()
#3、给队列取个名字
channel.queue_declare (queue = 'hello')

#4、用给定的交换机, 路由key来公开频道
channel.basic_publish( exchange = '', routing_key = 'hello', body = 'Hello World')

print("[X] Sent 'Hello World'")
#5、刷写缓冲
connection.close()
___________________________________________________________
#consumer.py
import pika

def callback(ch, method, properties, body):
    print("[x] Received %r" %(body,))

#1、连接服务器
connection = pika.BlockingConnection ( pika.ConnectionParameters(
                                        host = 'localhost') )
#2、创建一个新频道                                       
channel = connection.channel()
#3、给队列取个名字
channel.queue_declare (queue='hello')

print( "[*] Waiting for messages. To exit press CTRL+C")
#设置消息回调,从'hello'队列取数据
channel.basic_consume ( callback, queue='hello', no_ack=True)
channel.start_consuming()

保证任务的完成

  • 如果producer在工作中挂掉,应当重新分配任务。(开启ack)
def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)
    time.sleep( body.count('.') )
    print " [x] Done"
    ch.basic_ack(delivery_tag = method.delivery_tag)

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

开启ack后,一定要记得在调用basic_ack返回ack,不然 server会积聚message

  • Server在工作中挂掉,重启后应该还有Messages。
    将队列声明为持久化(durable)

    channel.queue_declare(queue='hello', durable=True)

    将消息设为持久化(mode=2)

    channel.basic_publish(exchange='',
                      routing_key="task_queue",
                      body=message,
                      properties=pika.BasicProperties(
                         delivery_mode = 2, # make message persistent
                      ))

公平调度

禁止盲目的转发消息,直到worker处理完

channel.basic_qos(prefetch_count=1)

使用交换机

  • 扇形(funout)交换机(将同一个Message路由给绑定它身上的所有队列,无视绑定的路由键。)感觉有点像局域网的广播。

  • 直连(direct)交换机器根据消息携带的路由键将消息投递给对应队列。

  • 主题交换机通过对消息的路由键和队列到交换机的绑定模式之间的匹配,将消息路由给一个或多个队列。

  • 头交换机

默认exchange=“”使用直连交换机,使用扇形交换机的最好场景是发布/订阅
使用扇形交换机

#emit_log.py
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()
#1、声明一个logs的扇形交换机
channel.exchange_declare(exchange='logs',
                         type='fanout')

message = ' '.join(sys.argv[1:]) or "info: Hello World!"
#频道指定,routing_key会被忽略
channel.basic_publish(exchange='logs',
                      routing_key='',
                      body=message)
print " [x] Sent %r" % (message,)
connection.close()
#receive_logs.py
import pika

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

channel = connection.channel()
channel.exchange_declare(exchange='logs',
                         type='fanout')

#exclusive指定,消费者断开连接,就销毁队列
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

#交换机和匿名队列进行绑定
channel.queue_bind(exchange='logs',
                   queue=queue_name)

print ' [*] Waiting for logs. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print " [x] %r" % (body,)

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

channel.start_consuming()

交换机和队列绑定

(拒绝盲目接收)

channel.queue_bind(exchange=exchange_name,
                   queue=queue_name)
#emit_log_direct.py
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='direct_logs',
                         type='direct')

severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
#直连交换机,设置rk。
channel.basic_publish(exchange='direct_logs',
                      routing_key=severity,
                      body=message)
print " [x] Sent %r:%r" % (severity, message)
connection.close()
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='direct_logs',
                         type='direct')

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

severities = sys.argv[1:]
if not severities:
    print >> sys.stderr, "Usage: %s [info] [warning] [error]" % \
                         (sys.argv[0],)
    sys.exit(1)

for severity in severities:
    #在对列上设置rk标签,表示要接受此种rk的信息
    channel.queue_bind(exchange='direct_logs',
                       queue=queue_name,
                       routing_key=severity)

print ' [*] Waiting for logs. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print " [x] %r:%r" % (method.routing_key, body,)

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

channel.start_consuming()

主题交换机使用

主题交换机就是带有“正则“的直连交换机

  • (星号) 用来表示一个单词.

  • (井号) 用来表示任意数量(零个或多个)单词

*.*.DOG     #只要是狗就行 
YELLOW#     #只要是黄色就行 YELLOW.CAT

主题交换机Demo

#logger.py
import pika


routing_key = 'nova.error'
body = 'nova failed to start up'

conn = pika.BlockingConnection ( pika.ConnectionParameters(
                                host = 'localhost'))

channel = conn.channel()

channel.exchange_declare(exchange='logger', type='topic')

channel.basic_publish( exchange='logger',
                        routing_key=routing_key ,
                        body=body)


print "[x] Sent %r:%r"%(routing_key,body)

conn.close()

#——————————————————————————————————————————————————————————————
#recevicer.py
import pika


routing_key = 'nova.#'
conn = pika.BlockingConnection ( pika.ConnectionParameters (
                                 host='localhost'))
channel = conn.channel()

channel.exchange_declare( exchange='logger', type= 'topic')

result = channel.queue_declare ( exclusive=False)

queue_name = result.method.queue

channel.queue_bind (exchange = 'logger', queue = queue_name, routing_key=routing_key)


print ' [*] Waiting for logs. To exit press CTRL+C'

print(routing_key)
def callback(ch, method, properties, body):
    print " [x] %r:%r" % (method.routing_key, body,)

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

channel.start_consuming()

总结:

1、采用默认交换机的时候,consumer和producer的路由键相同名必须相同。交换机两边队列名不需要相等。
2、在扇形交换机中,路由键被忽略,所有与交换机绑定的队列都会收到信号。
3、直连交换机上,利用路由键来筛选出感兴趣的信息。
4、主题交换机,是带正则的直连交换机。

参考资料:http://www.rabbitmq.com/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值