刚刚我们说的Pub/Sub
订阅模式中介绍了Exchange
,Exchange
有四种类型(direct
、topic
、headers
、fanout
),Pub/Sub
订阅强调的是Fanout
类型的Exchange
,Routing
模式则强调的是Exchange
的direct
模式,Routing
模式也叫直连模式
;
Routing模式官网介绍:https://www.rabbitmq.com/tutorials/tutorial-four-java.html
package com.lscl.rabbitmq;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Producer04_Routing {
public static void main(String[] args) throws Exception {
// 创建连接工厂,用于获取频道channel
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.40.132");
factory.setPort(5672);
factory.setUsername("lscl");
factory.setPassword("admin");
factory.setVirtualHost("/lscl");
// 2.创建连接
Connection connection = factory.newConnection();
// 3.创建频道
Channel channel = connection.createChannel();
String exchangeName = "test_direct";
//5. 创建交换机(routing模式)
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT,true,false,false,null);
//6. 创建队列
String queue1Name = "test_direct_queue1";
String queue2Name = "test_direct_queue2";
channel.queueDeclare(queue1Name,true,false,false,null);
channel.queueDeclare(queue2Name,true,false,false,null);
//7. 绑定队列和交换机
/*
queueBind(String queue, String exchange, String routingKey)
参数:
1. queue:队列名称
2. exchange:交换机名称
3. routingKey:路由键,绑定规则
如果交换机的类型为fanout ,routingKey设置为""
*/
//队列1绑定 red,green
channel.queueBind(queue1Name,exchangeName,"red");
channel.queueBind(queue1Name,exchangeName,"green");
//队列2绑定 red,blue
channel.queueBind(queue2Name,exchangeName,"red");
channel.queueBind(queue2Name,exchangeName,"blue");
String body = "routing....";
//8. 发送消息(路由规则为red,很明显两个队列都能接受到消息)
channel.basicPublish(exchangeName,"red",null,body.getBytes());
//9. 释放资源
channel.close();
connection.close();
}
}
运行完毕之后,查看RabbitMQ UI面板,发现多了一个Exchange(test_direct)和两个Queues(test_direct_queue1
、test_direct_queue2
)
package com.lscl.rabbitmq;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer05_Routing {
public static void main(String[] args) throws Exception{
// 创建连接工厂,用于获取频道channel
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.40.132");
factory.setPort(5672);
factory.setUsername("lscl");
factory.setPassword("admin");
factory.setVirtualHost("/lscl");
// 2.创建连接
Connection connection = factory.newConnection();
// 3.创建频道
Channel channel = connection.createChannel();
// 定义队列
String queueName = "test_direct_queue1";
channel.queueDeclare(queueName,true,false,false,null);
// 接收消息
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body:"+new String(body));
}
};
// 接受queue1的消息
channel.basicConsume(queueName,true,consumer);
// 不释放资源,让rabbitmq一直监听
}
}
和消费者-1代码一样,只是队列名换了
String queueName = "test_direct_queue2";
在Routing
模式下(Exchange
类型为direct
,也叫直连模式)消息的发送会指定一个routing key
,Exchange会根据此routing Key
来路由到指定的Queues
,类似于拉了100人进微信群,但是你只想让这100人里面是部门主管的人接受到你的消息(根据某些条件发送消息,微信暂时还没有这个功能)
Routing模式也可有不同的范围,感兴趣的同学可以自己测试:
多个队列绑定相同的routing key:当发送的routing key为black时,那么Q1和Q2都能够接收到消息
当发送的routing key为error时,C1和C2都能够消费消息,当发送的routing key为info、warning时,只有C2能够消费消息
下一篇:《RabbitMQ系列教程-第四章-05-RabbitMQ工作模式之Topics主题模式》