RabbitMQ的交换机的四种类型

RabbitMQ的交换机的四种类型

一、Direct类型交换机

Direct类型交换机

///1.在发送消息的时候,通过Direct类型的路由转发;

//要求Direct类型交换机和队列绑定;绑定需要一个标识,生产者在发送消息的时候,也需要指定一个标识,消息发送给交换机以后,交换机进行标识的匹配;知道和交换机绑定队列完全吻合的标识,只要是匹配到了,就把消息通过交换机转发给当前绑定标识吻合的这个队列中去;

//Direct类型的交换机可以做点啥?

//如果说有一个生产者发送了很多消息,需要把消息分类处理;

//消息需要分几类,就可以定义几个队列;分别把队列和交换机在绑定的是偶,分别给出不同的表示,发送消息的时候,就给出不同标识,就可以把消息发送到不同的经过分类的队列中去了;

//记录日志:记录日志,分类的记录,如果是异常,就需要另外的处理;

//还需要来一个所有日志的记录;

//定义一个记录所有日志的队列,定义一个专门为异常日志存在的队列;

//定义一个Dirct类型的交换机,分别绑定不同的标识,日志生产出来以后,就可以根据日志的类型不同,发给路由,把类型作为标识,路由匹配后,就可以转发到不同的队列中去中,就可以把日志分类处理;

生产者

C# 复制代码
public class DirectExchangeProducer
{
    public static void Send()
    {
        var factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (var connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                #region 删除队列和交换机
                channel.ExchangeDelete("DirectExChange");
                channel.QueueDelete("DirectExchangeLogAllQueue");
                channel.QueueDelete("DirectExchangeErrorQueue");
                #endregion

                channel.QueueDeclare(queue: "DirectExchangeLogAllQueue", durable: true, exclusive: false, autoDelete: false, arguments: null); 
                channel.QueueDeclare(queue: "DirectExchangeErrorQueue", durable: true, exclusive: false, autoDelete: false, arguments: null); 
                //交换机的类型:type:ExchangeType
                channel.ExchangeDeclare(exchange: "DirectExChange", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);

                //定义四种类型的日志
                string[] logtypes = new string[] { "debug", "info", "warn", "error" };

                //把交换机和队列绑定,把所有的日志类型作为标识绑定起来;
                //DirectExchangeLogAllQueue:用来接受所有的日志消息

                //交换机和队列可以绑定多个标识
                foreach (string logtype in logtypes)
                {
                    channel.QueueBind(queue: "DirectExchangeLogAllQueue",
                            exchange: "DirectExChange",
                            routingKey: logtype);
                }

                //针对异常处理的:这里DirectExChange 绑定DirectExchangeErrorQueue,只指定一个标识就是error
                channel.QueueBind(queue: "DirectExchangeErrorQueue",
                          exchange: "DirectExChange",
                          routingKey: "error");


                //通过取模,得到四种类型的日志各自25个日志信息;
                List<LogMsgModel> logList = new List<LogMsgModel>();
                for (int i = 1; i <= 100; i++)
                {
                    if (i % 4 == 0)
                    {
                        logList.Add(new LogMsgModel() { LogType = "info", Msg = Encoding.UTF8.GetBytes($"info第{i}条信息") });
                    }
                    if (i % 4 == 1)
                    {
                        logList.Add(new LogMsgModel() { LogType = "debug", Msg = Encoding.UTF8.GetBytes($"debug第{i}条信息") });
                    }
                    if (i % 4 == 2)
                    {
                        logList.Add(new LogMsgModel() { LogType = "warn", Msg = Encoding.UTF8.GetBytes($"warn第{i}条信息") });
                    }
                    if (i % 4 == 3)
                    {
                        logList.Add(new LogMsgModel() { LogType = "error", Msg = Encoding.UTF8.GetBytes($"error第{i}条信息") });
                    }
                }

                Console.WriteLine("生产者发送100条日志信息");

                logList = logList.OrderBy(l => l.LogType).ToList();

                //发送日志信息
                foreach (var log in logList)
                {
                    channel.BasicPublish(exchange: "DirectExChange",
                                        routingKey: log.LogType,
                                        basicProperties: null,
                                        body: log.Msg);
                    Console.WriteLine($"{Encoding.UTF8.GetString(log.Msg)}  已发送~~");
                }

            }
        }
    }


    public class LogMsgModel
    {
        public string LogType { get; set; }

        public byte[] Msg { get; set; }
    }
}

消费者

