rabbitmq
exchange-header
头部交换机,header,web请求时传递参数的一个地方,这里也是类似,它是根据header中的参数来匹配对应的队列
约定:header["x-match"],这是一个特殊的header,取值all或any,all指全部都匹配才匹配,any指只要有一个参数匹配则匹配
定义队列,这里需要指定x-match
Dictionary<string, object> aHeader = new Dictionary<string, object>();
aHeader.Add("format", "pdf");
aHeader.Add("type", "report");
aHeader.Add("x-match", "all");
channel.QueueBind(queue: "queue.A",
exchange: "agreements",
routingKey: string.Empty,
arguments: aHeader);
发布消息指定header,注意,这里不需要指定x-match
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
Dictionary<string, object> mHeader1 = new Dictionary<string, object>();
mHeader1.Add("format", "pdf");
mHeader1.Add("type", "report");
properties.Headers = mHeader1;
channel.BasicPublish(exchange: "agreements",
routingKey: string.Empty,
basicProperties: properties,
body: body);
更多代码
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
// Headers类型的exchange, 名称 agreements
channel.ExchangeDeclare(exchange: "agreements",
type: ExchangeType.Headers,
durable: true,
autoDelete: false,
arguments: null);
// 创建queue.A队列
channel.QueueDeclare(queue: "queue.A", durable: true, exclusive: false, autoDelete: false, arguments: null);
//创建 queue.B 队列
channel.QueueDeclare(queue: "queue.B", durable: true, exclusive: false, autoDelete: false, arguments: null);
//创建 queue.C 队列
channel.QueueDeclare(queue: "queue.C", durable: true, exclusive: false, autoDelete: false, arguments: null);
//绑定 agreements --> queue.A 使用arguments (format=pdf, type=report, x-match=all)
Dictionary<string, object> aHeader = new Dictionary<string, object>();
aHeader.Add("format", "pdf");
aHeader.Add("type", "report");
aHeader.Add("x-match", "all");
channel.QueueBind(queue: "queue.A",
exchange: "agreements",
routingKey: string.Empty,
arguments: aHeader);
//绑定 agreements --> queue.B 使用arguments (format=pdf, type=log, x-match=any)
Dictionary<string, object> bHeader = new Dictionary<string, object>();
bHeader.Add("format", "pdf");
bHeader.Add("type", "log");
bHeader.Add("x-match", "any");
channel.QueueBind(queue: "queue.B",
exchange: "agreements",
routingKey: string.Empty,
arguments: bHeader);
//绑定 agreements --> queue.C 使用arguments (format=zip, type=report, x-match=all)
Dictionary<string, object> cHeader = new Dictionary<string, object>();
cHeader.Add("format", "zip");
cHeader.Add("type", "report");
cHeader.Add("x-match", "all");
channel.QueueBind(queue: "queue.C",
exchange: "agreements",
routingKey: string.Empty,
arguments: cHeader);
string message1 = "hello world";
var body = Encoding.UTF8.GetBytes(message1);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
Dictionary<string, object> mHeader1 = new Dictionary<string, object>();
mHeader1.Add("format", "pdf");
mHeader1.Add("type", "report");
properties.Headers = mHeader1;
//此消息路由到 queue.A 和 queue.B
//queue.A 的binding (format=pdf, type=report, x-match=all)
//queue.B 的binding (format = pdf, type = log, x - match = any)
channel.BasicPublish(exchange: "agreements",
routingKey: string.Empty,
basicProperties: properties,
body: body);
string message2 = "hello world";
body = Encoding.UTF8.GetBytes(message2);
properties = channel.CreateBasicProperties();
properties.Persistent = true;
Dictionary<string, object> mHeader2 = new Dictionary<string, object>();
mHeader2.Add("type", "log");
properties.Headers = mHeader2;
//x-match 配置queue.B
//queue.B 的binding (format = pdf, type = log, x-match = any)
channel.BasicPublish(exchange: "agreements",
routingKey: string.Empty,
basicProperties: properties,
body: body);
string message3= "hello world";
body = Encoding.UTF8.GetBytes(message3);
properties = channel.CreateBasicProperties();
properties.Persistent = true;
Dictionary<string, object> mHeader3 = new Dictionary<string, object>();
mHeader3.Add("format", "zip");
properties.Headers = mHeader3;
//配置失败,不会被路由
channel.BasicPublish(exchange: "agreements",
routingKey: string.Empty,
basicProperties: properties,
body: body);
}