微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Redis节点使用ioredisclusterRetryStrategy异常处理

如何解决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 举报,一经查实,本站将立刻删除。