Search in sources :

Example 41 with HostAndPort

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

the class ClusterJedisCommandsTestBase method setUp.

@Before
public void setUp() throws InterruptedException {
    node1 = new Jedis(nodeInfo1);
    node1.auth("cluster");
    node1.flushAll();
    node2 = new Jedis(nodeInfo2);
    node2.auth("cluster");
    node2.flushAll();
    node3 = new Jedis(nodeInfo3);
    node3.auth("cluster");
    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 slotsPerNode = CLUSTER_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++) {
    for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0; i < CLUSTER_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));
    // cluster = new JedisCluster(jedisClusterNode, 2000, 2000, 5, "cluster", new JedisPoolConfig());
    // cluster = new JedisCluster(jedisClusterNode, DefaultJedisClientConfig.builder().password("cluster").build(), 5);
    cluster = new JedisCluster(jedisClusterNode, null, "cluster");
}
Also used : Jedis(redis.clients.jedis.Jedis) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) Before(org.junit.Before)

Example 42 with HostAndPort

use of redis.clients.jedis.HostAndPort in project iris by chicc999.

the class JedisClusterFactory method parseHostAndPort.

private Set<HostAndPort> parseHostAndPort() throws Exception {
    try {
        Properties prop = new Properties();
        prop.load(this.addressConfig.getInputStream());
        Set<HostAndPort> haps = new HashSet<HostAndPort>();
        for (Object key : prop.keySet()) {
            if (!((String) key).startsWith(addressKeyPrefix)) {
                continue;
            }
            String val = (String) prop.get(key);
            boolean isIpPort = p.matcher(val).matches();
            if (!isIpPort) {
                throw new IllegalArgumentException("ip 或 port 不合法");
            }
            String[] ipAndPort = val.split(":");
            HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
            haps.add(hap);
        }
        return haps;
    } catch (IllegalArgumentException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new Exception("解析 jedis 配置文件失败", ex);
    }
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) Properties(java.util.Properties) HashSet(java.util.HashSet)

Example 43 with HostAndPort

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

the class RedisClusterCacheTest method setUp.

@Before
public void setUp() {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(cacheConfig.getMaxTotalConnections());
    poolConfig.setMaxIdle(cacheConfig.getMaxIdleConnections());
    poolConfig.setMinIdle(cacheConfig.getMinIdleConnections());
    // orginal MockJedisCluster does not provide full support for all public get/set interfaces
    // some methods must be overriden for test cases
    cache = new RedisClusterCache(new MockJedisCluster(Collections.singleton(new HostAndPort("localhost", 6379))) {

        final ConcurrentHashMap<String, byte[]> cacheStorage = new ConcurrentHashMap<>();

        @Override
        public String setex(final byte[] key, final int seconds, final byte[] value) {
            cacheStorage.put(StringUtils.encodeBase64String(key), value);
            return null;
        }

        @Override
        public byte[] get(final byte[] key) {
            return cacheStorage.get(StringUtils.encodeBase64String(key));
        }

        @Override
        public List<byte[]> mget(final byte[]... keys) {
            mgetCount.incrementAndGet();
            List<byte[]> ret = new ArrayList<>();
            for (byte[] key : keys) {
                String k = StringUtils.encodeBase64String(key);
                byte[] value = cacheStorage.get(k);
                ret.add(value);
            }
            return ret;
        }
    }, cacheConfig);
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) MockJedisCluster(com.fiftyonred.mock_jedis.MockJedisCluster) Before(org.junit.Before)

Example 44 with HostAndPort

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

the class ClusterBinaryJedisCommandsTest method setUp.

@Before
public void setUp() throws InterruptedException {
    node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
    node1.auth("cluster");
    node1.flushAll();
    node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
    node2.auth("cluster");
    node2.flushAll();
    node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort());
    node3.auth("cluster");
    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, 2000, 2000, 5, "cluster", new JedisPoolConfig());
}
Also used : Jedis(redis.clients.jedis.Jedis) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Before(org.junit.Before)

Example 45 with HostAndPort

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

the class JedisSentinelPoolTest method initializeWithNotAvailableSentinelsShouldThrowException.

@Test(expected = JedisConnectionException.class)
public void initializeWithNotAvailableSentinelsShouldThrowException() {
    Set<String> wrongSentinels = new HashSet<String>();
    wrongSentinels.add(new HostAndPort("localhost", 65432).toString());
    wrongSentinels.add(new HostAndPort("localhost", 65431).toString());
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, wrongSentinels);
    pool.destroy();
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

HostAndPort (redis.clients.jedis.HostAndPort)95 Test (org.junit.Test)42 JedisCluster (redis.clients.jedis.JedisCluster)33 HashSet (java.util.HashSet)31 Jedis (redis.clients.jedis.Jedis)24 LinkedHashSet (java.util.LinkedHashSet)18 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)10 Before (org.junit.Before)9 ArrayList (java.util.ArrayList)7 JedisPool (redis.clients.jedis.JedisPool)7 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 JSONObject (com.alibaba.fastjson.JSONObject)2