rabbitmq系列——(4 Exchange Type -- Fanout)

标签:http   cli   publish   received   key   collect   extension   local   array   

  发布订阅模式是 生产者 通过这个路由发送到绑定额多个队列中去,然后不同的 消费者 消费不同的队列 从而做到发布订阅模式

1. 生产者

using RabbitMQMsgProducer.MessageProducer;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using RabbitMQMsgProducer.ExchangeDemo;

namespace RabbitMQMsgProducer
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                {
                    // ExchangeType: Fanout
                    // 发布订阅模式是 生产者 通过这个路由发送到绑定额多个队列中去,然后不同的 消费者 消费不同的队列 从而做到发布订阅模式
                    ProducerFanoutExchange.Send();
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace RabbitMQMsgProducer.ExchangeDemo
{
    public class ProducerFanoutExchange
    {
        /// <summary>
        /// 发布订阅模式是 生产者 通过这个路由发送到绑定额多个队列中去,然后不同的 消费者 消费不同的队列 从而做到发布订阅模式
        /// </summary>
        public static void Send()
        {
            var factory = new ConnectionFactory();
            factory.HostName = "localhost";//服务地址
            factory.UserName = "guest";//用户名
            factory.Password = "guest";//密码 
            string exchangeName = "FanoutExchange";
            string queueName001 = "FanoutExchange001";
            string queueName002 = "FanoutExchange002";
            using (var connection = factory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    // 声明exchange
                    channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Fanout, durable: true, autoDelete: false, arguments: null);
                    // 声明queue
                    channel.QueueDeclare(queue: queueName001, durable: true, exclusive: false, autoDelete: false, arguments: null);
                    channel.QueueDeclare(queue: queueName002, durable: true, exclusive: false, autoDelete: false, arguments: null);
                    // 绑定
                    channel.QueueBind(queue: queueName001, exchange: exchangeName, routingKey: string.Empty, arguments: null);
                    channel.QueueBind(queue: queueName002, exchange: exchangeName, routingKey: string.Empty, arguments: null);
                    
                    int i = 1;
                    while (true)
                    {
                        var msg = $"the message is {i}";
                        var body = Encoding.UTF8.GetBytes(msg);
                        //基本发布
                        channel.BasicPublish(exchange: exchangeName,
                                             routingKey: string.Empty,
                                             basicProperties: null,
                                             body: body);
                        Console.WriteLine($"the message _ {msg} _ is send .");
                        Thread.Sleep(2000);
                        i++;
                        if (i > 29)
                        {
                            break;
                        }
                    }

                }
            }
        }
    }
}

 

2. 消费者

2.1  消费者001

using RabbitMQMsgConsumer001.ExchangeDemo;
using RabbitMQMsgConsumer001.MessageConsumer;
using System;
using System.Threading.Tasks;

namespace RabbitMQMsgConsumer001
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                {
                    // ExchangeType: Fanout
                    // 发布订阅模式是 生产者 通过这个路由发送到绑定额多个队列中去,然后不同的 消费者 消费不同的队列 从而做到发布订阅模式
                    ConsumerFanoutExchange.Receive();
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Text;

namespace RabbitMQMsgConsumer001.ExchangeDemo
{
    public class ConsumerFanoutExchange
    {
        public static void Receive()
        {
            var factory = new ConnectionFactory();
            factory.HostName = "localhost";//服务地址
            factory.UserName = "guest";//用户名
            factory.Password = "guest";//密码 
            string exchangeName = "FanoutExchange";
            string queueName001 = "FanoutExchange001";
            //string queueName002 = "FanoutExchange002";
            using (var connection = factory.CreateConnection())
            {
                //创建通道channel
                using (var channel = connection.CreateModel())
                {
                    //声明交换机exchang
                    channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Fanout, durable: true, autoDelete: false, arguments: null);
                    //声明队列queue
                    channel.QueueDeclare(queue: queueName001, durable: true, exclusive: false, autoDelete: false, arguments: null);
                    //绑定
                    channel.QueueBind(queue: queueName001, exchange: exchangeName, routingKey: string.Empty, arguments: null);
                    //定义消费者                                      
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body.ToArray());
                        Console.WriteLine($"consumer 001 received:_ {message} _ , ok.");
                    };
                    Console.WriteLine("the consumer 001 is ready .");
                    //处理消息
                    channel.BasicConsume(queue: queueName001, autoAck: true, consumer: consumer);
                    Console.ReadLine();
                }

            }
        }
    }
}

 

2.2  消费者002

using RabbitMQMsgConsumer002.ExchangeDemo;
using System;

namespace RabbitMQMsgConsumer002
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                {
                    // ExchangeType: Fanout
                    // 发布订阅模式是 生产者 通过这个路由发送到绑定额多个队列中去,然后不同的 消费者 消费不同的队列 从而做到发布订阅模式
                    ConsumerFanoutExchange.Receive();
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Text;

namespace RabbitMQMsgConsumer002.ExchangeDemo
{
    public class ConsumerFanoutExchange
    {
        public static void Receive()
        {
            var factory = new ConnectionFactory();
            factory.HostName = "localhost";//服务地址
            factory.UserName = "guest";//用户名
            factory.Password = "guest";//密码 
            string exchangeName = "FanoutExchange";
            //string queueName001 = "FanoutExchange001";
            string queueName002 = "FanoutExchange002";
            using (var connection = factory.CreateConnection())
            {
                //创建通道channel
                using (var channel = connection.CreateModel())
                {
                    //声明交换机exchang
                    channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Fanout, durable: true, autoDelete: false, arguments: null);
                    //声明队列queue
                    channel.QueueDeclare(queue: queueName002, durable: true, exclusive: false, autoDelete: false, arguments: null);
                    //绑定
                    channel.QueueBind(queue: queueName002, exchange: exchangeName, routingKey: string.Empty, arguments: null);
                    //定义消费者                                      
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body.ToArray());
                        Console.WriteLine($"consumer 002 received:_ {message} _ , ok.");
                    };
                    Console.WriteLine("the consumer 002 is ready .");
                    //处理消息
                    channel.BasicConsume(queue: queueName002, autoAck: true, consumer: consumer);
                    Console.ReadLine();
                }

            }
        }
    }
}

 

3. 结果

技术图片

 

rabbitmq系列——(4 Exchange Type -- Fanout)

标签:http   cli   publish   received   key   collect   extension   local   array   

原文地址:https://www.cnblogs.com/Fletcher/p/14178239.html

版权声明:完美者 发表于 2021-01-19 12:14:10。
转载请注明:rabbitmq系列——(4 Exchange Type -- Fanout) | 完美导航

暂无评论

暂无评论...