Search in sources :

Example 1 with RedissonMasterSlaveTemplate

use of com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonMasterSlaveTemplate 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);
    }
}
Also used : RedisTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate) Set(java.util.Set) RedissonReplicatedTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonReplicatedTemplate) ClusterServersConfig(org.redisson.config.ClusterServersConfig) SingleServerConfig(org.redisson.config.SingleServerConfig) RedissionSingleTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSingleTemplate) Attachment(com.pamirs.attach.plugin.dynamic.Attachment) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) RedissonMasterSlaveTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonMasterSlaveTemplate) URI(java.net.URI) ReplicatedServersConfig(org.redisson.config.ReplicatedServersConfig) Reflect(com.shulie.instrument.simulator.api.reflect.Reflect) AbstractTemplate(com.pamirs.attach.plugin.dynamic.template.AbstractTemplate) SentinelServersConfig(org.redisson.config.SentinelServersConfig) List(java.util.List) RedissionSentinelTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSentinelTemplate)

Aggregations

Attachment (com.pamirs.attach.plugin.dynamic.Attachment)1 AbstractTemplate (com.pamirs.attach.plugin.dynamic.template.AbstractTemplate)1 RedisTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate)1 RedissionSentinelTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSentinelTemplate)1 RedissionSingleTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSingleTemplate)1 RedissonMasterSlaveTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonMasterSlaveTemplate)1 RedissonReplicatedTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonReplicatedTemplate)1 Reflect (com.shulie.instrument.simulator.api.reflect.Reflect)1 URI (java.net.URI)1 List (java.util.List)1 Set (java.util.Set)1 ClusterServersConfig (org.redisson.config.ClusterServersConfig)1 MasterSlaveServersConfig (org.redisson.config.MasterSlaveServersConfig)1 ReplicatedServersConfig (org.redisson.config.ReplicatedServersConfig)1 SentinelServersConfig (org.redisson.config.SentinelServersConfig)1 SingleServerConfig (org.redisson.config.SingleServerConfig)1