RabbitMQ中的Routing模型

文章目录

  • Routing模型
    • 1.开发生产者
    • 2.开发消费者1
    • 3.开发消费者2
    • 4.执行效果

Routing模型

RabbitMQ中的Routing模型_第1张图片

Routing模型是对Fanout模型的一种改进,Routing模型可以通过交换机给匹配对应的RoutingKey,也就是交换机可以选择相对应的队列名称来发送对应的消息,而不是Fanout模型中给所有队列发消息。

需要注意的是在声明交换机时需要把类型声明为direct类型。

1.开发生产者

package direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import util.RabbitMQUtils;

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

/**
 * Created with IntelliJ IDEA
 * Description:
 * Author: sudi
 * Date: 2020/12/29
 * TIME: 13:07
 */
public class Send {
     
    public static void main(String[] args) throws IOException, TimeoutException {
     
        //获取连接
        Connection connection = RabbitMQUtils.getConnection("192.168.1.18", "/ems", 5672, "ems", "123");
        //获取连接通道对象
        Channel channel = connection.createChannel();

        //声明交换机 参数一:交换机的名字 参数二:交换机的类型
        channel.exchangeDeclare("log_direct", "direct");

        //发送消息
        String routingKey = "waring";
        // 参数一:通道连接交换机的名称
        // 参数二:交换机连接队列的名称
        // 参数三:额外的参数
        // 参数四:发送的消息
        channel.basicPublish("log_direct", routingKey, null, ("hello log_direct" + routingKey).getBytes());

        //关闭连接
        RabbitMQUtils.closeConnectionAndChannel(channel,connection);
    }
}

2.开发消费者1

package direct;

import com.rabbitmq.client.*;
import util.RabbitMQUtils;

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

/**
 * Created with IntelliJ IDEA
 * Description:
 * Author: sudi
 * Date: 2020/12/29
 * TIME: 13:13
 */
public class Accept1 {
     
    public static void main(String[] args) throws IOException, TimeoutException {
     
        //获取连接
        Connection connection = RabbitMQUtils.getConnection("192.168.1.18", "/ems", 5672, "ems", "123");
        Channel channel = connection.createChannel();

        //通道绑定交换机
        channel.exchangeDeclare("log_direct", "direct");

        //声明临时队列
        String queueName = channel.queueDeclare("", false, true, false, null).getQueue();

        //交换机与临时队列绑定
        channel.queueBind(queueName, "log_direct", "info");

        //消费消息
        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));
            }
        });
    }
}

3.开发消费者2

package direct;

import com.rabbitmq.client.*;
import util.RabbitMQUtils;

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

/**
 * Created with IntelliJ IDEA
 * Description:
 * Author: sudi
 * Date: 2020/12/29
 * TIME: 13:13
 */
public class Accept2 {
     
    public static void main(String[] args) throws IOException, TimeoutException {
     
        //获取连接
        Connection connection = RabbitMQUtils.getConnection("192.168.1.18", "/ems", 5672, "ems", "123");
        Channel channel = connection.createChannel();

        //通道绑定交换机
        channel.exchangeDeclare("log_direct", "direct");

        //声明临时队列
        String queueName = channel.queueDeclare("", false, true, false, null).getQueue();

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

        //消费消息
        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));
            }
        });
    }
}

4.执行效果

因为生产者中声明了RoutingKey是Waring,因为消费者2交换机与临时队列绑定时绑定了RoutingKey为Waring,所以只有消费者2能收到消息。而消费者1交换机与临时队列绑定的RoutingKey是Info,所以消费者1收不到消息。

RabbitMQ中的Routing模型_第2张图片
RabbitMQ中的Routing模型_第3张图片

你可能感兴趣的:(RabbitMQ,交换机,队列,rabbitmq,java)