首页 > 其他 > 详细

RabbitMQ Direct交换机代码实现

时间:2021-06-17 09:49:44      阅读:17      评论:0      收藏:0      [点我收藏+]

RabbitMQ交换机有四种类型:Direct,Fanout,Topic,Header

先简单介绍Direct交换机的代码实现

先创建连接

public class MQHelper
    {
        public IConnection GetConnection()
        {
            var factory = new ConnectionFactory();
            factory.HostName = "localhost";
            factory.UserName = "guest";
            factory.Password = "guest";
            return factory.CreateConnection();
        }
    }

直流交换机代码:

public class DirectExchange
    {      
        public void DirectPublish()
        {
            MQHelper mh = new MQHelper();
            using (var conn = mh.GetConnection())
            {
                using(IModel channel = conn.CreateModel())
                {
                    //声明队列
                    channel.QueueDeclare(queue: "DirectLogAll", durable: true, exclusive: false, autoDelete: false, arguments: null);
                    channel.QueueDeclare(queue: "DirectLogError", 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 log in logtypes)
                    {
                        //绑定交换机和队列(所有日志类型队列)
                        channel.QueueBind(queue: "DirectLogAll", exchange: "DirectExchange", routingKey: log);
                    }

                    //错误日志队列
                    channel.QueueBind(queue: "DirectLogError", exchange: "DirectExchange", routingKey: "error");


                    List<LogModel> list = new List<LogModel>();
                    //100条消息
                    for(int i = 1; i <= 100; i++)
                    {
                        if (i % 4 == 0)
                        {
                            list.Add(new LogModel() { LogType = "debug", Msg = Encoding.UTF8.GetBytes($"debug第{i}条消息") });
                        }else if(i % 4 == 1)
                        {
                            list.Add(new LogModel() { LogType = "info", Msg = Encoding.UTF8.GetBytes($"info第{i}条消息") });
                        }
                        else if (i % 4 == 2)
                        {
                            list.Add(new LogModel() { LogType = "warn", Msg = Encoding.UTF8.GetBytes($"warn第{i}条消息") });
                        }
                        else
                        {
                            list.Add(new LogModel() { LogType = "error", Msg = Encoding.UTF8.GetBytes($"error第{i}条消息") });
                        }
                    }

                    //发送
                    foreach(var log in list)
                    {
                        channel.BasicPublish(exchange: "DirectExchange", routingKey: log.LogType, basicProperties: null, body: log.Msg);
                        //记录
                        Console.WriteLine($"{Encoding.UTF8.GetString(log.Msg)} 已发送");
                    }

                }
            }
        }  
    }

这是模拟发送100条日志信息到RabbitMQ,四种类型各25条,根据路由键routingKey分别传到两条队列。其中日志消息

public class LogModel
    {
        public string LogType { get; set; }     //日志类型

        public byte[] Msg { get; set; }        //消息正文
    }

最终在RabbitMQ的本地localhost:15672上可以看到

技术分享图片

这是发送消息到了RabbitMQ上待消费。

RabbitMQ Direct交换机代码实现

原文:https://www.cnblogs.com/AduBlog/p/14891574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!