Redis版本:3.0.7
操作环境:Linux
一、redis 主从配置的作用是什么
redis主从配置,配置master 只能为写,slave只能为读,在客户端对poolconnect请求时候,,会将读请求转到slave上面,写请求转到master上面,同时,master和slave有同步功能,这就实现了(数据层)读写分离对上层(逻辑层)透明的正常逻辑。无需再通过中间件或者代码进行读写分析实现。
二、如何实现主从配置
以一台服务器,配置两个端口号为例子做介绍
redis实现主从配置最关键的两个两个配置文件是redis.conf和sentinel.conf,分别配合redis-server和redis-sentinel使用;下面做详细讲解。
主配置文件:
#开启后台运行模式
daemonize yes
#指定进程id存放位置,也可以用默认的
pidfile /usr/local/webserver/redis/run/redis.pid
#指定端口号
port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "" 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 /usr/local/webserver/redis/db slave-serve-stale-data yes
#从redis只能读
slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100
#本redis密码
requirepass 123456
#主redis密码
masterauth "123456" appendonly yes appendfilename "appendonly.aof" appendfsync everysec 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-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
sentinel.conf(哨兵机制):
port 26379
dir "/usr/local/webserver/redis/db"
# 守护进程模式
daemonize yes
# 指明日志文件名
logfile "./sentinel.log"
#设置监控的主redis的ip以及端口号,以及投票最低数
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 18000
sentinel auth-pass mymaster 123456
从配置文件:
redis.conf:
daemonize yes pidfile /usr/local/webserver/redis-slave1/run/redis.pid port 63791 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "" 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 /usr/local/webserver/redis-slave1/db 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 requirepass 123456
masterauth "123456" appendonly yes appendfilename "appendonly.aof" appendfsync everysec 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-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 #指定主redis以及相应的端口号
slaveof 127.0.0.1 6379
sentinel.conf:
#sentinel端口
port 263791
#工作路径,注意路径不要和主重复
dir "/usr/local/webserver/redis-slave1/db"
# 守护进程模式
daemonize yes
# 指明日志文件名
logfile "./sentinel.log"
#哨兵监控的master,主从配置一样,
sentinel monitor mymaster 127.0.0.1 6379 1
# master或slave多长时间(默认30秒)不能使用后标记为s_down状态。
sentinel down-after-milliseconds mymaster 5000
#若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。
sentinel failover-timeout mymaster 18000
#设置master和slaves验证密码
sentinel auth-pass mymaster 123456
同理,如果要实现多个从配置,可以按照以上从配置方式多定义几个端口号就可以了。
在进行自动故障转移的时候,选中为主redis的redis.conf文件会将原来slaveof那一行去掉,并重新在从redis配置文件中指定主redis的信息
三、java中调用
import java.util.HashSet;
import java.util.Set; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool; public class RedisSentinelTest { @SuppressWarnings("deprecation")
public static void main(String[] args) { Set<String> sentinels = new HashSet<String>();
String hostAndPort1 = "127.0.0.1:26379";
String hostAndPort2 = "127.0.0.1:26380";
sentinels.add(hostAndPort1);
sentinels.add(hostAndPort2); String clusterName = "mymaster";
String password = "123456"; JedisSentinelPool redisSentinelJedisPool = new JedisSentinelPool(clusterName,sentinels,password); Jedis jedis = null;
try {
jedis = redisSentinelJedisPool.getResource();
// jedis.set("key", "value");
System.out.println(jedis.get("key"));
} catch (Exception e) {
e.printStackTrace();
} finally {
redisSentinelJedisPool.returnBrokenResource(jedis);
} redisSentinelJedisPool.close();
} }