消息非持久化
消息非持久化的情况下,如果服务器宕机或者异常情况时,消息队列和消息都会不见。
producer端:
import pika
connection=pika.BlockingConnection(pika.ConnectionParameters('localhost'))#连接
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()
consumer端:
import pika
connection=pika.BlockingConnection(pika.ConnectionParameters('localhost'))#连接
channel=connection.channel()#声明一个管道
channel.queue_declare(queue='hello')#声明一个队列
def callback(ch,method,properties,body):
print("[x] received %r"%body)
ch.basic_ack(delivery_tag=method.delivery_tag)#手动确认
channel.basic_consume(callback,queue='hello',no_ack=True)#如果收到消息就调用callback
print('[x] waiting for logs')
channel.start_consuming()#从这里开始收消息,永远收下去
消息持久化
RabbitMQ在服务器端没有声明队列和消息持久时,队列和消息是存在在内存中,服务器宕机了,队列和消息也不会保留。
服务器声明持久化,客户端想接收消息时,必须也要声明queue时也要声明持久化,不然客户端执行会报错。
producer端:
相对于非持久端
在声明队列时:
channel.queue_declare(queue='hello',durable=True)
并且对消息也持久化:
channel.basic_publish(exchange='',routing_key='hello', body="Hello World!",properties= pika.BasicProperties(delivery_mode=2,))
producer端:
import pika
connection=pika.BlockingConnection(pika.ConnectionParameters('localhost'))#连接
channel=connection.channel()#声明一个管道
channel.queue_declare(queue='hello',durable=True)#声明一个队列
channel.basic_publish(exchange='',routing_key='hello', body="Hello World!", properties= pika.BasicProperties(delivery_mode=2,))
print("[x] sent Hello World!")
connection.close()
consumer端:
相对于非持久化的区别,同样声明队列时加了durable=True
channel.basic_qos(prefetch_count=1)#消息公平发放
这个是消息的公平发放,什么意思呢,就是有的机器配置高,处理消息的能力强,但是有的配置低,处理消息的能力弱,当有一个producer发了消息,这个机器处理慢,所以检测到这个情况,producer就先给已经处理完的那个consumer发消息,就不给还没有处理完信息的consumer发消息,直至它处理完才可以接收消息。
consumer端:
import pika
connection=pika.BlockingConnection(pika.ConnectionParameters('localhost'))#连接
channel=connection.channel()#声明一个管道
channel.queue_declare(queue='hello',durable=True)#声明一个队列
def callback(ch,method,properties,body):
print("[x] received %r"%body)
channel.basic_qos(prefetch_count=1)#消息公平发放
channel.basic_consume(callback,queue='hello',no_ack=True)#如果收到消息就调用callback
print('[x] waiting for logs')
channel.start_consuming()#从这里开始收消息,永远收下去
广播模式 exchange,type='fanout'
之前的例子都是1对1的消息发送和接收,即消息只能发送到指定的queue中,但有时候你想消息被所有的queue收到,类似广播的效果,就要用到exchange了。
exchange在定义的时候是有类型的,以决定到底是哪些queue符合条件可以接收消息
fanout:所有bind到此exchange的queue都可以接收消息
publisher端:
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs',
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()
suscriber端:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs',
type='fanout')
result = channel.queue_declare(exclusive=True) #不指定queue名字,rabbit会随机分配一个名字,exclusive=True会在使用此queue的消费者断开后,自动将queue删除
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()
有选择的接收消息 exchange,type='direct'
队列绑定关键词,发送者根据关键字发送消息给exchange,exchange根据关键字发送消息给指定队列
direct:通过routingKey和exchange决定哪个唯一的queue可以接收消息
publisher端:
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!'
channel.basic_publish(exchange='direct_logs',
routing_key=severity,
body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()
suscriber端:
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:
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(callback,
queue=queue_name,
no_ack=True)
channel.start_consuming()
细致的消息过滤广播模式 exchange,type='topic'
topic:所有符合routingKey的routingKey所绑定的queue可以收到消息
publisher端:
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',
type='topic')
routing_key = sys.argv[1] if len(sys.argv) > 1 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()
subscriber端:
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',
type='topic')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
binding_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(callback,
queue=queue_name,
no_ack=True)
channel.start_consuming()
关于RPC