由于云服务器存在闪断现象,项目线上会存在基于redis的功能在闪断时段内出现异常,所以redis需要做master-slave模式。直接上代码:

原单机redis,RedisConnectionFactory设置代码

```
@Bean
public RedisConnectionFactory jedisConnectionFactory(){
JedisPoolConfig poolConfig=new JedisPoolConfig();
poolConfig.setMaxIdle(5);
poolConfig.setMinIdle(1);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setNumTestsPerEvictionRun(10);
poolConfig.setTimeBetweenEvictionRunsMillis(60000);

JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
//单机redis的host、port设置
jedisConnectionFactory.setHostName(redishost);
jedisConnectionFactory.setPort(redisport);
jedisConnectionFactory.setDatabase(0);

return jedisConnectionFactory;
}
```
哨兵节点设置代码

```
@Bean
public RedisConnectionFactory jedisConnectionFactory(){
JedisPoolConfig poolConfig=new JedisPoolConfig();
poolConfig.setMaxIdle(5);
poolConfig.setMinIdle(1);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setNumTestsPerEvictionRun(10);
poolConfig.setTimeBetweenEvictionRunsMillis(60000);
//哨兵节点host、port设置,可设置多个哨兵,只需要链式新增 .sentinel(sentinelhost, sentinelport)
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster")
.sentinel(sentinelhost, sentinelport);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfig,poolConfig);

return jedisConnectionFactory;
}
```

由此,在master节点down了的时候,slave节点会自动升级到master。然后我们的服务会自动新建redis连接池到新的master节点。

```
2016-07-29 10:42:28,525 [MasterListener-mymaster-[10.10.180.37:26379]] INFO redis.clients.jedis.JedisSentinelPool - Created JedisPool to master at 10.10.129.188:6379
```

当哨兵节点down了的时候,服务日志会提示Lost connection to Sentinel,此时redis连接池不会断开,可继续使用,不影响业务,但是这时当master失去响应时就不会自动切换连接池了。为了避免这种情况可以配置多个哨兵(sentinel)节点,也可以通过sentinel配置文件的方式,当哨兵节点down了之后,通知运维同学。

