1、什么是return机制?
上篇博客rabbitMQ-生产者可靠发送-消息确认-Confirm
说了消息的生产者可靠发送,可以依靠confirm模式。但是confirm模式只保证消息成功从生产者发送到交换机,但是如果交换机没有找到queue呢?如果根据路由发送到具体的队列queue的时候,没有匹配的队列呢?这种情况confirm模式不能保证,这就得依赖return回退机制了。
return回退机制就是当交换机路由到queue的时候,没有匹配的队列,return模式可以在消息发送之后异步监听到mq的回调,也就是回执行ReturnCallBack
2、springBoot开启rabbitmq的return模式
2.1引入依赖
<!--引入rabbitmq继承依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
2.2配置application.yml
在开启confirm模式的基础上
spring:
application:
name: rabbitmq-springboot
rabbitmq:
host: 127.0.0.1
port: 5672
username: ems
password: 123
virtual-host: /ems
# 发送者开启 confirm 确认机制
publisher-confirms: true
# 发送者开启 return 确认机制
publisher-returns: true
2.3交换机、队列、binging的配置类
package com.lst;
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 QueueConfig {
//定义队列
@Bean(name = "TestQueue")
public Queue returnTestQueue() {
return new Queue("TestQueue", true, false, false);
}
//定义交换机
@Bean(name = "TestExchange")
public DirectExchange TestExchange() {
return new DirectExchange("TestExchange");
}
//将队列绑定交换机
@Bean
public Binding confirmTestExchangeAndQueue(
@Qualifier("TestExchange") DirectExchange TestExchange,
@Qualifier("TestQueue") Queue TestQueue) {
return BindingBuilder.bind(TestQueue).to(TestExchange).with("testRoutingKey");
}
}
2.4生产者接口
package com.lst.hello;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestRabbitMq {
//注入rabbitTemplate对象
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 确认模式步骤
* 1、确认模式开启
* 2、在rabbitTemple中定义回调函数
*/
/**
* 回退 return模式开启
* 1、开启回退模式 publisher-returns: true
* 2、设置ReturnCallBack
* 3、设置exchange处理消息的模式
* 3.1 如果没有路由到queue则丢弃(默认)
* 3.2 如果没有路由到queue 返回消息发送方 ReturnCallBack
*
*/
@PostMapping("/test")
public void test() {
//定义回调
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
/**
*
* @param correlationData 相关的配置
* @param b :ack 交换机是否成功收到了消息 true代表成功 false代表失败
* @param s :失败的原因
*/
@Override
public void confirm(CorrelationData correlationData, boolean b, String s) {
if (b) {//接收成功
System.out.println("发送成功");
} else {//接受失败
System.out.println("接收失败" + s);
}
}
});
//定义return回调
rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
@Override
public void returnedMessage(ReturnedMessage returnedMessage) {
System.out.println(returnedMessage.toString());
//可以做重新路由的处理
}
});
//发送消息
rabbitTemplate.convertAndSend("TestExchange", "testRoutingKey111", "hello");
}
}
3、回调信息
2021-08-28 18:32:48.286 INFO 9228 --- [nio-7071-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-08-28 18:32:48.287 INFO 9228 --- [nio-7071-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
ReturnedMessage [message=(Body:'hello' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, deliveryTag=0]),
replyCode=312,
replyText=NO_ROUTE,
exchange=TestExchange,
routingKey=testRoutingKey111]
returnMessage中的属性:
- message:发送的消息对象
- replyCode:错误码
- replyText:错误信息
- exchange:交换机
- routingKey:路由key