C# 复制代码
public class DirectExchangeConsumerLogAll
{
    public static void Consumption()
    {
        var factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (var connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "DirectExchangeLogAllQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);
                channel.ExchangeDeclare(exchange: "DirectExChange", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
                
                string[] logtypes = new string[] { "debug", "info", "warn", "error" };
                foreach (string logtype in logtypes)
                {
                    channel.QueueBind(queue: "DirectExchangeLogAllQueue",
                            exchange: "DirectExChange",
                            routingKey: logtype);
                }

                //消费队列中的所有消息;                                   
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body.ToArray());
                    Console.WriteLine($"【{message}】,写入文本~~");
                };
                //处理消息
                channel.BasicConsume(queue: "DirectExchangeLogAllQueue",
                                     autoAck: true,
                                     consumer: consumer);
                Console.ReadLine();
            }
        }
    }
}

二、fanout 类型交换机

///fanout类型的Exchange路由规则非常简单,它会把所有发送到该Exchange的消息路由到所有与它绑定的Queue中。

//交换机和队列绑定不需要指定标识;对于生产者发过来的消息,发给交换机以后,只要是整个交换机和队列有绑定,交换机就转发给队列;

//生产者发送的消息都可以转发给和他绑定额队列;

//广播式;发布订阅模式;一个生产者把消息发送过去,多个消费者都可以接受到了;

生产者

C# 复制代码
public class FanoutExchange
{
    public static void Send()
    {
        var factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (var connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "FanoutExchangeZhaoxi001", durable: true, exclusive: false, autoDelete: false, arguments: null);
                channel.QueueDeclare(queue: "FanoutExchangeZhaoxi002", durable: true, exclusive: false, autoDelete: false, arguments: null);
                //在这里声明一个Fanout 类型的交换机
                channel.ExchangeDeclare(exchange: "FanoutExchange", type: ExchangeType.Fanout, durable: true, autoDelete: false, arguments: null);
                //交换机绑定队列,不需要标识
                channel.QueueBind(queue: "FanoutExchangeZhaoxi001", exchange: "FanoutExchange", routingKey: string.Empty, arguments: null);
                channel.QueueBind(queue: "FanoutExchangeZhaoxi002", exchange: "FanoutExchange", routingKey: string.Empty, arguments: null);
                
                //在控制台输入消息,按enter键发送消息
                int i = 1;
                while (true)
                {
                    var message = $"通知{i}";
                    if (i>10)
                    {
                        Console.WriteLine("请输入通知~~");
                       message = Console.ReadLine();
                    } 
                    var body = Encoding.UTF8.GetBytes(message);
                    //基本发布
                    channel.BasicPublish(exchange: "FanoutExchange",
                                         routingKey: string.Empty,
                                         basicProperties: null,
                                         body: body);
                    Console.WriteLine($"通知【{message}】已发送到队列");
                    Thread.Sleep(2000);
                    i++;
                }
            }
        }
    }
}

消费者

C# 复制代码
public class FanoutExchange
{
    public static void Consumption()
    {
        var factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (var connection = factory.CreateConnection())
        {
            //创建通道channel
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "FanoutExchangeZhaoxi001", durable: true, exclusive: false, autoDelete: false, arguments: null);
                channel.QueueDeclare(queue: "FanoutExchangeZhaoxi002", durable: true, exclusive: false, autoDelete: false, arguments: null);
                //在这里声明一个Fanout 类型的交换机
                channel.ExchangeDeclare(exchange: "FanoutExchange", type: ExchangeType.Fanout, durable: true, autoDelete: false, arguments: null);
                //交换机绑定队列,不需要标识
                channel.QueueBind(queue: "FanoutExchangeZhaoxi001", exchange: "FanoutExchange", routingKey: string.Empty, arguments: null);
                channel.QueueBind(queue: "FanoutExchangeZhaoxi002", exchange: "FanoutExchange", routingKey: string.Empty, arguments: null);


                //定义消费者                                      
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body.ToArray());
                    //只是为了演示,并没有存入文本文件
                    Console.WriteLine($"消费者0:接收成功!【{message}】,邮件通知");
                };
                Console.WriteLine("消费者0:通知服务准备就绪...");
                //处理消息
                channel.BasicConsume(queue: "FanoutExchangeZhaoxi002",
                                     autoAck: true,
                                     consumer: consumer);
                Console.ReadLine();
            }

        }
    }


    public static void Consumption1()
    {
        var factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (var connection = factory.CreateConnection())
        {
            //创建通道channel
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "FanoutExchangeZhaoxi001", durable: true, exclusive: false, autoDelete: false, arguments: null);
                channel.QueueDeclare(queue: "FanoutExchangeZhaoxi002", durable: true, exclusive: false, autoDelete: false, arguments: null);
                //在这里声明一个Fanout 类型的交换机
                channel.ExchangeDeclare(exchange: "FanoutExchange", type: ExchangeType.Fanout, durable: true, autoDelete: false, arguments: null);
                //交换机绑定队列,不需要标识
                channel.QueueBind(queue: "FanoutExchangeZhaoxi001", exchange: "FanoutExchange", routingKey: string.Empty, arguments: null);
                channel.QueueBind(queue: "FanoutExchangeZhaoxi002", exchange: "FanoutExchange", routingKey: string.Empty, arguments: null);
                //定义消费者                                      
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body.ToArray());
                    //只是为了演示,并没有存入文本文件
                    Console.WriteLine($"消费者1:接收成功!【{message}】,邮件通知");
                };
                Console.WriteLine("消费者1:通知服务准备就绪...");
                //处理消息
                channel.BasicConsume(queue: "FanoutExchangeZhaoxi001",
                                     autoAck: true,
                                     consumer: consumer);
                Console.ReadLine();
            }
        }
    }
}

