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以外,任何一个能够匹配就转发到该队列中去;

消费者

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

相关推荐
李昊哲小课27 分钟前
deepin 安装 kafka
大数据·分布式·zookeeper·数据分析·kafka
Kobebryant-Manba34 分钟前
zookeeper+kafka的windows下安装
分布式·zookeeper·kafka
_oP_i8 小时前
Pinpoint 是一个开源的分布式追踪系统
java·分布式·开源
攻心的子乐10 小时前
Kafka可视化工具 Offset Explorer (以前叫Kafka Tool)
分布式·kafka
小林想被监督学习10 小时前
RabbitMQ 的7种工作模式
分布式·rabbitmq
码农君莫笑10 小时前
使用blazor开发信息管理系统的应用场景
数据库·信息可视化·c#·.net·visual studio
初晴~12 小时前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
有一个好名字12 小时前
zookeeper分布式锁模拟12306买票
分布式·zookeeper·云原生
可喜~可乐13 小时前
C# WPF开发
microsoft·c#·wpf
yukai0800815 小时前
【最后203篇系列】002 - 两个小坑(容器时间错误和kafka模块报错
分布式·kafka