use of com.pamirs.attach.plugin.common.datasource.redisserver.RedisServerMatchStrategy in project LinkAgent by shulieTech.
the class Key method create.
LettuceConnectionFactory create() {
RedisServerMatchStrategy matcher = null;
if (biz == null) {
return null;
}
Object standaloneConfiguration = biz.getStandaloneConfiguration();
Object clusterConfiguration = biz.getClusterConfiguration();
Object sentinelConfiguration = biz.getSentinelConfiguration();
if (clusterConfiguration != null) {
matcher = CLUSTER_MODE_MATCHER;
Set<RedisNode> nodes = ((RedisClusterConfiguration) clusterConfiguration).getClusterNodes();
String password = null;
try {
char[] passwdbyte = ((RedisClusterConfiguration) clusterConfiguration).getPassword().get();
password = new String(passwdbyte);
} catch (NoSuchElementException t) {
// ignore
}
List<Key> keys = new ArrayList<Key>();
Iterator<RedisNode> iterator = nodes.iterator();
while (iterator.hasNext()) {
RedisNode node = iterator.next();
keys.add(new Key(node.getHost(), node.getPort(), 0, password));
}
ShadowRedisConfig shadowRedisConfig = matcher.getConfig(keys);
String shadowPassword = shadowRedisConfig.getPassword();
/**
* 创建影子配置
*/
RedisClusterConfiguration shadowRedisClusterConfiguration = new RedisClusterConfiguration();
/**
* 填充密码
*/
if (!StringUtil.isEmpty(shadowPassword)) {
shadowRedisClusterConfiguration.setPassword(shadowPassword);
}
/**
* 填充连接地址
*/
for (String configNode : shadowRedisConfig.getNodes().split(",")) {
shadowRedisClusterConfiguration.addClusterNode(RedisNode.newRedisNode().listeningAt(configNode.split(":")[0], Integer.parseInt(configNode.split(":")[1])).build());
}
/**
* 构建连接
*/
LettuceConnectionFactory shadowConnectionFactory = new LettuceConnectionFactory(shadowRedisClusterConfiguration);
/**
* 初始化连接
*/
shadowConnectionFactory.afterPropertiesSet();
return shadowConnectionFactory;
} else if (sentinelConfiguration != null) {
RedisSentinelConfiguration redisSentinelConfiguration = (RedisSentinelConfiguration) sentinelConfiguration;
String masterName = redisSentinelConfiguration.getMaster().getName();
String password = null;
try {
char[] passwdbyte = redisSentinelConfiguration.getPassword().get();
password = new String(passwdbyte);
} catch (NoSuchElementException e) {
//
}
Integer database = Integer.parseInt(String.valueOf(redisSentinelConfiguration.getDatabase()));
Set<RedisNode> nodes = redisSentinelConfiguration.getSentinels();
} else /**
*standalone肯定不为空 放后面 因为可能为localhost
*/
if (standaloneConfiguration != null && !("localhost".equals(((RedisStandaloneConfiguration) standaloneConfiguration).getHostName()))) {
matcher = SINGLE_MODE_MATCHER;
RedisStandaloneConfiguration configuration = (RedisStandaloneConfiguration) standaloneConfiguration;
String host = configuration.getHostName();
Integer port = configuration.getPort();
Integer db = configuration.getDatabase();
String password = null;
try {
char[] passwdbyte = configuration.getPassword().get();
password = new String(passwdbyte);
} catch (NoSuchElementException e) {
//
}
ShadowRedisConfig shadowRedisConfig = matcher.getConfig(new Key(host, port, db, password));
if (shadowRedisConfig == null) {
return null;
} else {
String[] spilter = shadowRedisConfig.getNodes().split(":");
String shadowHost = spilter[0];
Integer shadowPort = Integer.valueOf(spilter[1]);
Integer shadowDb = shadowRedisConfig.getDatabase();
String shadowPassword = shadowRedisConfig.getPassword();
LettuceConnectionFactory shadowConnectionFactory = new LettuceConnectionFactory();
shadowConnectionFactory.setHostName(shadowHost);
shadowConnectionFactory.setPort(shadowPort);
/**
* 填充密码
*/
if (shadowPassword != null) {
shadowConnectionFactory.setPassword(shadowPassword);
}
/**
* 填充db
*/
if (shadowDb != null) {
shadowConnectionFactory.setDatabase(shadowDb);
} else {
shadowConnectionFactory.setDatabase(biz.getDatabase());
}
/**
* 填充额外属性
*/
extraProperties(biz, shadowConnectionFactory);
// 初始化
shadowConnectionFactory.afterPropertiesSet();
/* DefaultListableBeanFactory defaultListableBeanFactory = PradarSpringUtil.getBeanFactory();*/
return shadowConnectionFactory;
}
}
return null;
}
Aggregations