zoukankan      html  css  js  c++  java
  • 4.Direct交换机之使用指定routingkey完成日志记录场景

    标题 :
    4.Direct交换机之使用指定routingkey完成日志记录场景
    目录 :
    RabbitMQ
    序号 :
    4

            const string logOthersQueueName = "log-queue-others";
    



    var connection = connectionFactory.CreateConnection();
    var channel = connection.CreateModel();

    //设置服务质量
    channel.BasicQos(0, 1, false);

    //定义一个类型为Direct的交换机
    channel.ExchangeDeclare(exchangeName, ExchangeType.Direct, true, false, null);


    //定义一个队列,将用于存放日志级别为 Warn,Info,Debug的日志
    channel.QueueDeclare(queue: logOthersQueueName, durable: true, exclusive: false, autoDelete: false, arguments: null);



    //将debug,info,warn 都绑定到log-queue-others"队列中
    var routingKeys = new[] { "info", "warn", "debug" };
    foreach (var routingKey in routingKeys)
    {
    channel.QueueBind(logOthersQueueName, exchangeName, routingKey, null);
    }

    //定义消费者
    var consumer = new EventingBasicConsumer(channel);
    consumer.Received += (model, ea) =>
    {
    var body = ea.Body;
    var message = Encoding.UTF8.GetString(body);
    Console.WriteLine(" [x] Received {0} " + DateTime.Now, message);
    channel.BasicAck(ea.DeliveryTag, false);
    };
    channel.BasicConsume(queue: logOthersQueueName, false, consumer: consumer);


    Console.WriteLine(" Press [enter] to exit.");
    Console.ReadLine();
    }
    }
    }

    - 这里我们把log-queue-others队列绑定到了交换机上,并指定了routingKey为info,warn和debug.这样会产生三组路由.info,warn,debug三种级别的日志均被路由到log-queue-others队列上
    - 按照预期设计,将消费发送序号!=10的倍数的日志.
    ![](https://www.showdoc.cc/server/api/common/visitfile/sign/0461d279c67986a58dce83d93c7db102?showdoc=.jpg)
    ​
    ###引用链接
    https://www.rabbitmq.com/tutorials/tutorial-four-dotnet.html
    请尽量按照自己期望的生活 email:18980489167@189.cn
  • 相关阅读:
    Delphi 字符串操作
    SQL Browser (数据浏览器)
    .NET 3.5和VS 2008中的ASP.NET AJAX(转帖)
    delphi 最快速编码 URLDecode URLEncode
    Delphi 2007 如何安装控件
    Delphi TWebBrowser编程简述(转帖)
    delphi TStringList的用法
    Javascript+xmlhttp调用Webservice以及注意事项
    使用VSS 的Shadow folder的一点问题
    delphi 如何将XML格式的字符串导入ClientDataSet中
  • 原文地址:https://www.cnblogs.com/gytangyao/p/11406090.html
Copyright © 2011-2022 走看看