0


RabbitMQ 入门教程

介绍

RabbitMQ 是一个开源的消息代理和队列服务器,实现了 AMQP 0-9-1 标准。它能够接收、存储并转发消息数据。

安装与配置

1. 安装 RabbitMQ

For Linux (Ubuntu/Debian)


sudo apt update

sudo apt install rabbitmq-server

For macOS (Homebrew)


brew install rabbitmq

2. 启动服务


rabbitmq-server

3. 配置环境变量


export RABBITMQ_NODENAME=my_rabbit

4. 开启管理插件


rabbitmq-plugins enable rabbitmq_management

5. 访问管理界面

发布/订阅模式

发布/订阅模式中,消息生产者不会直接将消息发送给队列。而是通过交换机(Exchange)来完成。

创建生产者


import pika

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

channel = connection.channel()

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

message = "Hello World!"

channel.basic_publish(exchange='logs',

routing_key='',

body=message)

print(" [x] Sent %r" % message)

connection.close()

创建消费者


import pika

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

print(" [x] Received %r" % body)

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

channel = connection.channel()

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

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

channel.queue_bind(exchange='logs', queue=queue_name)

channel.basic_consume(queue=queue_name,

on_message_callback=callback,

auto_ack=True)

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

channel.start_consuming()

工作队列

工作队列可以将任务分发到多个工作者之间,以实现负载均衡。

创建生产者


import pika

import time

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

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

message = "Hello World!"

channel.basic_publish(exchange='',

routing_key='task_queue',

body=message,

properties=pika.BasicProperties(

delivery_mode=2, # make message persistent

))

print(" [x] Sent %r" % message)

connection.close()

创建消费者


import pika

import time

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

print(" [x] Received %r" % body)

time.sleep(body.count(b'.'))

print(" [x] Done")

ch.basic_ack(delivery_tag=method.delivery_tag)

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

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

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

channel.basic_qos(prefetch_count=1)

channel.basic_consume(queue='task_queue',

on_message_callback=callback)

channel.start_consuming()

直接路由

直接路由模式允许消息被精确地路由到指定的队列。

创建生产者


import pika

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

channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

severity = 'info'

message = "Info: Hello World!"

channel.basic_publish(exchange='direct_logs',

routing_key=severity,

body=message)

print(" [x] Sent %r:%r" % (severity, message))

connection.close()

创建消费者


import pika

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

print(" [x] Received %r" % body)

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

channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

severities = ['error', 'info']

for severity in severities:

channel.queue_bind(exchange='direct_logs', queue=queue_name, routing_key=severity)

channel.basic_consume(queue=queue_name,

on_message_callback=callback,

auto_ack=True)

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

channel.start_consuming()

主题订阅

主题模式允许根据绑定键进行模糊匹配,从而将消息发送给多个队列。

创建生产者


import pika

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

channel = connection.channel()

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

routing_key = 'kern.critical'

message = "A critical kernel error"

channel.basic_publish(exchange='topic_logs',

routing_key=routing_key,

body=message)

print(" [x] Sent %r:%r" % (routing_key, message))

connection.close()

创建消费者


import pika

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

print(" [x] %r:%r" % (method.routing_key, body))

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

channel = connection.channel()

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

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

binding_keys = ['kern.*', '*.critical']

for binding_key in binding_keys:

channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key=binding_key)

channel.basic_consume(queue=queue_name,

on_message_callback=callback,

auto_ack=True)

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

channel.start_consuming()

本文转载自: https://blog.csdn.net/qq_40698086/article/details/141804804
版权归原作者 寂然如故 所有, 如有侵权,请联系我们删除。

“RabbitMQ 入门教程”的评论:

还没有评论