• RabbitMQ消息机制广播分发


            public static void SendMessage()
            {
                var factory = new ConnectionFactory();
                factory.HostName = "192.168.1.117";
                factory.UserName = "witbank";
                factory.Password = "123456";
                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        const string EXCHANGE_NAME = "logs";
                        const string ROUTING_KEY = "";
                        channel.ExchangeDeclare(EXCHANGE_NAME, "fanout");//广播
                        while (true)
                        {
                            string message = "请输入要发送的信息";
    
                            Console.WriteLine(message);
                            message = Console.ReadLine();
                            var body = Encoding.UTF8.GetBytes(message);
                            channel.BasicPublish(EXCHANGE_NAME, ROUTING_KEY, null, body);//不需要指定routing key,设置了fanout,指了也没有用.
                            Console.WriteLine(" [x] Sent {0}", message);
                        }
                    }
                }
            }
    

      

            public static void ReceiveMessage()
            {
                var factory = new ConnectionFactory();
                factory.HostName = "192.168.1.117";
                factory.UserName = "witbank";
                factory.Password = "123456";
                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        const string EXCHANGE_NAME = "logs";
                        const string ROUTING_KEY = "";
                        channel.ExchangeDeclare(EXCHANGE_NAME, "fanout");//广播
                        QueueDeclareOk queueOk = channel.QueueDeclare();//每当Consumer连接时,我们需要一个新的,空的queue。因为我们不对老的log感兴趣。幸运的是,如果在声明queue时不指定名字,那么RabbitMQ会随机为我们选择这个名字。
                        ////现在我们已经创建了fanout类型的exchange和没有名字的queue(实际上是RabbitMQ帮我们取了名字)。
                        ////那exchange怎么样知道它的Message发送到哪个queue呢?答案就是通过bindings:绑定。
                        string queueName = queueOk.QueueName;//得到RabbitMQ帮我们取了名字
                        channel.QueueBind(queueName, EXCHANGE_NAME, ROUTING_KEY);//不需要指定routing key,设置了fanout,指了也没有用.
                        var consumer = new QueueingBasicConsumer(channel);
                        channel.BasicConsume(queueName, true, consumer);
                        Console.WriteLine(" [*] Waiting for messages." + "To exit press CTRL+C");
                        while (true)
                        {
                            var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();//挂起的操作
                            var body = ea.Body;
                            var message = Encoding.UTF8.GetString(body);
                            Console.WriteLine(" [x] Received {0}", message);
                        }
                    }
                }
            }
    

      

  • 相关阅读:
    BZOJ 3506 机械排序臂 splay
    BZOJ 2843 LCT
    BZOJ 3669 魔法森林
    BZOJ 2049 LCT
    BZOJ 3223 文艺平衡树 splay
    BZOJ 1433 假期的宿舍 二分图匹配
    BZOJ 1051 受欢迎的牛 强连通块
    BZOJ 1503 郁闷的出纳员 treap
    BZOJ 1096 ZJOI2007 仓库设计 斜率优化dp
    BZOJ 1396: 识别子串( 后缀数组 + 线段树 )
  • 原文地址:https://www.cnblogs.com/happygx/p/6114077.html
Copyright © 2020-2023  润新知