资讯专栏INFORMATION COLUMN

python开发-实现RabbitMQ的消息队列

EastWoodYang / 669人阅读

摘要:最近在研究做消息队列时,顺便看了一下做消息队列的实现。远程连接时需要认证实例化连接对象实例化链接参数对象创建新的通道模式向绑定到指定的中发送消息,消费者从中取出数据,类似于广播模式发布订阅模式。

最近在研究redis做消息队列时,顺便看了一下RabbitMQ做消息队列的实现。以下是总结的RabbitMQ中三种exchange模式的实现,分别是fanout, direct和topic。

base.py:

import pika
        
        
# 获取认证对象,参数是用户名、密码。远程连接时需要认证
credentials = pika.PlainCredentials("admin", "admin")

# BlockingConnection(): 实例化连接对象
# ConnectionParameters(): 实例化链接参数对象
connection = pika.BlockingConnection(pika.ConnectionParameters(
    "192.168.0.102", 5672, "/", credentials))

# 创建新的channel(通道)
channel = connection.channel()

fanout模式:向绑定到指定exchange的queue中发送消息,消费者从queue中取出数据,类似于广播模式、发布订阅模式。
绑定方式: 在接收端channel.queue_bind(exchange="logs", queue=queue_name)
代码:
publisher.py:

from base import channel, connection
        
# 声明exchange, 不声明queue
channel.exchange_declare(exchange="logs", exchange_type="fanout")  # 广播
message = "hello fanout"
channel.basic_publish(
    exchange="logs",
    routing_key="",
    body=message
)
connection.close()

consumer.py:

from base import channel, connection
        
# 声明exchange
channel.exchange_declare(exchange="logs", exchange_type="fanout")

# 不指定queue名字, rabbitmq会随机分配一个名字, 消息处理完成后queue会自动删除
result = channel.queue_declare(exclusive=True)  

# 获取queue名字
queue_name = result.method.queue

# 绑定exchange和queue
channel.queue_bind(exchange="logs", queue=queue_name)


def callback(ch, method, properties, body):
    print("body:%s" % body)


channel.basic_consume(
    callback,
    queue=queue_name
)


channel.start_consuming()

direct模式:发送端绑定一个routing_key1, queue中绑定若干个routing_key2, 若key1与key2相等,或者key1在key2中,则消息就会发送到这个queue中,再由相应的消费者去queue中取数据。
publisher.py:

from base import channel, connection
            
            
channel.exchange_declare(exchange="direct_test", exchange_type="direct")

message = "hello"

channel.basic_publish(
    exchange="direct_test",
    routing_key="info",  # 绑定key
    body=message
)
connection.close()

consumer01.py:

from base import channel, connection
            
            
channel.exchange_declare(exchange="direct_test", exchange_type="direct")
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue


channel.queue_bind(
    exchange="direct_test",
    queue=queue_name,
    # 绑定的key,与publisher中的相同
    routing_key="info"  
)


def callback(ch, method, properties, body):
    print("body:%s" % body)


channel.basic_consume(
    callback,
    queue=queue_name
)


channel.start_consuming()

consumer02.py:

from base import channel, connection


channel.exchange_declare(exchange="direct_test", exchange_type="direct")
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue


channel.queue_bind(
    exchange="direct_test",
    queue=queue_name,
    # 绑定的key
    routing_key="error"   
)


def callback(ch, method, properties, bosy):
    print("body:%s" % body)


channel.basic_consume(
    callback,
    queue=queue_name
)


channel.start_consuming()

consumer03.py:

from base import channel, connection
            
            
channel.exchange_declare(exchange="direct_test", exchange_type="direct")
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue


key_list = ["info", "warning"]
for key in key_list:
    channel.queue_bind(
        exchange="direct_test",
        queue=queue_name,
        # 一个queue同时绑定多个key,有一个key满足条件时就可以收到数据
        routing_key=key  
    )


def callback(ch, method, properties, body):
    print("body:%s" % body)


channel.basic_consume(
    callback,
    queue=queue_name
)


channel.start_consuming()

执行:

python producer.py
python consumer01.py
python consumer02.py
python consumer03.py

结果:

consumer01.py: body:b"hello"
consumer02.py没收到结果
consumer03.py: body:b"hello"

topic模式不是太好理解,我的理解如下:
对于发送端绑定的routing_key1,queue绑定若干个routing_key2;若routing_key1满足任意一个routing_key2,则该消息就会通过exchange发送到这个queue中,然后由接收端从queue中取出其实就是direct模式的扩展。

