Search in sources :

Example 81 with HostAndPort

use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.

the class ClusterBinaryJedisCommandsTest method setUp.

@Before
public void setUp() throws InterruptedException {
    node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
    node1.connect();
    node1.flushAll();
    node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
    node2.connect();
    node2.flushAll();
    node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort());
    node3.connect();
    node3.flushAll();
    // ---- configure cluster
    // add nodes to cluster
    node1.clusterMeet("127.0.0.1", nodeInfo2.getPort());
    node1.clusterMeet("127.0.0.1", nodeInfo3.getPort());
    // split available slots across the three nodes
    int slotsPerNode = JedisCluster.HASHSLOTS / 3;
    int[] node1Slots = new int[slotsPerNode];
    int[] node2Slots = new int[slotsPerNode + 1];
    int[] node3Slots = new int[slotsPerNode];
    for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0; i < JedisCluster.HASHSLOTS; i++) {
        if (i < slotsPerNode) {
            node1Slots[slot1++] = i;
        } else if (i > slotsPerNode * 2) {
            node3Slots[slot3++] = i;
        } else {
            node2Slots[slot2++] = i;
        }
    }
    node1.clusterAddSlots(node1Slots);
    node2.clusterAddSlots(node2Slots);
    node3.clusterAddSlots(node3Slots);
    waitForClusterReady();
    jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
    jedisCluster = new JedisCluster(jedisClusterNode);
}
Also used : Jedis(redis.clients.jedis.Jedis) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) Before(org.junit.Before)

Example 82 with HostAndPort

use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.

the class ClusterScriptingCommandsTest method setUp.

@Before
public void setUp() throws InterruptedException {
    node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
    node1.connect();
    node1.flushAll();
    node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
    node2.connect();
    node2.flushAll();
    node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort());
    node3.connect();
    node3.flushAll();
    // ---- configure cluster
    // add nodes to cluster
    node1.clusterMeet("127.0.0.1", nodeInfo2.getPort());
    node1.clusterMeet("127.0.0.1", nodeInfo3.getPort());
    // split available slots across the three nodes
    int slotsPerNode = JedisCluster.HASHSLOTS / 3;
    int[] node1Slots = new int[slotsPerNode];
    int[] node2Slots = new int[slotsPerNode + 1];
    int[] node3Slots = new int[slotsPerNode];
    for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0; i < JedisCluster.HASHSLOTS; i++) {
        if (i < slotsPerNode) {
            node1Slots[slot1++] = i;
        } else if (i > slotsPerNode * 2) {
            node3Slots[slot3++] = i;
        } else {
            node2Slots[slot2++] = i;
        }
    }
    node1.clusterAddSlots(node1Slots);
    node2.clusterAddSlots(node2Slots);
    node3.clusterAddSlots(node3Slots);
    waitForClusterReady();
    jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
    jedisCluster = new JedisCluster(jedisClusterNode);
}
Also used : Jedis(redis.clients.jedis.Jedis) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) Before(org.junit.Before)

Example 83 with HostAndPort

use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.

the class HostAndPortUtil method parseHosts.

public static List<HostAndPort> parseHosts(String envHosts, List<HostAndPort> existingHostsAndPorts) {
    if (null != envHosts && 0 < envHosts.length()) {
        String[] hostDefs = envHosts.split(",");
        if (null != hostDefs && 2 <= hostDefs.length) {
            List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPort>(hostDefs.length);
            for (String hostDef : hostDefs) {
                String[] hostAndPort = hostDef.split(":");
                if (null != hostAndPort && 2 == hostAndPort.length) {
                    String host = hostAndPort[0];
                    int port = Protocol.DEFAULT_PORT;
                    try {
                        port = Integer.parseInt(hostAndPort[1]);
                    } catch (final NumberFormatException nfe) {
                    }
                    envHostsAndPorts.add(new HostAndPort(host, port));
                }
            }
            return envHostsAndPorts;
        }
    }
    return existingHostsAndPorts;
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) ArrayList(java.util.ArrayList)

Example 84 with HostAndPort

use of redis.clients.jedis.HostAndPort in project druid by druid-io.

the class RedisCacheFactory method create.

