ps:一键安装配置redis-sentinel高可用服务环境是本人将安装配置命令写成
shell 脚本工具,一键执行即可完成redis高可用服务。请大家到我的资源里去下载就能使用,非常方便。

 

接下来我要为大家描述一键安装配置redis-sentinel高可用服务的思路(并附上java连接redis-sentinel的代码):

1:首先下载官网的 redis 解压

1.5:(有部分linux系统还没有编译环境的需要此操作)安装相应编译环境 yum -y install gcc gcc-c++
libstdc++-devel

2:安装redis(make MALLOC=libc ->make install)

3:重新创建3份redis.conf文件,分别以端口号区分开,编辑好相应的配置 后以配置文件的方式分启动3个redis 服务

4:重新创建3份sentinel.conf文件,分别以端口号区分开,编辑好相应的配置 后以配置文件的方式分启动3个sentinel服务

注意坑:

1. master /slave 的 redis.conf 最好配置密码(一定要配置密码:否则 JedisSentinelPool 报错) 

2. master /slave 的 redis.conf 最好配置的密码相同

3. master /slave 的 redis.conf 配置 protected-mode no(sentinel.conf 里面同样需要配置)
 

 

 SentinelPool.java
import org.alves.utils.PropertiesUtil; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig; import
redis.clients.jedis.JedisSentinelPool; import java.util.HashSet; import
java.util.Set; /** * @Create by :Alves(412301546@qq.com) * @Localtion:杭州.China
* @CreateTime :2018/10/5:1:03 * @Version : v1.0.1 * @Describe : */ public class
SentinelPool { private static JedisSentinelPool jedisSentinelPool; static {//
加载 init // 建立连接池配置参数 JedisPoolConfig config = new JedisPoolConfig(); // 设置最大连接数
config.setMaxTotal(10); // 设置最大阻塞时间,记住是毫秒数milliseconds
config.setMaxWaitMillis(30000); // 设置空间连接 config.setMaxIdle(2); boolean borrow
= true; config.setTestOnBorrow(borrow); Set<String> sentinels = new
HashSet<String>(); sentinels.add("您的redis ip:26379"); sentinels.add("您的redis
ip::26380"); sentinels.add("您的redis ip::26381"); String sentinelMasterName =
"mymaster"; String password = "你的redis密码"; jedisSentinelPool = new
JedisSentinelPool(sentinelMasterName,sentinels,password); } /** * 从 sentinel
连接池中获取 Jedis 实例 * @return */ public static Jedis getJedis(){ Jedis jedis =
null; try { jedis = jedisSentinelPool.getResource(); }catch (Exception e){
e.printStackTrace(); }finally { if (jedis != null){ jedis.close(); } } return
jedis; } }
 SentinelPoolUtil.java
import redis.clients.jedis.Jedis; /** * @Author : 412301546@qq.com * @Location
: HangZhou.China * @Date : 2018/10/29 0029 17:09 * @Version : V1.0.0 *
@Description: 工具类 */ public class SentinelPoolUtil { /** * 设置key的有效期,单位是秒 *
@param key * @param exTime * @return */ public static Long expire(String
key,int exTime){ Jedis jedis = null; Long result = null; try{ jedis =
SentinelPool.getJedis(); result = jedis.expire(key,exTime); }catch (Exception
e){ System.out.println("expire key:"+key +" error . "+e);
SentinelPool.close(jedis); return result; } SentinelPool.close(jedis); return
result; } /** * 放入Redis键值 并设置失效时间,exTime的单位是秒 * @param key * @param exTime *
@param value * @return */ public static String setex(String key,int
exTime,String value){ Jedis jedis = null; String result = null; try{ jedis =
SentinelPool.getJedis(); result = jedis.setex(key,exTime,value); }catch
(Exception e){ System.out.println("setex key:"+key + " value:"+value+" error .
"+e); SentinelPool.close(jedis); return result; } SentinelPool.close(jedis);
return result; } /** * 放入Redis * @param key * @param value * @return */ public
static String set(String key,String value){ Jedis jedis = null; String result =
null; try{ jedis = SentinelPool.getJedis(); result = jedis.set(key,value);
}catch (Exception e){ System.out.println("set key:"+key + " value:"+value+"
error . "+e); SentinelPool.close(jedis); return result; }
SentinelPool.close(jedis); return result; } /** * 获取Redis * @param key *
@return */ public static String get(String key){ Jedis jedis = null; String
result = null; try{ jedis = SentinelPool.getJedis(); result = jedis.get(key);
}catch(Exception e){ System.out.println("get key:"+key +" error . "+e);
SentinelPool.close(jedis); return result; } SentinelPool.close(jedis); return
result; } /** * * @param key * @return */ public static Long del(String key){
Jedis jedis = null; Long result = null; try{ jedis = SentinelPool.getJedis();
result = jedis.del(key); }catch(Exception e){ System.out.println("delete
key:"+key +" error . "+e); SentinelPool.close(jedis); return result; }
SentinelPool.close(jedis); return result; } //测试 public static void
main(String[] args) { String a = SentinelPoolUtil.get("a");
SentinelPoolUtil.setex("two",20,"two 的值"); String b =
SentinelPoolUtil.get("two"); System.out.println(a); System.out.println(b); } }