绑定方式:
发送端绑定:

    channel.basic_publish(
        exchange="topic_logs",
        routing_key=routing_key,
        body=message
    )

接收端绑定:

    channel.queue_bind(
        exchange="topic_logs",
        queue=queue_name,
        routing_key=binding_key
    )

publisher.py:

import sys
from base import channel, connection


# 声明exchange
channel.exchange_declare(exchange="topic_test", exchange_type="topic")

# 待发送消息
message = " ".join(sys.argv[1:]) or "hello topic"

# 发布消息
channel.basic_publish(
    exchange="topic_test",
    routing_key="mysql.error",   # 绑定的routing_key
    body=message
)
connection.close()

consumer01.py:

from base import channel, connection
            
            
channel.exchange_declare(exchange="topic_test", exchange_type="topic")
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue


channel.queue_bind(
    exchange="topic_test",
    queue=queue_name,
    routing_key="*.error"    # 绑定的routing_key
)


def callback(ch, method, properties, body):
    print("body:%s" % body)


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


channel.start_consuming()

consumer02.py:

from base import channel, connection
            
            
channel.exchange_declare(exchange="topic_test", exchange_type="topic")
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue


channel.queue_bind(
    exchange="topic_test",
    queue=queue_name,
    routing_key="mysql.*"    # 绑定的routing_key
)


def callback(ch, method, properties, body):
    print("body:%s" % body)


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


channel.start_consuming()

执行:

python publisher02.py "this is a topic test"
python consumer01.py
python consumer02.py

结果:

consumer01.py的结果: body:b"this is a topic test"
consumer02.py的结果: body:b"this is a topic test"

说明通过绑定相应的routing_key,两个消费者都收到了消息

将publisher.py的routing_key改成"mysql.info"
再此执行:

python publisher02.py "this is a topic test"
python consumer01.py
python consumer02.py

结果:

consumer01.py没收到结果
consumer02.py的结果: body:b"this is a topic test"

通过这个例子我们就能明白topic的运行方式了。

参考自: https://blog.csdn.net/fgf00/a...

今天就说到这里,如有问题,欢迎交流指正!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/44948.html

相关文章

  • rabbitmq direct reply-to 在springAMQP和python之间使用

    摘要:在发送后端监听声明的排他队列,当收到消息后比对正确则处理消息断开监听连接,然后此队列被系统自动回收。并且通过也看到了这条消息的返回。此时我们基本已经将问题锁定在端了。 背景 公司的一个项目使用rabbitmq作为broker进行交互,并且数据的查询方法使用RPC模式,RPC Client端使用java编写并使用springAMQP包与rabbitmq交互,在RPC Server端使用p...

    mikasa 评论0 收藏0
  • 转: RabbitMQ与PHP(一)

    摘要:需要特别明确的概念交换机的持久化,并不等于消息的持久化。消息的处理,是有两种方式,一次性。在上述示例中,使用的,意味着接收全部的消息。注意与是两个不同的队列。后端处理,可以针对每一个启动一个或多个,以提高消息处理的实时性。 RabbitMQ与PHP(一) 项目中使用RabbitMQ作为队列处理用户消息通知,消息由前端PHP代码产生,处理消息使用Python,这就导致代码一致性问题,调...

    wpw 评论0 收藏0
  • MQ对比之RabbitMQ & Redis

    摘要:消息队列选择是一个由开发的的开源实现的产品,是一个消息代理,从生产者接收消息并传递消息至消费者,期间可根据规则路由缓存持久化消息。绑定队列和交换机之间的关系。根据消息的属性和的属性来转发消息。 消息队列选择:RabbitMQ & Redis RabbitMQ RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现的产品,Rabbi...

    notebin 评论0 收藏0
  • rabbitmq中文教程python版 - 介绍

    摘要:每当我们收到一条消息,这个回调函数就被皮卡库调用。接下来,我们需要告诉这个特定的回调函数应该从我们的队列接收消息为了让这个命令成功,我们必须确保我们想要订阅的队列存在。生产者计划将在每次运行后停止欢呼我们能够通过发送我们的第一条消息。 源码:https://github.com/ltoddy/rabbitmq-tutorial 介绍 RabbitMQ是一个消息代理:它接受和转发消息。你...

    yimo 评论0 收藏0

发表评论

0条评论

EastWoodYang

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<