RabbitMQ exchange使用-路由模式(routing)
1 简介
路由模式 和 订阅模式的区别:
路由模式多了一个routingKey 参数;
不同的消费者绑定不同的队列和 routingKey 到交换机;
发送者发送消息,指定 不同的routingKey,从而指定让固定的几个消费者使用
内置交换机类型 :direct
图中 ,error 、info、warning就是routingKey
2 代码
2.1生产者
package com.yzcq.rabbitmq.routing;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.yzcq.rabbitmq.util.ConnectionUtil;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* 发布和订阅模式-生产者
*/
public class Send {
private static final String EXCHANGE_NAME = "test-exchange-routing-name";
public static void main(String[] argv) throws IOException, TimeoutException {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
//声明一个交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
String severity = "error";
//循环发消息发消息
for (int i = 0; i < 50; i++) {
String routingKey = severity;
String message = "测试路由功能 发布者【" + i + "】";
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
System.out.println(" [x] Sent '" + severity + "':'" + message + "'");
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
channel.close();
connection.close();
}
}
2.2 消费者info路由key
package com.yzcq.rabbitmq.routing;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.yzcq.rabbitmq.util.ConnectionUtil;
import java.io.IOException;
/**
* 发布和订阅模式-消费者1
*/
public class Receiver1 {
private static final String EXCHANGE_NAME = "test-exchange-routing-name";
public static void main(String[] argv) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
//声明一个交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
//获取一个临时队列
String queueName = channel.queueDeclare().getQueue();
String routingKey = "info";
//将队列 、 路由key 绑定到交换机上
channel.queueBind(queueName, EXCHANGE_NAME, routingKey);
System.out.println(" [1] 路由key:" + routingKey + ",等待接受消息. 如果要退出按 CTRL+C");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [1] Received '" + message + "'");
try {
Thread.sleep(2000);
}
catch (InterruptedException _ignored) {
Thread.currentThread().interrupt();
}
}
};
channel.basicConsume(queueName, true, consumer);
}
}
2.3 消费者error路由key
package com.yzcq.rabbitmq.routing;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.yzcq.rabbitmq.util.ConnectionUtil;
import java.io.IOException;
/**
* 发布和订阅模式-消费者2
*/
public class Receiver2 {
private static final String EXCHANGE_NAME = "test-exchange-routing-name";
public static void main(String[] argv) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
//声明一个交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
//获取一个临时队列
String queueName = channel.queueDeclare().getQueue();
String routingKey = "error";
//将队列 、 路由key 绑定到交换机上
channel.queueBind(queueName, EXCHANGE_NAME, routingKey);
System.out.println(" [2] 路由key:" + routingKey + ",等待接受消息. 如果要退出按 CTRL+C");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [2] Received '" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
}