rabbitMq第四种模型--direct

在Fanout模式中,一条消息,会被所有订阅的队列都消费。
但是,在某些场景下,我们希望不同的消息被不同的队列消费。
这时就要用到Direct类型的Exchange。

在Direct模型下:队列与交换机的绑定,不能是任意绑定了,
而是要指定一个RoutingKey(路由key)
消息的发送方在 向 Exchange发送消息时,
也必须指定消息的 RoutingKey。

Exchange不再把消息交给每一个绑定的队列,
而是根据消息的Routing Key进行判断,
只有队列的Routingkey与消息的 Routing key完全一致,
才会接收到消息

rabbitMq第四种模型--direct_第1张图片
生产者

package com.zuoan.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.zuoan.utils.RabbitMQUtils;

import java.io.IOException;

/**
 * @Description: TODO
 * @Author: 黄石军
 * @CreateTime: 2022/4/10 15:05
 * @Company:
 */
public class Provider {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtils.getConnection("192.168.188.100");

        Channel channel = connection.createChannel();
        //参数一交换机名称,参数二,路由模式
        channel.exchangeDeclare("logs_routing","direct");

        //发送消息
        String routingKey = "error";
        channel.basicPublish("logs_routing",routingKey,null,("这是directt发布的routingKey"+routingKey+"发送的消息").getBytes());

        //关闭资源
        RabbitMQUtils.closeChannelAndConnection(channel,connection);
    }
}

消费者1

package com.zuoan.routing;

import com.rabbitmq.client.*;
import com.zuoan.utils.RabbitMQUtils;

import java.io.IOException;

/**
 * @Description: TODO
 * @Author: 黄石军
 * @CreateTime: 2022/4/10 15:32
 * @Company:
 */
public class Consumer {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtils.getConnection("192.168.188.100");

        Channel channel = connection.createChannel();
        //声明交换机以及交换类型
        channel.exchangeDeclare("logs_routing","direct");

        //创建临时队列
        String queue = channel.queueDeclare().getQueue();

        //基于路由key绑定队列和交换机
        channel.queueBind(queue,"logs_routing","error");

        //消费消息
        channel.basicConsume(queue,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));
            }
        });
    }
}

消费者2

package com.zuoan.routing;

import com.rabbitmq.client.*;
import com.zuoan.utils.RabbitMQUtils;

import java.io.IOException;

/**
 * @Description: TODO
 * @Author: 黄石军
 * @CreateTime: 2022/4/10 15:31
 * @Company:
 */
public class Consumer1 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtils.getConnection("192.168.188.100");

        Channel channel = connection.createChannel();

        channel.exchangeDeclare("logs_routing","direct");

        //创建临时队列
        String queue = channel.queueDeclare().getQueue();

        //临时队列绑定交换机
        channel.queueBind(queue,"logs_routing","info");
        channel.queueBind(queue,"logs_routing","error");
        channel.queueBind(queue,"logs_routing","waring");

        //消费消息
        channel.basicConsume(queue,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者1"+new String(body));
            }
        });
    }
}

根据工具类在我博客分类MQ第一章里
https://blog.csdn.net/weixin_45886609/article/details/124064801

你可能感兴趣的:(MQ,rabbitmq,中间件)