Last active
August 4, 2022 14:30
-
-
Save Ajido/e1c9cbfdc7628fc5b688 to your computer and use it in GitHub Desktop.
Node.js Redis Sentinel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
let Redis = require('ioredis'); | |
let redis = new Redis({ | |
sentinels: [ | |
{ host: '127.0.0.1', port: 15379 }, | |
{ host: '127.0.0.1', port: 15380 }, | |
{ host: '127.0.0.1', port: 15381 } | |
], | |
name: 'mymaster' | |
}); | |
let func = Redis.Promise.coroutine(function* () { | |
let key = 'foo:' + Math.random(); | |
try { | |
yield redis.setex(key, 10, 'xxxxxxxxx').timeout(1000); | |
let value = yield redis.get(key).timeout(1000); | |
console.log(Date.now(), value); | |
} | |
catch (err) { | |
console.error(err); | |
} | |
}); | |
let loop = function() { | |
func().then(loop).catch(function(err) { | |
console.error(err.stack); | |
process.exit(1); | |
}); | |
} | |
redis.once('ready', function() { | |
loop(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wget http://download.redis.io/releases/redis-2.8.19.tar.gz | |
tar xzf redis-2.8.17.tar.gz | |
cd redis-2.8.17 | |
make | |
192.168.1.102 $ redis-cli -p 6379 "slaveof 192.168.1.101 6379" | |
192.168.1.103 $ redis-cli -p 6379 "slaveof 192.168.1.101 6379" | |
192.168.1.102 $ redis-cli -p 26379 "SENTINEL REMOVE mymaster" | |
192.168.1.102 $ redis-cli -p 26379 "SENTINEL MONITOR mymaster 192.168.1.101 6379 2" | |
192.168.1.102 $ redis-cli -p 26379 "SENTINEL SET mymaster down-after-milliseconds 5000" | |
192.168.1.103 $ redis-cli -p 26379 "SENTINEL REMOVE mymaster" | |
192.168.1.103 $ redis-cli -p 26379 "SENTINEL MONITOR mymaster 192.168.1.101 6379 2" | |
192.168.1.103 $ redis-cli -p 26379 "SENTINEL SET mymaster down-after-milliseconds 5000" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/init.d/redis | |
#!/bin/sh | |
# | |
# Simple Redis init.d script conceived to work on Linux systems | |
# as it does use of the /proc filesystem. | |
# chkconfig: - 85 15 | |
# description: redis-server | |
# processname: redis | |
. /etc/rc.d/init.d/functions | |
REDISPORT=6379 | |
EXEC=/usr/local/bin/redis-server | |
CLIEXEC=/usr/local/bin/redis-cli | |
PIDFILE=/var/run/redis.pid | |
CONF="/etc/redis.conf" | |
case "$1" in | |
start) | |
if [ -f $PIDFILE ]; then | |
echo "$PIDFILE exists, process is already running or crashed" | |
else | |
echo "Starting Redis server..." | |
$EXEC $CONF | |
sleep 2 | |
echo '-100' > /proc/$(cat $PIDFILE)/oom_score_adj | |
fi | |
;; | |
stop) | |
if [ ! -f $PIDFILE ]; then | |
echo "$PIDFILE does not exist, process is not running" | |
else | |
PID=$(cat $PIDFILE) | |
echo "Stopping ..." | |
$CLIEXEC -p $REDISPORT shutdown | |
while [ -x /proc/${PID} ]; do | |
echo "Waiting for Redis to shutdown ..." | |
sleep 1 | |
done | |
echo "Redis stopped" | |
fi | |
;; | |
status) | |
status redis | |
;; | |
*) | |
echo "Please use start or stop as first argument" | |
;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/init.d/redis-sentinel | |
#!/bin/sh | |
# | |
# Simple Redis init.d script conceived to work on Linux systems | |
# as it does use of the /proc filesystem. | |
# chkconfig: - 85 15 | |
# description: redis-sentinel-server | |
# processname: redis | |
. /etc/rc.d/init.d/functions | |
REDISPORT=26379 | |
EXEC=/usr/local/bin/redis-server | |
CLIEXEC=/usr/local/bin/redis-cli | |
PIDFILE=/var/run/redis-sentinel.pid | |
CONF="/etc/redis-sentinel.conf" | |
case "$1" in | |
start) | |
if [ -f $PIDFILE ]; then | |
echo "$PIDFILE exists, process is already running or crashed" | |
else | |
echo "Starting Redis-Sentinel server..." | |
$EXEC $CONF --sentinel | |
sleep 2 | |
echo '-100' > /proc/$(cat $PIDFILE)/oom_score_adj | |
fi | |
;; | |
stop) | |
if [ ! -f $PIDFILE ]; then | |
echo "$PIDFILE does not exist, process is not running" | |
else | |
PID=$(cat $PIDFILE) | |
echo "Stopping ..." | |
$CLIEXEC -p $REDISPORT shutdown | |
while [ -x /proc/${PID} ]; do | |
echo "Waiting for Redis-Sentinel to shutdown ..." | |
sleep 1 | |
done | |
echo "Redis-Sentinel stopped" | |
fi | |
;; | |
status) | |
status redis | |
;; | |
*) | |
echo "Please use start or stop as first argument" | |
;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sentinel = require('redis-sentinel'); | |
var redis = require('redis'); | |
var Sentinel = sentinel.Sentinel([ | |
{ host: '192.168.1.101', port: 26379 }, | |
{ host: '192.168.1.102', port: 26379 }, | |
{ host: '192.168.1.103', port: 26379 } | |
]); | |
var redisMaster = Sentinel.createClient('mymaster', { enable_offline_queue: true, retry_max_delay: 3000 }); | |
var redisSlave = redis.createClient('/var/tmp/redis.sock', { enable_offline_queue: true, retry_max_delay: 3000 }); | |
var index = 0; | |
function test() { | |
index++; | |
var r = Math.random(); | |
redisMaster.set('foo' + index + r, 'OK' + index); | |
setTimeout(function() { | |
redisSlave.get('foo' + index + r, function(err, reply) { | |
if (err) { | |
console.log(err.stack); | |
setImmediate(test); | |
return; | |
} | |
console.log((reply || '').toString()); | |
setImmediate(test); | |
}); | |
}, 10); | |
} | |
redisMaster.on('error', function(err) { | |
console.log(err.stack); | |
}); | |
redisSlave.on('error', function(err) { | |
console.log(err.stack); | |
}); | |
test(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/redis-sentinel.conf | |
daemonize yes | |
port 26379 | |
dir "/tmp" | |
logfile "/var/log/redis-sentinel.log" | |
pidfile "/var/run/redis-sentinel.pid" | |
sentinel monitor mymaster 192.168.1.101 6379 2 | |
sentinel down-after-milliseconds mymaster 5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/redis.conf | |
daemonize yes | |
pidfile "/var/run/redis.pid" | |
port 6379 | |
unixsocket "/var/tmp/redis.sock" | |
unixsocketperm 777 | |
tcp-backlog 511 | |
timeout 0 | |
tcp-keepalive 0 | |
loglevel notice | |
logfile "/var/log/redis.log" | |
databases 16 | |
save 900 1 | |
save 300 10 | |
save 60 10000 | |
stop-writes-on-bgsave-error yes | |
rdbcompression yes | |
rdbchecksum yes | |
dbfilename "dump.rdb" | |
dir "/var/redis" | |
slave-serve-stale-data yes | |
slave-read-only yes | |
repl-disable-tcp-nodelay no | |
slave-priority 100 | |
appendonly no | |
appendfilename "appendonly.aof" | |
appendfsync always | |
no-appendfsync-on-rewrite no | |
auto-aof-rewrite-percentage 100 | |
auto-aof-rewrite-min-size 64mb | |
aof-load-truncated yes | |
lua-time-limit 5000 | |
slowlog-log-slower-than 10000 | |
slowlog-max-len 128 | |
latency-monitor-threshold 0 | |
notify-keyspace-events "" | |
hash-max-ziplist-entries 512 | |
hash-max-ziplist-value 64 | |
list-max-ziplist-entries 512 | |
list-max-ziplist-value 64 | |
set-max-intset-entries 512 | |
zset-max-ziplist-entries 128 | |
zset-max-ziplist-value 64 | |
hll-sparse-max-bytes 3000 | |
activerehashing yes | |
client-output-buffer-limit normal 0 0 0 | |
client-output-buffer-limit slave 256mb 64mb 60 | |
client-output-buffer-limit pubsub 32mb 8mb 60 | |
hz 10 | |
aof-rewrite-incremental-fsync yes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
redis/ioredis#70