Redis Sentinel主从高可用方案
Redis Sentinel主从高可用方案
本文介绍一种通过Jed和Sentinel实现Redis集群(主从)的高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在Redis2.4中,Redis2.8中Sentinel更加稳定),
附:
Redis Cluster集群主从方案:http://www.cnblogs.com/soul-wonder/p/8891256.html
Redis Sentinel主从高可用方案:http://www.cnblogs.com/soul-wonder/p/8891217.html
一、Sentinel介绍
Sentinel是Redis的高可用性(HA)解决方案,由一个或多个Sentinel实例组成的Sentinel系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进行下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器,然后由新的主服务器代替已下线的主服务器继续处理命令请求。Redis提供的sentinel(哨兵)机制,通过sentinel模式启动redis后,自动监控master/slave的运行状态,基本原理是:心跳机制+投票裁决
- 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
 - 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
 - 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
 
二、Sentinel的主从原理
 
 
 
之前介绍过为什么Jedis要用2.2.2及以上版本,因为主从实例地址(IP PORT)是不同的,当故障发生进行主从切换后,应用程序无法知道新地址,故在Jedis2.2.2中新增了对Sentinel的支持,应用通过redis.clients.jedis.JedisSentinelPool.getResource()取得的Jedis实例会及时更新到新的主实例地址。
三、Redis Sentinel配置
这里我采用2个哨兵,1个主redis,2个从redis的方式,配置文件如下:

sentinel_63791.conf 配置:
port 63791
daemonize yes
logfile "/var/log/sentinel_63791.log"
#master-1
sentinel monitor master-1 192.168.78.99 6379 2
sentinel down-after-milliseconds master-1 5000
sentinel failover-timeout master-1 18000
sentinel auth-pass master-1 yingjun
sentinel parallel-syncs master-1 1
sentinel_63792.conf 配置:
port 63792
daemonize yes
logfile "/var/log/sentinel_63792.log"
#master-1
sentinel monitor master-1 192.168.78.99 6379 2
sentinel down-after-milliseconds master-1 5000
sentinel failover-timeout master-1 18000
sentinel auth-pass master-1 yingjun
sentinel parallel-syncs master-1 1
redis_master_6379.conf 配置:
在原配置文件中作如下修改:
port 6379
daemonize yes
requirepass yingjun
masterauth yingjun
redis_slave_6380.conf 配置:
在原配置文件中作如下修改:
port 6380
daemonize yes
requirepass yingjun
slaveof 192.168.78.99 6379
masterauth yingjun
redis_slave_6381.conf 配置:
在原配置文件中作如下修改:
port 6381
daemonize yes
requirepass yingjun
slaveof 192.168.78.99 6379
masterauth yingjun
按如下顺序依次启动服务:
./redis-server ../conf/redis_master_6379.conf
./redis-server ../conf/redis_slave_6381.conf
./redis-server ../conf/redis_slave_6382.conf
./redis-sentinel ../conf/sentinel_63791.conf
./redis-sentinel ../conf/sentinel_63792.conf
查看进程是否都已经启动:

查看master的状态:

查看slave的状态:

查看sentinel的状态:

接下来验证redis sentinel的主从切换:
- 首先关闭主redis(6379)服务(shutdown)。
 查看哨兵,发现端口号为6380的从服务变成了主服务,sentinel自动完成了故障切换。

- 启动刚才被shutdown的6379服务并查看,发现它变成了从服务。
 
