<七>rabbitmq案例实战进阶
文章目录
rabbitmq案例实战进阶
消息到达交换机,生产者ack成功 如果发送交换机,没有匹配路由到队列,ack返回的是true
只要到交换机,返回的是TRUE
生产者消息确认
confirm消息确认
如何验证确认不到,做取反验证
消息没有发送成功不能在模板类里边重新发送
rabbit服务器主程序发送消息过程中rabbitmq服务器处于关闭状态,主程序和confirmCallback都在等待connection的恢复
rabbitTemplate convertAndSend会创建一个新的连接,导致第一条发送消息和重新发送消息两个线程死锁
解决办法,将没有发送成功的内容发送到缓存中,定时任务轮训发送
return消息机制

设置为true的话就可以通过回调函数接收不能正确发送的消息
如何触发return机制,将订单队列与其队列解绑,死信交换机和死信队列解绑
如果发送交换机成功,但是没有匹配路由到指定的队列, 这个时候ack返回是true(这是一个坑)
消费者消息确认


自动确认



则会自动重试直到过期
手动确认

1首先生产者发送一条消息,关闭消费者
2关掉mysql连接
3最后启动消费者,查看是否会重试三次

限流
限流的配置
给10个线程的话
在消费者的代码里打断点

此时设置限流是一个
每次只消费一个
幂等性
测试异常情况
生产者启动 消费者启动

此时等待消费的确认,此时关闭消费者,则未确认消费的消息重新变成准备的状态
此时把消费确认的代码的注释去掉,启动消费者则又一次消费成功,造成重复消费的问题
// 解决幂等性问题
Object orderMessage = redisTemplate.boundHashOps("orderMessage").get(message.getMessageProperties().getCorrel ationId());
if (orderMessage == null) {
System.out.println("已经消费了, 不在重复消费");
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); return; }死信队列消费者

