Search in sources :

Example 1 with ShadowRedisConfig

use of com.pamirs.pradar.internal.config.ShadowRedisConfig in project LinkAgent by shulieTech.

the class AbstractRedisServerFactory method create.

private RedisClientMediator<?> create(Object obj) {
    ShadowRedisConfig shadowRedisConfig = serverMatch.getConfig(obj);
    if (null == shadowRedisConfig) {
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.RedisServer).setErrorCode("redisServer-0001").setMessage("没有配置影子Redis Server").setDetail("没有配置影子Redis Server").report();
        // 抛出相关异常信息
        throw new PressureMeasureError("not found redis shadow server config error1.");
    }
    validationConfig(shadowRedisConfig);
    RedisClientMediator<T> mediator = createMediator(obj, shadowRedisConfig);
    putMediator(obj, mediator);
    return mediator;
}
Also used : PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) ShadowRedisConfig(com.pamirs.pradar.internal.config.ShadowRedisConfig)

Example 2 with ShadowRedisConfig

use of com.pamirs.pradar.internal.config.ShadowRedisConfig in project LinkAgent by shulieTech.

the class JedisMatchStrategy method getConfig.

/**
 * 这里保证JedisCluster不能通过此校验方式。该校验规则给Jedis MasterSlave使用
 *
 * @param obj 任意对象
 * @return
 */
@Override
public ShadowRedisConfig getConfig(Object obj) {
    if (GlobalConfig.getInstance().getShadowRedisConfigs().size() > 0 && obj != null) {
        try {
            List<String> nodes = strategy.match(obj);
            for (String configKey : GlobalConfig.getInstance().getShadowRedisConfigs().keySet()) {
                List<String> configKeys = configKey.contains(",") ? Arrays.asList(StringUtils.split(configKey, ',')) : Collections.singletonList(configKey);
                // 这里保证JedisCluster不能通过此校验方式。该校验规则给Jedis MasterSlave使用
                ShadowRedisConfig config = GlobalConfig.getInstance().getShadowRedisConfig(configKey);
                if (StringUtils.isNotBlank(config.getMaster())) {
                    for (String key : nodes) {
                        if (configKeys.contains(key)) {
                            if (config.getDatabase() == null) {
                                return GlobalConfig.getInstance().getShadowRedisConfig(configKey);
                            } else {
                                // 兼容单节点多db的场景
                                Integer db = ((JedisNodesStrategy) strategy).getDb(obj);
                                if (db != null) {
                                    ShadowRedisConfig shadowRedisConfig = GlobalConfig.getInstance().getShadowRedisConfig(configKey);
                                    if (db.equals(config.getDatabase())) {
                                        return shadowRedisConfig;
                                    }
                                }
                            }
                        }
                    }
                } else {
                    int count = 0;
                    for (String key : nodes) {
                        if (!configKeys.contains(key)) {
                            count = 0;
                            break;
                        }
                        count++;
                    }
                    if (count == nodes.size() && configKeys.size() == count) {
                        return GlobalConfig.getInstance().getShadowRedisConfig(configKey);
                    }
                }
            }
        } catch (Throwable e) {
            LOGGER.error("", e);
            ErrorReporter.buildError().setErrorType(ErrorTypeEnum.RedisServer).setErrorCode("redisServer-0001").setMessage("获取影子数据源失败!").setDetail(ExceptionUtils.getStackTrace(e)).report();
        }
    }
    return null;
}
Also used : ShadowRedisConfig(com.pamirs.pradar.internal.config.ShadowRedisConfig)

Example 3 with ShadowRedisConfig

use of com.pamirs.pradar.internal.config.ShadowRedisConfig in project LinkAgent by shulieTech.

the class RedissonFactory method createMediator.

