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
  • 相关阅读:
    html基础知识点
    uni-app之tabBar的自己配置
    uni-app之导航配置pages.json
    js获取链接?后边的参数名称或者值
    验证码输入自动聚焦下一个input或者删除自动聚焦上一个input
    VUE中/deep/深度作用域
    vue环境下新建项目
    vue中展示数据
    VUE环境项目搭建以及简单的运行例子
    ios设置音乐audio自动播放
  • 原文地址:https://www.cnblogs.com/gytangyao/p/11406090.html
Copyright © 2011-2022 走看看