/*** 初始化切片池*/
private static void initialShardedPool() throwsException {long start =System.nanoTime();
IRedisBaseSV sv= (IRedisBaseSV) ServiceFactory.getService(IRedisBaseSV.class);//初始化JedisPoolConfig
CFG_REDIS_PARAMETERBean[] para = sv.getRedisConfig("DEFAULT");for (int i = 0; i < para.length; i++) {if(para[i].getParameterName().equalsIgnoreCase(RedisConstants.REDIS_SERVER_SHARED_MAXACTIVE))
config.setMaxActive(Integer.parseInt(para[i].getParameterValue()));if(para[i].getParameterName().equalsIgnoreCase(RedisConstants.REDIS_SERVER_SHARED_MAXIDLE))
config.setMaxIdle(Integer.parseInt(para[i].getParameterValue()));if(para[i].getParameterName().equalsIgnoreCase(RedisConstants.REDIS_SERVER_SHARED_MAXWAIT))
config.setMaxWait(Long.parseLong(para[i].getParameterValue()));if(para[i].getParameterName().equalsIgnoreCase(
RedisConstants.REDIS_SERVER_SHARED_TESTONBORROW))
config.setTestOnBorrow(Boolean.getBoolean(para[i].getParameterValue()));if(para[i].getParameterName().equalsIgnoreCase(RedisConstants.REDIS_SERVER_SHARED_NEED_SYN))
NEED_WRITE_SYNCHRONIZE= Boolean.parseBoolean(para[i].getParameterValue()); //Boolean.getBoolean不对
}
CFG_REDIS_SERVERBean[] servers=sv.getRedisServer();
HashMap map= newHashMap();for (int i = 0; i < servers.length; i++) {
map.put(servers[i].getBelongGroup().toUpperCase(), servers[i].getBelongGroup().toUpperCase());
}
serverGroupCodeList= newArrayList(map.values());
ShardedJedisPool[] readConnectionPools;
ShardedJedisPool[] writeConnectionPools;
ShardedJedisPool[] persistConnectionPools;
readConnectionPools= newShardedJedisPool[serverGroupCodeList.size()];
writeConnectionPools= newShardedJedisPool[serverGroupCodeList.size()];
persistConnectionPools= newShardedJedisPool[serverGroupCodeList.size()];for (int i = 0; i < serverGroupCodeList.size(); i++) {
List readShards = new ArrayList();
List writeShards = new ArrayList();
List persistShards = new ArrayList();//遍历所有的redis server实例
for (int j = 0; j < servers.length; j++) {if(servers[j].getBelongGroup()
.equalsIgnoreCase(String.valueOf(serverGroupCodeList.get(i)))&&servers[j].getUseType().equalsIgnoreCase(
String.valueOf(RedisConstants.READ_ONLY_CONNECTION))) {//先测试该连接是否可用
readServersAll.add(servers[j]);
Boolean connectionTest=TestConnection(servers[j].getServerIp(), (int) servers[j].getServerPort(),
decryption(servers[j].getRequirepass()), servers[j].getBelongGroup());if(connectionTest) {
JedisShardInfo jds=
new JedisShardInfo(servers[j].getServerIp(), (int) servers[j].getServerPort());if (null !=servers[j].getRequirepass()&& !"".equals(servers[j].getRequirepass().trim()))
jds.setPassword(decryption(servers[j].getRequirepass()));
readShards.add(jds);
}
}else if(servers[j].getBelongGroup().equalsIgnoreCase(
String.valueOf(serverGroupCodeList.get(i)))&&servers[j].getUseType().equalsIgnoreCase(
String.valueOf(RedisConstants.WRITE_ONLY_CONNECTION))) {
Boolean connectionTest=TestConnection(servers[j].getServerIp(), (int) servers[j].getServerPort(),
decryption(servers[j].getRequirepass()), servers[j].getBelongGroup());if(connectionTest) {
JedisShardInfo jds=
new JedisShardInfo(servers[j].getServerIp(), (int) servers[j].getServerPort());if (null !=servers[j].getRequirepass()&& !"".equals(servers[j].getRequirepass().trim()))
jds.setPassword(decryption(servers[j].getRequirepass()));
writeShards.add(jds);
}
}else if(servers[j].getBelongGroup().equalsIgnoreCase(
String.valueOf(serverGroupCodeList.get(i)))&&servers[j].getUseType().equalsIgnoreCase(
String.valueOf(RedisConstants.PERSIST_ONLYL_CONNECTION))) {
Boolean connectionTest=TestConnection(servers[j].getServerIp(), (int) servers[j].getServerPort(),
decryption(servers[j].getRequirepass()), servers[j].getBelongGroup());if(connectionTest) {
JedisShardInfo jds=
new JedisShardInfo(servers[j].getServerIp(), (int) servers[j].getServerPort());if (null !=servers[j].getRequirepass()&& !"".equals(servers[j].getRequirepass().trim()))
jds.setPassword(decryption(servers[j].getRequirepass()));
persistShards.add(jds);
}
}
}//构造池 每个组分别对应三个分片池,可读分片池,可写分片池,持久化分片池
readConnectionPools[i] =
newShardedJedisPool(config, readShards, Hashing.MURMUR_HASH,
Sharded.DEFAULT_KEY_TAG_PATTERN);
writeConnectionPools[i]=
newShardedJedisPool(config, writeShards, Hashing.MURMUR_HASH,
Sharded.DEFAULT_KEY_TAG_PATTERN);
persistConnectionPools[i]=
newShardedJedisPool(config, persistShards, Hashing.MURMUR_HASH,
Sharded.DEFAULT_KEY_TAG_PATTERN);//按照组名 分别存可读池,可写池,持久化池于对应的map中【此处如果拿到一个可读池,依然可以做写操作】
readPoolMap.put(serverGroupCodeList.get(i), readConnectionPools[i]);
writePoolMap.put(serverGroupCodeList.get(i), writeConnectionPools[i]);
persistPoolMap.put(serverGroupCodeList.get(i), persistConnectionPools[i]);
}long end =System.nanoTime();
log.debug("初始化连接池用时:" + (end -start));
}