网站首页 > 精选文章 / 正文
RabbitMQ是现在一个比较流行的消息队列,提供了多种的交换机类型,其中比较常见的就是Direct交换机,通过这个交换机,可以实现消息的精确路由操作,下面我们就来详细介绍下如何在Spring Boot中整合实现RabbitMQ的Direct模式。
什么是Direct模式?
RabbitMQ的Direct交换机(Direct Exchange)是一种比较简单的通过直接路由的方式来实现消息发送操作的交换机。在Direct模式中,当消息发送到交换机的时候,会根据消息的的路由键(Routing Key)与队列绑定的路由键进行匹配,只有当消息的路由键与队列的绑定键完全匹配时,消息才会被投递到该队列。
也就是说,Direct模式是基于路由键的精确匹配,所以在消息投递的时候非常高效且可靠。
Spring Boot整合RabbitMQ Direct模式
引入RabbitMQ依赖
首先需要在项目的pom.xml文件中引入Spring AMQP相关的依赖项,如下所示。spring-boot-starter-amqp是Spring Boot用于整合RabbitMQ的starter,它会自动配置与RabbitMQ相关的基础设施。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
配置RabbitMQ
接下来就是在Spring Boot项目的配置文件中添加好RabbitMQ的连接配置,用来连接RabbitMQ的服务,如下所示。
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
listener:
simple:
concurrency: 3
max-concurrency: 10
创建消息队列和交换机
配置完成之后,Spring Boot会自动连接RabbitMQ的服务,然后我们就可以通过@Bean注解来创建交换机、队列和绑定等操作,在Direct模式下,我们需要创建一个Direct类型的交换机,并将队列绑定到交换机上,如下所示。
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
// 定义交换机,类型为Direct
@Bean
public DirectExchange directExchange() {
return new DirectExchange("direct-exchange");
}
// 定义队列
@Bean
public Queue queue1() {
return new Queue("queue1");
}
@Bean
public Queue queue2() {
return new Queue("queue2");
}
// 将队列与交换机进行绑定,并指定路由键
@Bean
public Binding binding1(DirectExchange directExchange, Queue queue1) {
return BindingBuilder.bind(queue1).to(directExchange).with("routing-key-1");
}
@Bean
public Binding binding2(DirectExchange directExchange, Queue queue2) {
return BindingBuilder.bind(queue2).to(directExchange).with("routing-key-2");
}
}
发送消息
创建配置Bean操作完成之后,我们可以通过一个简单的服务操作来测试消息发送操作,当消息发送的时候,我们需要指定路由键来确保消息能够路由到正确的队列,如下所示。sendMessage方法通过AmqpTemplate发送消息,并指定路由键。路由键将决定消息路由到哪个队列。
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MessageSender {
@Autowired
private AmqpTemplate amqpTemplate;
public void sendMessage(String message, String routingKey) {
// 发送消息到指定的交换机,并指定路由键
amqpTemplate.convertAndSend("direct-exchange", routingKey, message);
System.out.println("消息发送成功: " + message);
}
}
接收消息
接收消息时,我们需要使用@RabbitListener注解来创建消费者。来监听队列并处理消息。如下所示。通过@RabbitListener注解将方法与队列进行关联。当消息到达指定队列时,消息将被传递到对应的方法。
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class MessageListener {
@RabbitListener(queues = "queue1")
public void receiveMessageFromQueue1(String message) {
System.out.println("从queue1接收到消息: " + message);
}
@RabbitListener(queues = "queue2")
public void receiveMessageFromQueue2(String message) {
System.out.println("从queue2接收到消息: " + message);
}
}
测试
在应用启动后,我们可以尝试通过调用MessageSender的sendMessage方法来发送消息,如下所示。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitMQApplication implements CommandLineRunner {
@Autowired
private MessageSender messageSender;
public static void main(String[] args) {
SpringApplication.run(RabbitMQApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// 发送消息到队列1
messageSender.sendMessage("Hello, Queue1!", "routing-key-1");
// 发送消息到队列2
messageSender.sendMessage("Hello, Queue2!", "routing-key-2");
}
}
运行Spring Boot应用后,就可以看到发送和接收结果,如下所示。
消息发送成功: Hello, Queue1!
消息发送成功: Hello, Queue2!
从queue1接收到消息: Hello, Queue1!
从queue2接收到消息: Hello, Queue2!
总结
通过上面的操作,我们就可以实现在Spring Boot应用中使用RabbitMQ的Direct模式来实现消息的发送操作,Direct交换机通过路由键实现生产者和消费者之间的消息的精确投递。在实际开发过程中,我们可以通过RabbitMQ的Direct模式来实现高效可靠的分布式消息传递方案。
Tags:rabbitmq配置
猜你喜欢
- 2024-12-28 用rabbitmq实现消息重发的功能 rabbitmq查看消息内容
- 2024-12-28 秃头大牛一文竟然就把SpringCloudStream(SCS)给讲明白了?
- 2024-12-28 详细介绍一下RabbitMQ的消息持久化机制?
- 2024-12-28 SpringBoot整合RabbitMQ实现消息的发送和接收操作?
- 2024-12-28 RabbitMQ持久化机制、内存磁盘控制
- 2024-12-28 在Spring Boot中如何基于RabbitMQ实现流量削峰?
- 2024-12-28 Spring Boot中如何通过RabbitMQ接收秒杀流量
- 2024-12-28 「MQ中间件」 RabbitMQ死信队列及内存监控
- 2024-12-28 RabbitMQ 巧解消息积压难题 rabbitmq消息堆积处理
- 2024-12-28 rabbitmq 4种集群模式 rabbitmq集群