use of com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonReplicatedTemplate in project LinkAgent by shulieTech.
the class BaseRedissonTimeSeriesMethodInterceptor method attachment.
/**
* 入参是org.redisson.config.BaseConfig
* 但是有的版本是私有类
*
* @param config
*/
private void attachment(Object config) {
try {
if (Pradar.isClusterTest() || config == null) {
return;
}
ClusterServersConfig config1 = null;
Reflect reflect = Reflect.on(config);
String password = null;
try {
password = reflect.get("password");
} catch (Throwable t) {
}
Integer database = null;
try {
database = Integer.parseInt(String.valueOf(reflect.get("database")));
} catch (Throwable t) {
}
AbstractTemplate template;
if (config instanceof ClusterServersConfig) {
List nodeAddresses = reflect.get("nodeAddresses");
StringBuilder nodeBuilder = new StringBuilder();
for (Object object : nodeAddresses) {
if (URI.class.isAssignableFrom(object.getClass())) {
URI uri = (URI) object;
nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
} else if (String.class.isAssignableFrom(object.getClass())) {
nodeBuilder.append(removePre(object.toString())).append(",");
}
}
String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
template = new RedisTemplate.RedissionClusterTemplate().setNodes(nodes).setPassword(password);
} else if (config instanceof MasterSlaveServersConfig) {
Set nodeAddresses = reflect.get("slaveAddresses");
StringBuilder nodeBuilder = new StringBuilder();
for (Object object : nodeAddresses) {
if (URI.class.isAssignableFrom(object.getClass())) {
URI uri = (URI) object;
nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
} else if (String.class.isAssignableFrom(object.getClass())) {
nodeBuilder.append(removePre(object.toString())).append(",");
}
}
String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
Object masterNodeObj = reflect.get("masterAddress");
String masterAddr = "";
if (masterNodeObj instanceof URI) {
masterAddr = ((URI) masterNodeObj).getHost().concat(":").concat(String.valueOf(((URI) masterNodeObj).getPort()));
;
} else {
masterAddr = removePre(String.valueOf(masterNodeObj));
}
template = new RedissonMasterSlaveTemplate().setMaster(masterAddr).setNodes(nodes).setPassword(password).setDatabase(database);
} else if (config instanceof SingleServerConfig) {
String nodes = "";
Object address = Reflect.on(config).get("address");
if (String.class.isAssignableFrom(address.getClass())) {
nodes = removePre((String) address);
} else if (URI.class.isAssignableFrom(address.getClass())) {
URI uri = (URI) address;
nodes = uri.getHost().concat(":").concat(String.valueOf(uri.getPort()));
}
template = new RedissionSingleTemplate().setNodes(nodes).setPassword(password).setDatabase(database);
} else if (config instanceof SentinelServersConfig) {
List sentinelAddresses = reflect.get("sentinelAddresses");
StringBuilder nodeBuilder = new StringBuilder();
for (Object object : sentinelAddresses) {
if (URI.class.isAssignableFrom(object.getClass())) {
URI uri = (URI) object;
nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
} else if (String.class.isAssignableFrom(object.getClass())) {
nodeBuilder.append(removePre(object.toString())).append(",");
}
}
String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
template = new RedissionSentinelTemplate().setNodes(nodes).setMaster(removePre(((SentinelServersConfig) config).getMasterName())).setNodes(nodes).setDatabase((database));
} else if (config instanceof ReplicatedServersConfig) {
List nodeAddresses = reflect.get("nodeAddresses");
StringBuilder nodeBuilder = new StringBuilder();
for (Object object : nodeAddresses) {
if (URI.class.isAssignableFrom(object.getClass())) {
URI uri = (URI) object;
nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
} else if (String.class.isAssignableFrom(object.getClass())) {
nodeBuilder.append(removePre(object.toString())).append(",");
}
}
String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
template = new RedissonReplicatedTemplate().setDatabase(database).setNodes(nodes).setPassword(password);
} else {
LOGGER.error("Redisson not instanceof any know config:{}", config);
return;
}
final Attachment attachment = new Attachment(null, RedissonConstants.PLUGIN_NAME, new String[] { RedissonConstants.MIDDLEWARE_NAME }, template);
// ResourceManager.set(attachment);
Pradar.getInvokeContext().setExt(attachment);
} catch (Throwable t) {
LOGGER.error("Redisson attachment error", t);
}
}
Aggregations