Search in sources :

Example 71 with HostAndPort

use of redis.clients.jedis.HostAndPort 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 72 with HostAndPort

use of redis.clients.jedis.HostAndPort in project incubator-dubbo-ops by apache.

the class RedisMetaDataCollector method init.

@Override
public void init() {
    timeout = url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
    password = url.getPassword();
    if (url.getParameter(CLUSTER_KEY, false)) {
        jedisClusterNodes = new HashSet<>();
        String[] addresses = COMMA_SPLIT_PATTERN.split(url.getAddress());
        for (String address : addresses) {
            URL tmpUrl = url.setAddress(address);
            jedisClusterNodes.add(new HostAndPort(tmpUrl.getHost(), tmpUrl.getPort()));
        }
    } else {
        int database = url.getParameter(REDIS_DATABASE_KEY, 0);
        pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), timeout, url.getPassword(), database);
    }
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisPool(redis.clients.jedis.JedisPool) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) URL(org.apache.dubbo.common.URL)

Example 73 with HostAndPort

use of redis.clients.jedis.HostAndPort 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 74 with HostAndPort

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

the class RedisSentinelTest method testSentinel.

@Test
public void testSentinel() {
    JedisSentinelPool sentinelPool = ClientBuilder.redisSentinel(appId).setConnectionTimeout(2000).setSoTimeout(1000).build("");
    HostAndPort currentHostMaster = sentinelPool.getCurrentHostMaster();
    logger.info("current master: {}", currentHostMaster.toString());
    Jedis jedis = sentinelPool.getResource();
    for (int i = 0; i < 10; i++) {
        jedis.lpush("mylist", "list-" + i);
    }
    jedis.close();
    sentinelPool.destroy();
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) Jedis(redis.clients.jedis.Jedis) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) BaseTest(com.sohu.tv.test.base.BaseTest) Test(org.junit.Test)

Example 75 with HostAndPort

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

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