Message:它是服务器和应用程序之间传送消息数据
Message:本质上就是一段数据,由Properties和PayLoad(Boby)组成
Message常用属性
Delivery mode: 是否持久化,1 : Non-persistent(非持久化),2 : Persistent(持久化)
Headers:
自定义属性:自定义属性一般放到Headers中,进行发送
content_type : 消息内容的类型
content_encoding: 消息内容的编码格式
priority: 消息的优先级,它的值一般是0-9,9的优先级最高
correlation_id:关联id,在我们实际的工作中,一般把他作为消息的唯一Id
reply_to: 用于指定回复的队列的名称,比如某条消息发送失败了让它回到指定的队列中
expiration: 消息的失效时间(毫秒)
message_id: 消息id
timestamp:消息的时间戳
type: 类型
user_id: 用户id
app_id: 应用程序id
cluster_id: 集群id
ProducterApp:生产者
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;
namespace ProducterApp
{
class Program
{
/// 连接配置
/// </summary>
private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() //创建一个工厂连接对象
{
HostName = "192.168.31.30",
UserName = "admin",
Password = "admin",
VirtualHost = "/vhost001", //如果不设置,虚拟主机名称路径默认为 /
Port = 5672, //注意:5672 --是client端通信口 15672 -- 是管理界面ui端口
};
/// <summary>
/// 路由名称
/// </summary>
const string ExchangeName = "exchange001";
//队列名称
const string QueueName = "queue001";
static void Main(string[] args)
{
using (IConnection conn = rabbitMqFactory.CreateConnection()) //创建一个连接
{
using (IModel channel = conn.CreateModel()) //创建一个Channel
{
channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); //声明一个Exchange(交换机)
channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);//声明一个队列
string routingKey = "routing-" + QueueName;//这个routingKey的值可以随便你自己设置,一般情况下直接设置为QueueName的值就行
channel.QueueBind(QueueName, ExchangeName, routingKey: routingKey);
IBasicProperties props = channel.CreateBasicProperties();
props.DeliveryMode = 2; //1:非持久化 2:持续久化 (即:当值为2的时候,我们一个消息发送到服务器上之后,如果消息还没有被消费者消费,服务器重启了之后,这条消息依然存在)
props.Persistent = true;
props.ContentEncoding = "UTF-8"; //注意要大写
props.Expiration = "250000"; //消息过期时间为25秒,(在这15秒之后如果消息没有被消费,消息也过期了。当然你也可以不设置哦)
//props.UserId = "116"; //说实话这个userid我知道怎么用
//props.MessageId = Guid.NewGuid().ToString("N");
//我们也可以设定自定义属性,把自定义的属性放到Headers中进行发送
var dir= new Dictionary<string, object>();
dir.Add("name", "张三");//特别要注意,字符串传递过去的时候是实际是以byte[]数组的形式传递过去的
dir.Add("age", 25);
props.Headers = dir;
for (int i = 0; i < 5; i++)
{
props.MessageId = Guid.NewGuid().ToString("N"); //设定这条消息的MessageId(每条消息的MessageId都是唯一的)
string msg = "你好,这是我的第" + i + "条消息;时间:" + DateTime.Now.ToString("yyyy:MM:dd");
var msgBody = Encoding.UTF8.GetBytes(msg); //发送的消息必须是二进制的
channel.BasicPublish(exchange: ExchangeName, routingKey: routingKey, basicProperties: props, body: msgBody);
}
}
}
}
}
}
CustomerApp:消费者
using RabbitMQ.Client;
using System;
using System.Text;
namespace CustomerApp
{
class Program
{
/// <summary>
/// 连接配置
/// </summary>
private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
{
HostName = "192.168.31.30",
UserName = "admin",
Password = "admin",
Port = 5672,
VirtualHost = "/vhost001",
};
/// <summary>
/// 路由名称
/// </summary>
const string ExchangeName = "exchange001";
//队列名称
const string QueueName = "queue001";
const string routingKey = "routing-" + QueueName;
static void Main(string[] args)
{
using (IConnection conn = rabbitMqFactory.CreateConnection())
{
using (IModel channel = conn.CreateModel())
{
//创建交换机这里将Exchange(交换机)Type设定为fanout 【消费者端的交换机名称要与生产者端的交换机名称保持一致】
channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null);
//创建队列【消费者端需要创建队列,生产者端不需要创建队列】
channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
channel.QueueBind(QueueName, ExchangeName, routingKey: routingKey); //fanout模式下Exchange与Queue绑定不需要指定RoutingKey,所以这里设为空字符串就行
while (true)
{
BasicGetResult msgResponse = channel.BasicGet(QueueName, autoAck: true);//这个true表示消费完这条数据是否删除,true表示删除,false表示不删除
if (msgResponse != null)
{
var msgBody = Encoding.UTF8.GetString(msgResponse.Body);
var msgId = msgResponse.BasicProperties.MessageId;
//var userId = msgResponse.BasicProperties.UserId;
//这里Header中的消息
byte[] namebyte = (byte[])msgResponse.BasicProperties.Headers["name"]; //这里对objet进行强类型转换为byte[]
string name = Encoding.UTF8.GetString(namebyte);
int age = (int)msgResponse.BasicProperties.Headers["age"]; //年龄是int类型的直接强类型转换
Console.WriteLine("姓名:{0} 年龄:{1}", name, age);
//这里是Body中的消息
Console.WriteLine(string.Format("消息内容:{0};MessageId:{1}", msgBody, msgId));
}
}
}
}
}
}
}