```
2016-07-29 10:50:36,820 [MasterListener-mymaster-[10.10.180.37:26379]] ERROR redis.clients.jedis.JedisSentinelPool - Lost connection to Sentinel at 10.10.180.37:26379. Sleeping 5000ms and retrying.
2016-07-29 10:50:41,821 [MasterListener-mymaster-[10.10.180.37:26379]] ERROR redis.clients.jedis.JedisSentinelPool - Lost connection to Sentinel at 10.10.180.37:26379. Sleeping 5000ms and retrying.
2016-07-29 10:50:46,823 [MasterListener-mymaster-[10.10.180.37:26379]] ERROR redis.clients.jedis.JedisSentinelPool - Lost connection to Sentinel at 10.10.180.37:26379. Sleeping 5000ms and retrying.
2016-07-29 10:50:51,825 [MasterListener-mymaster-[10.10.180.37:26379]] ERROR redis.clients.jedis.JedisSentinelPool - Lost connection to Sentinel at 10.10.180.37:26379. Sleeping 5000ms and retrying.
2016-07-29 10:50:56,826 [MasterListener-mymaster-[10.10.180.37:26379]] ERROR redis.clients.jedis.JedisSentinelPool - Lost connection to Sentinel at 10.10.180.37:26379. Sleeping 5000ms and retrying.

redis配置master-slave模式的更多相关文章

  1. Redis的master/slave复制

    摘自:Redis的master/slave复制 Redis的master/slave数据复制方式可以是一主一从或者是一主多从的方式,Redis在master是非阻塞模式,也就是说在slave执行数据同 ...

  2. Redis主从复制(Master/Slave)

    Redis主从复制(Master/Slave) 修改配置文件 拷贝多个redis.conf文件分别配置如下参数: 开启daemonize yes pidfile port logfile dbfile ...

  3. jenkins的Master/Slave模式

    一. Master/Slave模式 分担jenkins服务器的压力,任务分配到其它执行机来执行 Master:Jenkins服务器 Slave:执行机(奴隶机).执行Master分配的任务,并返回任务 ...

  4. Jenkins—Master/Slave模式

    Jenkins可部署在windows或者linux平台上,项目系统的用户多数为windows系统.如果Jenkins部署在linux上,而自动化任务要在windows平台执行,那么就需要使用Jenki ...

  5. Redis主从复制(Master/Slave) 与哨兵模式

    Redis主从复制是什么? 行话:也就是我们所说的主从复制,主机数据更新后根据配置和策略, 自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主 Redis主从复制 ...

  6. redis之master.slave主从复制

    简介 主机数据更新后根据配置和策略,自动同步到备机的master/slave机制,master以写为主,slave以读为主 从库配置 配置从库,不配主库 配置从库: 格式: slaveof 主库ip ...

  7. MySQL master/slave 模式

    1 .复制 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的 数据复制到其它主机(slaves)上,并重 ...

  8. ActiveMQ集群支持Master/Slave模式

    现在ActiveMQ, 在Failover方面有两种解决方案:Pure Master Slave和Shared File System Master Slave.      先看Pure Master ...

  9. ActiveMQ使用Zookeeper+LevelDb配置Master/Slave集群

    前言: 本文介绍的AMQ集群是Master-Slave模式的,官网介绍三种方案: (1)基于共享文件系统的,(2)基于JDBC,(3)基于可复制的LevelDB. 关于三种方式的对比网上已经有很多,本 ...

随机推荐

  1. tomcat服务器重启后session可以继续使用

    原文:http://blog.csdn.net/e421083458/article/details/8651312 测试没有效果 配置server.xml文件,加入session保存操作 <C ...

  2. 负载均衡---在window与linux下配置nginx

    最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx,sq ...

  3. C#调用API向外部程序发送数据

    C#调用API向外部程序发送数据 最近有可能要做一个项目.在项目中有这么一个功能,在A程序中调用B程序,同时在A程序中进行登陆后,要将A程序的登录名和密码自动填充到B程序的登陆对话框中,这样B程序就不 ...

  4. .a 库文件信息查看

    在Linux 下经常需要链接一些 *.a的库文件,那怎么查看这些*.a 中包 含哪些文件.函数.变量: 1. 查看文件:ar -t *.a 2. 查看函数.变量:nm *.a

  5. MySQL中SYSDATE()和NOW()函数的区别和联系

    MySQL中有5个函数需要计算当前时间的值: NOW.返回时间,格式如:2012-09-23 06:48:28 CURDATE,返回时间的日期,格式如:2012-09-23 CURTIME,返回时间, ...

  6. [Android Studio] Android Studio如何删除module(转载)

    转载地址:http://blog.csdn.net/hyr83960944/article/details/37519299 当你想在Android Studio中删除某个module时,大家习惯性的 ...

  7. DICOM中的入门概念

    DICOM标准是医学影像界技术人员逃不掉的标准.本系列专题是JATI对DICOM标准的阐述,力图使PACS管理员和软件工程师都能理解. DICOM标准的提出者DICOM标准委员会是ISO组织的合作者. ...

  8. DWG/DGN格式导入Arcgis;转化为shp格式;更改地理坐标;导入Google Earth【转】

      其实本来,我就是需要把一个autocad的dwg/dgn格式的东西导入到google earth里面:但是首先我对dwg/dgn格式的东西根本就不熟:其次我拿到的dwg/dgn格式文件是用的HK8 ...

  9. Unity3d笔试题大全

    1.       [C#语言基础]请简述拆箱和装箱. 答: 装箱操作: 值类型隐式转换为object类型或由此值类型实现的任何接口类型的过程. 1.在堆中开辟内存空间. 2.将值类型的数据复制到堆中. ...

  10. 关于docker的15个小tip

    1. 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubuntu): $ ID=$(docker run ubuntu echo hello world) he ...