127.0.0.1:6379|127.0.0.1:6380|127.0.0.1:6381|127.0.0.1:6382|127.0.0.1:6383这里,对每一行的配置会以“|”分隔。最前面的redis会作为主节点。后面的redis会作为最前面一台的备份Jedis,存放在bakRedisMap(Map)中。
/** * 从备份Redis群中取出可用的Jedis,如果均失败,则抛出异常。底层采用ping * * @param bakjedisArray * @param index * @return */ private final Jedis getWorkBakJedis(Jedis curJedis, Jedis[] bakjedisArray, int index) { if (index == 0) { System.out.println(getJedisUniqueName(curJedis) + " JedisConnectionException!"); bakjedisArray = redisConfig.getBakRedisMap().get(getJedisUniqueName(curJedis)); if (bakjedisArray == null) { throw new JedisConnectionException(getJedisUniqueName(curJedis) + " No BakRedis!"); } } Jedis bakjedis = bakjedisArray[index]; try { bakjedis.ping(); return bakjedis; } catch (JedisConnectionException e) { System.out.println(getJedisUniqueName(bakjedis) + " JedisConnectionException!"); if (index == bakjedisArray.length - 1) {// 当前节点是最后的节点,直接抛出异常 throw new JedisConnectionException( getJedisUniqueName(curJedis) + " All bak Jedis ConnectionException!"); } else { return getWorkBakJedis(curJedis, bakjedisArray, index + 1); } } }