为了在Spring Boot应用中使用RabbitMQ发送和接收多个主题的消息,我们需要进行几个关键步骤的设计,包括配置RabbitMQ、定义消息队列和交换器、发送消息、以及接收消息。下面是一个完整的示例,展示如何实现这一功能。
1. 引入依赖
确保在pom.xml
中包含Spring AMQP和RabbitMQ的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 其他依赖 -->
</dependencies>
2. 配置RabbitMQ
在application.properties
或application.yml
中配置RabbitMQ连接信息:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3. 定义消息队列和交换器
创建配置类来定义队列、交换器及它们之间的绑定关系:
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
public static final String TOPIC_EXCHANGE_NAME = "myTopicExchange";
public static final String QUEUE_1_NAME = "queue1";
public static final String QUEUE_2_NAME = "queue2";
@Bean
public TopicExchange topicExchange() {
return new TopicExchange(TOPIC_EXCHANGE_NAME);
}
@Bean
public Queue queue1() {
return new Queue(QUEUE_1_NAME, true); // durable
}
@Bean
public Queue queue2() {
return new Queue(QUEUE_2_NAME, true); // durable
}
@Bean
public Binding bindingQueue1ToTopicExchange() {
return BindingBuilder.bind(queue1()).to(topicExchange()).with("topic1.*");
}
@Bean
public Binding bindingQueue2ToTopicExchange() {
return BindingBuilder.bind(queue2()).to(topicExchange()).with("topic2.*");
}
}
4. 发送消息
创建一个服务类来处理消息的发送:
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MessageSenderService {
private final RabbitTemplate rabbitTemplate;
@Autowired
public MessageSenderService(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void sendMessageToTopic1(String message) {
rabbitTemplate.convertAndSend(RabbitConfig.TOPIC_EXCHANGE_NAME, "topic1.key", message);
}
public void sendMessageToTopic2(String message) {
rabbitTemplate.convertAndSend(RabbitConfig.TOPIC_EXCHANGE_NAME, "topic2.key", message);
}
}
5. 接收消息
创建监听器来接收不同队列中的消息:
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MessageReceiverService {
@RabbitListener(queues = RabbitConfig.QUEUE_1_NAME)
public void receiveMessageFromQueue1(String message) {
System.out.println("Received from queue1: " + message);
}
@RabbitListener(queues = RabbitConfig.QUEUE_2_NAME)
public void receiveMessageFromQueue2(String message) {
System.out.println("Received from queue2: " + message);
}
}
6. 使用MessageSenderService
在控制器或其他服务中注入MessageSenderService
并调用相应的方法来发送消息:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/messages")
public class MessageController {
private final MessageSenderService messageSenderService;
@Autowired
public MessageController(MessageSenderService messageSenderService) {
this.messageSenderService = messageSenderService;
}
@PostMapping("/send-to-topic1")
public String sendToTopic1(@RequestBody String message) {
messageSenderService.sendMessageToTopic1(message);
return "Message sent to topic1";
}
@PostMapping("/send-to-topic2")
public String sendToTopic2(@RequestBody String message) {
messageSenderService.sendMessageToTopic2(message);
return "Message sent to topic2";
}
}