三、Jedis Sentinel教程
Maven依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
<!-- spring-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.4.RELEASE</version>
</dependency>
redis的配置文件:
#redis config
redis.pass=yingjun
redis.pool.maxTotal=105
redis.pool.maxIdle=10
redis.pool.maxWaitMillis=60000
redis.pool.testOnBorrow=true sentinel1.ip=192.168.78.99
sentinel1.port=63791 sentinel2.ip=192.168.78.99
sentinel2.port=63792
Spring的配置文件:
<!-- Redis 配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean> <bean id="sentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="master-1"></property>
</bean>
</property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${sentinel1.ip}"></constructor-arg>
<constructor-arg name="port" value="${sentinel1.port}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${sentinel2.ip}"></constructor-arg>
<constructor-arg name="port" value="${sentinel2.port}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<!-- Jedis ConnectionFactory连接配置 -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="password" value="${redis.pass}"></property>
<property name="poolConfig" >
<ref bean="jedisPoolConfig"/>
</property>
<constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
</bean> <!-- redisTemplate配置,redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
代码中直接用redisTemplate调用:
@Override
public boolean add(final KeyToken tkey) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(tkey.getIndex());
byte[] name = serializer.serialize(tkey.getExpire_time());
return connection.setNX(key, name);
} });
return result;
}
JedisPool:
private static JedisSentinelPool pool = null;
private static Set<String> sentinels;
private static String redisPassword = "123456"; /**
* 创建连接池
*
*/
private static void createJedisPool() {
String redisSentinels = EposUtil.getRedishosts();
sentinels = new HashSet<String>();
sentinels.add(redisSentinels.split(",")[0]);
sentinels.add(redisSentinels.split(",")[1]);
sentinels.add(redisSentinels.split(",")[2]);
pool = new JedisSentinelPool("mymaster", sentinels);
} /**
* 在多线程环境同步初始化
*/
private static synchronized void poolInit() {
if (pool == null) {
createJedisPool();
}
} /**
* 获取一个jedis 对象
*
* @return
*/
public static Jedis getJedis() {
if (pool == null)
poolInit();
return pool.getResource();
} /**
* 向redis中存储值,不带失效时间
*
* @param key
* @param value
*/
public static void setValue(String key, String value) {
Jedis jedis = getJedis(); try {
jedis.auth(redisPassword);
jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
} /**
* 向redis中存储值,带失效时间
*
* @param key
* @param value
* @param seconds
* 失效时间
*/
public static void setValueAndExpires(String key, String value, int seconds) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.auth(redisPassword);
jedis.set(key, value);
jedis.expire(key, seconds);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
} /**
* 获取值
*
* @param key
* @return
*/
public static String getValue(String key) {
Jedis jedis = null;
String result = null; try {
jedis = getJedis();
jedis.auth(redisPassword);
result = jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
} return result;
} /**
* 加锁
*
* @param key
*/
public static boolean lock(String key, int seconds) {
Jedis jedis = null;
boolean result =false;
try {
jedis =getJedis();
jedis.auth(redisPassword);
long res = jedis.setnx(key, "LOCKED");
if(res==1L){
jedis.expire(key, seconds);
result= true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
return result;
} /**
* 释放锁
*/
public static void release(String key) {
Jedis jedis = null; try {
jedis = getJedis();
jedis.auth(redisPassword);
jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
}
Redis Sentinel主从高可用方案的更多相关文章
- Redis Sentinel(哨兵)主从高可用方案
		
环境搭建 三台服务器: 192.168.126.100(master) 192.168.126.110(slaver) 192.168.126.120(slaver) 拷贝192.168.126.10 ...
 - redis-3.0.1 sentinel 主从高可用 详细配置
		
最近项目上线部署,要求redis作高可用,由于redis cluster还不是特别成熟,就选择了redis sentinel做高可用.redis本身有replication,实现主从备份.结合sent ...
 - Redis主从复制与高可用方案
		
redis简单介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库.Redis与其他key – value缓存产品有以下三个特点: 支持数据的持久化,可以将内存中 ...
 - 转载:Redis主从复制与高可用方案
		
转载自: https://www.cnblogs.com/lizhaojun-ops/p/9447016.html 原文链接:http://gudaoyufu.com/?p=1230 redis主从复 ...
 - Redis Sentinel实现高可用配置
		
一般情况下yum安装redis的启动目录在:”/usr/sbin” :配置目录在”/etc/redis/”在其目录下会有默认的redis.conf和redis-sentinel.conf redis高 ...
 - Redis笔记5-redis高可用方案
		
一.哨兵机制 有了主从复制的实现以后,如果想对主服务器进行监控,那么在redis2.6以后提供了一个"哨兵"的机制.顾名思义,哨兵的含义就是监控redis系统的运行状态.可以启动多 ...
 - (转)基于Redis Sentinel的Redis集群(主从&Sharding)高可用方案
		
转载自:http://warm-breeze.iteye.com/blog/2020413 本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedi ...
 - 基于Redis Sentinel的Redis集群(主从Sharding)高可用方案(转)
		
本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在 ...
 - 基于Redis Sentinel的Redis集群(主从&Sharding)高可用方案
		
本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在 ...
 
随机推荐
- 点击一次按钮,发生多次ajax请求
			
项目中遇到了两种情况: 1.点击一次发生两次请求. 原因:submit类型的按钮,默认有提交行为,发生两次提交的原因是在执行完ajax请求后,并没有阻止submit的行为,所以解决方法有两种: a.不 ...
 - 09_Python定义方法_Python编程之路
			
有关Python判断与循环的内容我们上几节已经跟大家一起学习了,这一节我们主要针对def 做一个讲解 def 定义一个方法 在项目编程中,我们往往要做很多重复的事,比如一个排序的功能(当然Python ...
 - zipline-benchmarks.py文件改写
			
改写原因:在这个模块中的 get_benchmark_returns() 方法回去谷歌财经下载对应SPY(类似于上证指数)的数据,但是Google上下载的数据在最后写入Io操作的时候会报一个恶心的编码 ...
 - [Linux]使用awk批量杀进程的命令
			
碰到需要杀掉某一类进程的时候,如何批量杀掉这些进程,使用awk命令是很好的选择. ps -ef|grep aaa|grep -v grep|awk '{print "kill -9 &quo ...
 - ng-model,ng-value,ng-bind,{{}}----angularJS数据绑定
			
最典型用法 双向绑定 <input type="text" value="{{apple}}" ng-model="apple" &g ...
 - 对Spring IOC和AOP的理解
			
控制反转(IOC)是什么?(理解好Ioc的关键是要明确"谁控制谁,控制什么,为何是反转(有反转就应该有正转了),哪些方面反转了") 1.Ioc-Inversion of Contr ...
 - 【推荐】CentOS安装gcc-4.9.4+更新环境+更新动态库
			
注:以下所有操作均在CentOS 6.8 x86_64位系统下完成. CentOS上yum安装的gcc版本过低(4.4.7),在安装某些软件的时候不支持,所以这里需要对其进行升级. #gcc的安装# ...
 - Dev GridControl GridView常用属性
			
1.隐藏最上面的GroupPanel: gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值: sValue=Table.Rows[g ...
 - Python系列之 - 面向对象(2)
			
类的三大特性 类的三大特性包括: 封装.继承.多态 一 封装 封装就是将类所用到的所有字段.属性.方法都包含在类代码段里面,当实例调用直接调用类中的方法即可. class People(object) ...
 - Spring--bean的作用范围
			
在Spring中,bean的作用范围分以下几种: singleton:spring ioc容器中仅有一个bean实例,bean以单例的方式存在 prototype:每次从容器中调用bean时,都返回一 ...