redis主备搭建以及测试
2018-11-26 本文已影响0人
斜月86
0 redis下载
yum -y install gcc automake autoconf libtool make
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
tar -xvf redis-4.0.11.tar.gz
cd redis-4.0.11
make
1、需要准备的文件如下
mkdir conf 放入配置文件
## redis 配置文件
redis-6379.conf
redis-6380.conf
redis-6381.conf
## sentinel配置文件
sentinel-26379.conf
sentinel-26380.conf
sentinel-26381.conf
## start
## 启动redis
./redis-4.0.11/src/redis-server conf/redis-6379.conf
./redis-4.0.11/src/redis-server conf/redis-6380.conf
./redis-4.0.11/src/redis-server conf/redis-6381.conf
## 启动sentinel
./redis-4.0.11/src/redis-sentinel conf/sentinel-26379.conf
./redis-4.0.11/src/redis-sentinel conf/sentinel-26380.conf
./redis-4.0.11/src/redis-sentinel conf/sentinel-26381.conf
redis-6379 配置内容
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump-6379.rdb"
dir "/root"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
##连接密码
requirepass foobared
masterauth foobared
appendonly no
appendfilename "appendonly-6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
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-size -2
list-compress-depth 0
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
redis-6380.conf配置
protected-mode no
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6380.pid"
loglevel notice
logfile "6380.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump-6380.rdb"
dir "/root"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly-6380.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
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-size -2
list-compress-depth 0
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
slaveof 192.168.101.79 6381
requirepass "foobared"
masterauth "foobared"
redis-6381.conf
protected-mode no
port 6381
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6381.pid"
loglevel notice
logfile "6381.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump-6381.rdb"
dir "/root"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly-6381.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
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-size -2
list-compress-depth 0
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
requirepass "foobared"
masterauth "foobared"
sentinel-26379.conf
port 26379
dir "/tmp"
daemonize yes
protected-mode no
logfile "26379.log"
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.101.79 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel auth-pass mymaster foobared
sentinel-26380.conf
port 26380
dir "/tmp"
daemonize yes
protected-mode no
logfile "26380.log"
sentinel monitor mymaster 192.168.101.79 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel auth-pass mymaster foobared
sentinel-26381.conf
port 26381
dir "/tmp"
daemonize yes
protected-mode no
logfile "26381.log"
sentinel monitor mymaster 192.168.101.79 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel auth-pass mymaster foobared
2 java 连接测试
JedisPoolConfig pool = new JedisPoolConfig();
pool.setTestOnReturn(true);
pool.setMaxWaitMillis(10000);
pool.setMaxIdle(3);
pool.setMaxWaitMillis(10000);
pool.setMaxTotal(200);
//声明一个set 存放哨兵集群的地址和端口
Set<String> sentinels = new HashSet<String>();
sentinels.add("192.168.101.79:26379");
sentinels.add("192.168.101.79:26380");
sentinels.add("192.168.101.79:26381");
// 名称 sentinel pool timeout auth
JedisSentinelPool sentinelPool = new JedisSentinelPool("mymaster", sentinels, pool,2000,"foobared");
// 使用sentinelPool获取jedis对象
Jedis jedis = sentinelPool.getResource();
jedis.set("dddddd","fffff");
System.out.println(jedis.get("dddddd"));