rabbitMQ之direct模型(订阅模式)(智能路由)

注意:在fanout模式中,会被所有订阅的消费者都能接受收到,,但是,在某些场景下,我们希望不同的消息被不同的队列消费,这时就要用到direct类型的exchange
在direct模型中:
1.队列和交换机的绑定,不能是任意绑定了,而是要指定一个RoutingKey。
2.消息的发送方在向Exchange发送消息的时候,也必须指定消息的RouingKey。
3.Exchange不能把消息交给每一个绑定的队列,而是根据消息的RouingKey进行判断,只有队列的RoutingKey
与消息的RoutingKey完全一致,才会接受到消息流程
在这里插入图片描述
图解:
p:生产者,向exchange发送消息时,会指定一个rouingkey,
x:Exchange(交换机),接受生产者的消息,然后把消息递交给与routingkey完全匹配的队列
C1:消费者,其所在队列指定了需要rouing key为error的消息
C2消费者,其所在队列指定了需要rouing key为info、erro、warning的消息。
开发消费者:

package com.example.demo.config.rabbitMQ.direct;

import com.example.demo.config.rabbitMQ.RabbitMQGetConnection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

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

public class ProviderDirect {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection= RabbitMQGetConnection.getConnection();
        Channel channel=connection.createChannel();
        channel.exchangeDeclare("logs_direct","direct");
        String routingKey="warning";
        channel.basicPublish("logs_direct",routingKey,null,("这是基于routingKey模型发布的routingKey="+routingKey).getBytes());
        channel.close();
        connection.close();
    }
}

消费者一代码:

package com.example.demo.config.rabbitMQ.direct;

import com.example.demo.config.rabbitMQ.RabbitMQGetConnection;
import com.rabbitmq.client.*;

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

public class ReciverMessage1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection= RabbitMQGetConnection.getConnection();
        Channel channel=connection.createChannel();
        //参数1:交换机的名称
        //参数2:连接方式direct
        channel.exchangeDeclare("logs_direct","direct");
        //获取队列临时链接的名字
        String queueName=channel.queueDeclare().getQueue();
        //基于route key绑定队列和交换机
        channel.queueBind(queueName,"logs_direct","error");
        //获取消费的消息
        channel.basicConsume(queueName,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者:"+new String(body));
            }
        });


    }
}

消费者2代码:


package com.example.demo.config.rabbitMQ.direct;

import com.example.demo.config.rabbitMQ.RabbitMQGetConnection;
import com.rabbitmq.client.*;

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

public class ReciverMessage2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection= RabbitMQGetConnection.getConnection();
        Channel channel=connection.createChannel();
        channel.exchangeDeclare("logs_direct","direct");
        String queueName=channel.queueDeclare().getQueue();
        channel.queueBind(queueName,"logs_direct","info");
        channel.queueBind(queueName,"logs_direct","error");
        channel.queueBind(queueName,"logs_direct","warning");
        channel.basicConsume(queueName,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2:"+new String(body));
            }
        });
    }
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值