您的位置:首页 > 文旅 > 美景 > RabbitMQ 入门教程

RabbitMQ 入门教程

2025/4/18 19:44:58 来源:https://blog.csdn.net/qq_40698086/article/details/142044766  浏览:    关键词:RabbitMQ 入门教程

概述

RabbitMQ 是一个开源的消息代理和队列服务器,实现高级消息队列协议 (AMQP) 0-9-1。本教程将引导你完成搭建和使用 RabbitMQ 的过程。

安装与配置

安装 RabbitMQ

Ubuntu/Debian

```bash

sudo apt update

sudo apt install rabbitmq-server

```

macOS (Homebrew)

```bash

brew install rabbitmq

```

启动服务

```bash

rabbitmq-server

```

配置环境变量

```bash

export RABBITMQ_NODENAME=rabbit

```

基础概念

- Exchange - 收到消息后将其路由到不同的队列。

- Queue - 存储消息直到它们被消费。

- Binding - 告诉交换机如何将消息路由到队列。

- Routing Key - 交换机向队列发送消息的规则。

Python 示例

安装客户端库

```bash

pip install pika

```

发布消息

```python

import pika

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

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',

routing_key='hello',

body='Hello World!')

print(" [x] Sent 'Hello World!'")

connection.close()

```

接收消息

```python

import pika

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

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

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

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_consume(queue='hello',

on_message_callback=callback,

auto_ack=True)

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

channel.start_consuming()

```

进阶主题

Direct Exchange

```python

import pika

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

channel = connection.channel()

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

severity = sys.argv[1] if len(sys.argv) > 1 else 'info'

message = ' '.join(sys.argv[2:]) or 'Hello World!'

channel.basic_publish(exchange='direct_logs',

routing_key=severity,

body=message)

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

connection.close()

```

Fanout Exchange

```python

import pika

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)

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

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

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

channel.basic_consume(queue=queue_name,

on_message_callback=callback,

auto_ack=True)

channel.start_consuming()

```

Topic Exchange

```python

import pika

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

channel = connection.channel()

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

routing_key = sys.argv[1] if len(sys.argv) > 2 else 'anonymous.info'

message = ' '.join(sys.argv[2:]) or 'Hello World!'

channel.basic_publish(exchange='topic_logs',

routing_key=routing_key,

body=message)

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

connection.close()

```

高级特性

持久化消息

```python

channel.basic_publish(exchange='',

routing_key='hello',

body='Hello World!',

properties=pika.BasicProperties(

delivery_mode = 2, # make message persistent

))

```

事务

```python

channel.confirm_delivery()

channel.basic_publish(exchange='',

routing_key='hello',

body='Hello World!')

```

手动确认消息

```python

channel.basic_consume(queue='hello',

on_message_callback=callback,

auto_ack=False)

```

发布确认

```python

channel.confirm_delivery()

channel.basic_publish(exchange='',

routing_key='hello',

body='Hello World!')

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com