zoukankan      html  css  js  c++  java
  • 5.Fanout交换机之新订单通知商户场景

    标题 :
    5.Fanout交换机之新订单通知商户场景
    目录 :
    RabbitMQ
    序号 :
    5

            const string newOrderQueueName = "neworder-queue-sms";
    



    var connection = connectionFactory.CreateConnection();
    var channel = connection.CreateModel();
    //设置服务质量
    channel.BasicQos(0, 1, false);
    //定义一个类型为Fanout的交换机
    channel.ExchangeDeclare(exchangeName, ExchangeType.Fanout, true, false, null);
    //定义一个队列,将用于存放顾客下的订单
    channel.QueueDeclare(queue: newOrderQueueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
    //将队列绑定到交换机上
    channel.QueueBind(newOrderQueueName, exchangeName, "2", null);
    //定义消费者
    var consumer = new EventingBasicConsumer(channel);
    consumer.Received += (model, ea) =>
    {
    var body = ea.Body;
    var message = Encoding.UTF8.GetString(body);
    Console.WriteLine(" [x] 短信消费者,需要把订单信息通过短信通知给商家. 这是订单信息:{0}" , message);
    channel.BasicAck(ea.DeliveryTag, false);
    };
    channel.BasicConsume(queue: newOrderQueueName, false, consumer: consumer);

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

    ​
    - 需要将交换机类型设置为ExchangeType.Fanout
    - 需要将队列绑定到我们定义的fanout交换机上
    ![](https://www.showdoc.cc/server/api/common/visitfile/sign/370756d47726a5775254c2b4863f2f28?showdoc=.jpg)
    ​
    ​
    ### 另外的实验
    ​
    - 忽略routingKey的实验,可以看到fanout交换机忽略了routingKey
    ​
    ```csharp
    #发送
    channel.BasicPublish(exchangeName, routingKey: "1", basicProperties: null, body: body);
    ​
    #消费
    channel.QueueBind(newOrderQueueName, exchangeName, "2", null);
    channel.QueueBind(newOrderQueueName, exchangeName, "3", null);
    

    引用链接

    http://rubybunny.info/articles/exchanges.html

    请尽量按照自己期望的生活 email:18980489167@189.cn
  • 相关阅读:
    [20190507]sga_target=0注意修改_kghdsidx_count设置.txt
    进程与线程的区别和联系
    html option选中 回显 取值
    application.properties详解 --springBoot配置文件
    Address already in use: JVM_Bind:8080错误的解决办法
    垃圾回收监视和分析
    垃圾回收器种类
    Java垃圾回收是如何工作的?
    Java垃圾回收简介
    关于堆栈的详细讲解
  • 原文地址:https://www.cnblogs.com/gytangyao/p/11406093.html
Copyright © 2011-2022 走看看