fix: 解决 redis mq 消息丢失问题

pull/2/head
gaibu 2022-12-23 10:28:59 +08:00
parent a6c92816f0
commit 46f3790492
2 changed files with 52 additions and 43 deletions

View File

@ -8,9 +8,11 @@ import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import cn.iocoder.yudao.framework.mq.core.interceptor.RedisMessageInterceptor; import cn.iocoder.yudao.framework.mq.core.interceptor.RedisMessageInterceptor;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener; import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener;
import cn.iocoder.yudao.framework.mq.scheduler.PendingMessageScheduler; import cn.iocoder.yudao.framework.mq.scheduler.RedisPendingMessageResendJob;
import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.connection.RedisServerCommands;
@ -25,6 +27,7 @@ import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.stream.DefaultStreamMessageListenerContainerX; import org.springframework.data.redis.stream.DefaultStreamMessageListenerContainerX;
import org.springframework.data.redis.stream.StreamMessageListenerContainer; import org.springframework.data.redis.stream.StreamMessageListenerContainer;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -35,6 +38,7 @@ import java.util.Properties;
* @author * @author
*/ */
@Slf4j @Slf4j
@EnableScheduling // 启用定时任务,用于 RedisPendingMessageResendJob 重发消息
@AutoConfiguration(after = YudaoRedisAutoConfiguration.class) @AutoConfiguration(after = YudaoRedisAutoConfiguration.class)
public class YudaoMQAutoConfiguration { public class YudaoMQAutoConfiguration {
@ -70,17 +74,19 @@ public class YudaoMQAutoConfiguration {
} }
/** /**
* * Redis Stream
* @return
*/ */
@Bean @Bean
public PendingMessageScheduler pendingMessageScheduler(){ public RedisPendingMessageResendJob redisPendingMessageResendJob(List<AbstractStreamMessageListener<?>> listeners,
return new PendingMessageScheduler(); RedisMQTemplate redisTemplate,
@Value("${spring.application.name}") String groupName,
RedissonClient redissonClient) {
return new RedisPendingMessageResendJob(listeners, redisTemplate, groupName, redissonClient);
} }
/** /**
* Redis Stream * Redis Stream
* * <p>
* Redis Stream xreadgroup https://www.geek-book.com/src/docs/redis/redis/redis.io/commands/xreadgroup.html * Redis Stream xreadgroup https://www.geek-book.com/src/docs/redis/redis/redis.io/commands/xreadgroup.html
*/ */
@Bean(initMethod = "start", destroyMethod = "stop") @Bean(initMethod = "start", destroyMethod = "stop")
@ -108,7 +114,8 @@ public class YudaoMQAutoConfiguration {
// 创建 listener 对应的消费者分组 // 创建 listener 对应的消费者分组
try { try {
redisTemplate.opsForStream().createGroup(listener.getStreamKey(), listener.getGroup()); redisTemplate.opsForStream().createGroup(listener.getStreamKey(), listener.getGroup());
} catch (Exception ignore) {} } catch (Exception ignore) {
}
// 设置 listener 对应的 redisTemplate // 设置 listener 对应的 redisTemplate
listener.setRedisMQTemplate(redisMQTemplate); listener.setRedisMQTemplate(redisMQTemplate);
// 创建 Consumer 对象 // 创建 Consumer 对象

View File

@ -1,12 +1,11 @@
package cn.iocoder.yudao.framework.mq.scheduler; package cn.iocoder.yudao.framework.mq.scheduler;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener; import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock; import org.redisson.api.RLock;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.stream.Consumer; import org.springframework.data.redis.connection.stream.Consumer;
import org.springframework.data.redis.connection.stream.MapRecord; import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.PendingMessagesSummary; import org.springframework.data.redis.connection.stream.PendingMessagesSummary;
@ -14,51 +13,53 @@ import org.springframework.data.redis.connection.stream.ReadOffset;
import org.springframework.data.redis.connection.stream.StreamOffset; import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.connection.stream.StreamRecords; import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.core.StreamOperations; import org.springframework.data.redis.core.StreamOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
/** /**
* crash * crash
*/ */
@Slf4j @Slf4j
@EnableScheduling public class RedisPendingMessageResendJob {
public class PendingMessageScheduler {
private static final String LOCK_KEY = "redis:pending:msg:lock"; private static final String LOCK_KEY = "redis:pending:msg:lock";
@Autowired
private List<AbstractStreamMessageListener<?>> listeners; private final List<AbstractStreamMessageListener<?>> listeners;
@Autowired private final RedisMQTemplate redisTemplate;
private RedisMQTemplate redisTemplate; private final String groupName;
@Value("${spring.application.name}") private final RedissonClient redissonClient;
private String groupName;
@Autowired public RedisPendingMessageResendJob(List<AbstractStreamMessageListener<?>> listeners, RedisMQTemplate redisTemplate, String groupName, RedissonClient redissonClient) {
private RedissonClient redissonClient; this.listeners = listeners;
this.redisTemplate = redisTemplate;
this.groupName = groupName;
this.redissonClient = redissonClient;
}
/** /**
* * ,35
*/ */
@Scheduled(fixedRate = 60 * 1000) @Scheduled(cron = "35 * * * * ?")
public void processPendingMessage() { public void messageResend() {
final RLock lock = redissonClient.getLock(LOCK_KEY); RLock lock = redissonClient.getLock(LOCK_KEY);
try { log.info("[messageResend][尝试获取锁]");
// 尝试加锁,最多等待 30 秒,上锁以后 60 秒自动解 // 尝试加
boolean lockFlag = lock.tryLock(30, 60, TimeUnit.SECONDS); if (lock.tryLock()) {
if (lockFlag) { try {
execute(); execute();
} catch (Exception ex) {
log.error("[messageResend][执行异常]", ex);
} finally {
lock.unlock();
} }
} catch (InterruptedException e) {
log.error("获取锁失败", e);
} }
} }
private void execute() { private void execute() {
StreamOperations<String, Object, Object> ops = redisTemplate.getRedisTemplate().opsForStream(); StreamOperations<String, Object, Object> ops = redisTemplate.getRedisTemplate().opsForStream();
for (AbstractStreamMessageListener<?> listener : listeners) { listeners.forEach(listener -> {
PendingMessagesSummary pendingMessagesSummary = ops.pending(listener.getStreamKey(), groupName); PendingMessagesSummary pendingMessagesSummary = ops.pending(listener.getStreamKey(), groupName);
// 每个消费者的pending消息数量 // 每个消费者的pending消息数量
Map<String, Long> pendingMessagesPerConsumer = pendingMessagesSummary.getPendingMessagesPerConsumer(); Map<String, Long> pendingMessagesPerConsumer = pendingMessagesSummary.getPendingMessagesPerConsumer();
@ -69,17 +70,18 @@ public class PendingMessageScheduler {
// 从消费者的pending队列中读取消息 // 从消费者的pending队列中读取消息
List<MapRecord<String, Object, Object>> retVal = ops.read(Consumer.from(groupName, consumerName), StreamOffset.create(listener.getStreamKey(), ReadOffset.from("0"))); List<MapRecord<String, Object, Object>> retVal = ops.read(Consumer.from(groupName, consumerName), StreamOffset.create(listener.getStreamKey(), ReadOffset.from("0")));
if (CollUtil.isNotEmpty(retVal)) {
for (MapRecord<String, Object, Object> record : retVal) {
// 重新投递消息
redisTemplate.getRedisTemplate().opsForStream().add(StreamRecords.newRecord()
.ofObject(record.getValue()) // 设置内容
.withStreamKey(listener.getStreamKey()));
for (MapRecord<String, Object, Object> record : retVal) { // ack 消息消费完成
// 重新投递消息 redisTemplate.getRedisTemplate().opsForStream().acknowledge(groupName, record);
redisTemplate.getRedisTemplate().opsForStream().add(StreamRecords.newRecord() }
.ofObject(record.getValue()) // 设置内容
.withStreamKey(listener.getStreamKey()));
// ack 消息消费完成
redisTemplate.getRedisTemplate().opsForStream().acknowledge(groupName, record);
} }
}); });
} });
} }
} }