rabbitmq之消息安全

一、消息安全之ack

1、生产者

import pika

# connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1'))

credentials = pika.PlainCredentials("admin","admin")
connection = pika.BlockingConnection(pika.ConnectionParameters('101.133.225.166',credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='myqueue')
channel.basic_publish(exchange='', routing_key='myqueue', body='hello world')
connection.close()

2、消费者

import pika

credentials = pika.PlainCredentials("admin","admin")
connection = pika.BlockingConnection(pika.ConnectionParameters('101.133.225.166',credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='myqueue')

def callback(ch, method, properties, body):
    print("消费者接受到了任务: %r" % body)
    # auto_ack=True,一收到消息就回复确认,队列收到确认就会删除该消息
    # auto_ack=False,不会立即自动回复确认,加上下面这行,待消息处理完了,再回复确认
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_consume(queue='lqz', on_message_callback=callback, auto_ack=False)
channel.start_consuming()

二、消息安全之durable持久化

1、生产者

import pika

# connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1'))

credentials = pika.PlainCredentials("admin","admin")
connection = pika.BlockingConnection(pika.ConnectionParameters('101.133.225.166', credentials=credentials))
channel = connection.channel()
# durable=True,支持持久化,必须新队列
channel.queue_declare(queue='myqueue',durable=True)
channel.basic_publish(exchange='', routing_key='myqueue', body='111', properties=pika.BasicProperties(delivery_mode=2)) # 消息持久化
connection.close()

2、消费者

import pika

credentials = pika.PlainCredentials("admin", "admin")
connection = pika.BlockingConnection(pika.ConnectionParameters('101.133.225.166', credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='lqz1')

def callback(ch, method, properties, body):
    print("消费者接受到了任务: %r" % body)

channel.basic_consume(queue='myqueue', on_message_callback=callback, auto_ack=False)
channel.start_consuming()

三、闲置消费

1、简述:正常情况如果有多个消费者,是按照顺序第一个消息给第一个消费者,第二个消息给第二个消费者,但是可能第一个消息的消费者处理消息很耗时,一直没结束,就可以让第二个消费者优先获得闲置的消息

2、生产者

import pika

# connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1'))

credentials = pika.PlainCredentials("admin","admin")
connection = pika.BlockingConnection(pika.ConnectionParameters('101.133.225.166',credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='myqueue',durable=True)
channel.basic_publish(exchange='', routing_key='myqueue', body='111', properties=pika.BasicProperties(delivery_mode=2))
connection.close()

3、消费者

import pika

credentials = pika.PlainCredentials("admin","admin")
connection = pika.BlockingConnection(pika.ConnectionParameters('101.133.225.166',credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='myqueue')

def callback(ch, method, properties, body):
    print("消费者接受到了任务: %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_qos(prefetch_count=1) # 谁闲置谁获取
channel.basic_consume(queue='lqz123', on_message_callback=callback, auto_ack=False)
channel.start_consuming()
posted @ 2020-09-17 16:13  专业搬砖人士  阅读(202)  评论(0)    收藏  举报