---
title: "Redis主从监控，故障自动切换"
author: "Java4ever"
date: "2015-03-04T10:32:12Z"
tags: []
categories: ["数据库"]
slug: "redis"
draft: false
lastmod: "2015-03-04T10:31:28Z"
id: 2188616
url: "https://java4ever.iteye.com/blog/2188616"
---

系统环境 ：Centos 6.6

Redis版本：2.8.19   
官方地址：[http://redis.io/](http://redis.io/)

1、首先下载编译安装redis   
$ wget [http://download.redis.io/releases/redis-2.8.19.tar.gz](http://download.redis.io/releases/redis-2.8.19.tar.gz)   
$ tar xzf redis-2.8.19.tar.gz   
$ cd redis-2.8.19   
$ make 

一主（Master）两从（Slave1,Slave2）   
将编译好的redis-2.8.19里文件分别拷贝三份到目录Master、Slave1、Slave2、Sentinel1

   
![](http://dl2.iteye.com/upload/attachment/0106/3421/19a38d6f-0839-3daa-99d9-0d953c6b8972.png)  
 

配置redis端口

Master实例：默认配置 端口6379

Slave1和Slave2实例：修改redis.config 文件，配置主redis地址和端口：slaveof 192.168.0.138 6379 （记得#去掉），

同时记得修改端口，配置Slave1 端口6380，Slave2端口6381

   
![](http://dl2.iteye.com/upload/attachment/0106/3450/dd62890a-f60a-303d-a037-f1dabf508f0e.png)  
 

分别进入Master、Slave1、Slave2目录，启动redis

![](http://dl2.iteye.com/upload/attachment/0106/3426/ef7fe2fe-c205-39b5-ba70-e7ad44aacddf.png)  
 

$src/redis-server redis.conf

启动结果

![](http://dl2.iteye.com/upload/attachment/0106/3428/19eaa3d7-3654-3436-9e60-9bc9e4d519d4.png)  
 

此时你可以启动主的redis-cli做操作

![](http://dl2.iteye.com/upload/attachment/0106/3430/fa7788bd-08aa-3de5-b266-52f37af8fa12.png)  
 

如果redis客户端查看工具RedisDesktopManager可以查看

   
![](http://dl2.iteye.com/upload/attachment/0106/3432/b276e75d-79cf-38f8-a994-73b4d145ecc5.png)  
 

到目前为止，redis主从同步完成，下面是redis集群：

采用Sentinel方案，利用以上的Redis的Master/Slave的复制，可以打造一个高可用的Redis集群;

![](http://dl2.iteye.com/upload/attachment/0106/3434/730ecbb8-4c81-32ee-bebd-973b1ae11d3a.png)  
 

其中三个Redis Sentinel其实就是监控所有redis实例，官方翻译俗称“哨兵”，其实就是它负责监控redis实例；

更多了解请阅读[http://redis.readthedocs.org/en/latest/topic/sentinel.html](http://redis.readthedocs.org/en/latest/topic/sentinel.html)

 

新增配置mysentinel.conf放入Sentinel1目录

#redismaster是Master的名称，192.168.0.138是Master的Ip，1表示确认一个Master为O\_DOWN最少需要多少个哨兵认可

sentinel monitor redismaster 192.168.0.138 6379 1

#多少秒（默认30秒）后Maste不可用被确认为S\_DOWN状态

sentinel down-after-milliseconds redismaster 10000

sentinel config-epoch redismaster 3

sentinel leader-epoch redismaster 3

 

这里为了方便，只是配置一个哨兵，启动

\[[root@localhost](mailto:root@localhost) ~]# cd ../home/max/Redis/Sentinel1/   
\[[root@localhost](mailto:root@localhost) Sentinel1]# src/redis-sentinel mysentinel.conf  

![](http://dl2.iteye.com/upload/attachment/0106/3436/e782a26a-56dc-3142-a1a7-5aa96b1170e2.png)  
 

完成！

你可以测试，当主实例停用后，从自动切换成主，如图

![](http://dl2.iteye.com/upload/attachment/0106/3441/24780670-975f-3ee1-ab2b-5d8522b6bbb5.png)  
 

同时，mysentinel.conf 自动修改成如下：

#redismaster是Master的名称，192.168.0.138是Master的Ip，1表示确认一个Master为O\_DOWN最少需要多少个哨兵认可

sentinel monitor redismaster 192.168.0.138 6381 1

#多少秒（默认30秒）后Maste不可用被确认为S\_DOWN状态

sentinel down-after-milliseconds redismaster 10000

sentinel config-epoch redismaster 3

sentinel leader-epoch redismaster 3

\# Generated by CONFIG REWRITE

port 26379

dir "/home/max/Redis/Sentinel1"

sentinel known-slave redismaster 192.168.0.138 6380

sentinel known-slave redismaster 192.168.0.138 6379

 

sentinel current-epoch 3

 

 

重新启动主

![](http://dl2.iteye.com/upload/attachment/0106/3444/3a019579-f39a-3cad-8ce9-e63631073ba0.png)  
 

发现这个主自动被当作从处理；

 

下面是简单介绍客户端代码使用，利用jedis-2.5.2.jar；

结合spring配置如下

#链接数据库

redis.default.db=5

#客户端超时时间单位是毫秒

redis.timeout=100000

#最大连接数

redis.maxActive=10000

#最大空闲数

redis.maxIdle=100

#最大建立连接等待时间

redis.maxWait=1000

#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个

redis.testOnBorrow=true

 

 

 &lt;!-- jedis pool配置 --&gt;

 &lt;bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"&gt;

  &lt;property name="maxTotal" value="${redis.maxActive}" /&gt;

  &lt;property name="maxIdle" value="${redis.maxIdle}" /&gt;

  &lt;property name="maxWaitMillis" value="${redis.maxWait}" /&gt;

  &lt;!-- 向调用者输出“链接”资源时，是否检测是有有效，如果无效则从连接池中移除，并尝试获取继续获取。设为true，一个挂都不能用 --&gt;

  &lt;property name="testOnBorrow" value="${redis.testOnBorrow}" /&gt;

  &lt;!-- 向连接池“归还”链接时，是否检测“链接”对象的有效性。 --&gt;

  &lt;property name="testOnReturn" value="true" /&gt;

 &lt;/bean&gt;

 

 &lt;bean id="jedisPool" class="redis.clients.jedis.JedisSentinelPool"&gt;

  &lt;constructor-arg index="0" value="redismaster" /&gt;

  &lt;constructor-arg index="1"&gt;

   &lt;set&gt;

    &lt;value&gt;192.168.0.138:26379&lt;/value&gt;&lt;!--配置了一个哨兵 --&gt;

   &lt;/set&gt;

  &lt;/constructor-arg&gt;

  &lt;constructor-arg index="2" ref="jedisPoolConfig" /&gt;

  &lt;constructor-arg index="3" value="${redis.timeout}" /&gt;

  &lt;constructor-arg index="4"&gt;&lt;null/&gt;&lt;/constructor-arg&gt;

  &lt;constructor-arg index="5" value="${redis.default.db}" /&gt;

 &lt;/bean&gt;

 

测试类代码

  ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-redis.xml");

  JedisSentinelPool pool = (JedisSentinelPool) ac.getBean("jedisPool");

  Jedis jedis = null;

  try

  {

   jedis = pool.getResource();

      jedis.hset("max", "zheng", "hello");

  }

  catch (JedisException je)

  {

   throw je;

  }

  finally

  {

   if (jedis != null)

    pool.returnResource(jedis);

  }