注意:在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));
}
});
}
}