redis的哨兵集群链接池

来源:互联网 发布:安徽快三遗漏数据查询 编辑:程序博客网 时间:2024/05/21 17:19

redis的sentinel哨兵机制早已出世许久,之前也一直没 用过,怎奈项目需要融入redis,但是项目是2,0的spring公司领导还不让升spring,所以无法用配置文件融入,孤儿手写的链接池,当时配的是一主两从,于是打开jedis源码进行分析,终于找到一个既JedisSentinelPool这个类。哈哈,打开它发现n多重载,



  public JedisSentinelPool(String masterName, Set<String> sentinels, GenericObjectPoolConfig poolConfig, int timeout, String password)
  {
    this(masterName, sentinels, poolConfig, timeout, password, 0);
  }

masterName:乃是主节点的名称,sentinels:表示哨兵链接地址的集合,poolConfig:链接池,timeout:是客户端超时时间,默认2秒,password:是redis链接密码

于是我初始化了链接池,static{
try {
GenericObjectPoolConfig poolConfig=new GenericObjectPoolConfig();
//设置最大空闲实例
poolConfig.setMaxIdle(100);
//设置最大实例
poolConfig.setMaxTotal(1000);
//设置最小实例
poolConfig.setMinIdle(20);
//设置最大等待时间
poolConfig.setMaxWaitMillis(30000);
//开启jmx
poolConfig.setJmxEnabled(true); 
//关闭validate验证
poolConfig.setTestOnBorrow(true);
//加载配置文件
Properties props = new Properties();  
InputStream is = RedisUtil.class.getClassLoader().getResourceAsStream("redis.properties");
props.load(is);
//将哨兵添加到集合中
Set<String> sentinel=new HashSet<String>();
sentinel.add(props.getProperty("redis.server.ip1"));

//节点名称,哨兵集合,链接池
jedisSentinel=new JedisSentinelPool("mymaster0",sentinel,poolConfig,Integer.valueOf(props.getProperty("redis.timeout")));
} catch (IOException e) {
e.printStackTrace();
System.out.println("redis链接池初始化错误");
}
}

其实他是链接的哨兵,进而获取的redis的链接地址,直接通过jedisSentinel。getResource()就可以从链接池中获取jedis实例,用完也不不用销毁,直接调用returnResource()方法。

建议将哨兵也搭建成集群,这样可以达到高可用性。

这是下载工具类地址:

http://download.csdn.net/download/qq_38665235/9945678

http://download.csdn.net/download/qq_38665235/9945685





原创粉丝点击