5、Rabbitmq交换机Fanout

本文详细介绍了如何使用Fanout交换机在RabbitMQ中广播消息到多个队列,包括声明交换机、队列绑定、消费者监听及消息发送的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

b2a60e76d5d767d09ff5908847031eeb58b.jpg

当生产者发送消息到Fanout交换机,Fanout会把该消息发送到绑定该交换机上的所有队列。

 

1、声明交换机

    /**
     * 1、声明交换机
     */
    @Test
    public void decalreExchange() throws Exception {

        String exchange = "hello_fanout";
        // 获取到连接
        Connection connection = ConnectionUtil.getConnection();
        // 获取通道
        Channel channel = connection.createChannel();

        // 声明exchange,指定类型为fanout
        channel.exchangeDeclare(exchange, BuiltinExchangeType.FANOUT,true,false,false,new HashMap<>());
    }

 

运行后,可以看到交换机已创建成功

df135557b82803cc838d1d6a5ed35f5a843.jpg

 

2、声明队列并绑定到交换机,这里绑定的时候routing key为空。

    /**
     * 2、声明队列并绑定到交换机
     */
    @Test
    public void decalreQueueAndBind() throws Exception {

        String exchange = "hello_fanout";
        // 获取到连接
        Connection connection = ConnectionUtil.getConnection();
        // 获取通道
        Channel channel = connection.createChannel();

        //将队列hello_fanout_c1 绑定到交换机hello_fanout上
        String queueName1 = "hello_fanout_c1";
        // 声明队列
        channel.queueDeclare(queueName1, false, false, false, null);
        // 绑定队列到交换机
        channel.queueBind(queueName1, exchange, "");

        //将队列hello_fanout_c2 绑定到交换机hello_fanout上
        String queueName2 = "hello_fanout_c2";
        // 声明队列
        channel.queueDeclare(queueName2, false, false, false, null);
        // 绑定队列到交换机
        channel.queueBind(queueName2, exchange, "");

    }

运行后,可以看到,队列已创建成功,并绑定到了交换机上。

69980bc59a65a660d20a6c486ae15f92edf.jpg

 

343db7903599a9f4959e620e676a04c452f.jpg

cdaa60491efb5343df70035d2cf8ff50bdb.jpg

 

定义两个消费者,并启动

//消费者1
@Slf4j
public class Consumer1 {

    public static void main(String[] argv) throws Exception {
        String queueName = "hello_fanout_c1";
        // 获取到连接
        Connection connection = ConnectionUtil.getConnection();
        // 获取通道
        Channel channel = connection.createChannel();

        // 定义队列的消费者
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            // 获取消息,并且处理,这个方法类似事件监听,如果有消息的时候,会被自动调用
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                // body 即消息体
                String msg = new String(body);
                log.debug("Consumer1 consume msg:{}",msg);
            }
        };
        // 监听队列,自动返回完成
        channel.basicConsume(queueName, true, consumer);
    }
}

 

// 消费者2
@Slf4j
public class Consumer2 {


    public static void main(String[] argv) throws Exception {
        String queueName = "hello_fanout_c2";
        // 获取到连接
        Connection connection = ConnectionUtil.getConnection();
        // 获取通道
        Channel channel = connection.createChannel();

        // 定义队列的消费者
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            // 获取消息,并且处理,这个方法类似事件监听,如果有消息的时候,会被自动调用
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                // body 即消息体
                String msg = new String(body);
                log.debug("Consumer1 consume msg:{}",msg);
            }
        };
        // 监听队列,自动返回完成
        channel.basicConsume(queueName, true, consumer);
    }
}

 

发送消息

    /**
     * 生产者发送消息
     * @throws Exception
     */
    @Test
    public void sendMessage() throws Exception {
        String exchange = "hello_fanout";
        // 获取到连接
        Connection connection = ConnectionUtil.getConnection();
        // 获取通道
        Channel channel = connection.createChannel();
        // 消息内容
        String message = "Less is more fanout";
        // 发布消息到Exchange 这里routing key 为"",因为该交换机发送给所有的队列
        channel.basicPublish(exchange, "", null, message.getBytes());
        log.debug("Producer send message:{}",message);
        channel.close();
        connection.close();
    }

 

两个消费者都收到了该条消息

7b5f5a2121ee86dbf136485849047ef0904.jpg

a2ae3d55c015481ec2128c536b1b5229eca.jpg

详细源码地址

https://github.com/suzhe2018/rabbitmq-item

a68c3d9a3172bbc01c3f5dfef8392929015.jpg

 

转载于:https://my.oschina.net/suzheworld/blog/3002696

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值