public static Cache create(final RedisCacheConfig config) {
    if (config.getCluster() != null && StringUtils.isNotBlank(config.getCluster().getNodes())) {
        Set<HostAndPort> nodes = Arrays.stream(config.getCluster().getNodes().split(",")).map(String::trim).filter(StringUtils::isNotBlank).map(hostAndPort -> {
            int index = hostAndPort.indexOf(':');
            if (index <= 0 || index == hostAndPort.length()) {
                throw new IAE("Invalid redis cluster configuration: %s", hostAndPort);
            }
            int port;
            try {
                port = Integer.parseInt(hostAndPort.substring(index + 1));
            } catch (NumberFormatException e) {
                throw new IAE("Invalid port in %s", hostAndPort);
            }
            if (port <= 0 || port > 65535) {
                throw new IAE("Invalid port in %s", hostAndPort);
            }
            return new HostAndPort(hostAndPort.substring(0, index), port);
        }).collect(Collectors.toSet());
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(config.getMaxTotalConnections());
        poolConfig.setMaxIdle(config.getMaxIdleConnections());
        poolConfig.setMinIdle(config.getMinIdleConnections());
        JedisCluster cluster;
        if (config.getPassword() != null) {
            cluster = new JedisCluster(nodes, // connection timeout
            config.getTimeout().getMillisecondsAsInt(), // read timeout
            config.getTimeout().getMillisecondsAsInt(), config.getCluster().getMaxRedirection(), config.getPassword().getPassword(), poolConfig);
        } else {
            cluster = new JedisCluster(nodes, // connection timeout and read timeout
            config.getTimeout().getMillisecondsAsInt(), config.getCluster().getMaxRedirection(), poolConfig);
        }
        return new RedisClusterCache(cluster, config);
    } else {
        if (StringUtils.isBlank(config.getHost())) {
            throw new IAE("Invalid redis configuration. no redis server or cluster configured.");
        }
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(config.getMaxTotalConnections());
        poolConfig.setMaxIdle(config.getMaxIdleConnections());
        poolConfig.setMinIdle(config.getMinIdleConnections());
        return new RedisStandaloneCache(new JedisPool(poolConfig, config.getHost(), config.getPort(), // connection timeout and read timeout
        config.getTimeout().getMillisecondsAsInt(), config.getPassword() == null ? null : config.getPassword().getPassword(), config.getDatabase(), null), config);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) Set(java.util.Set) JedisPool(redis.clients.jedis.JedisPool) IAE(org.apache.druid.java.util.common.IAE) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Collectors(java.util.stream.Collectors) HostAndPort(redis.clients.jedis.HostAndPort) StringUtils(org.apache.commons.lang.StringUtils) JedisCluster(redis.clients.jedis.JedisCluster) JedisPool(redis.clients.jedis.JedisPool) IAE(org.apache.druid.java.util.common.IAE) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig)

Example 85 with HostAndPort

use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.

the class ExceptionsTest method askData.

@Test
public void askData() {
    HostAndPort hap = new HostAndPort("", 0);
    int slot = -1;
    try {
        throw new JedisAskDataException(MESSAGE, hap, slot);
    } catch (Exception e) {
        assertSame(JedisAskDataException.class, e.getClass());
        assertEquals(MESSAGE, e.getMessage());
        assertNull(e.getCause());
    }
    try {
        throw new JedisAskDataException(CAUSE, hap, slot);
    } catch (Exception e) {
        assertSame(JedisAskDataException.class, e.getClass());
        assertEquals(CAUSE, e.getCause());
        assertEquals(CAUSE.toString(), e.getMessage());
    }
    try {
        throw new JedisAskDataException(MESSAGE, CAUSE, hap, slot);
    } catch (Exception e) {
        assertSame(JedisAskDataException.class, e.getClass());
        assertEquals(MESSAGE, e.getMessage());
        assertEquals(CAUSE, e.getCause());
    }
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) Test(org.junit.Test)

Aggregations

HostAndPort (redis.clients.jedis.HostAndPort)102 Test (org.junit.Test)42 HashSet (java.util.HashSet)35 JedisCluster (redis.clients.jedis.JedisCluster)35 Jedis (redis.clients.jedis.Jedis)25 LinkedHashSet (java.util.LinkedHashSet)18 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)12 Before (org.junit.Before)9 JedisPool (redis.clients.jedis.JedisPool)9 ArrayList (java.util.ArrayList)8 ClusterNodeInformation (redis.clients.util.ClusterNodeInformation)6 AppDesc (com.sohu.cache.entity.AppDesc)5 InstanceInfo (com.sohu.cache.entity.InstanceInfo)5 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)5 IOException (java.io.IOException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 JedisPubSub (redis.clients.jedis.JedisPubSub)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Properties (java.util.Properties)3