@Override
protected RedisClientMediator createMediator(Object obj, ShadowRedisConfig shadowRedisConfig) {
    try {
        Config oldConfig = null;
        Config newConfig = null;
        Object client = null;
        Object shadowClient = null;
        if (RedissonClient.class.isAssignableFrom(obj.getClass())) {
            client = (RedissonClient) obj;
            oldConfig = ((RedissonClient) client).getConfig();
            newConfig = generate(oldConfig, shadowRedisConfig);
            shadowClient = Redisson.create(newConfig);
        } else if (RedissonReactiveClient.class.isAssignableFrom(obj.getClass())) {
            client = (RedissonReactiveClient) obj;
            oldConfig = ((RedissonReactiveClient) client).getConfig();
            newConfig = generate(oldConfig, shadowRedisConfig);
            shadowClient = Redisson.createReactive(newConfig);
        }
        RedisClientMediator mediator = new RedisClientMediator(obj, shadowClient);
        putMediator(obj, mediator);
        return mediator;
    } catch (Throwable e) {
        logger.error(Throwables.getStackTraceAsString(e));
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.RedisServer).setDetail("build redisson shadow db error.").setMessage(Throwables.getStackTraceAsString(e)).report();
    }
    return null;
}
Also used : SingleServerConfig(org.redisson.config.SingleServerConfig) ClusterServersConfig(org.redisson.config.ClusterServersConfig) Config(org.redisson.config.Config) ReplicatedServersConfig(org.redisson.config.ReplicatedServersConfig) SentinelServersConfig(org.redisson.config.SentinelServersConfig) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) ShadowRedisConfig(com.pamirs.pradar.internal.config.ShadowRedisConfig) RedisClientMediator(com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator) RedissonReactiveClient(org.redisson.api.RedissonReactiveClient)

Example 4 with ShadowRedisConfig

use of com.pamirs.pradar.internal.config.ShadowRedisConfig 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;
}
Also used : RedisNode(org.springframework.data.redis.connection.RedisNode) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) RedisServerMatchStrategy(com.pamirs.attach.plugin.common.datasource.redisserver.RedisServerMatchStrategy) RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) ShadowRedisConfig(com.pamirs.pradar.internal.config.ShadowRedisConfig)

Example 5 with ShadowRedisConfig

use of com.pamirs.pradar.internal.config.ShadowRedisConfig in project LinkAgent by shulieTech.

the class RedissonFactory method generate.

/**
 * generate new Config
 *
 * @param oldConfig
 * @return
 */
