python连接rabbitmq

 

 

 

rabbitmq

生产消费模式

一、简单模式

comsumer.py

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.34.61', port=5672))
channel = connection.channel()

# 声明队列
channel.queue_declare(queue='hello')


def callback(ch, method, properties, body):

    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_consume(queue='hello',
                      on_message_callback=callback,
                      auto_ack=True  # 无应答模式
                      # auto_ack=False  # 应答模式
                      )
View Code

producer.py

import pika

# 创建连接对象
connection = pika.BlockingConnection(pika.ConnectionParameters( host='192.168.34.61', port=5672))
# 获取频道对象
channel = connection.channel()

# 创建队列
channel.queue_declare(queue='hello')


# 向队列插入数据

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='345')

connection.close()
View Code

 二、发布订阅模式(多个comsumer都获取)

comsumer.py

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='192.168.34.61', port=5672))
channel = connection.channel()

#  创建交换机
channel.exchange_declare(exchange='logs',
                         exchange_type='fanout')
# 创建随机队列
result = channel.queue_declare(queue='s1')
# queue_name = result.method.queue # 随机队列名
# print("queue_name",queue_name)

# 将队列绑定到指定的交换机上
channel.queue_bind(exchange='logs',
                   queue="s1")

def callback(ch, method, properties, body):
    print(" [x] %r" % body)

channel.basic_consume(
                      queue='s1',
                      on_message_callback=callback,
                      # auto_ack=True  # 无应答模式
                      auto_ack=True  # 应答模式
)

channel.start_consuming()
View Code
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='192.168.34.61', port=5672))
channel = connection.channel()

#  创建交换机
channel.exchange_declare(exchange='logs',
                         exchange_type='fanout')
# 创建随机队列
result = channel.queue_declare(queue='s2')
# queue_name = result.method.queue # 随机队列名
# print("queue_name",queue_name)

# 将队列绑定到指定的交换机上
channel.queue_bind(exchange='logs',
                   queue="s2")

def callback(ch, method, properties, body):
    print(" [x] %r" % body)

channel.basic_consume(
                      queue='s2',
                      on_message_callback=callback,
                      # auto_ack=True  # 无应答模式
                      auto_ack=True  # 应答模式
)

channel.start_consuming()
View Code

produscer,py

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='192.168.34.61', port=5672))
channel = connection.channel()

channel.exchange_declare(exchange='logs',
                         exchange_type='fanout')


channel.basic_publish(exchange='logs',
                      routing_key='',
                      body="Hi,yuan")

connection.close()
View Code

 

 

posted @ 2019-12-04 14:50  $world  阅读(39)  评论(0编辑  收藏  举报