Redis节点使用ioredisclusterRetryStrategy异常处理

问题描述

我在运行具有 6 个节点的 redis 集群的 mac 上进行了本地设置。我一直在尝试处理连接错误并限制集群脱机时的重试次数(故意试图引发对 mongo 的回退),但无论我传入的集群/redis 选项如何,它都会不断重试,除非我返回“null” clusterRetryStrategy。这是我的 redis 服务的快照。

import Redis from 'ioredis';

export class RedisService {
    public readonly redisClient: Redis.Cluster;
    public readonly keyPrefix = 'MyRedisKey';

    public readonly clusterOptions: Redis.ClusterOptions = {
        enableReadyCheck: true,retryDelayOnClusterDown: 3000,retryDelayOnFailover: 30000,retryDelayOnTryAgain: 30000,clusterRetryStrategy: (times: number) => {
            // notes: this is the only way I have figured how to throw errors without retying for ever when connection to the cluster fails. 
            // tried to use maxRetriesPerRequest=1,different variations of retryStrategy
            return null;
        },slotsRefreshInterval: 10000,redisOptions: {
            keyPrefix: this.keyPrefix,autoResubscribe: true,autoResendUnfulfilledCommands: false,password: process.env?.REdis_PASSWORD,enableOfflineQueue: false,maxRetriesPerRequest: 1
        }
    };

    constructor() {
        const nodes: any = ['127.0.0.1:30001','127.0.0.1:30002','127.0.0.1:30003','127.0.0.1:30004','127.0.0.1:30005','127.0.0.1:30006'];
        this.redisClient = new Redis.Cluster(nodes,this.clusterOptions);
    }

    public hget = async (field: string): Promise<any> => {
        const response = await this.redisClient.hget(this.keyPrefix,field);
        return response;
    }
}

如果我的 redis 集群停止并且我调用此服务,例如:

public readonly redisService: RedisService = new RedisService();
try {
  const item = await this.redisService.hget('test');
} catch(e){
  console.error(e);
}

我不断收到错误“[ioredis] 未处理的错误事件:ClusterallFailedError:无法刷新插槽缓存。”并且它永远不会落入 catch 块中。

顺便说一下,我尝试了此处列出的解决方案,但没有奏效。 Redis (ioredis) - Unable to catch connection error in order to handle them gracefully

以下是我使用的 ioredis npm 包的版本。

"ioredis": "4.19.4"
"@types/ioredis": "4.17.10"

感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)