Search in sources :

Example 31 with JedisCluster

use of redis.clients.jedis.JedisCluster in project duangframework by tcrct.

the class AbstractRedisClient method call.

public <T> T call(JedisAction cacheAction) {
    T result = null;
    if (!isCluster()) {
        Jedis jedis = JedisPoolUtils.getJedis();
        try {
            result = (T) cacheAction.execute(jedis);
        } catch (Exception e) {
            // JedisPoolUtils.returnBrokenResource(jedis);
            logger.warn(e.getMessage(), e);
        }
    // finally {
    // JedisPoolUtils.returnResource(jedis);
    // }
    } else {
        JedisCluster jedisCluster = JedisClusterPoolUtils.getJedisCluster();
        result = (T) cacheAction.execute(jedisCluster);
    }
    return result;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisCluster(redis.clients.jedis.JedisCluster)

Example 32 with JedisCluster

use of redis.clients.jedis.JedisCluster in project duangframework by tcrct.

the class JedisClusterPoolUtils method createJedisPool.

/**
 * 建立连接池 真实环境,一般把配置参数缺抽取出来。
 */
private static void createJedisPool() {
    try {
        String[] ipArray = getClusterIps();
        if (ToolsKit.isEmpty(ipArray)) {
            throw new EmptyNullException("ipArray is null");
        }
        // 只给集群里一个实例就可以
        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
        for (int i = 0; i < ipArray.length; i++) {
            String[] items = ipArray[i].split(":");
            jedisClusterNodes.add(new HostAndPort(items[0], Integer.parseInt(items[1])));
        }
        // 配置信息
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxTotal(10000);
        config.setMaxIdle(500);
        config.setMinIdle(100);
        config.setMaxWaitMillis(5000);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        jedisCluster = new JedisCluster(jedisClusterNodes, config);
        if (null != jedisCluster) {
            logger.warn("Connent Redis Cluster:  " + ToolsKit.toJsonString(jedisClusterNodes) + " is Success...");
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.info("初始化 redis 出错啦...");
    }
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) JedisCluster(redis.clients.jedis.JedisCluster) IOException(java.io.IOException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) HashSet(java.util.HashSet)

Example 33 with JedisCluster

use of redis.clients.jedis.JedisCluster in project oxCore by GluuFederation.

the class RedisClusterProvider method create.

public void create() {
    try {
        LOG.debug("Starting RedisClusterProvider ... configuration:" + getRedisConfiguration());
        JedisPoolConfig poolConfig = createPoolConfig();
        String password = redisConfiguration.getPassword();
        if (redisConfiguration.getUseSSL()) {
            RedisProviderFactory.setSSLSystemProperties(redisConfiguration);
            pool = new JedisCluster(hosts(getRedisConfiguration().getServers()), redisConfiguration.getConnectionTimeout(), redisConfiguration.getSoTimeout(), redisConfiguration.getMaxRetryAttempts(), password, UUID.randomUUID().toString(), poolConfig, true);
        } else {
            pool = new JedisCluster(hosts(getRedisConfiguration().getServers()), redisConfiguration.getConnectionTimeout(), redisConfiguration.getSoTimeout(), redisConfiguration.getMaxRetryAttempts(), password, poolConfig);
        }
        testConnection();
        LOG.debug("RedisClusterProvider started.");
    } catch (Exception e) {
        LOG.error("Failed to start RedisClusterProvider.", e);
        throw new IllegalStateException("Error starting RedisClusterProvider", e);
    }
}
Also used : JedisCluster(redis.clients.jedis.JedisCluster) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig)

Example 34 with JedisCluster

use of redis.clients.jedis.JedisCluster in project YCSB by brianfrankcooper.

the class RedisClient method init.

public void init() throws DBException {
    Properties props = getProperties();
    int port;
    String portString = props.getProperty(PORT_PROPERTY);
    if (portString != null) {
        port = Integer.parseInt(portString);
    } else {
        port = Protocol.DEFAULT_PORT;
    }
    String host = props.getProperty(HOST_PROPERTY);
    boolean clusterEnabled = Boolean.parseBoolean(props.getProperty(CLUSTER_PROPERTY));
    if (clusterEnabled) {
        Set<HostAndPort> jedisClusterNodes = new HashSet<>();
        jedisClusterNodes.add(new HostAndPort(host, port));
        jedis = new JedisCluster(jedisClusterNodes);
    } else {
        String redisTimeout = props.getProperty(TIMEOUT_PROPERTY);
        if (redisTimeout != null) {
            jedis = new Jedis(host, port, Integer.parseInt(redisTimeout));
        } else {
            jedis = new Jedis(host, port);
        }
        ((Jedis) jedis).connect();
    }
    String password = props.getProperty(PASSWORD_PROPERTY);
    if (password != null) {
        ((BasicCommands) jedis).auth(password);
    }
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) Jedis(redis.clients.jedis.Jedis) JedisCluster(redis.clients.jedis.JedisCluster) Properties(java.util.Properties) BasicCommands(redis.clients.jedis.BasicCommands) HashSet(java.util.HashSet)

Example 35 with JedisCluster

use of redis.clients.jedis.JedisCluster in project tech by ffyyhh995511.

the class JedisClusterService method get.

public String get(String key) throws IOException {
    String res = null;
    JedisCluster jedisCluster = null;
    try {
        jedisCluster = new JedisCluster(jedisClusterNodes, poolConfig);
        res = jedisCluster.get(key);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (jedisCluster != null) {
            jedisCluster.close();
        }
    }
    return res;
}
Also used : JedisCluster(redis.clients.jedis.JedisCluster) IOException(java.io.IOException)

Aggregations

JedisCluster (redis.clients.jedis.JedisCluster)44 HostAndPort (redis.clients.jedis.HostAndPort)35 HashSet (java.util.HashSet)22 Test (org.junit.Test)19 LinkedHashSet (java.util.LinkedHashSet)18 Jedis (redis.clients.jedis.Jedis)12 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)10 IOException (java.io.IOException)9 Before (org.junit.Before)7 JedisPool (redis.clients.jedis.JedisPool)6 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1