在使用rabbitmq作为消息队列的时候,我们需要了解下rabbitmq的消息模式。rabbitmq的消息模式有5种,分别是:simple模式,Fanout模式,Direct模式,Topic模式,work模式,。本文主要介绍的是Topic模式。
Topic模式和主要是在Direct模式下进行升级的,即在路由里面添加了通配符,这样子绑定的队列里面如果路由routing key有匹配的,都可以收到这种消息。
这里的通配符我们一般使用#或者*号替代,例如:
*.order.* user.#
这里的*代表的是匹配一个单词
这里的#代表的是匹配0个或者多个单词,下面使用代码演示一下:
一、添加maven依赖:
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.9.0</version> </dependency>
二、编写生产者:
package com.rabbitmq.producer; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.MessageProperties; import lombok.extern.slf4j.Slf4j; @Slf4j public class TopicProducer { private Connection connection = null; private Channel channel = null; private ConnectionFactory factory = null; // 发布订阅模式需要一个交换机 private String exchangeName = "topic-exchange"; // 发布订阅模式需要多个队列绑定到交换机上 private String quename1 = "test1"; private String quename2 = "test2"; private String quename3 = "test3"; /** * 初始化rabbitmq连接 * * @throws TimeoutException * @throws IOException */ @Before public void init() throws IOException, TimeoutException { // 1、初始化连接参数 factory = new ConnectionFactory(); factory.setHost("192.168.31.20"); // 设置rabbitmq的登录账号和密码 factory.setUsername("guest"); factory.setPassword("guest"); factory.setPort(5673); // 默认端口是5672,这里服务器端口冲突,因此改到5673 factory.setVirtualHost("/"); // 2、创建连接、通道 connection = factory.newConnection(); channel = connection.createChannel(); // 绑定交换机,并且类型选择使用DIRECT channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC); // 为交换机绑定队列 channel.queueDeclare(quename1, true, false, false, null); channel.queueDeclare(quename2, true, false, false, null); channel.queueDeclare(quename3, true, false, false, null); //为队列绑定channel,在这里的第三个参数就是routingkey channel.queueBind(quename1, exchangeName, "*.yellow.*"); channel.queueBind(quename2, exchangeName, "*.blue.*"); channel.queueBind(quename3, exchangeName, "#.red.#"); } @After public void destroy() { // 关闭通道 if (channel != null && channel.isOpen()) { try { channel.close(); } catch (Exception e) { log.error(e.getMessage(), e); } } // 关闭连接 if (connection != null && connection.isOpen()) { try { connection.close(); } catch (Exception e) { log.error(e.getMessage(), e); } } } @Test public void sendMsg() throws Exception { // 发送消息内容 String message = "take a message red"; // 发送消息我们就指定的是交换机,同时需要标注本消息的routingkey channel.basicPublish(exchangeName, "red.apple", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes(StandardCharsets.UTF_8)); log.info("发送消息完成,消息内容是:{}", message); } }
三、编写消费者
package com.rabbitmq.producer; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.CancelCallback; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.DeliverCallback; import com.rabbitmq.client.Delivery; import lombok.extern.slf4j.Slf4j; @Slf4j public class TopicConsumer { private static final String queueName = "test3"; public static void main(String[] args) throws IOException, TimeoutException { // 1、创建连接工程 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.31.20"); factory.setUsername("guest"); factory.setPassword("guest"); factory.setPort(5673); factory.setVirtualHost("/"); // 2、获取 Connection和 Channel Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.basicConsume(queueName, true, new DeliverCallback() { @Override public void handle(String consumerTag, Delivery delivery) throws IOException { log.info("消息队列:{} 接收到消息:{}",queueName,new String(delivery.getBody(), "UTF-8")); } }, new CancelCallback() { @Override public void handle(String consumerTag) throws IOException { } }); log.info("消费者启动成功,准备开始消费数据"); } }
然后测试运行即可看到只有test3收到了消息
以上就是Rabbitmq消息模式中的topic消息模式。
备注:
1、需要把生产者的消息模式修改为topic。
2、生产者需要为每一个队列绑定一个通配符
3、绑定通配符里面的.也是存在的,我们一般发送的话,都归类下,所以一般习惯使用.例如:red.apple user.order product.order这种
还没有评论,来说两句吧...