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
  • 相关阅读:
    第一章:进销存系统基本功能
    SpringBoot 整合 Docker
    Java的脚本机制、编译器API
    Java 定时任务
    监听文件修改的四种方法
    SpringBoot Actuator — 埋点和监控
    Kafka消息队列
    OpenSSL配置HTTPS
    Java 国际化
    备忘录模式
  • 原文地址:https://www.cnblogs.com/gytangyao/p/11406090.html
Copyright © 2011-2022 走看看