rabbitmq监控之消息确认ack
一直以来,学习rabbitmq都是跟着各种各样的教程、博客、视频和文档,撸起袖子就是干!!!最后,也成功了。
当然,成功的标志也仅仅是生产者发送了消息,消费者消费了消息
。
真正在实际项目中,一旦出问题,需要分析问题的时候,仅仅了解这些是不够的。
老祖宗说过:实践,是检验真理的唯一标准。所以,研究分析一下消息确认模式ack的整个过程,到底怎么回事
一、测试环境
使用springboot环境:
- 一个Fanout交换机
fanout.exchange - 两个队列:
fanout.queue1和fanout.queue2
pom依赖:
<!-- 添加springboot对amqp的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application配置:
# RabbitMQ 基本配置
spring.rabbitmq.host=192.168.183.220
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
## 生产端配置
# 开启发布确认,就是confirm模式. 消费端ack应答后,才将消息从队列中删除
spring.rabbitmq.publisher-confirms=true
# 发布返回
spring.rabbitmq.publisher-returns=true
## 消费端配置
# 手动ack
spring.rabbitmq.listener.simple.acknowledge-mode=manual
# 消费者最小数量
spring.rabbitmq.listener.simple.concurrency=1
# 消费者最大数量
spring.rabbitmq.listener.simple.max-concurrency=10
# 在单个请求中处理的消息个数,他应该大于等于事务数量(unack的最大数量)
spring.rabbitmq.listener.simple.prefetch=1
## 模板配置
#设置为 true 后 消费者在消息没有被路由到合适队列情况下会被return监听,而不会自动删除
spring.rabbitmq.template.mandatory=true
RabbitConfig.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
private static final Logger log= LoggerFactory.getLogger(RabbitConfig.class);
@Bean
public Queue queue() {
return new Queue("queue");
}
@Bean(name = "FQ1")
public Queue fanoutQueue1() {
return new Queue("fanout.queue1");
}
@Bean(name = "FQ2")
public Queue fanoutQueue2() {
return new Queue("fanout.queue2");
}
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanout.exchange");
}
@Bean
public Binding bindingFQ1(@Qualifier("FQ1") Queue queue, FanoutExchange exchange){
return BindingBuilder.bind(queue).to(exchange);
}
@Bean
public Binding bindingFQ2(@Qualifier("FQ2") Queue queue, FanoutExchange exchange){
return BindingBuilder.bind(queue).to(exchange);
}
/**
* 定制化amqp模版
*
* ConfirmCallback接口用于ack回调 即消息发送到exchange ack
* ReturnCallback接口用于消息发送失败回调 即消息发送不到任何一个队列中 ack
*/
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
// 消息返回, 需要配置 publisher-returns: true
rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
String correlationId = message.getMessageProperties().getCorrelationId();
log.debug("消息:{} 发送失败, 应答码:{} 原因:{} 交换机: {} 路由键: {}", correlationId, replyCode, replyText, exchange, routingKey);
});
// 消息确认, 需要配置 publisher-confirms: true
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if (ack) {
// log.debug("消息发送到exchange成功,id: {}", correlationData.getId());
log.debug("消息发送到exchange成功");
} else {
log.debug("消息发送到exchange失败,原因: {}", cause);
}
});
return rabbitTemplate;
}
}
HelloSender.java
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class HelloSender {
@Autowired
private AmqpTemplate template;
public void sendAck(String msg) {
template.convertAndSend("fanout.exchange","",msg);
}
}
HelloReceive.java
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class HelloReceive {
//手动确认消息
@RabbitListener(queues = "fanout.queue1")
public void FQ1(Message message, Channel channel) throws IOException {
// 采用手动应答模式, 手动确认应答更为安全稳定
System.out.println("FQ1:" + new String(message.getBody()));
// 第一个参数是消息标识, 第二个是批量确认; false当前消息确认, true此次之前的消息确认
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}
// 不确认消息,消息会重回队列
@RabbitListener(queues = "fanout.queue2")
public void FQ2(String str) {
System.out.println("FQ2:" + str);
}
}
单元测试
import com.lyf.springboot.SpringbootApplication;
import com.lyf.springboot.rabbitmq.HelloSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// SpringbootApplication Springboo启动类
@SpringBootTest(classes= SpringbootApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestRabbitMQ {
@Autowired
private HelloSender helloSender;
@Test
public void testRabbit2() {
for (int i = 0; i < 10; i++) {
helloSender.sendAck("haha~"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
二、启动测试
在确认消息的地方加上断点,方便查看消息确认的过程。

rabbitmq后台管理界面:

Message
- Ready: 队列中等待消费的消息
- Unacked:队列中等待被确认的消息(此时消息已到达消费者,但是未被确认)
- Total:队列中消息总数
启动测试

一开始两个队列都收到了1条消息,因为开启了confirm模式,所以Message的Unacked状态都为1,Total为1。

收到第2条消息后,队列queue1执行了ack确认,所以队列中只有1条消息,1条消息等待被确认;队列queue2没有被ack确认,所以Ready=1,Unacked=1,Total=2。

收到第10条消息后,队列queue1依然是Ready=0,Unacked=1,Total=1;而队列queue2一直没有被ack确认,所以Ready=9,Unacked=1,Total=10。

消息发送完后,队列queue1已经没有消息了,队列queue2还有10条等待被消费的消息。默认未被ack的消息重回队列中。
spring.rabbitmq.listener.simple.default-requeue-rejected=true参考文档:
- SpringBoot 整合 RabbitMQ https://www.cnblogs.com/haixiang/p/10959551.html
- springboot整合rabbitmq,支持消息确认机制 https://www.cnblogs.com/milicool/p/9662447.html
- Spring Boot + RabbitMQ 配置参数解释 https://www.cnblogs.com/qts-hope/p/11242559.html
- RabbitMQ入门教程(十二):消息确认Ack https://blog.csdn.net/vbirdbest/article/details/78699913
rabbitmq监控之消息确认ack的更多相关文章
- RabbitMQ的消息确认ACK机制
1.什么是消息确认ACK. 答:如果在处理消息的过程中,消费者的服务器在处理消息的时候出现异常,那么可能这条正在处理的消息就没有完成消息消费,数据就会丢失.为了确保数据不会丢失,RabbitMQ支持消 ...
- RabbitMQ入门教程(十二):消息确认Ack
原文:RabbitMQ入门教程(十二):消息确认Ack 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csd ...
- rabbitmq++:RabbitMQ的消息确认ACK机制介绍
1):什么是消息确认ACK. 答:如果在处理消息的过程中,消费者的服务器在处理消息的时候出现异常,那么可能这条正在处理的消息就没有完成消息消费,数据就会丢失.为了确保数据不会丢失,RabbitMQ支持 ...
- springboot + rabbitmq 用了消息确认机制,感觉掉坑里了
本文收录在个人博客:www.chengxy-nds.top,技术资源共享,一起进步 最近部门号召大伙多组织一些技术分享会,说是要活跃公司的技术氛围,但早就看穿一切的我知道,这 T M 就是为了刷KPI ...
- RabbitMQ 消费者的消息确认机制
消息确认的几点说明: 1. Basic.Ack 发回给 RabbitMQ 以告知,可以将相应 message 从 RabbitMQ 的消息缓存中移除.2. Basic.Ack 未被 consumer ...
- 【RabbitMQ】6、rabbitmq生产者的消息确认
通过Publisher Confirms and Returns机制,生产者可以判断消息是否发送到了exchange及queue,而通过消费者确认机制,Rabbitmq可以决定是否重发消息给消费者,以 ...
- 【rabbitmq】rabbitmq概念解析--消息确认--示例程序
概述 本示例程序全部来自rabbitmq官方示例程序,rabbitmq-demo: 官方共有6个demo,针对不同的语言(如 C#,Java,Spring-AMQP等),都有不同的示例程序: 本示例程 ...
- rabbitmq生产者的消息确认
通过Publisher Confirms and Returns机制,生产者可以判断消息是否发送到了exchange及queue,而通过消费者确认机制,Rabbitmq可以决定是否重发消息给消费者,以 ...
- springboot整合rabbitmq实现生产者消息确认、死信交换器、未路由到队列的消息
在上篇文章 springboot 整合 rabbitmq 中,我们实现了springboot 和rabbitmq的简单整合,这篇文章主要是对上篇文章功能的增强,主要完成如下功能. 需求: 生产者在启 ...
随机推荐
- Parallel.For循环与普通的for循环
前两天看书发现了一个新的循环Parallel.For,这个循环在循环期间可以创建多个线程并行循环,就是说循环的内容是无序的.这让我想到了我前面的牛牛模拟计算是可以用到这个循环的,我前面的牛牛模拟计算是 ...
- 根据motif binding来确定target gene | HOMER | FIMO | MEME
主流的motif数据库 JASPAR dbcorrdb - SCENIC使用的 TRANSFAC® 7.0 Public 2005 and TRANSCompel 7.0 Public 2005 - ...
- [.NET逆向] [入门级]de4dot参数详解
为了避免被0xd4d(de4dot作者)认为是"N00bUser"为了认识到Some of the advanced options may be incompatible, ca ...
- Refused to execute script from '...' because its MIME type ('') is not executable, and strict MIME type checking is enabled.
写在前面 部署项目到weblogic上启动首页访问空白, 浏览器控制台报如题错误. web.xml中把响应头添加防止攻击的报文过滤器禁用就行了(仅仅是为了启动), 以下为转载内容, 可以根据需要自行测 ...
- 【laravel5.5+Passport】laravel5的前后端分离之Passport设计
项目中使用到了laravel5的passport组件,进行前后端分离的 api认证部分: 前后端分离的api认证,我们用的是: [密码授权令牌],需要用户登录->指定client_id/clie ...
- Vue 与 动态组件 import 的尝试
<template> <component :is='fuck' :data='data'></component> </template> <s ...
- javascript常用方法 - Array
//1.Aarry方法 // 1.1 Array.from(arrayLike[, mapFn[, thisArg]]) // @arrayLike 想要转换成数组的伪数组对象或可迭代对象. // @ ...
- angular7post提交的例子
postDemo() { const params = new HttpParams(); '); '); this._httpClient.post('http://127.0.0.1:12345/ ...
- zp本地包
https://pan.baidu.com/s/13670pdPNvG_o1coYFnovXA 密码: 3pk3
- Ubuntu下配置Window CIFS共享
转自:https://blog.csdn.net/wanfengzhong/article/details/52550074 1. 准备windows共享文件夹 2. 安装 cifs-utilssud ...