美文网首页
发布-订阅

发布-订阅

作者: aq_wzj | 来源:发表于2019-10-16 11:06 被阅读0次

参考 https://www.cnblogs.com/shenh/p/10497244.html

模式一:fanout

  1. 不用指定routing_key
  2. 消息会发给所有的订阅者
  3. 需要先启动订阅者
# 发布者
import json
import pika

credentials = pika.PlainCredentials('root', 'root')  # mq的用户名、密码
connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='10.131.70.49', port=5672,  # mq的IP、端口
    virtual_host='/',
    credentials=credentials
))
channel = connection.channel()
channel.exchange_declare(
    exchange='python-text',  # 指定队列
    durable=True,  # 消息持久化
    exchange_type='fanout' # 发布的模式
)
for i in range(10):
    message = json.dumps({"Order":"1000%s"%i})
    channel.basic_publish(
        exchange='python-text',
        routing_key='',
        body=message,
    )
connection.close()

# 订阅者
import pika

credentials = pika.PlainCredentials('root', 'root')
connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='10.131.70.49', port=5672, virtual_host='/', credentials=credentials))
channel = connection.channel()
# 创建临时队列,consumer关闭后,队列自动删除
result = channel.queue_declare('', exclusive=True)
# 声明exchange,由exchange指定消息在哪个队列传递,如不存在,则创建。durable = True 代表exchange持久化存储,False 非持久化存储
channel.exchange_declare(exchange='python-text', durable=True, exchange_type='fanout')
# 绑定exchange和队列  exchange 使我们能够确切地指定消息应该到哪个队列去
channel.queue_bind(exchange='python-text', queue=result.method.queue)


# 定义一个回调函数来处理消息队列中的消息,这里是打印出来
def callback(ch, method, properties, body):
    ch.basic_ack(delivery_tag=method.delivery_tag)
    print(body.decode())


channel.basic_consume(
    result.method.queue,
    callback,
    # 设置成 False,在调用callback函数时,未收到确认标识,消息会重回队列。True,无论调用callback成功与否,消息都被消费掉
    False)
channel.start_consuming()

模式二:direct

  1. 传递或接受消息时需要指定 routing_key

这里发布了两种,10个OrderID和一个OrderIDd

# 发布
import json
import pika

credentials = pika.PlainCredentials('root', 'root')  # mq的用户名、密码
connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='10.131.70.49', port=5672,  # mq的IP、端口
    virtual_host='/',
    credentials=credentials
))
channel = connection.channel()
channel.exchange_declare(
    exchange='python-text',  # 指定队列
    durable=True,  # 消息持久化
    exchange_type='direct'  # 发布的模式
)
for i in range(10):
    message = json.dumps({"Order": "1000%s" % i})
    channel.basic_publish(
        exchange='python-text',
        routing_key='OrderID',
        body=message,
    )
message = json.dumps({"Order": "others"})
channel.basic_publish(
    exchange='python-text',
    routing_key='OrderIDd',
    body=message,
)
connection.close()

这里订阅OrderID会接收到十条消息

# 订阅OrderID
import pika

credentials = pika.PlainCredentials('root', 'root')
connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='10.131.70.49', port=5672, virtual_host='/', credentials=credentials))
channel = connection.channel()
# 创建临时队列,consumer关闭后,队列自动删除
result = channel.queue_declare('', exclusive=True)
# 声明exchange,由exchange指定消息在哪个队列传递,如不存在,则创建。durable = True 代表exchange持久化存储,False 非持久化存储
channel.exchange_declare(exchange='python-text', durable=True, exchange_type='direct')
# 绑定exchange和队列  exchange 使我们能够确切地指定消息应该到哪个队列去
channel.queue_bind(
    exchange='python-text', queue=result.method.queue,routing_key='OrderID'
)


# 定义一个回调函数来处理消息队列中的消息,这里是打印出来
def callback(ch, method, properties, body):
    ch.basic_ack(delivery_tag=method.delivery_tag)
    print(body.decode())


channel.basic_consume(
    result.method.queue,
    callback,
    # 设置成 False,在调用callback函数时,未收到确认标识,消息会重回队列。True,无论调用callback成功与否,消息都被消费掉
    False)
channel.start_consuming()

这里订阅OrderIDd会接收到一条消息

# 订阅OrderIDd
import pika

credentials = pika.PlainCredentials('root', 'root')
connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='10.131.70.49', port=5672, virtual_host='/', credentials=credentials))
channel = connection.channel()
# 创建临时队列,consumer关闭后,队列自动删除
result = channel.queue_declare('', exclusive=True)
# 声明exchange,由exchange指定消息在哪个队列传递,如不存在,则创建。durable = True 代表exchange持久化存储,False 非持久化存储
channel.exchange_declare(exchange='python-text', durable=True, exchange_type='direct')
# 绑定exchange和队列  exchange 使我们能够确切地指定消息应该到哪个队列去
channel.queue_bind(
    exchange='python-text', queue=result.method.queue,routing_key='OrderIDd'
)


# 定义一个回调函数来处理消息队列中的消息,这里是打印出来
def callback(ch, method, properties, body):
    ch.basic_ack(delivery_tag=method.delivery_tag)
    print(body.decode())


channel.basic_consume(
    result.method.queue,
    callback,
    # 设置成 False,在调用callback函数时,未收到确认标识,消息会重回队列。True,无论调用callback成功与否,消息都被消费掉
    False)
channel.start_consuming()

相关文章

  • 发布-订阅

    参考 https://www.cnblogs.com/shenh/p/10497244.html 模式一:fan...

  • 发布订阅

    https://developer.mozilla.org/zh-CN/docs/Web/API/EventTar...

  • 发布订阅

    期望的数据类型{event: [fn1, fn2],}

  • 发布订阅

    //观察者模式和订阅发布模式的不同点在于,订阅发布模式 订阅者和发布者是解耦的,他们的关联是通过第三方来的//例子:

  • 发布订阅

  • 发布订阅

    最开始是听一位大佬讲 最开始的发布订阅库 是受dom 二级 事件的启发 后来经过封装 形成的一个库 那么 什么是...

  • 发布订阅模式(观察者模式)

    发布订阅模式(观察者模式) 发布订阅也叫观察者模式 发布 && 订阅 使用

  • 设计模式之发布订阅模式(1) 一文搞懂发布订阅模式

    目录 发布/订阅者模式的优点 实现发布/订阅者模式需要考虑的点 何时应使用发布/订阅者模式 发布/订阅者模式与观察...

  • JS-简单实现发布订阅模式

    发布订阅模式主要涉及三个对象:发布者、订阅者、主题对象。 发布-订阅模式 定义  发布-订阅模式又称观察者模式,它...

  • redis 学习(12)-- redis 发布订阅

    redis 发布订阅 发布订阅模式中的角色 发布者(publisher) 订阅者(subscriber) 频道(c...

网友评论

      本文标题:发布-订阅

      本文链接:https://www.haomeiwen.com/subject/fhmymctx.html