如何创建与Master和Slave的Redis连接
How to create a Redis connection with Master and Slave
我正在尝试进行 Redis 连接,我有一个 "master" 端口和两个从站。我想用哨兵来做这件事。
我实际连接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,如果master挂了,这会自动连接到slave。
希望大家帮帮我,
罗斯.
我终于搞定了,效果很好。
我会把我的代码放在这里,希望这对其他人有帮助!
var Redis = require('ioredis');
// preferredSlaves array format
var preferredSlaves = [
{ ip: config.redis_slave_1, port: config.redis_port, prio: 1 },
{ ip: config.redis_slave_2, port: config.redis_port, prio: 2 }
];
var redis = new Redis({
port: config.redis_port,
host: config.redis_host,
sentinels: [{ host: config.redis_sentinel_1, port: config.redis_sentinel }, { host: config.redis_sentinel_2, port: config.redis_sentinel }, { host: config.redis_sentinel_3, port: config.redis_sentinel }],
name: config.redis_master_name,
password: config.redis_password,
preferredSlaves: preferredSlaves
});
redis.on('connect', function(err, res) {
logger.info('Connected to Redis ' + process.pid);
redisIsReady = true;
console.log('Connected to Redis ' + process.pid + " REDIS " + JSON.stringify(redis.options) )
});
redis.on('error', function(err) {
logger.error('Error connecting to Redis ' + process.pid);
redisIsReady = false;
console.log('error to Redis ' + err)
});
感谢您的所有回复,
问候!!
我正在尝试进行 Redis 连接,我有一个 "master" 端口和两个从站。我想用哨兵来做这件事。
我实际连接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,如果master挂了,这会自动连接到slave。
希望大家帮帮我,
罗斯.
我终于搞定了,效果很好。
我会把我的代码放在这里,希望这对其他人有帮助!
var Redis = require('ioredis');
// preferredSlaves array format
var preferredSlaves = [
{ ip: config.redis_slave_1, port: config.redis_port, prio: 1 },
{ ip: config.redis_slave_2, port: config.redis_port, prio: 2 }
];
var redis = new Redis({
port: config.redis_port,
host: config.redis_host,
sentinels: [{ host: config.redis_sentinel_1, port: config.redis_sentinel }, { host: config.redis_sentinel_2, port: config.redis_sentinel }, { host: config.redis_sentinel_3, port: config.redis_sentinel }],
name: config.redis_master_name,
password: config.redis_password,
preferredSlaves: preferredSlaves
});
redis.on('connect', function(err, res) {
logger.info('Connected to Redis ' + process.pid);
redisIsReady = true;
console.log('Connected to Redis ' + process.pid + " REDIS " + JSON.stringify(redis.options) )
});
redis.on('error', function(err) {
logger.error('Error connecting to Redis ' + process.pid);
redisIsReady = false;
console.log('error to Redis ' + err)
});
感谢您的所有回复,
问候!!