RabbitMQ 是一款广泛使用的消息队列中间件,用于在分布式系统中实现异步通信、提高系统性能、解耦服务等。本文将详细介绍 RabbitMQ 的基础安装与配置、常见使用方法与模式,最后探讨它的应用场景。
一、RabbitMQ 的基础安装与配置
1.1 安装 RabbitMQ
RabbitMQ 需要依赖于 Erlang 环境,因此需要先安装 Erlang。我们以 Ubuntu 为例介绍如何安装和配置 RabbitMQ。
1.1.1 安装 Erlang
-
更新软件包列表:
bash
sudo apt-get update
-
安装 Erlang 依赖包:
bash
sudo apt-get install -y erlang
1.1.2 安装 RabbitMQ
-
添加 RabbitMQ 的 APT 库:
bash
echo 'deb https://dl.bintray.com/rabbitmq/debian bionic main' | sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list
-
添加公钥:
bash
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
-
更新软件包列表并安装 RabbitMQ:
bash
sudo apt-get update sudo apt-get install -y rabbitmq-server
-
启动 RabbitMQ 服务:
bash
sudo systemctl start rabbitmq-server sudo systemctl enable rabbitmq-server
1.2 配置 RabbitMQ
1.2.1 启用插件
RabbitMQ 提供一系列插件以增强其功能,我们首先启用管理插件 rabbitmq_management
。
bash
sudo rabbitmq-plugins enable rabbitmq_management
这将启动一个基于 HTTP 的管理控制台,可以通过浏览器访问:
http://<你的服务器IP>:15672
默认的登录用户名和密码都是 guest
。
1.2.2 创建用户和虚拟主机
为确保安全性,我们创建一个新的用户和虚拟主机(Virtual Host)。
-
添加新用户:
bash
sudo rabbitmqctl add_user myuser mypassword
-
创建虚拟主机:
bash
sudo rabbitmqctl add_vhost myvhost
-
授权新用户访问新虚拟主机:
bash
sudo rabbitmqctl set_permissions -p myvhost myuser ".*" ".*" ".*"
二、RabbitMQ 的使用方法与模式
2.1 基本概念
- 生产者(Producer):发送消息的应用程序。
- 消费者(Consumer):接收消息的应用程序。
- 队列(Queue):消息存储的地方。
- 交换机(Exchange):接收生产者发送的消息,根据路由键分发到绑定的队列上。
- 绑定(Binding):将交换机和队列关联起来的规则。
2.2 工作模式
RabbitMQ 支持多种工作模式,每种模式都适用于不同的场景。
2.2.1 简单队列模式
这是最基本的模式,生产者将消息发送到队列,消费者从队列接收消息。
示例代码:
生产者:
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Send {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
}
}
}
消费者:
import com.rabbitmq.client.*;
public class Recv {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
}
}
2.2.2 工作队列模式
在此模式下,多个消费者同时从队列中获取消息,实现负载均衡。生产者发送的消息在队列中,多个消费者可以并发地处理消息。
示例代码:
生产者和消费者的代码与简单队列模式类似,但我们可以启动多个消费者实例来处理消息。
2.2.3 发布订阅模式(Pub/Sub)
在发布订阅模式下,消息被发送到交换机,多个队列绑定到该交换机,所有绑定的队列都能接收到消息。
示例代码:
生产者:
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class EmitLog {
private final static String EXCHANGE_NAME = "logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String message = "info: Hello World!";
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
}
}
}
消费者:
import com.rabbitmq.client.*;
public class ReceiveLogs {
private final static String EXCHANGE_NAME = "logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
}
}
2.2.4 路由模式
在路由模式下,消息发送到交换机,并根据消息的路由键(routing key)分发到相应绑定的队列中。
示例代码:
生产者:
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class EmitLogDirect {
private final static String EXCHANGE_NAME = "direct_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String severity = "info";
String message = "This is a direct message";
channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
}
}
}
消费者:
import com.rabbitmq.client.*;
public class ReceiveLogsDirect {
private final static String EXCHANGE_NAME = "direct_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String queueName = channel.queueDeclare().getQueue();
String severity = "info";
channel.queueBind(queueName, EXCHANGE_NAME, severity);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
}
}
2.2.5 主题模式
在主题模式下,消息发送到交换机,并根据消息的路由键进行模糊匹配,分发到相应的队列中。
示例代码:
生产者:
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class EmitLogTopic {
private final static String EXCHANGE_NAME = "topic_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String routingKey = "kern.critical";
String message = "A critical kernel error";
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
}
}
}
消费者:
import com.rabbitmq.client.*;
public class ReceiveLogsTopic {
private final static String EXCHANGE_NAME = "topic_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String queueName = channel.queueDeclare().getQueue();
String bindingKey = "kern.*";
channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
}
}
三、RabbitMQ 的应用场景
3.1 异步处理
在一些耗时操作中,比如发送邮件、图片处理等,可以将任务发送到队列中,由专门的消费者来异步处理,从而加快主流程的响应速度。
3.2 系统解耦
通过消息队列,各系统之间不再直接依赖,而是通过消息队列进行通信,实现系统的松耦合,提高系统的灵活性和可维护性。
3.3 流量削峰
在流量高峰期,使用消息队列将高峰期的请求缓冲到队列中,再以平稳的速率进行处理,有效防止后台服务因高并发而被压垮。
3.4 日志收集
在分布式系统中,可以通过消息队列聚合各服务的日志,集中存储在队列中,由专门的消费者来处理和分析日志。
3.5 分布式事务
通过消息队列,可以实现分布式系统中的最终一致性,保证各子系统的数据一致性。
四 不同模式的使用场景
简单说明一下:
模式 | 适用场景简介 |
---|---|
简单队列模式 | 简单任务队列,单任务消息传递。 |
工作队列模式 | 后台任务处理、订单处理、异步电子邮件通知,负载均衡。 |
发布订阅模式 | 实时通知系统、日志系统、实时统计系统,一对多广播消息传递。 |
路由模式 | 多级日志系统、区域订单处理、动态配置更新,根据路由键精准分发消息。 |
主题模式 | 实时数据处理、通知系统和数据同步系统,根据复杂路由规则模糊匹配消息。 |