RabbitMQ 四种Exchange类型
- RabbitMQ Exchange类型之Direct Exchange
- RabbitMQ Exchange类型之Topic Exchange
- RabbitMQ Exchange类型之fanout Exchange
- RabbitMQ Exchange类型之headers Exchange
1. Topic Exchange介绍
多对多正则匹配,所有发送到Topic Exchange的消息被转发到匹配的RouteKey中指定Topic的Queue。
其中符号#
匹配一个或多个词,符号*
只能匹配其后面的一个词,比如:
user.# # 可以匹配到 user.add user.add.batch
user.* # 只能匹配到 user.add ,不能匹配到 user.add.batch
下图说明 Topic Exchange工作原理
2. Java代码实现
- 生产者代码
public static void main(String[] args) throws Exception{
//1 创建ConnectionFactory
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
//设置虚拟主机
connectionFactory.setVirtualHost("/");
//创建Connection
Connection connection = connectionFactory.newConnection();
//创建Channel
Channel channel = connection.createChannel();
//交换机名称
String exchangeName = "test_topic_exchange";
// routingKey
String routingKey1 = "user.topic.save";
String routingKey2 = "user.topic.update";
String routingKey3 = "user.topic.del";
//发送消息
String msg = "RabbitMQ Topic Exchange TEST";
channel.basicPublish(exchangeName, routingKey1, null , msg.getBytes());
channel.basicPublish(exchangeName, routingKey2, null , msg.getBytes());
channel.basicPublish(exchangeName, routingKey3, null , msg.getBytes());
}
- 消费者代码
public static void main(String[] args) throws Exception{
ConnectionFactory connectionFactory = new ConnectionFactory() ;
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setAutomaticRecoveryEnabled(true);
connectionFactory.setNetworkRecoveryInterval(3000);
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
//交换机名称
String exchangeName = "test_topic_exchange";
//交换机类型
String exchangeType = "topic";
//消息队列名称
String queueName = "test_topic_queue";
//配置所有以user.开头的消息
String routingKey = "user.#";
//声明交换机
channel.exchangeDeclare(exchangeName, exchangeType, true, false, false, null);
//表示一个队列
channel.queueDeclare(queueName, false, false, false, null);
//建立Exchange 和Queue绑定关系
channel.queueBind(queueName, exchangeName, routingKey);
//参数说明:队列名称、是否自动ACK、DefaultConsumer(消费者)
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));
}
});
}
依次启动消费者
和生产者
,在消费者
控制台可以看到接收到消息的输出
同时在RabbitMQ管理平台,可以看到除了默认的Exchange,还有一个我们自己的Exchange:test_topic_exchange
类型为topic
点击名称Exchange名称可以查看绑定其绑定关系
RabbitMQ系列文章目录
1、RabbitMQ Windows/CentOS7平台安装手册
2、RabbitMQ中一些重要概念
3、RabbitMQ Exchange类型之Direct Exchange
4、RabbitMQ Exchange类型之Topic Exchange
5、RabbitMQ Exchange类型之fanout Exchange
6、RabbitMQ Exchange类型之headers Exchang
7、Confirm消息确认机制
8、RabbitMQ中ReturnListener的使用
9、RabbitMQ消费端限流
10、ACK确认机制与消息补偿
11、RabbitMQ队列/消息的生存时间(Time-To-Live)
12、RabbitMQ死信队列(Dead Letter Exchanges)
13、Spring AMQP API详解
14、Spring Boot整合RabbitMQ