栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RabbitMq04 direct路由模式

RabbitMq04 direct路由模式

一、


路由模式特点:
队列与交换机的绑定,不能是任意绑定了,而是要指定一个 RoutingKey (路由key)
消息的发送方在 向 Exchange发送消息时,也必须指定消息的 RoutingKey 。
Exchange不再把消息交给每一个绑定的队列,而是根据消息的 Routing Key 进行判断,只有队列的
Routingkey 与消息的 Routing key 完全一致,才会接收到消息

二、案例需求

一个频道
一个交换机(类型为:direct)
两个路由键(info and error)
两个队列(一个队列为info,一个队列为info and error)
一个生产者
两个消费者

三、代码

product:

package com.xiaoxu.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.xiaoxu.ConnectionUtis;

import java.io.IOException;
import java.util.concurrent.TimeoutException;


public class Producter {

    //定义两个消息队列
    static  final  String QUEUE_DIRECT_1 = "queue_direct_1";
    static  final  String QUEUE_DIRECT_2 = "queue_direct_2";
    //defining a exchange
    static final  String EXCHANGE_NAME = "exchange_direct";

    public static void main(String[] args) throws IOException, TimeoutException {
        //1、创建连接工厂
        //2. 创建连接;(抽取一个获取连接的工具类)
        Connection connection = ConnectionUtis.getConnection();
        // 3. 创建频道;
        Channel channel = connection.createChannel();

        //statement exchange
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        //5. 声明队列;
        
        channel.queueDeclare(QUEUE_DIRECT_1,true,false,false,null);
        channel.queueDeclare(QUEUE_DIRECT_2,true,false,false,null);
        // 5. 队列绑定交换机
        channel.queueBind(QUEUE_DIRECT_1, EXCHANGE_NAME, "infor");
        channel.queueBind(QUEUE_DIRECT_2, EXCHANGE_NAME, "error");
        channel.queueBind(QUEUE_DIRECT_2, EXCHANGE_NAME, "infor");

        // 6. 监听队列
        

        String message = "我是一个消息。。infor";
        channel.basicPublish(EXCHANGE_NAME, "infor", null, message.getBytes());
        System.out.println(message);

        message = "我是一个消息。。error";
        channel.basicPublish(EXCHANGE_NAME,"error",null,message.getBytes());
        System.out.println(message);

        //7、关闭资源
        channel.close();
        connection.close();
    }
}

consumer1:

package com.xiaoxu.direct;


import com.rabbitmq.client.*;
import com.xiaoxu.ConnectionUtis;


import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtis.getConnection();
        //创建频道
        Channel channel = connection.createChannel();
        //statement exchange
        channel.exchangeDeclare(Producter.EXCHANGE_NAME,BuiltinExchangeType.FANOUT);
        // create queue
        channel.queueDeclare(Producter.QUEUE_DIRECT_1,true,false,false,null);
        //bainding  queue and exchange
        channel.queueBind(Producter.QUEUE_DIRECT_1,Producter.EXCHANGE_NAME,"infor");


        //creat consumer and set information
        DefaultConsumer consumer1 = new DefaultConsumer(channel) {
            @Override
          public void handleDelivery(String consumerTag, Envelope envelope,
                                     AMQP.BasicProperties properties, byte[] body) throws IOException{

                //路由key
                System.out.println("路由key为:" + envelope.getRoutingKey());
                //交换机
                System.out.println("交换机为:" + envelope.getExchange());
                //消息id
                System.out.println("消息id为:" + envelope.getDeliveryTag());
                //收到的消息
                System.out.println("接收到的消息为:" + new String(body, "utf-8"));

                System.out.println("我是消费者一号。。。");
            }
        };

        //listen to the message
        channel.basicConsume(Producter.QUEUE_DIRECT_1,true,consumer1);
    }
}

consumer2:

package com.xiaoxu.direct;


import com.rabbitmq.client.*;
import com.xiaoxu.ConnectionUtis;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer2 {

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtis.getConnection();
        //创建频道
        Channel channel = connection.createChannel();
        //statement exchange
        channel.exchangeDeclare(Producter.EXCHANGE_NAME,BuiltinExchangeType.FANOUT);
        // create queue
        channel.queueDeclare(Producter.QUEUE_DIRECT_2,true,false,false,null);
        //bainding  queue and exchange
        channel.queueBind(Producter.QUEUE_DIRECT_2,Producter.EXCHANGE_NAME,"infor");
        channel.queueBind(Producter.QUEUE_DIRECT_2,Producter.EXCHANGE_NAME,"error");

        //creat consumer and set information
        DefaultConsumer consumer1 = new DefaultConsumer(channel) {
            @Override
          public void handleDelivery(String consumerTag, Envelope envelope,
                                     AMQP.BasicProperties properties, byte[] body) throws IOException{

                //路由key
                System.out.println("路由key为:" + envelope.getRoutingKey());
                //交换机
                System.out.println("交换机为:" + envelope.getExchange());
                //消息id
                System.out.println("消息id为:" + envelope.getDeliveryTag());
                //收到的消息
                System.out.println("接收到的消息为:" + new String(body, "utf-8"));

                System.out.println("我是消费者二号。。。");
            }
        };

        //listen to the message
        channel.basicConsume(Producter.QUEUE_DIRECT_2,true,consumer1);


    }
}

四、测试




五、小结

启动所有消费者,然后使用生产者发送消息;在消费者对应的控制台可以查看到生产者发送对应routing key对应队
列的消息;到达按照需要接收的效果。
Routing模式要求队列在绑定交换机时要指定routing key,消息会转发到符合routing key的队列。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/439213.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号