RabbitMQ高级特性
如何保证消息可靠性
- 保证消息从发送者到交换机的可靠性: 使用Confirm确认机制。
- 保证消息从交换机到队列的可靠性; 使用return回退机制。
- 消息在队列中的可靠性。 设置队列和消息的持久化。
- 保证消息从队列到消费者的可靠性。 使用消费端的手动确认机制。
1、消息可靠性传递
为确保消息的可靠性传递,提供了两种方式:
confirm 确认模式
return 退回模式
确认模式:
1.开启确认模式
server:
port: 8001
spring:
rabbitmq:
host: 192.168.137.159
#开启rabbitMQ的生产方确认模式
publisher-confirm-type: correlated
2.设置RabbitTemplate的确认回调函数
@SpringBootTest
public class RabbitTest {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 保证发送方到交换机的可靠性。
* 设置rabbitTemplate的确认回调函数。如果消息到达交换机则返回true,如果消息没有到达交换机则返回一个false
*/
@Test
public void testConfirm(){
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean b, String s) {
System.out.println("消息是否到了交换机:"+b);
if(b==false){//消息没有到达交换机 根据业务需求。
System.out.println("继续发送消息");
//取消订单
}
}
});
rabbitTemplate.convertAndSend("zyb_change","","hello confirm");
}
}
改了交换机名称之后:
退回模式:
只有交换机到队列失败时才会触发该方法
1、开启退回模式
server:
port: 8001
spring:
rabbitmq:
host: 192.168.137.159
#开启rabbitMQ的生产方确认模式
publisher-confirm-type: correlated
# 开启发布者退回模式
publisher-returns: true
2、设置RabbitTemplate回调的函数
@Test
public void testReturn(){
rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
@Override
public void returnedMessage(ReturnedMessage returnedMessage) {
//只要交换机到队列失败时才会触发该方法。 可以继续发送也可以取消相应的业务功能。
System.out.println("消息从交换机到队列失败"+returnedMessage.getReplyText());
}
});
for(int i=0;i<1000;i++) {
rabbitTemplate.convertAndSend("zyb_exchange_direct", "error", "hello confirm:"+i);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210506192254462.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1pfWVVCTw==,size_16,color_FFFFFF,t_70#pic_center
更改路由key为“abc”之后
2、Consumer ACK:消费端收到消息后的确认方式
自动确认是指,当消息一旦被Consumer接收到,则自动确认收到,并将相应 message 从 RabbitMQ 的消息队列中移除。但是在实际业务处理中,很可能消息接收到,业务处理出现异常,那么该消息就会丢失。
如果设置了手动确认方式,则需要在业务处理成功后,调用channel.basicAck(),手动签收,如果出现异常,则调用channel.basicNack()方法,让其自动重新发送消息。
1、消费端配置手动开启确认模式
server:
port: 8001
spring:
rabbitmq:
host: 192.168.137.159
listener:
simple:
#表示手动确认
acknowledge-mode: manual
# 表示自动确认模式
# acknowledge-mode: none
@Component
public class MyListener {
@RabbitListener(queues = "zyb_queue_direct01")
public void listener(Message message, Channel channel) throws Exception{
long deliveryTag = message.getMessageProperties().getDeliveryTag();
byte[] body = message.getBody();
String msg=new String(body);
System.out.println(msg);
try {
//int c = 10 / 0;
System.out.println("处理业务逻辑");
//消费端手动确认消息
//long deliveryTag, 表示的标识。
// boolean multiple:是否允许多确认
channel.basicAck(deliveryTag,true); //从队列中删除该消息。
}catch (Exception e){
//(long deliveryTag, boolean multiple, boolean requeue: 是否让队列再次发送该消息。
channel.basicNack(deliveryTag,true,true);
}
}
}
3、消费端限流
必须为手动确认模式
必须配置限流个数
server:
port: 8001
spring:
rabbitmq:
host: 192.168.137.159
listener:
simple:
#表示手动确认
acknowledge-mode: manual
# 表示自动确认模式
# acknowledge-mode: none
# 设置每次消费的个数。
prefetch: 100
package com.zyb.listener;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @Author zyb
* @Description TODO
* @date 2021/5/6 18:47
* @Version 1.0
*/
@Component
public class MyListener {
@RabbitListener(queues = "zyb_queue_direct01")
public void listener(Message message, Channel channel) throws Exception{
long deliveryTag = message.getMessageProperties().getDeliveryTag();
byte[] body = message.getBody();
String msg = new String(body);
try {
//int c = 10 / 0;
//System.out.println("处理业务逻辑");
//消费端手动确认消息
//long deliveryTag, 表示的标识。
// boolean multiple:是否允许多确认
channel.basicAck(deliveryTag,true);
} catch (Exception e) {
//(long deliveryTag, boolean multiple, boolean requeue: 是否让队列再次发送该消息
channel.basicNack(deliveryTag,true,true);
}
}
}
4、TTL
//为队列设置过期时间 相当于该队列里面的消息都由过期时间
@Test
public void testSend(){
rabbitTemplate.convertAndSend("myexchange","","hello xiaoxi");
}
//设置消息的过期时间 如果由设置了队列的过期时间 也设置了消息的过期时间 谁的过期时间短 以谁为准。
//该消息必须在头部才能从队列中移除。
@Test
public void testSend02(){
for(int i=0;i<10;i++) {
if(i==3){
MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
message.getMessageProperties().setExpiration("20000");
return message;
}
};
//String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor
rabbitTemplate.convertAndSend("myexchange", "", "hello xiaoxi"+i, messagePostProcessor);
}else {
//String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor
rabbitTemplate.convertAndSend("myexchange", "", "hello xiaoxi"+i);
}
}
}
@Test
public void send(){
rabbitTemplate.convertAndSend("myexchange","","我饿了");
}