def route(): channel.exchange_declare(exchange='logs2', exchange_type='direct') message = "info: Hello Yuan!" channel.basic_publish(exchange='logs2', routing_key='info11', # 关键字 body=message) print(" [x] Sent %r" % message) connection.close()
def fabu(): channel.exchange_declare( exchange='logs', # 交换机名称 exchange_type='fanout' # 模式 发布订阅 ) message = "info: Hello World!" # 向logs交换机插入数据 channel.basic_publish( exchange='logs', routing_key='', # 不涉及到关键字所以这里为空 body=message) print(" [x] Sent %r" % message) # 关闭连接 connection.close()
def fabu_always(): channel.exchange_declare( exchange='logs', # 交换机名称 exchange_type='fanout' # 模式 发布订阅 ) x = 1 while True: # time.sleep(.3) message = "info: Hello World 这是我生成的一个任务你来完成它 {}!".format(x) # 向logs交换机插入数据 channel.basic_publish( exchange='logs', routing_key='', # 不涉及到关键字所以这里为空 body=message) print(" [x] Sent %r" % message) x += 1
def dingyue(): # 声明交换机,防止消费者先启动而找不到交换机 channel.exchange_declare(exchange='logs', exchange_type='fanout') # 创建队列 result = channel.queue_declare("", exclusive=True) # 创建随机的队列 queue_name = result.method.queue # 获取队列名字 # 将指定队列绑定到交换机上 channel.queue_bind(exchange='logs', queue=queue_name) print(' [*] Waiting for logs. To exit press CTRL+C') channel.basic_consume(queue=queue_name, auto_ack=True, on_message_callback=callback) channel.start_consuming()
def route_mothed(): channel.exchange_declare(exchange='logs2', exchange_type='direct') result = channel.queue_declare("", exclusive=True) queue_name = result.method.queue severities = ["info11", "warning", "error"] # 创建多个关键字 for severity in severities: channel.queue_bind( exchange='logs2', queue=queue_name, routing_key=severity # 关键字 ) print(' [*] Waiting for logs. To exit press CTRL+C') channel.basic_consume(queue=queue_name, auto_ack=True, on_message_callback=callback) channel.start_consuming()