RabbitMQ 入门教程

引言

RabbitMQ 是一个开源的消息代理和队列服务器,实现高级消息队列协议 (AMQP) 0-9-1。它能帮助开发者构建可靠、可扩展的应用程序,通过消息传递来解耦组件。

安装与配置

安装 RabbitMQ

1. 下载安装包:

- [RabbitMQ Download Page](https://www.rabbitmq.com/download.html)

2. 安装:

- Follow the installation instructions for your platform.

3. 启动服务:

```bash

sudo service rabbitmq-server start

```

配置管理插件

1. 启用管理插件:

```bash

sudo rabbitmq-plugins enable rabbitmq_management

```

2. 重启服务:

```bash

sudo service rabbitmq-server restart

```

3. 访问管理界面:

- Open `http://localhost:15672/` in your web browser.

- Default credentials: `guest/guest`.

快速入门

创建生产者

Python 示例

```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 示例

```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()

```

基本概念

- Exchange: Routes messages to queues.

- Queue: Stores messages until they are sent to consumers.

- Binding: Connects an exchange to a queue.

- Message: Unit of information sent between applications.

模式

发布订阅模式

生产者

```python

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)

```

消费者

```python

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()

```

工作队列模式

生产者

```python

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,))

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

```

消费者

```python

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

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

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

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

ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_qos(prefetch_count=1)

channel.basic_consume(queue='task_queue', on_message_callback=callback)

channel.start_consuming()

```

高级特性

- 持久化: Ensure messages are not lost if RabbitMQ crashes.

- 公平调度: Distribute tasks evenly among workers.

- 确认机制: Confirm that messages have been delivered.

总结

RabbitMQ is a powerful tool for building robust messaging systems. By mastering its basic and advanced features, you can create highly scalable and fault-tolerant applications.

你可能感兴趣的:(ruby,开发语言,后端)