赞
踩
消息队列 RabbitMQ[一] RabbitMQ的下载与安装
消息队列 RabbitMQ[二] RabbitMQ可视化管控台创建用户并为用户分配虚拟机
消息队列 RabbitMQ[三] RabbitMQ的HelloWorld工作模式(SpringBoot方式与amqp-client方式)
消息队列 RabbitMQ[四] RabbitMQ的Publish/Subscribe(发布/订阅)工作模式(SpringBoot方式与amqp-client方式)
消息队列 RabbitMQ[五] RabbitMQ的Routing工作模式(SpringBoot方式与amqp-client方式)
消息队列 RabbitMQ[六] RabbitMQ的Topics工作模式(SpringBoot方式与amqp-client方式)
消息队列 RabbitMQ[七] RabbitMQ保证消息的可靠性传递(Confirm Return Ack)
消息队列 RabbitMQ[八] SpringBoot Consumer 限流机制
消息队列 RabbitMQ[九] SpringBoot 设置消息过期时间TTL
消息队列 RabbitMQ[十] SpringBoot 死信队列与延迟队列实现思路
Routing工作模式架构图:
如图所示我们需要一个类型为DIRECT的交换机以及两个队列,生产者发布消息到交换机,交换机根据对应的routingkey发布到指定的与它绑定的队列,比如我们发送消息时,给消息绑定了一个叫orange的routingkey,那么消息就会被存储到Q1中,routingkey为black或green则会被存储到Q2中
1. 引入依赖:
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.8.0</version>
</dependency>
2. 通过可视化管理界面添加用户并分配虚拟机
消息队列 RabbitMQ[二] RabbitMQ可视化管控台创建用户并为用户分配虚拟机
3. 生产者代码:
import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; public class Producer_Routing { public static void main(String[] args) throws IOException, TimeoutException { // 1. 创建连接工厂 ConnectionFactory connectionFactory = new ConnectionFactory(); // 2. 设置参数 connectionFactory.setHost("127.0.0.1"); // IP地址 默认地址localhost connectionFactory.setPort(5672); // 端口号 默认5672 connectionFactory.setVirtualHost("/demo_virtual"); // 虚拟机名称 默认/ connectionFactory.setUsername("zdy"); // 用户名 默认guest connectionFactory.setPassword("zdy"); // 密码 默认guest // 3. 创建连接 Connection Connection connection = connectionFactory.newConnection(); // 4. 创建频道 Channel Channel channel = connection.createChannel(); // 5. 创建交换机 /* * exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments) * exchange: 交换机名称 * type: 交换机类型 * DIRECT("direct"),定向 * FANOUT("fanout"),广播 发送到每一个与该交换机绑定的队列 * TOPIC("topic"),通配符方式 * durable: 是否持久化 * autoDelete: 是否自动删除 * internal: 内部使用 一般为false * arguments: 参数 * */ String exchangeName = "direct_exchange"; channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true, false, false, null); // 6. 创建队列 /* * queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments) * queue: 队列名称 * durable: 是否持久化(当MQ关闭再启动时数据还在) * exclusive: 是否独占(只能有一个消费者监听该队列) * autoDelete: 当没有消费者时,是否自动删除队列 * arguments: 参数 * */ String queue1Name = "direct_queue1"; String queue2Name = "direct_queue2"; channel.queueDeclare(queue1Name, true, false, false, null); channel.queueDeclare(queue2Name, true, false, false, null); // 7. 绑定交换机与队列 /* * queueBind(String queue, String exchange, String routingKey) * queue: 队列名称 * exchange: 交换机名称 * routingKey: 路由key * */ channel.queueBind(queue1Name, exchangeName, "error"); // routingkey为error的消息会存储到queue1Name队列中 channel.queueBind(queue2Name, exchangeName, "info"); // routingkey为info的消息会存储到queue2Name队列中 channel.queueBind(queue2Name, exchangeName, "warning"); // routingkey为warning的消息会存储到queue2Name队列中 channel.queueBind(queue2Name, exchangeName, "error"); // routingkey为error的消息会存储到queue2Name队列中 String body = "hello rabbitmq"; channel.basicPublish(exchangeName, "info", null, body.getBytes()); channel.close(); connection.close(); } }
发送routingkey为error的消息,两个队列都会存储
发送routingkey为info的消息,只有direct_queue2会存储该消息
4. 消费者代码:
在这里插入代码片
direct_queue1消费成功
direct_queue2消费成功
1. 引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>2.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> <version>2.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.3.12.RELEASE</version> </dependency>
2. 编写生产者代码(创建新的SpringBoot项目)
2.1 编写配置文件application.yml
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: zdy
password: zdy
virtual-host: /demo_virtual
2.2. 编写RabbitMQ配置文件
import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig { public static final String EXCHANGE_NAME = "direct_exchange"; public static final String QUEUE1_NAME = "direct_queue1"; public static final String QUEUE2_NAME = "direct_queue2"; // 1. 创建Exchange交换机 @Bean("direct_exchange") public Exchange createExchange(){ return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build(); } // 2. 创建Queue队列 @Bean("direct_queue1") public Queue createQueue1(){ return QueueBuilder.durable(QUEUE1_NAME).build(); } @Bean("direct_queue2") public Queue createQueue2(){ return QueueBuilder.durable(QUEUE2_NAME).build(); } // 3. 创建绑定交换机与队列 @Bean public Binding createBinding1Error(@Qualifier("direct_queue1") Queue queue, @Qualifier("direct_exchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("error").noargs(); } @Bean public Binding createBinding2Error(@Qualifier("direct_queue2") Queue queue, @Qualifier("direct_exchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("error").noargs(); } @Bean public Binding createBinding2Warn(@Qualifier("direct_queue2") Queue queue, @Qualifier("direct_exchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("warn").noargs(); } @Bean public Binding createBinding2Info(@Qualifier("direct_queue2") Queue queue, @Qualifier("direct_exchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("info").noargs(); } }
2.3. 编写测试类
import com.zdy.config.RabbitMQConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @RunWith(SpringRunner.class) public class ProducerTest { @Autowired private RabbitTemplate rabbitTemplate; @Test public void testRoutingSend(){ rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "error", "hello rabbitmq"); rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "info", "hello rabbitmq"); rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "warn", "hello rabbitmq"); } }
生产成功routingkey为error的消息两个都会存储,两外两个只有direct_queue2会存储
3. 编写消费者代码(创建新的SpringBoot项目)
3.1. 编写配置文件application.yml(与生产者的一样)
3.2. 编写消息监听类
import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class RabbitMQListener { @RabbitListener(queues = "direct_queue1") public void listenQueueFanoutQ1(Message message){ System.out.println(new String(message.getBody())); } @RabbitListener(queues = "direct_queue2") public void listenQueueFanoutQ2(Message message){ System.out.println(new String(message.getBody())); } }
三、代码结构:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。