我正在尝试进行 Redis 连接,我有一个“主”端口和两个从属端口。我想用哨兵做到这一点。
我认为连接redis的实际代码实际上已被弃用。
这是我的代码。
var redis = require('redis');
var client = redis.createClient(config.redis_port, config.redis_host,
{no_ready_check: true});
if (config.redis_password != null) {
client.auth(config.redis_password, function (err) {
if (err) throw err;
});
}
client.on('connect', function(err, res) {
logger.info('Connected to Redis ' + process.pid);
redisIsReady = true;
});
client.on('error', function(err) {
logger.error('Error connecting to Redis ' + process.pid);
redisIsReady = false;
});
client.get(objectRequest.customerId, function(err, reply) {
if (reply != null && reply >= config.max_requests) {
var json = JSON.stringify({errorCode: validationErrors.TOO_MANY_REQUEST,
description: errorMessage[validationErrors.TOO_MANY_REQUEST]});
res.setHeader('Retry-After', config.retry_after);
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', json.length);
res.writeHead(429);
res.write(json);
return res.end();
}
// Set a value with an expiration
client.incr(objectRequest.customerId);
client.expire(objectRequest.customerId, config.retry_after);
});
我正在阅读其他帖子,我认为使用 ioredis 可能会很酷。但是我对Redis了解不多...
我想与redis建立连接,如果主服务器关闭,则会自动连接到从服务器。
我希望你能帮助我,
罗斯。