三、Topic 类型交换机

Topic交换机:可以做到模糊匹配;

Exchange绑定队列需要制定标识 标识 可以有自己的规则;标识可以有占位符、通配符;*/#*匹配一个单词、#匹配多个单词,在Direct基础上加上模糊匹配;

生产者

C# 复制代码
public class TopicExchange
{
    public static void Send()
    {
        var factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (var connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                //声明一个Topic类型的交换机
                channel.ExchangeDeclare(exchange: "TopicExchange", type: ExchangeType.Topic, durable: true, autoDelete: false, arguments: null);

                channel.QueueDeclare(queue: "ChinaQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);

                channel.QueueDeclare(queue: "newsQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);

                channel.QueueBind(queue: "ChinaQueue", exchange: "TopicExchange", routingKey: "China.#", arguments: null);

                channel.QueueBind(queue: "newsQueue", exchange: "TopicExchange", routingKey: "#.news", arguments: null);
                {
                    string message = "来自中国的新闻消息。。。。";
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: "TopicExchange", routingKey: "China.news", basicProperties: null, body: body);
                    Console.WriteLine($"消息【{message}】已发送到队列");
                }

                {
                    string message = "来自中国的天气消息。。。。";
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: "TopicExchange", routingKey: "China.weather", basicProperties: null, body: body);
                    Console.WriteLine($"消息【{message}】已发送到队列");
                }
                {
                    string message = "来自美国的新闻消息。。。。";
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: "TopicExchange", routingKey: "usa.news", basicProperties: null, body: body);
                    Console.WriteLine($"消息【{message}】已发送到队列");
                }
                {
                    string message = "来自美国的天气消息。。。。";
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: "TopicExchange", routingKey: "usa.weather", basicProperties: null, body: body);
                    Console.WriteLine($"消息【{message}】已发送到队列");
                }
            }
        }
    }
}

消费者

C# 复制代码
public class TopicExchange
{
    public static void Consumption()
    {
        var factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (var connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: "TopicExchange", type: ExchangeType.Topic, durable: true, autoDelete: false, arguments: null);
                channel.QueueDeclare(queue: "ChinaQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);
                channel.QueueBind(queue: "ChinaQueue", exchange: "TopicExchange", routingKey: "China.#", arguments: null);
                //定义消费者                                      
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body.ToArray());
                    Console.WriteLine($"接收成功!【{message}】");
                };

                //处理消息
                channel.BasicConsume(queue: "ChinaQueue",
                                     autoAck: true,
                                     consumer: consumer);

                Console.WriteLine("对来自于中国的消息比较感兴趣的 消费者");
            }
        }
    }
}

四、Headers 类型交换机

//规则:headers类型的Exchange不依赖于routing key与binding key的匹配规则来路由消息,而是根据发送的消息内容中的headers属性进行匹配。在绑定Queue与Exchange时指定一组键值对以及x-match参数,x-match参数是字符串类型,可以设置为any或者all。如果设置为any,意思就是只要匹配到了headers表中的任何一对键值即可,all则代表需要全部匹配。

生产者

