Search in sources :

Example 1 with RedisClientMediator

use of com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator in project LinkAgent by shulieTech.

the class JedisSentinelFactory method createMediator.

@Override
public RedisClientMediator createMediator(Object obj, ShadowRedisConfig shadowConfig) {
    model.cachePressureNode(shadowConfig);
    RedisClientMediator mediator = null;
    if (Jedis.class.isAssignableFrom(obj.getClass())) {
        Object datasource = Reflect.on(obj).get("dataSource");
        if (datasource != null && JedisSentinelPool.class.isAssignableFrom(datasource.getClass())) {
            JedisSentinelPool pressureJedisPool = null;
            String masterName = shadowConfig.getMaster();
            Set nodes = new HashSet(shadowConfig.getNodeNums());
            String password = shadowConfig.getPassword();
            GenericObjectPoolConfig poolConfig = Reflect.on(datasource).get("poolConfig");
            password = StringUtil.isEmpty(password) ? null : password;
            int database = shadowConfig.getDatabase() == null ? 0 : shadowConfig.getDatabase();
            pressureJedisPool = new JedisSentinelPool(masterName, nodes, poolConfig, 2000, password, database);
            /* else if (StringUtil.isEmpty(password)) {
                    pressureJedisPool = new JedisSentinelPool(masterName, nodes, poolConfig);

                } else {
                    pressureJedisPool = new JedisSentinelPool(masterName, nodes, poolConfig, password);
                }*/
            mediator = new RedisClientMediator<JedisSentinelPool>(pressureJedisPool, (JedisSentinelPool) datasource, true);
        }
    }
    return mediator;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) RedisClientMediator(com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) HashSet(java.util.HashSet)

Example 2 with RedisClientMediator

use of com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator in project LinkAgent by shulieTech.

the class LettuceFactory method dynamic.

public Object dynamic(Object object) {
    Object o = connectionMap.get(object);
    if (o != null) {
        return o;
    }
    Object target = manager.removeField(object, LettuceConstants.DYNAMIC_FIELD_LETTUCE_TARGET);
    String method = manager.removeField(object, LettuceConstants.DYNAMIC_FIELD_LETTUCE_METHOD);
    Object[] args = manager.removeField(object, LettuceConstants.DYNAMIC_FIELD_LETTUCE_ARGS);
    Object result = manager.removeField(object, LettuceConstants.DYNAMIC_FIELD_LETTUCE_RESULT);
    RedisClientMediator mediator = (RedisClientMediator) getClient(target, method, args, result);
    Object performanceRedisClient = mediator.getPerformanceRedisClient();
    connectionMap.put(target, performanceRedisClient);
    return performanceRedisClient;
}
Also used : RedisClientMediator(com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator)

Example 3 with RedisClientMediator

use of com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator 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 RedisClientMediator

use of com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator in project LinkAgent by shulieTech.

the class RedissonFactory method clean.

@Override
public void clean(ShadowRedisConfig config) {
    Map<Object, RedisClientMediator<?>> mediators = getMediators();
    String master = config.getMaster();
    List<String> nodes = config.getNodes().contains(",") ? Arrays.asList(StringUtils.split(config.getNodes(), ",")) : Arrays.asList(config.getNodes());
    nodes = new ArrayList<String>(nodes);
    if (StringUtils.isNotBlank(master)) {
        nodes.add(master);
    }
    if (mediators == null) {
        return;
    }
    for (Map.Entry entry : mediators.entrySet()) {
        RedisClientMediator value = (RedisClientMediator) entry.getValue();
        if (value == null) {
            continue;
        }
        Object performanceClient = value.getPerformanceRedisClient();
        if (performanceClient == null) {
            continue;
        }
        List<String> addrs = null;
        if (performanceClient instanceof RedissonClient) {
            RedissonClient client = (RedissonClient) performanceClient;
            addrs = getAddr(client.getConfig());
            if (equals(nodes, addrs)) {
                remove(entry.getKey());
                if (!client.isShutdown() && !client.isShuttingDown()) {
                    client.shutdown();
                }
                break;
            }
        } else if (performanceClient instanceof RedissonReactiveClient) {
            RedissonReactiveClient client = (RedissonReactiveClient) performanceClient;
            addrs = getAddr(client.getConfig());
            if (equals(nodes, addrs)) {
                remove(entry.getKey());
                if (!client.isShutdown() && !client.isShuttingDown()) {
                    client.shutdown();
                }
                break;
            }
        } else {
            logger.error("unsupported redisson client ,disable all registerd shadow server..");
        // clear();
        }
    }
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisClientMediator(com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator) Map(java.util.Map) RedissonReactiveClient(org.redisson.api.RedissonReactiveClient)

Example 5 with RedisClientMediator

use of com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator in project LinkAgent by shulieTech.

the class LettuceFactory method createMediator.

@Override
public RedisClientMediator<AbstractRedisClient> createMediator(Object obj, ShadowRedisConfig shadowRedisConfig) {
    AbstractRedisClient client = (AbstractRedisClient) obj;
    RedisClientMediator<AbstractRedisClient> mediator = null;
    try {
        if (client instanceof RedisClusterClient) {
            RedisClusterClient redisClient = (RedisClusterClient) client;
            Iterable<RedisURI> redisURIS = Reflect.on(redisClient).call("getInitialUris").get();
            Iterable<RedisURI> performanceRedisUris = performanceRedisUris(redisURIS, shadowRedisConfig);
            // redisURIS需要修改ip:port等信息
            RedisClusterClient performanceRedisClient = RedisClusterClient.create(redisClient.getResources(), performanceRedisUris);
            mediator = new RedisClientMediator<AbstractRedisClient>(performanceRedisClient, client, true);
        } else if (client instanceof RedisClient) {
            RedisClient redisClient = (RedisClient) client;
            RedisURI redisURI = Reflect.on(redisClient).get("redisURI");
            // 单机redisUri,如果控制台配置多个,只取第一个node节点
            RedisURI performanceRedisUri = performanceRedisUri(redisURI, shadowRedisConfig.getNodeNums(), 0, shadowRedisConfig.getDatabase(), shadowRedisConfig.getPassword());
            RedisClient performanceRedisClient = RedisClient.create(redisClient.getResources(), performanceRedisUri);
            mediator = new RedisClientMediator<AbstractRedisClient>(performanceRedisClient, client, true);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.RedisServer).setErrorCode("redisServer-0001").setMessage("redis server lettuce error!").setDetail(ExceptionUtils.getStackTrace(e)).report();
    }
    return mediator;
}
Also used : RedisClient(io.lettuce.core.RedisClient) AbstractRedisClient(io.lettuce.core.AbstractRedisClient) AbstractRedisClient(io.lettuce.core.AbstractRedisClient) RedisURI(io.lettuce.core.RedisURI) RedisClusterClient(io.lettuce.core.cluster.RedisClusterClient) RedisClientMediator(com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator)

Aggregations

RedisClientMediator (com.pamirs.attach.plugin.common.datasource.redisserver.RedisClientMediator)8 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)2 ShadowRedisConfig (com.pamirs.pradar.internal.config.ShadowRedisConfig)2 RedissonReactiveClient (org.redisson.api.RedissonReactiveClient)2 AbstractRedisClient (io.lettuce.core.AbstractRedisClient)1 RedisClient (io.lettuce.core.RedisClient)1 RedisURI (io.lettuce.core.RedisURI)1 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 RedissonClient (org.redisson.api.RedissonClient)1 ClusterServersConfig (org.redisson.config.ClusterServersConfig)1 Config (org.redisson.config.Config)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 BinaryJedis (redis.clients.jedis.BinaryJedis)1