在 Spring Boot 项目中,通常在application.properties或application.yml文件中配置 RabbitMQ 的连接信息。以application.yml为例,配置如下:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual - host: /
上述配置中:
通过配置类可以方便地创建消息队列、交换机以及它们之间的绑定关系。在 Spring Boot 中,使用@Configuration注解来定义配置类,并使用@Bean注解创建所需的 Bean。
下面分别展示direct、topic、fanout类型交换机的配置示例:
Direct Exchange(直连交换机)配置示例:
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DirectRabbitConfig {
// 定义队列
@Bean
public Queue directQueue() {
return new Queue("direct.queue", true);
}
// 定义直连交换机
@Bean
public DirectExchange directExchange() {
return new DirectExchange("direct.exchange", true, false);
}
// 绑定队列和交换机,设置路由键
@Bean
public Binding directBinding() {
return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct.routing.key");
}
}
在这个配置中:
Topic Exchange(主题交换机)配置示例:
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TopicRabbitConfig {
// 定义第一个队列
@Bean
public Queue topicQueue1() {
return new Queue("topic.queue1", true);
}
// 定义第二个队列
@Bean
public Queue topicQueue2() {
return new Queue("topic.queue2", true);
}
// 定义主题交换机
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("topic.exchange", true, false);
}
// 绑定第一个队列和交换机,设置路由键模式为topic.#
@Bean
public Binding topicBinding1() {
return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.#");
}
// 绑定第二个队列和交换机,设置路由键模式为topic.man
@Bean
public Binding topicBinding2() {
return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.man");
}
}
这里:
Fanout Exchange(扇形交换机)配置示例:
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutRabbitConfig {
// 定义第一个队列
@Bean
public Queue fanoutQueue1() {
return new Queue("fanout.queue1", true);
}
// 定义第二个队列
@Bean
public Queue fanoutQueue2() {
return new Queue("fanout.queue2", true);
}
// 定义扇形交换机
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanout.exchange", true, false);
}
// 绑定第一个队列和交换机
@Bean
public Binding fanoutBinding1() {
return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
}
// 绑定第二个队列和交换机
@Bean
public Binding fanoutBinding2() {
return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
}
}
在这个配置类中:
创建一个消息生产者类,通过注入RabbitTemplate来发送消息。RabbitTemplate是 Spring AMQP 提供的用于发送消息的核心类,它封装了与 RabbitMQ 交互的细节,提供了便捷的消息发送方法。
以下是一个消息生产者类的示例,包含普通消息发送和带自定义属性消息发送的示例:
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class RabbitMQProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
// 发送普通消息
public void sendMessage(String exchange, String routingKey, String message) {
rabbitTemplate.convertAndSend(exchange, routingKey, message);
System.out.println("Sent message: " + message);
}
// 发送带自定义属性的消息
public void sendMessageWithProperties(String exchange, String routingKey, String message) {
Map
headers.put("customHeader", "customValue");
rabbitTemplate.convertAndSend(exchange, routingKey, message, messagePostProcessor -> {
messagePostProcessor.getMessageProperties().setHeaders(headers);
return messagePostProcessor;
});
System.out.println("Sent message with properties: " + message);
}
}
在这个生产者类中:
创建消息消费者类,使用@RabbitListener注解来监听指定的队列,并处理接收到的消息。@RabbitListener是 Spring AMQP 提供的注解,用于声明一个方法作为消息监听器,当队列中有新消息时,该方法会被自动调用。
以下是一个消息消费者类的示例,涵盖消息处理逻辑和异常处理:
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQConsumer {
@RabbitListener(queues = "direct.queue")
public void receiveMessage(String message) {
try {
// 消息处理逻辑
System.out.println("Received message: " + message);
// 模拟业务处理
Thread.sleep(1000);
System.out.println("Message processed successfully.");
} catch (Exception e) {
// 异常处理
System.out.println("Error processing message: " + e.getMessage());
// 可以根据具体业务需求进行重试、记录日志或其他操作
}
}
}
在这个消费者类中: