分类:
RabbitMq
headers类型交换机
是通过 muliple attributes 代替 routing key.
x-match [all/any]
all: 所有地方header头信息必须匹配
any:只要有一个匹配就ok了
说白了就是||和&&的关系
代码】】
any的写法:
if(name=="jack" || age==20)
{
queue1.add(msg);
}
all的写法:
if(name=="jack" && age==20)
{
queue2.add(msg);
}
消费端:
//创建一个连接工厂 ConnectionFactory factory = new ConnectionFactory() { HostName = "192.168.1.63", UserName = "hbadmin", Password = "hbadmin" }; //创建一个连接 using (var conntion = factory.CreateConnection()) { //创建channe using (var channe = conntion.CreateModel()) { //创建交换机(直接形交换机) (已有) channe.ExchangeDeclare("myheadersexchange", ExchangeType.Headers, true, false //创建队列 //姓名,是否持久化,是否是专有的,是否自动删除, //当有很多个队列同时在获取数据时 channe.QueueDeclare("myheadersqueue1", true, false, false, null); var dic = new Dictionary<string, object>(); dic.Add("x-match", "any"); //dic.Add("x-match", "all"); dic.Add("username", "jack"); dic.Add("password", "12345"); //交换机绑定队列 channe.QueueBind("myheadersqueue1", "myheadersexchange", string.Empty, dic); //应当用到事件 { var consumer = new EventingBasicConsumer(channe); consumer.Received += (sender, e) => { string msg = Encoding.UTF8.GetString(e.Body); Console.WriteLine(msg); }; //执行加载的事件 channe.BasicConsume("myheadersqueue1", true, consumer); Console.WriteLine("consumerl端启动完毕!!!"); } Console.Read(); } }
数据服务端:
ConnectionFactory factory = new ConnectionFactory() { HostName = "192.168.1.63", UserName = "hbadmin", Password = "hbadmin" }; //创建一个连接 using (var conntion = factory.CreateConnection()) { //创建channe using (var channe = conntion.CreateModel()) { var properties = channe.CreateBasicProperties(); properties.Headers = new Dictionary<string,object>(); properties.Headers.Add("username", "jack"); properties.Headers.Add("password", "123"); for (int i = 0; i < 100; i++) { var qmsg = $"{i}:你好"; //消息 var msg = Encoding.UTF8.GetBytes(qmsg); //发布消息 channe.BasicPublish("myheadersexchange", routingKey: string.Empty, basicProperties: properties, body: msg); } Console.Read(); } }
当把消费端改为any时:
当把消费端改为all时:
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
评价
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术