package com.boxuegu.seckillorder.consumer;
import com.boxuegu.seckillorder.mapper.TbSeckillErrorMapper;
import com.boxuegu.seckillorder.mapper.TbSeckillGoodsMapper;
import com.boxuegu.seckillorder.pojo.TbSeckillError;
import com.fasterxml.jackson.databind.type.MapType;
import com.rabbitmq.client.Channel;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.ConnectException;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* 处理死信队列信息.将错误入库
*/
@Component
public class SeckillOrder_dlx {
@Autowired
TbSeckillErrorMapper seckillErrorMapper;
@Autowired
RedisTemplate redisTemplate;
@RabbitListener(queues = {"seckillOrder_deadLetterQueue"})
public void receive(Message message, Channel channel) throws IOException {
// 创建json消息转换器
Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
// 获取消息
Map map = (Map) jackson2JsonMessageConverter.fromMessage(message);
//1.获取消息
Long seckillId = ((Integer) map.get("seckillId")).longValue();
//2.获取消息ID
String correlationId = message.getMessageProperties().getCorrelationId();
TbSeckillError seckillError = new TbSeckillError();
seckillError.setSeckillId(seckillId);
seckillError.setCorrelationId(correlationId);
//3.获取缓存错误
Map errorMap = (Map) redisTemplate.boundHashOps("orderMessageStatus").
get(message.getMessageProperties().getCorrelationId());
if (errorMap != null) {// nack信息
seckillError.setStatus(Integer.parseInt((String) errorMap.get("status")));
seckillError.setErrorMsg((String) errorMap.get("errorMsg"));
// 清除错误信息
redisTemplate.boundHashOps("orderMessageStatus").delete(correlationId);
} else {
seckillError.setStatus(-7);// 消息TTL过期或路由失败
seckillError.setErrorMsg("消息TTL过期或路由失败");
}
// 将错误信息保存入库
seckillErrorMapper.insert(seckillError);
System.out.println(correlationId + "消息以保存数据库");
}
}
延迟队列
解决以下问题
1 生产者发送消息到交换机,交换机返回给生产者nack的时候,如果出现网络抖动的情况下,生产者无法获取nack的消息,那么就无法重启重试的机制
2 消息路由到死信队列的时候,路由失败的时候,那么错误信息无法保存
3 消费的时候,消费者突然宕机 的时候,那么broker无法得知是否已经消费
RabbitConfig
/**
* 声明延时检查的死信交换机
*/
@Bean
public FanoutExchange deadLetterCheckExchange() {
return new FanoutExchange("seckillOrder_deadLetterCheck_exchange", true, false);
}
/**
* 声明延时检查的死信队列,处理延时队列的消息(生产者发送消息时同时发送延时检查)
*/
@Bean
public Queue deadLetterCheckQueue() {
return new Queue("seckillOrder_deadLetterCheckQueue", true, false, false, null);
}
/**
* 绑定 延时检查死信交换机与延时检查死信队列进行绑定
*/
@Bean
public Binding deadLetterCheckBinding() {
return BindingBuilder.bind(this.deadLetterCheckQueue()).to(this.deadLetterCheckExchange());
}
/**
* 声明延时检查队列
*/
@Bean
public Queue seckillOrder_check() {
Map<String, Object> arguments = new HashMap<>();
// 消息生存周期 > 生产者与消费者整个投递过程所花费的时间
arguments.put("x-message-ttl", 20000);
// 死信交换机
arguments.put("x-dead-letter-exchange", "seckillOrder_deadLetterCheck_exchange");
return new Queue("seckillOrder_check", true, false, false, arguments);
}
/**
* 绑定延时检查队列
*/
@Bean
public Binding binding_checkQueue() {
return BindingBuilder.bind(this.seckillOrder_check()).to(this.topicExchange()).with("seckillOrder.#.check");
}
生产者
// 发送延时检查 rabbitTemplate.convertAndSend("seckillOrder_topic_exchange","seckillOrder.check",correlationId,correlationData);
System.out.println("发送延时检查成功");消费者
package com.boxuegu.seckillorder.consumer;
import com.boxuegu.seckillorder.mapper.TbSeckillErrorMapper;
import com.boxuegu.seckillorder.pojo.TbSeckillError;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
/**
* 延时队列消费者.检查整个消息投递过程
*/
@Component
public class SeckillOrder_dlxCheck {
@Autowired
TbSeckillErrorMapper seckillErrorMapper;
@Autowired
RedisTemplate redisTemplate;
@RabbitListener(queues = {"seckillOrder_deadLetterCheckQueue"})
public void receive(String correlationId, Message message, Channel channel) throws IOException {
// 获取初始消息
Map map = (Map) redisTemplate.boundHashOps("orderMessage").get(correlationId);
if (map == null) {
System.out.println(correlationId + "消息投递成功.");
} else {
System.out.println(correlationId + "消息投递失败.");
// 查询正常死信队列中是否已经保存错误信息,如果死信队列中已经保存则不再保存
TbSeckillError selectByCorrelationId = seckillErrorMapper.selectByCorrelationId(correlationId);
if (selectByCorrelationId == null) {
TbSeckillError seckillError = new TbSeckillError();
seckillError.setSeckillId((Long) map.get("seckillId"));
seckillError.setCorrelationId(correlationId);
Map errorMap = (Map) redisTemplate.boundHashOps("orderMessageStatus").get(correlationId);
if (errorMap != null) {// 路由死信队列失败
seckillError.setStatus(Integer.parseInt((String) errorMap.get("status")));
seckillError.setErrorMsg((String) errorMap.get("errorMsg"));
// 清除错误信息
redisTemplate.boundHashOps("orderMessageStatus").delete(correlationId);
} else {
seckillError.setStatus(-8);//生产者没有接收到NACK或者消费者宕机
}
seckillErrorMapper.insert(seckillError);
System.out.println("消息投递失败,错误信息入库");
} else {
System.out.println("已经保存错误信息则不再保存.");
}
}
}
}
消费失败后的后续解决方案

使用高可用集群


评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论