主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主
存在复制延迟的缺点,主机的数据不一定能在挂掉前,完全复制到从机,从机过多,会加剧这一缺点
root@LK:~# mkdir /myredis root@LK:~# cd /myredis
redis.conf
配置文件复制到新的文件夹下root@LK:/myredis# cp /etc/redis.conf /myredis/redis.conf root@LK:/myredis# ls redis.conf
配置一主两从,需要三个配置文件
vi redis6379.conf
include /myredis/redis.conf pidfile /var/run/redis_6379.pid port 6379 dbfilename dump6379.rdb
root@LK:/myredis# redis-server redis6379.conf root@LK:/myredis# redis-server redis6380.conf root@LK:/myredis# redis-server redis6381.conf root@LK:/myredis# ps -ef|grep redis lk 57615 2002 0 21:15 ? 00:00:00 redis-server 127.0.0.1:6379 lk 57623 2002 0 21:15 ? 00:00:00 redis-server 127.0.0.1:6380 lk 57631 2002 0 21:15 ? 00:00:00 redis-server 127.0.0.1:6381 lk 57668 57291 0 21:16 pts/0 00:00:00 grep --color=auto redis
root@LK:/myredis# redis-cli -p 6379 127.0.0.1:6379> info replication
#结果 # Replication #当前角色是主机 role:master #没有从服务器 connected_slaves:0 master_failover_state:no-failover master_replid:98ff0d93dbe4c1492ff7f7f1ff6284dac86414dc master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
slaveof 主机ip 端口号
slaveof 127.0.0.1 6379
127.0.0.1:6379> info replication
# Replication role:master #从服务器2个 connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=0,lag=5 slave1:ip=127.0.0.1,port=6381,state=online,offset=0,lag=5 master_failover_state:no-failover master_replid:e003ed0236c8d42eaebef789c116a3f4091e2961 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:112 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:112
测试
127.0.0.1:6379> set k1 v1 OK 127.0.0.1:6379> keys * 1) "k1"
127.0.0.1:6380> keys * 1) "k1"
slaveof no one
,变成主机能够后台监视主机是否故障,如果故障根据投票数自动将从机转换为主机
sentinel.conf
文件,名字不能错root@LK:/myredis# vi sentinel.conf
sentinel monitor mymaster 127.0.0.1 6379 1
mymaster为监控对象起的服务器别名,1表示为至少有1个哨兵同意迁移服务器
4. 启动哨兵/usr/local/bin/redis-sentinel
root@LK:/myredis# redis-sentinel sentinel.conf
66666:X 25 Jun 2022 00:39:56.063 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 7.0.2 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 66666 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 66666:X 25 Jun 2022 00:39:56.065 * Sentinel new configuration saved on disk 66666:X 25 Jun 2022 00:39:56.065 # Sentinel ID is d788eafe8daf0f8c748239fe6cb52e7e55b64d8a 66666:X 25 Jun 2022 00:39:56.065 # +monitor master mymaster 127.0.0.1 6379 quorum 1 66666:X 25 Jun 2022 00:39:56.066 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379 66666:X 25 Jun 2022 00:39:56.068 * Sentinel new configuration saved on disk 66666:X 25 Jun 2022 00:39:56.068 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379 66666:X 25 Jun 2022 00:39:56.069 * Sentinel new configuration saved on disk
slave-priority 100
,值越小,优先级越高,新版本replica-priority 100
)private static JedisSentinelPool jedisSentinelPool=null; public static Jedis getJedisFromSentinel(){ if(jedisSentinelPool==null){ Set<String> sentinelSet=new HashSet<>(); sentinelSet.add("127.0.0.1:26379"); JedisPoolConfig jedisPoolConfig =new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(10); //最大可用连接数 jedisPoolConfig.setMaxIdle(5); //最大闲置连接数 jedisPoolConfig.setMinIdle(5); //最小闲置连接数 jedisPoolConfig.setBlockWhenExhausted(true); //连接耗尽是否等待 jedisPoolConfig.setMaxWaitMillis(2000); //等待时间 jedisPoolConfig.setTestOnBorrow(true); //取连接的时候进行一下测试 ping pong jedisSentinelPool=new JedisSentinelPool("mymaster",sentinelSet,jedisPoolConfig); return jedisSentinelPool.getResource(); }else{ return jedisSentinelPool.getResource(); } }