C# 复制代码
public class HeaderExchange
{
    public static void Send()
    {
        var factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (var connection = factory.CreateConnection())
        {
            using (var channel = connection.CreateModel())
            {

                //声明Headers类型的交换机:HeaderExchange
                channel.ExchangeDeclare(exchange: "HeaderExchange", type: ExchangeType.Headers, durable: false, autoDelete: false, arguments: null);

                channel.QueueDeclare(queue: "HeaderExchangeAllqueue", durable: false, exclusive: false, autoDelete: false, arguments: null);
                channel.QueueDeclare(queue: "HeaderExchangeAnyqueue", durable: false, exclusive: false, autoDelete: false, arguments: null);


                Console.WriteLine("生产者准备就绪....");


                //绑定的时候,需要给arguments 指定一个字典的实例;根据字典中的 { "x-match","all/any"},
                //如果:{ "x-match","all"}, 发送消息的时候,带的参数列表必须和arguments参数中除了x-match以外,其他的必须都具备才能转发到对应的队列中去;
                //如果:{ "x-match","any"},发送消息的时候,带的参数列表必须和arguments参数中除了x-match以外,任何一个能够匹配就转发到该队列中去;

                channel.QueueBind(queue: "HeaderExchangeAllqueue", exchange: "HeaderExchange", routingKey: string.Empty,
                    arguments: new Dictionary<string, object> {
                                                                { "x-match","all"},
                                                                { "teacher","Richard"},
                                                                { "pass","123"}});
                {
                    string message = "teacher和pass都相同时发送的消息";
                    IBasicProperties props = channel.CreateBasicProperties();
                    props.Headers = new Dictionary<string, object>() {
                                                                       { "teacher","Richard"},
                                                                       { "pass","123"}
                                                                      };
                    var body = Encoding.UTF8.GetBytes(message);
                    //基本发布
                    channel.BasicPublish(exchange: "HeaderExchange",
                                         routingKey: string.Empty,
                                         basicProperties: props,
                                         body: body);
                    Console.WriteLine($"消息【{message}】已发送");
                }
                {
                    string message = "teacher和pass有一个不相同时发送的消息";
                    var props = channel.CreateBasicProperties();
                    props.Headers = new Dictionary<string, object>() {
                                                                       { "teacher","Richard"},
                                                                       { "pass","234"}
                                                                      };
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: "HeaderExchange",
                                         routingKey: string.Empty,
                                         basicProperties: props,
                                         body: body);
                    Console.WriteLine($"消息【{message}】已发送");
                }
                Console.WriteLine("**************************************************************");
                {
                    channel.QueueBind(queue: "HeaderExchangeAnyqueue", exchange: "HeaderExchange", routingKey: string.Empty,
                    arguments: new Dictionary<string, object> {
                                        { "x-match","any"},
                                        { "teacher","Richard"},
                                        { "pass","123"},});

                    string msg = "teacher和pass完全相同时发送的消息";
                    var props = channel.CreateBasicProperties();
                    props.Headers = new Dictionary<string, object>() {
                                             { "teacher","Richard"},
                                             { "pass","123"}
                                        };
                    var body = Encoding.UTF8.GetBytes(msg);
                    channel.BasicPublish(exchange: "HeaderExchange",
                                         routingKey: string.Empty,
                                         basicProperties: props,
                                         body: body);
                    Console.WriteLine($"消息【{msg}】已发送");
                }

                {
                    string msg = "teacher和pass有一个不相同时发送的消息";
                    var props = channel.CreateBasicProperties();
                    props.Headers = new Dictionary<string, object>() {
                                             { "teacher","Richard"},
                                             { "pass","234"}
                                        };
                    var body = Encoding.UTF8.GetBytes(msg);
                    channel.BasicPublish(exchange: "HeaderExchange",
                                         routingKey: string.Empty,
                                         basicProperties: props,
                                         body: body);
                    Console.WriteLine($"消息【{msg}】已发送");
                }
            }
        }
        Console.ReadKey();
    }
}

如果:{ "x-match","all"}, 发送消息的时候,带的参数列表必须和arguments参数中除了x-match以外,其他的必须都具备才能转发到对应的队列中去;

如果:{ "x-match","any"},发送消息的时候,带的参数列表必须和arguments参数中除了x-match以外,任何一个能够匹配就转发到该队列中去;

消费者

消费者同前面,因为这种交换机是对发布者的限制

相关推荐
不能再留遗憾了2 小时前
RabbitMQ 高级特性——消息分发
分布式·rabbitmq·ruby
茶馆大橘3 小时前
微服务系列六:分布式事务与seata
分布式·docker·微服务·nacos·seata·springcloud
Envyᥫᩣ4 小时前
C#语言:从入门到精通
开发语言·c#
材料苦逼不会梦到计算机白富美5 小时前
golang分布式缓存项目 Day 1
分布式·缓存·golang
想进大厂的小王6 小时前
项目架构介绍以及Spring cloud、redis、mq 等组件的基本认识
redis·分布式·后端·spring cloud·微服务·架构
Java 第一深情6 小时前
高性能分布式缓存Redis-数据管理与性能提升之道
redis·分布式·缓存
许苑向上6 小时前
【零基础小白】 window环境下安装RabbitMQ
rabbitmq
ZHOU西口7 小时前
微服务实战系列之玩转Docker(十八)
分布式·docker·云原生·架构·数据安全·etcd·rbac
zmd-zk7 小时前
kafka+zookeeper的搭建
大数据·分布式·zookeeper·中间件·kafka
IT技术分享社区10 小时前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法