一、rabbitmq五种消息模型简介
RabbitMQ是一个开源的消息队列中间件,支持多种消息传输模式,如发布/订阅、路由、工作队列、RPC等。在消息传输过程中,RabbitMQ将消息存储在队列中并转发给接收方。
RabbitMQ的五种常用消息模型包括:简单队列模型、工作队列模型、发布/订阅模型、路由模型、主题模型。
二、rabbitmq五种消息模型应用场景
1、简单队列模型
应用场景:适用于只有一个消费者的场景,通常用于解耦和最终目标是将消息发布到集体中。
<!-- rabbitmq 生产者 -->
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queueName, false, false, false, null);
string message = "hello rabbitmq";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", queueName, null, body);
Console.WriteLine("Send Message:{0}", message);
}
<!-- rabbitmq 消费者 -->
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Received message: {0}", message);
channel.BasicAck(ea.DeliveryTag, false);
};
channel.BasicConsume(queueName, false, consumer);
2、工作队列模型
应用场景:适用于多个消费者的场景,任务可以平均地在多个消费者之间分配执行。
<!-- rabbitmq 生产者 -->
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queueName, true, false, false, null);
string message = "hello workQueue";
var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
channel.BasicPublish("", queueName, properties, body);
Console.WriteLine("Send Message:{0}", message);
}
<!-- rabbitmq 消费者1 -->
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Received message: {0}", message);
channel.BasicAck(ea.DeliveryTag, false);
};
channel.BasicConsume(queueName, false, consumer);
发送1000条消息,使用两个消费者消费:
3、发布/订阅模型
应用场景:适用于多个消费者需要接收同一个消息的场景。
<!-- rabbitmq 生产者 -->
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchangeName, ExchangeType.Fanout);
string message = "hello fanout";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchangeName, "", null, body);
Console.WriteLine("Send Message:{0}", message);
}
<!-- rabbitmq 消费者1 -->
channel.ExchangeDeclare(exchangeName, ExchangeType.Fanout);
var queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queueName, exchangeName, "");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Received message: {0}", message);
};
channel.BasicConsume(queueName, true, consumer);
启动两个消费者监听同一个队列:
4、路由模型
应用场景:适用于多个消费者根据不同的条件接收不同的消息。
<!-- rabbitmq 生产者 -->
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchangeName, ExchangeType.Direct);
string message = "hello direct";
var routingKey = "error";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchangeName, routingKey, null, body);
Console.WriteLine("Send Message:{0}", message);
}
<!-- rabbitmq 消费者1 -->
var queueName = channel.QueueDeclare().QueueName;
var routingKey = "error";
channel.QueueBind(queueName, exchangeName, routingKey);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Received message: {0}", message);
};
channel.BasicConsume(queueName, true, consumer);
5、主题模型
应用场景:适用于多个消费者通过实现通配符功能接收指定消息。
<!-- rabbitmq 生产者 -->
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchangeName, ExchangeType.Topic);
string message = "hello topic1";
var routingKey = "usa.news";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchangeName, routingKey, null, body);
Console.WriteLine("Send Message:{0}", message);
}
<!-- rabbitmq 消费者1 -->
var queueName = channel.QueueDeclare().QueueName;
var routingKey = "usa.#";
channel.QueueBind(queueName, exchangeName, routingKey);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Received message: {0}", message);
};
channel.BasicConsume(queueName, true, consumer);
三、rabbitmq五种消息模型常用
1、c#
RabbitMQ的C#客户端,使用简单方便。以下是使用C#发送消息和消费消息的代码示例:
<!-- rabbitmq 生产者 -->
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queueName, false, false, false, null);
string message = "hello rabbitmq";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", queueName, null, body);
Console.WriteLine("Send Message:{0}", message);
}
<!-- rabbitmq 消费者 -->
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Received message: {0}", message);
channel.BasicAck(ea.DeliveryTag, false);
};
channel.BasicConsume(queueName, false, consumer);
2、rabbitmq的五种消息模型
简单队列模型、工作队列模型、发布/订阅模型、路由模型、主题模型。
3、rabbitmq消息队列5种模式
简单队列模型、工作队列模型、发布/订阅模型、路由模型、主题模型。
4、rabbitmq消息队列原理
RabbitMQ采用AMQP协议实现,AMQP是Advanced Message Queuing Protocol的缩写,它是一个公开的、通用的、可编程的消息传递协议。它的目的是为面向消息的中间件设计一个标准的应用层传输协议,以便于不同的消息中间件系统可以相互操作。
RabbitMQ基于AMQP1.0协议、Erlang实现,提供了高可靠性、高扩展性、高可用性、高并发性和高性能的消息中间件功能。
5、rabbitmq的四种交换机模式
Direct、Topic、Fanout、Header。
例如,使用Direct交换机模式发送消息:
<!-- rabbitmq 生产者 -->
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchangeName, ExchangeType.Direct);
string message = "hello direct";
var routingKey = "error";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchangeName, routingKey, null, body);
Console.WriteLine("Send Message:{0}", message);
}
使用Direct交换机模式消费消息:
<!-- rabbitmq 消费者 -->
var queueName = channel.QueueDeclare().QueueName;
var routingKey = "error";
channel.QueueBind(queueName, exchangeName, routingKey);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Received message: {0}", message);
};
channel.BasicConsume(queueName, true, consumer);
四、rabbitmq使用场景
RabbitMQ可以应用于高并发、高可用、解耦系统的消息中间件。以下是RabbitMQ的常见应用场景:
1、任务异步处理:RabbitMQ能够异步处理任务,从而提升网站性能;
2、应用解耦:RabbitMQ可以用于应用系统间的解耦;
3、数据同步:RabbitMQ能够将数据同步到多个系统,保证数据的准确性;
4、消息通知:RabbitMQ可以作为消息通知的工具;
5、数据分析:RabbitMQ可以较好地应用于大数据分析的场景。
五、rabbitmq消息模型选取
在选择消息模型时,需要考虑以下因素:
1、消息的类型和特性;
2、消息的生命周期和传输效率;
3、消息的可靠性和幂等性。