Search in sources :

Example 36 with JedisCluster

use of redis.clients.jedis.JedisCluster in project storm by apache.

the class RedisClusterStateQuerier method retrieveValuesFromRedis.

/**
 * {@inheritDoc}
 */
@Override
protected List<String> retrieveValuesFromRedis(RedisClusterState state, List<String> keys) {
    JedisCluster jedisCluster = null;
    try {
        jedisCluster = state.getJedisCluster();
        List<String> redisVals = new ArrayList<String>();
        for (String key : keys) {
            switch(dataType) {
                case STRING:
                    redisVals.add(jedisCluster.get(key));
                    break;
                case HASH:
                    redisVals.add(jedisCluster.hget(additionalKey, key));
                    break;
                default:
                    throw new IllegalArgumentException("Cannot process such data type: " + dataType);
            }
        }
        return redisVals;
    } finally {
        if (jedisCluster != null) {
            state.returnJedisCluster(jedisCluster);
        }
    }
}
Also used : JedisCluster(redis.clients.jedis.JedisCluster) ArrayList(java.util.ArrayList)

Example 37 with JedisCluster

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

the class JedisClusterFactory method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Set<HostAndPort> haps = this.parseHostAndPort();
    jedisCluster = new JedisCluster(haps, timeout, maxRedirections, genericObjectPoolConfig);
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster)

Example 38 with JedisCluster

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

the class RedisClusterBuilder method build.

public JedisCluster build(String url) {
    if (jedisCluster == null) {
        while (true) {
            try {
                lock.tryLock(10, TimeUnit.SECONDS);
                if (jedisCluster != null) {
                    return jedisCluster;
                }
                String response = HttpUtils.doGet(url);
                JSONObject jsonObject = null;
                try {
                    jsonObject = JSONObject.parseObject(response);
                } catch (Exception e) {
                    logger.error("remote build error, appId: {}", appId, e);
                }
                if (jsonObject == null) {
                    logger.error("get cluster info for appId: {} error. continue...", appId);
                    continue;
                }
                int status = jsonObject.getIntValue("status");
                String message = jsonObject.getString("message");
                /**
                 * 检查客户端版本 *
                 */
                if (status == ClientStatusEnum.ERROR.getStatus()) {
                    throw new IllegalStateException(message);
                } else if (status == ClientStatusEnum.WARN.getStatus()) {
                    logger.warn(message);
                } else {
                    logger.info(message);
                }
                Set<HostAndPort> nodeList = new HashSet<HostAndPort>();
                // 形如 ip1:port1,ip2:port2,ip3:port3
                String nodeInfo = jsonObject.getString("shardInfo");
                // 为了兼容,如果允许直接nodeInfo.split(" ")
                nodeInfo = nodeInfo.replace(" ", ",");
                String[] nodeArray = nodeInfo.split(",");
                for (String node : nodeArray) {
                    String[] ipAndPort = node.split(":");
                    if (ipAndPort.length < 2) {
                        continue;
                    }
                    String ip = ipAndPort[0];
                    int port = Integer.parseInt(ipAndPort[1]);
                    nodeList.add(new HostAndPort(ip, port));
                }
                // 收集上报数据
                if (clientStatIsOpen) {
                    ClientDataCollectReportExecutor.getInstance(url);
                }
                String password = jsonObject.getString("password");
                if (StringUtil.isBlank(password)) {
                    jedisCluster = new JedisCluster(nodeList, connectionTimeout, soTimeout, maxRedirections, jedisPoolConfig);
                } else {
                    jedisCluster = new JedisCluster(nodeList, connectionTimeout, soTimeout, maxRedirections, password, jedisPoolConfig);
                }
                return jedisCluster;
            } catch (Throwable e) {
                logger.error(e.getMessage(), e);
            } finally {
                lock.unlock();
            }
            try {
                // 活锁
                TimeUnit.MILLISECONDS.sleep(200 + new Random().nextInt(1000));
            } catch (InterruptedException e) {
                logger.error(e.getMessage(), e);
            }
        }
    } else {
        return jedisCluster;
    }
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JSONObject(com.alibaba.fastjson.JSONObject) Random(java.util.Random) JedisCluster(redis.clients.jedis.JedisCluster) HashSet(java.util.HashSet)

Example 39 with JedisCluster

use of redis.clients.jedis.JedisCluster 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 40 with JedisCluster

use of redis.clients.jedis.JedisCluster 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)

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