目录
- 创建用户
- 简单示例
- 一个生产者,两个消费者轮询消费
- exchange类型
- fanout发布和订阅
- direct
- topic
创建用户
```sript
rabbitmqctl add_user admin 123123
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p '/' admin '.' '.' '.'
```
简单示例
send.py
#!/usr/bin/env python
import pikacredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.queue_declare(queue='hello')channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
receive.py
#!/usr/bin/env python
import pikacredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.queue_declare(queue='hello')def callback(ch, method, properties, body):print(" [x] Received %r" % body)channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
一个生产者,两个消费者轮询消费
send.py
#!/usr/bin/env python
import pika
import syscredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.queue_declare(queue='hello')message = ' '.join(sys.argv[1:]) or "Hello World!"channel.basic_publish(exchange='',routing_key='hello',body=message)
print(" [x] Sent %r" % message)
connection.close()
receive.py 两个消费端相同
#!/usr/bin/env python
import pika
import timecredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.queue_declare(queue='hello')def callback(ch, method, properties, body):print(" [x] Received %r" % body)time.sleep(body.count(b'.'))print(" [x] Done")channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
结果
发送:
[root@node1 3]# python send.py 1[x] Sent '1'
[root@node1 3]# python send.py 2[x] Sent '2'
[root@node1 3]# python send.py 3[x] Sent '3'
[root@node1 3]# python send.py 4[x] Sent '4'receive1:[x] Received '2'[x] Done[x] Received '4'[x] Donereceive2:[x] Received '1'[x] Done[x] Received '3'[x] Done
消息确认
确认消息必须在接收方的channel上回复,使用不同的channel将导致异常
send.py
#!/usr/bin/env python
import pika
import syscredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.queue_declare(queue='hello')message = ' '.join(sys.argv[1:]) or "Hello World!"channel.basic_publish(exchange='',routing_key='hello',body=message)
print(" [x] Sent %r" % message)
connection.close()
receive.py
#coding: utf-8
#!/usr/bin/env python
import pika
import timecredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.queue_declare(queue='hello')def callback(ch, method, properties, body):print(" [x] Received %r" % body)time.sleep(10) #模拟处理消息print(" [x] Done")ch.basic_ack(delivery_tag = method.delivery_tag) #手动确认消息处理完毕channel.basic_consume(queue='hello', on_message_callback=callback)#queue='hello', on_message_callback=callback, auto_ack=True) #auto_ack=True 表示消费完以后主动把状态通知rabbitmq 默认是Falseprint(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
查看当前未被确认的消息rabbitmqctl list_queues name messages_ready messages_unacknowledged
消息持久化
虽然,已经做到了消费端挂掉后消息不丢失,但当rabbitmq重启后,会丢失队列和消息,导致消息丢失。除非显示声明。
确保消息不丢失需要做两件事:
队列持久化
channel.queue_declare(queue='hello', durable=True)
消息持久化
python channel.basic_publish(exchange='', routing_key="task_queue", body=message, properties=pika.BasicProperties( delivery_mode = 2, # make message persistent ))
将消息持久化也并不能完全保证消息不丢失。(消息有可能保存到系统缓存,但还未同步到磁盘)
如果需要更强的保证可以使用publisher confirm
rabbitmq 对于已经存在的队列可以重复声明,但需要确保参数相同
公平调度
公平调度。在一个消费者未处理完一个消息之前不要分发新的消息给它,而是将这个新消息分发给另一个不是很忙的消费者进行处理。为了解决这个问题我们可以在消费者代码中使用 channel.basic.qos ( prefetch_count = 1 ),将消费者设置为公平调度。
通过 BasicQos 方法设置prefetchCount = 3。这样RabbitMQ就会使得每个Consumer在同一个时间点最多处理3个Message。换句话说,在接收到该Consumer的ack前,他它不会将新的Message分发给它。
send.py
#!/usr/bin/env python
import pika
import sysconnection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()channel.queue_declare(queue='task_queue', durable=True)message = ' '.join(sys.argv[1:]) or "Hello World!"
channel.basic_publish(exchange='',routing_key='task_queue',body=message,properties=pika.BasicProperties(delivery_mode=2, # make message persistent))
print(" [x] Sent %r" % message)
connection.close()
receive.py
#!/usr/bin/env python
import pika
import timeconnection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')def callback(ch, method, properties, body):print(" [x] Received %r" % body)time.sleep(body.count(b'.'))print(" [x] Done")ch.basic_ack(delivery_tag=method.delivery_tag)channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback)channel.start_consuming()
exchange类型
- fanout
- 把发送到该交换器的消息路由到所有与该交换器绑定的队列中
- direct
- 把消息路由到RoutingKey与BindingKey完全匹配的队列中
- topic
- 与direct相似,不过topic类型可以进行类似正则匹配
- headers
- 不依赖于路由键的匹配规则来路由消息,而是根据发送的消息内容中的headers属性进行匹配(性能差,用的少)
fanout发布和订阅
发布/订阅:向多个消费者传递同一条消息
send.py
#coding: utf-8
#!/usr/bin/env python
import pika
import syscredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.exchange_declare(exchange='logs', exchange_type='fanout')message = ' '.join(sys.argv[1:]) or "info: Hello World!"
channel.basic_publish(exchange='logs', routing_key='', body=message)
print(" [x] Sent %r" % message)
connection.close()
receive.py
#coding: utf-8
#!/usr/bin/env python
import pikacredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.exchange_declare(exchange='logs', exchange_type='fanout')result = channel.queue_declare('', exclusive=True) #创建临时队列,消费者关闭后队列将被删除
queue_name = result.method.queuechannel.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(queue=queue_name, on_message_callback=callback, auto_ack=True)channel.start_consuming()
direct
send.py
#!/usr/bin/env python
import pika
import syscredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.exchange_declare(exchange='direct_logs', exchange_type='direct')severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='direct_logs', routing_key=severity, body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()
receive.py
#!/usr/bin/env python
import pika
import syscredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.exchange_declare(exchange='direct_logs', exchange_type='direct')result = channel.queue_declare('', exclusive=True)
queue_name = result.method.queueseverities = sys.argv[1:]
if not severities:sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])sys.exit(1)for severity in severities: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(queue=queue_name, on_message_callback=callback, auto_ack=True)channel.start_consuming()
使用
[root@node1 5]# python send.py error "Run. Run. Or it will explode."[root@node1 5]# python receive.py warning error
[root@node1 5]# python receive.py error
topic
send.py
#!/usr/bin/env python
import pika
import syscredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.exchange_declare(exchange='topic_logs', exchange_type='topic')routing_key = sys.argv[1] if len(sys.argv) > 2 else 'anonymous.info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='topic_logs', routing_key=routing_key, body=message)
print(" [x] Sent %r:%r" % (routing_key, message))
connection.close()
receive.py
#!/usr/bin/env python
import pika
import syscredentials = pika.PlainCredentials('admin','123123')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.49.135',5673,'/',credentials))
channel = connection.channel()channel.exchange_declare(exchange='topic_logs', exchange_type='topic')result = channel.queue_declare('', exclusive=True)
queue_name = result.method.queuebinding_keys = sys.argv[1:]
if not binding_keys:sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])sys.exit(1)for binding_key in binding_keys:channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key=binding_key)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(queue=queue_name, on_message_callback=callback, auto_ack=True)channel.start_consuming()
参考
https://www.itsvse.com/thread-4667-1-1.html RabbitMQ BasicQos消费者并行处理限制
https://www.cnblogs.com/ccorz/p/5710098.html Python之操作RabbitMQ
https://www.cnblogs.com/wt11/p/5970297.html Python操作rabbitmq 实践笔记
https://www.cnblogs.com/kerwinC/p/5967584.html python RabbitMQ队列使用
https://www.rabbitmq.com/documentation.html 官网