python3 实现rabbitmq exchange topic类型

1 生产者 python 实现

# -*- coding: utf-8 -*-

    import pika
    
    hostname = 'localhost'
    port = 5672
    username = 'yangzhen'
    password = '123456'
    
    credentials = pika.PlainCredentials(username=username, password=password)
    parameters = pika.ConnectionParameters(host=hostname,port=port,credentials=credentials)
    connection = pika.BlockingConnection(parameters=parameters)#创建连接
    
    channel = connection.channel()
    
    #创建模糊匹配的exchange
    channel.exchange_declare(exchange='topic_logs', exchange_type='topic')
    
    #这里关键字必须为点号隔开的单词,以便于消费者进行匹配。
    routing_key = '[warn].kern'
    
    message = 'Hello World!'
    channel.basic_publish(exchange='topic_logs', routing_key=routing_key, body=message)
    
    print('[生产者] Send %r:%r' % (routing_key, message))
    connection.close()

2 消费者python实现

# -*- coding: utf-8 -*-
import pika
import sys

hostname = 'localhost'
port = 5672
username = 'yangzhen'
password = '123456'

credentials = pika.PlainCredentials(username=username, password=password)
parameters = pika.ConnectionParameters(host=hostname,port=port,credentials=credentials)
connection = pika.BlockingConnection(parameters=parameters)#创建连接

channel = connection.channel()
channel.exchange_declare(exchange='topic_logs', exchange_type='topic')

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

#绑定键。‘#’匹配所有字符,‘*’匹配一个单词
binding_keys = ['[warn].*', 'info.*']

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('[*] Writing 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=False)

channel.start_consuming()

3 从 web 界面观察

3.1. 生产者运行如下命令
[root@yangzhen rabbitmq]# watch -n 1 py3 send_topic.py
3.2. 消费者运行如下命令
[root@yangzhen rabbitmq]# py3 receive_topic.py
[*] Writing for logs. To exit press CTRL+C.
[x] ‘[warn].kern’:b’Hello World!’
[x] ‘[warn].kern’:b’Hello World!’
[x] ‘[warn].kern’:b’Hello World!’
3.3. 界面观察
在这里插入图片描述
可以看到 exchange 有消息传递。
参考文章:https://www.cnblogs.com/wt11/p/5970297.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RabbitMQ中,动态创建交换机和绑定是通过编程API实现的,通常使用的是AMQP(Advanced Message Queuing Protocol)客户端库,如Python的pika或Java的RabbitMQ Java Client。这里我将用Python的pika库为例来说明如何操作。 首先,确保你已经安装了`pika`库,可以通过`pip install pika`来安装。 1. **连接到RabbitMQ服务器**: ```python import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() ``` 这里假设RabbitMQ服务器运行在本地,并且默认监听的是5672端口。 2. **动态创建交换机**: ```python def create_exchange(exchange_name, exchange_type='direct'): channel.exchange_declare(exchange=exchange_name, exchange_type=exchange_type, durable=True) ``` `durable=True`确保交换机在服务器重启后仍然存在。你可以根据需要更改exchange_type,如`topic`、`fanout`等。 3. **动态创建并绑定交换机**: ```python def bind_exchange_to_queue(queue_name, exchange_name, routing_key): channel.queue_declare(queue=queue_name, durable=True) channel.queue_bind(exchange=exchange_name, queue=queue_name, routing_key=routing_key) # 示例 queue_name = 'my_queue' exchange_name = 'my_exchange' routing_key = 'routing_key_pattern' create_exchange(exchange_name) bind_exchange_to_queue(queue_name, exchange_name, routing_key) ``` `routing_key`用于指定消息发送到哪个队列。 4. **关闭连接**: ```python channel.close() connection.close() ``` 5. **注意事项**: - 以上代码示例为同步版本,实际生产环境中可能需要处理异常和错误回调。 - 如果你的应用程序需要频繁创建和删除交换机,考虑使用异步API或者事件驱动的方式,避免阻塞主线程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值