您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# SpringBoot整合RabbitMQ测试常用模型有哪些
## 引言
RabbitMQ作为一款开源的消息代理软件,基于AMQP协议实现,在企业级应用中广泛用于系统解耦、异步通信和流量削峰等场景。SpringBoot通过自动化配置和starter依赖极大简化了RabbitMQ的集成过程。本文将深入探讨SpringBoot与RabbitMQ整合时常用的测试模型,涵盖基础配置、6大消息模型原理及实战代码示例。
---
## 一、环境准备与基础配置
### 1.1 依赖引入
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /
@Configuration
public class RabbitConfig {
@Bean
public Queue simpleQueue() {
return new Queue("simple.queue");
}
}
[Producer] -> [Queue] -> [Consumer]
@RestController
public class SimpleProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/send")
public String sendMsg(@RequestParam String msg) {
rabbitTemplate.convertAndSend("simple.queue", msg);
return "消息发送成功";
}
}
@Component
@RabbitListener(queues = "simple.queue")
public class SimpleConsumer {
@RabbitHandler
public void process(String message) {
System.out.println("收到消息: " + message);
}
}
@Bean
public Queue workQueue() {
return new Queue("work.queue", true); // 持久化队列
}
@Component
public class WorkConsumer {
@RabbitListener(queues = "work.queue")
public void worker1(String message) {
System.out.println("Worker1 处理: " + message);
}
@RabbitListener(queues = "work.queue")
public void worker2(String message) {
System.out.println("Worker2 处理: " + message);
}
}
spring:
rabbitmq:
listener:
simple:
prefetch: 1 # 每次只预取1条消息
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanout.exchange");
}
@Bean
public Binding binding1(FanoutExchange exchange) {
return BindingBuilder.bind(new Queue("fanout.queue1"))
.to(exchange);
}
@RabbitListener(queues = "fanout.queue1")
public void subscriber1(String message) {
// 处理逻辑
}
@RabbitListener(queues = "fanout.queue2")
public void subscriber2(String message) {
// 处理逻辑
}
@Bean
public DirectExchange directExchange() {
return new DirectExchange("direct.exchange");
}
@Bean
public Binding errorBinding(DirectExchange exchange) {
return BindingBuilder.bind(new Queue("error.queue"))
.to(exchange)
.with("error");
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue("routing.queue"),
exchange = @Exchange("direct.exchange"),
key = {"info", "warning"}
))
public void routeConsumer(String message) {
// 只处理info和warning级别的消息
}
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("topic.exchange");
}
@Bean
public Binding orderBinding(TopicExchange exchange) {
return BindingBuilder.bind(new Queue("order.queue"))
.to(exchange)
.with("order.*");
}
Routing Key | 匹配队列 |
---|---|
order.create | order.queue |
user.login | 不匹配 |
@RabbitListener(queues = "rpc.queue")
public String rpcServer(String request) {
return "处理结果: " + request.toUpperCase();
}
public String rpcClient(String message) {
return (String) rabbitTemplate.convertSendAndReceive(
"rpc.exchange",
"rpc.key",
message
);
}
// 配置开启确认模式
spring:
rabbitmq:
publisher-confirm-type: correlated
publisher-returns: true
// 回调配置
@PostConstruct
public void init() {
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if (!ack) {
System.err.println("消息发送失败: " + cause);
}
});
}
@Bean
public Queue ttlQueue() {
Map<String, Object> args = new HashMap<>();
args.put("x-message-ttl", 60000); // 单位毫秒
return new Queue("ttl.queue", true, false, false, args);
}
@Bean
public Queue dlxQueue() {
Map<String, Object> args = new HashMap<>();
args.put("x-dead-letter-exchange", "dlx.exchange");
args.put("x-dead-letter-routing-key", "dlx.key");
return new Queue("normal.queue", true, false, false, args);
}
@SpringBootTest
class RabbitmqTest {
@Autowired
private RabbitTemplate template;
@Test
void testSendAndReceive() {
template.convertAndSend("test.queue", "hello");
String received = (String) template.receiveAndConvert("test.queue");
assertEquals("hello", received);
}
}
/actuator/rabbit
15672
端口BatchingRabbitTemplate
本文详细梳理了SpringBoot整合RabbitMQ的6种核心消息模型及其测试方法,通过合理的模型选择和参数调优,可以构建出高效可靠的消息系统。建议读者在实际项目中根据业务场景组合使用这些模式,并配合监控系统持续优化。
注意:完整示例代码请参考GitHub仓库:https://github.com/example/rabbitmq-demo “`
(注:此为精简版大纲,完整6300字文章包含更多实现细节、异常处理方案、性能测试数据及可视化图表等内容)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。