public Config generate(Config oldConfig, ShadowRedisConfig shadowRedisConfig) {
    String master = shadowRedisConfig.getMaster();
    master = master == null ? null : master.contains(":") ? RedissonUtils.addPre(shadowRedisConfig.getMaster()) : master;
    String nodeStr = shadowRedisConfig.getNodes();
    Integer database = shadowRedisConfig.getDatabase();
    List<String> nodes = nodeStr.contains(",") ? Arrays.asList(nodeStr.split(",")) : Arrays.asList(nodeStr);
    nodes = RedissonUtils.addPre(nodes);
    String[] nodesArrays = nodes.toArray(new String[nodes.size()]);
    String passwd = shadowRedisConfig.getPassword() != null && !"null".equals(shadowRedisConfig.getPassword()) ? shadowRedisConfig.getPassword() : null;
    Config newConfig = initBaseConfig(oldConfig);
    SingleServerConfig singleServerConfig = Reflect.on(oldConfig).get(RedissonConstants.DYNAMIC_FIELD_SINGLE_SERVER_CONFIG);
    ClusterServersConfig clusterServersConfig = Reflect.on(oldConfig).get(RedissonConstants.DYNAMIC_FIELD_CLUSTER_SERVERS_CONFIG);
    SentinelServersConfig sentinelServersConfig = Reflect.on(oldConfig).get(RedissonConstants.DYNAMIC_FIELD_SENTINEL_SERVERS_CONFIG);
    ReplicatedServersConfig replicatedServersConfig = Reflect.on(oldConfig).get(RedissonConstants.DYNAMIC_FIELD_REPLICATED_SERVERS_CONFIG);
    MasterSlaveServersConfig masterSlaveServersConfig = Reflect.on(oldConfig).get(RedissonConstants.DYNAMIC_FIELD_MASTER_SLAVE_SERVERS_CONFIG);
    if (singleServerConfig != null) {
        newConfig.useSingleServer().setAddress(nodes.get(0)).setIdleConnectionTimeout(oldConfig.useSingleServer().getIdleConnectionTimeout()).setConnectTimeout(oldConfig.useSingleServer().getConnectTimeout()).setConnectionPoolSize(oldConfig.useSingleServer().getConnectionPoolSize()).setTimeout(oldConfig.useSingleServer().getTimeout()).setClientName(Pradar.CLUSTER_TEST_PREFIX + oldConfig.useSingleServer().getClientName()).setDnsMonitoringInterval(oldConfig.useSingleServer().getDnsMonitoringInterval());
        if (passwd != null) {
            newConfig.useSingleServer().setPassword(passwd);
        }
        if (database != null) {
            newConfig.useSingleServer().setDatabase(database);
        }
    } else if (clusterServersConfig != null) {
        newConfig.useClusterServers().addNodeAddress(nodesArrays).setClientName(Pradar.CLUSTER_TEST_PREFIX + oldConfig.useClusterServers().getClientName());
        if (passwd != null) {
            newConfig.useClusterServers().setPassword(passwd);
        }
    } else if (sentinelServersConfig != null) {
        newConfig.useSentinelServers().addSentinelAddress(nodesArrays).setMasterName(sentinelServersConfig.getMasterName()).setClientName(Pradar.CLUSTER_TEST_PREFIX + sentinelServersConfig.getClientName()).setConnectTimeout(sentinelServersConfig.getConnectTimeout()).setIdleConnectionTimeout(sentinelServersConfig.getIdleConnectionTimeout()).setLoadBalancer(sentinelServersConfig.getLoadBalancer()).setTimeout(sentinelServersConfig.getTimeout()).setSlaveConnectionMinimumIdleSize(sentinelServersConfig.getSlaveConnectionMinimumIdleSize()).setRetryInterval(sentinelServersConfig.getRetryInterval()).setMasterConnectionMinimumIdleSize(sentinelServersConfig.getMasterConnectionMinimumIdleSize()).setMasterConnectionPoolSize(sentinelServersConfig.getMasterConnectionPoolSize()).setSlaveConnectionMinimumIdleSize(sentinelServersConfig.getSlaveConnectionMinimumIdleSize()).setSlaveConnectionPoolSize(sentinelServersConfig.getSlaveConnectionPoolSize());
        // 过滤检查
        setCheckSentinelsList(newConfig.useSentinelServers());
        if (master != null && !"null".equals(master)) {
            newConfig.useSentinelServers().setMasterName(master);
        }
        if (passwd != null) {
            newConfig.useSentinelServers().setPassword(passwd);
        }
        if (database != null) {
            newConfig.useSentinelServers().setDatabase(database);
        }
    } else if (replicatedServersConfig != null) {
        newConfig.useReplicatedServers().addNodeAddress(nodesArrays);
        if (passwd != null) {
            newConfig.useReplicatedServers().setPassword(passwd);
        }
        if (database != null) {
            newConfig.useReplicatedServers().setDatabase(database);
        }
    } else if (masterSlaveServersConfig != null) {
        newConfig.useMasterSlaveServers().setMasterAddress(master);
        newConfig.useMasterSlaveServers().addSlaveAddress(nodesArrays);
        if (database != null) {
            newConfig.useMasterSlaveServers().setDatabase(database);
        }
        if (passwd != null) {
            newConfig.useMasterSlaveServers().setPassword(passwd);
        }
    }
    return newConfig;
}
Also used : ReplicatedServersConfig(org.redisson.config.ReplicatedServersConfig) SingleServerConfig(org.redisson.config.SingleServerConfig) ClusterServersConfig(org.redisson.config.ClusterServersConfig) Config(org.redisson.config.Config) ReplicatedServersConfig(org.redisson.config.ReplicatedServersConfig) SentinelServersConfig(org.redisson.config.SentinelServersConfig) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) ShadowRedisConfig(com.pamirs.pradar.internal.config.ShadowRedisConfig) ClusterServersConfig(org.redisson.config.ClusterServersConfig) SentinelServersConfig(org.redisson.config.SentinelServersConfig) SingleServerConfig(org.redisson.config.SingleServerConfig) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig)

Aggregations

ShadowRedisConfig (com.pamirs.pradar.internal.config.ShadowRedisConfig)7 RedisClientMediator (com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator)2 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)2 ClusterServersConfig (org.redisson.config.ClusterServersConfig)2 Config (org.redisson.config.Config)2 MasterSlaveServersConfig (org.redisson.config.MasterSlaveServersConfig)2 ReplicatedServersConfig (org.redisson.config.ReplicatedServersConfig)2 SentinelServersConfig (org.redisson.config.SentinelServersConfig)2 SingleServerConfig (org.redisson.config.SingleServerConfig)2 RedisServerMatchStrategy (com.pamirs.attach.plugin.common.datasource.redisserver.RedisServerMatchStrategy)1 ShadowRedisServerDisableEvent (com.pamirs.pradar.pressurement.agent.event.impl.ShadowRedisServerDisableEvent)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 RedissonReactiveClient (org.redisson.api.RedissonReactiveClient)1 RedisClusterConfiguration (org.springframework.data.redis.connection.RedisClusterConfiguration)1 RedisNode (org.springframework.data.redis.connection.RedisNode)1 RedisSentinelConfiguration (org.springframework.data.redis.connection.RedisSentinelConfiguration)1 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)1 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)1