使用rabbitmq消息队列

一、前言

  在python中本身就是存在队列queue。一个是线程队列queue,另一个是进程multiprocessing中的队列Queue。

  线程queue:只用于线程之间的数据交互

  进程Queue:用于同一进程下父进程和子进程之间的数据交互,或者同属于一个父进程下的多个子进程之间的交互

二、RabbitMQ

  如果是多个进程间(不同的应用程序之间)、多个系统需要进行数据交互,那么就可以使用消息服务来解决这些问题。消息服务擅长于解决多系统、异构系统间的数据交换(消息通知/通讯)问题,你也可以把它用于系统间服务的相互调用(RPC)。RabbitMQ就是当前最主流的消息中间件之一。

  RabbitMQ的基础介绍:点击查看

  实现最简单的队列通信

  《使用rabbitmq消息队列》

  send端  

# -*- coding: UTF-8 -*-

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))

# 声明一个管道
channel = connection.channel()

# 声明queue
channel.queue_declare(queue='hello')  # queue的名称

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

  receive端  

# -*- coding: UTF-8 -*-

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel()

# You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.

# 这个queue名称就是刚才send端中声明的,再次声明就能保证找到生产者发送的数据
# 如果消费者在生产者之前先启动了,会找不到这个消息队列,就会报错
channel.queue_declare(queue='hello')


def callback(ch, method, properties, body):
    print('--->>', ch, '\n', method, '\n', properties)
    print(" [x] Received %r" % body)

# ch: 声明的管道channel对象内存地址
#

channel.basic_consume(callback,  # 如果收到消息就调用callback函数来处理消息
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

# 永远收下去,没有就在这卡住
channel.start_consuming()

三、连接远程rabbitmq

  连接远程rabbitmq可能会有认证问题,需要输入用户名和密码

  windows下设置用户名、密码和权限:here

  linux环境下设置用户名、密码和权限:here

  基本都在最后面

  send端的设置: 

# -*- conding:utf-8 -*-

import pika

# 认证信息
credentials = pika.PlainCredentials('bigberg', '111111')

connection = pika.BlockingConnection(pika.ConnectionParameters(
    '172.16.200.109', 5672, '/', credentials))

# 声明一个管道
channel = connection.channel()

# 声明queue
channel.queue_declare(queue='hello')  # queue的名称

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='from remote server')
print(" [x] Sent 'from remote server'")
connection.close()

  

 

    原文作者:Bigberg
    原文地址: https://www.cnblogs.com/bigberg/p/8136489.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