目前我正在使用https://github.com/mranney/node_redis作为我的节点redis客户端。
client.retry_delay
默认设置为250毫秒。
我尝试连接到redis,一旦连接成功,我手动停止redis服务器以查看client.retry_delay是否有效。但我没有看到它有效。
以下日志消息记录在ready
&使用createClient
end
个事件
[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服务器是否在节点发生故障后出现?
答案 0 :(得分:9)
我无法重现这一点。你可以试试这个代码,停止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);
}
}
答案 1 :(得分:1)
@ 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
。
答案 2 :(得分:-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);