2017-06-08 16 views
0

Redis接続をしようとしていますが、「マスタ」ポートと2つのスレーブがあります。私はセンチネルでこれをやりたい。マスタとスレーブで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への接続を行いたいと思います。マスターがダウンしていると、自動的にスレーブに接続します。

私は

ロス、あなたは私を助け願っています。

+0

時には 'objectRequest.customerId'の値を取得しようとすると、クライアントがサーバに接続されておらず、認証されている可能性があります。 – Vishnu

+0

それは私の問題ではない、私はredisへの接続をしたいとマスターがダウンしている場合、これは自動的にスレーブに接続します。あなたの答えに感謝します。 – Roth

+0

https://stackoverflow.com/questions/34155977/redis-promoting-a-slave-to-master-manual – Vishnu

答えて

0

私はついにそれを完成し、それはうまく動作します。

私はあなたに私のコードを教えてくれます、これは他人に役立つことを願っています!すべてのあなたの応答のための

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) 
}); 

おかげで、

よろしく!!

関連する問題