双机热备特指基于高可用系统中的两台服务器的热备(或高可用),因两机高可用在国内使用较多,故得名双机热备,双机高可用按工作中的切换方式分为:主-备方式(Active-Standby方式)和双主机方式(Active-Active方式),主-备方式即指的是一台服务器处于某种业务的激活状态(即Active状态),另一台服务器处于该业务的备用状态(即Standby状态)。而双主机方式即指两种不同业务分别在两台服务器上互为主备状态(即Active-Standby和Standby-Active状态)。
大白话就是,当主服务器挂了之后,从服务器立马切换为主服务器继续工作,当原先主服务器修复完善启动后,会自动充当从服务器的角色继续工作。这样就很好的避免了,由于一台主机出现故障,系统挂点的现象出现。
Sentinel(哨兵)是用于监控redis集群中Master状态的工具,已经集成在redis官方版本中,可以直接配置使用。
1. 两个linux系统,ip分别为192.11.3.1 和 192.11.3.2;
2. 两个linux分别安装redis
#网络下载redis压缩包
wget http://download.redis.io/releases/redis-4.0.6.tar.gz
#解压下载完成的redis包
tar -xzvf redis-4.0.6.tar.gz
#进入到redis目录内
cd redis-4.0.6
#编译
make
3.修改redis的redis.conf
1.在 redis.conf 中,
2.找到 bind 127.0.0.1 注释掉(不注释只能本机访问)
3.找到 protected-mode yes ,修改yes为no (关闭保护模式)
4.主从复制
- 192..11.3.1(master) 192.11.3.2(slave)
- 是什么:redis中的复制(master/slave)主从复制,主机数据更新后根据配置和策略自动同步到备机的master/slaver机制,master以写为主,slaver以读为主
- 作用:读写分离、容灾恢复
在 192.11.3.2 的 redis.conf 文件底部添加 slaveof 192.11.3.1 6379,使其成为192.11.3.1的slave,会自动保持和master的数据保持一致
5.双机热备
找到两个redis的 sentinel.conf (和redis.conf同目录),修改如下:
1. 找到 sentinel monitor mymaster 127.0.0.1 6379 2 ,替换为 sentinel monitor mymaster 192.11.3.1 6379 1 (两个conf都需要替换)(配置内容说明 参考 http://blog.csdn.net/a1282379904/article/details/52335051 )
2. 找到 # protected-mode no 去掉#
6.启动
依次启动两机的redis 服务和sentinel服务
7. java 连接 redis
Set<String> sentinels = new HashSet<String>();
String hostAndPort1 = "192.11.3.1:26379";
String hostAndPort2 = "192.11.3.2:26379";
sentinels.add(hostAndPort1);
sentinels.add(hostAndPort2);
String clusterName = "mymaster";
JedisSentinelPool redisSentinelJedisPool = new JedisSentinelPool(clusterName, sentinels);
Jedis jedis = null;
try {
jedis = redisSentinelJedisPool.getResource();
jedis.set("RNG", "niubi");
System.out.println(jedis.get("RNG"));
} catch (Exception e) {
e.printStackTrace();
} finally {
redisSentinelJedisPool.returnBrokenResource(jedis);
}
当kill掉192.11.3.1的redis服务后,192.11.3.2的redis会自动升为成为master,redis服务依然可用。