RabbitMQ

Exchange

exchange 包含4中类型, direct, topic,fanout,header

  • direct :
    exchage 和 queue 进行 binding 时会设置 routingKey ,

    channel.QueueBind(queue: "create_pdf_queue",
                    exchange: "pdf_events",
                    routingKey: "pdf_create",
                    arguments: null);
    

    然后,消息发送时也会设置对应的 routingKey , 只有两个 routingKey 完全相同,exchange才会选择对应的 binding 进行消息路由。

    channel.BasicPublish(exchange: "pdf_events",
                        routingKey: "pdf_create",
                        basicProperties: properties,
                        body: body);
    
  • topic
    类似 direct exchange , 只是此模式 routingKey 可以使用通配符 '*','#'.

  • fanout
    将消息路由到所有绑定的队列中, 无需对消息的 routingKey 进行匹配操作。

  • header
    路由规则是根据 header 进行判断,header 就是下面的 arguments 参数。

    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);
    

    其中 x-match 为特殊的 header , all 表示匹配所有的 header ,如果为 any 表示只要匹配其中一个 header 即可。
    发布消息时传入 header 值。

    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);
    

总结

一般来说direct和topic用来具体的路由消息,如果要用广播的消息一般用fanout的exchange。
header类型用的比较少,但还是知道一点好。

参考文档

https://www.cnblogs.com/julyluo/p/6265775.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,079评论 19 139
  • http://liuxing.info/2017/06/30/Spring%20AMQP%E4%B8%AD%E6%...
    sherlock_6981阅读 16,048评论 2 11
  • 一发一存一消费,没有最好的消息队列中间件(简称消息中间件),只有最合适的消息中间件。消息队列常用的使用场景: 非实...
    ohcomeyes阅读 1,871评论 0 5
  • RocketMQ是一款分布式、队列模型的消息中间件,具有以下特点: 能够保证严格的消息顺序 提供丰富的消息拉取模式...
    AI乔治阅读 2,101评论 2 5
  • 来源 RabbitMQ是用Erlang实现的一个高并发高可靠AMQP消息队列服务器。支持消息的持久化、事务、拥塞控...
    jiangmo阅读 10,415评论 2 34