1. 程式人生 > >RabbitMQ (六) : 訂閱者模式之路由模式 ( direct )

RabbitMQ (六) : 訂閱者模式之路由模式 ( direct )

efault rect 事件 .get 我們 兩個 create 發送消息 hello

路由模式下,生產者發送消息時需要指定一個路由鍵(routingKey),交換機只會把消息轉發給包含該路由鍵的隊列

這裏,我們改變一下聲明交換機的方式.

我們通過管理後臺添加一個交換機.

技術分享圖片

添加後,生產者和消費者的代碼中就不需要再聲明交換機了.同樣,也可以通過管理後臺添加隊列,那麽代碼中也不需要聲明隊列了.

生產者

    public class Producer
    {

        private const string ExchangeName = "test_exchange_direct";

        public static void Send()
        {
IConnection connection = ConnectionHelper.GetConnection(); IModel channel = connection.CreateModel(); string msg = "hello world "; //把消息發送到交換機,交換機再轉發到包含路由鍵"refuge"的隊列. channel.BasicPublish(ExchangeName, "refuge", null, Encoding.Default.GetBytes(msg)); Console.WriteLine($
"send {msg}"); channel.Close(); connection.Close(); } }

消費者1

    public class Consumer1
    {
        private const string QueueName = "test_exchange1_queue";
        private const string ExchangeName = "test_exchange_direct";

        public static
void Receive() { //獲取連接 RabbitMQ.Client.IConnection connection = ConnectionHelper.GetConnection(); //創建通道 RabbitMQ.Client.IModel channel = connection.CreateModel(); //聲明隊列 channel.QueueDeclare(QueueName, false, false, false, null); //聲明交換機 //channel.ExchangeDeclare(ExchangeName, "direct", false, false, null); //將隊列綁定到交換機上,路由鍵為"wjire" channel.QueueBind(QueueName, ExchangeName, "wjire", null); //添加消費者 EventingBasicConsumer consumer = new EventingBasicConsumer(channel); //註冊事件 consumer.Received += (s, e) => { byte[] bytes = e.Body; string str = Encoding.Default.GetString(bytes); Console.WriteLine("consumer1 : " + str); }; //監聽隊列 channel.BasicConsume(QueueName, true, "", false, false, null, consumer); } }

消費者2

    public class Consumer2
    {
        private const string QueueName = "test_exchange2_queue";
        private const string ExchangeName = "test_exchange_direct";
        public static void Receive()
        {
            //獲取連接
            RabbitMQ.Client.IConnection connection = ConnectionHelper.GetConnection();

            //創建通道
            RabbitMQ.Client.IModel channel = connection.CreateModel();

            //聲明隊列
            channel.QueueDeclare(QueueName, false, false, false, null);

            //聲明交換機
            //channel.ExchangeDeclare(ExchangeName, "direct", false, false, null);

            //將隊列綁定到交換機上,該隊列匹配兩個路由鍵,"refuge"和"wjire"
            channel.QueueBind(QueueName, ExchangeName, "refuge", null);
            channel.QueueBind(QueueName, ExchangeName, "wjire", null);

            //添加消費者
            EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

            //註冊事件
            consumer.Received += (s, e) =>
            {
                byte[] bytes = e.Body;
                string str = Encoding.Default.GetString(bytes);
                Console.WriteLine("         consumer2 : " + str);
            };

            //監聽隊列
            channel.BasicConsume(QueueName, true, "", false, false, null, consumer);
        }
    }

運行結果:

技術分享圖片

可以看到,只有消費者2消費了消息.

RabbitMQ (六) : 訂閱者模式之路由模式 ( direct )