——简介
- 为了保证高可用,通常采用一主二从三哨兵,共6个节点;此次为了方便简洁,只实现的一主一从二哨兵进行搭建
- 哨兵模式(主备模式)是在主从复制基础上实现自动切换,有点类似于zookeeper中的选举
- 主从模式需要手动实现切换
SLAVEOF no one
,且master重新加入时,与当前master无关系 - 哨兵模式实现自动切换,当上一个master重新加入时,成为现master的slave
- 复制原理
- slave启动成功连接到master后会发送一个sync命令
- Master接到命令启动后台的存盘进程,同时收集所有接收到的用于修改数据集命令,在后台进程执行完毕之后,master将传送整个数据文件到slave,以完成一次完全同步
- 全量复制:而slave服务在接收到数据库文件数据后,将其存盘并加载到内存中。
- 增量复制:Master继续将新的所有收集到的修改命令依次传给slave,完成同步
- 但是只要是重新连接master,一次完全同步(全量复制)将被自动执行
——搭建
目录
- Redis-master
- Redis-slave
- Redis-sentinel1
- Redis-sentinel2
配置文件
- master:redis.windows.conf
port 6380
bind 127.0.0.1
requirepass "123456"
- slave:redis.windows.conf
port 6381
bind 127.0.0.1
masterauth "123456"
- sentinel1:添加 sentinel.conf 文件
# 当前Sentinel服务运行的端口
port 6390
# 哨兵监听的主服务器 后面的1表示主机挂掉以后进行投票,只需要1票就可以从机变主机
sentinel monitor mymaster 127.0.0.1 6380 1
# 3s内mymaster无响应,则认为mymaster宕机了
sentinel down-after-milliseconds mymaster 3000
#如果10秒后,mysater仍没启动过来,则启动failover
sentinel failover-timeout mymaster 10000
# 设置哨兵sentinel 连接主从的密码 注意必须为主从设置一样的验证密码,没有的话不用设置
sentinel auth-pass mymaster 123456
# 执行故障转移时, 最多有1个从服务器同时对新的主服务器进行同步
sentinel config-epoch mymaster 21
- sentinel2:添加 sentinel.conf 文件
# 当前Sentinel服务运行的端口
port 6391
# 哨兵监听的主服务器 后面的1表示主机挂掉以后进行投票,只需要1票就可以从机变主机
sentinel monitor mymaster 127.0.0.1 6380 1
# 3s内mymaster无响应,则认为mymaster宕机了
sentinel down-after-milliseconds mymaster 3000
#如果10秒后,mysater仍没启动过来,则启动failover
sentinel failover-timeout mymaster 10000
# 设置哨兵sentinel 连接主从的密码 注意必须为主从设置一样的验证密码,没有的话不用设置
sentinel auth-pass mymaster 123456
# 执行故障转移时, 最多有1个从服务器同时对新的主服务器进行同步
sentinel config-epoch mymaster 21
——启动
- master、slave 的 redis 根目录下执行:
redis-server.exe redis.windows.conf
- sentinel1、sentinel2 的 redis 根目录下执行:
redis-server.exe sentinel.conf --sentinel
- 进入根目录连接客户端:
redis-cli.exe -p 6390
- 查看状态:
info sentinel
——Jedis集成
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import java.util.HashSet;
import java.util.Set;
/**
* jedis连接Redis哨兵模式
*
* @author xulejun
* @date 2021/9/23
*/
public class JedisTest {
@Test
public void testSentinel() {
String masterName = "mymaster";
String password = "123456";
// 设置参数
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 最大连接数, 默认8个
jedisPoolConfig.setMaxTotal(10);
// 最大空闲连接数, 默认8个
jedisPoolConfig.setMaxIdle(5);
// 最小空闲连接数, 默认0
jedisPoolConfig.setMinIdle(5);
// 哨兵信息,注意填写哨兵的地址
Set<String> sentinels = new HashSet<>();
sentinels.add("127.0.0.1:6390");
sentinels.add("127.0.0.1:6391");
// 创建连接池,当master宕机后,哨兵模式自动切换到slave上
JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels, jedisPoolConfig, password);
if ("6381".equals(String.valueOf(pool.getCurrentHostMaster().getPort()))) {
pool = new JedisSentinelPool(masterName, sentinels, jedisPoolConfig);
}
// 获取客户端
Jedis jedis = null;
try {
jedis = pool.getResource();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
pool.close();
}
}
// 执行命令,增、查
jedis.set("id", "1004");
String value = jedis.get("id");
System.out.println(value);
}
}