3

目前我正在使用https://github.com/mranney/node_redis作为我的节点 redis 客户端。

client.retry_delay默认设置为 250 毫秒。

我尝试连接到redis,一旦连接成功,我手动停止了redis服务器以查看是否client.retry_delay有效。但我没有看到它工作。

以下日志消息在使用创建的 redisClients上记录ready和事件:endcreateClient

[2012-03-30 15:13:05.498] [INFO] Development - Node Application is running on port 8090
[2012-03-30 15:13:08.507] [INFO] Development - Connection Successfully Established to  '127.0.0.1' '6379'
[2012-03-30 15:16:33.886] [FATAL] Development - Connection Terminated to  '127.0.0.1' '6379'

当服务器恢复正常时,我没有再次看到成功消息 [未触发就绪事件]。

我错过了什么吗?什么时候使用重试常量?是否有办法在节点发生故障后查找 redis 服务器是否启动?

4

3 回答 3

11

我无法重现这个。你能试试这段代码,停止你的redis服务器,然后检查日志输出吗?

var client = require('redis').createClient();

client.on('connect'     , log('connect'));
client.on('ready'       , log('ready'));
client.on('reconnecting', log('reconnecting'));
client.on('error'       , log('error'));
client.on('end'         , log('end'));

function log(type) {
    return function() {
        console.log(type, arguments);
    }
}
于 2012-03-30T10:34:50.990 回答
2

回答@ 2020 年 2 月

const redis = require('redis');
const log = (type, fn) => fn ? () => {
    console.log(`connection ${type}`);
} : console.log(`connection ${type}`);

// Option 1: One connection is enough per application
const client = redis.createClient('6379', "localhost", {
    retry_strategy: (options) => {
        const {error, total_retry_time, attempt} = options;
        if (error && error.code === "ECONNREFUSED") {
            log(error.code); // take actions or throw exception
        }
        if (total_retry_time > 1000 * 15) { //in ms i.e. 15 sec
            log('Retry time exhausted'); // take actions or throw exception
        }
        if (options.attempt > 10) {
            log('10 attempts done'); // take actions or throw exception
        }
        console.log("Attempting connection");
        // reconnect after
        return Math.min(options.attempt * 100, 3000); //in ms
    },
});

client.on('connect', log('connect', true));
client.on('ready', log('ready', true));
client.on('reconnecting', log('reconnecting', true));
client.on('error', log('error', true));
client.on('end', log('end', true));

对于完整的运行示例,请克隆node-cheat并运行node connect-retry.js.

于 2020-02-18T13:40:33.173 回答
-2

添加到上面的答案。小变化。提供的回调应该是方法名称,而不是执行方法本身。如下所示:

function redisCallbackHandler(message){
    console.log("Redis:"+ message);
}

var redis = require("redis");
var redisclient = redis.createClient();
redisclient.on('connect', redisCallbackHandler);
redisclient.on('ready', redisCallbackHandler);
redisclient.on('reconnecting', redisCallbackHandler);
redisclient.on('error', redisCallbackHandler);
redisclient.on('end', redisCallbackHandler);
于 2013-08-18T12:20:04.017 回答