redis.clients.jedis.JedisShardInfo Java Examples

The following examples show how to use redis.clients.jedis.JedisShardInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JedisApiTest.java    From easyooo-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 单机模拟分库
 * 耗时:1.244 seconds
 */
@Test
@Ignore
public void testShardNormal(){
	 List<JedisShardInfo> shards = Arrays.asList(
	            new JedisShardInfo("localhost",6379),
	            new JedisShardInfo("localhost",6379));

    ShardedJedis sharding = new ShardedJedis(shards);
    
    long start = System.currentTimeMillis();
    for (int i = 0; i < COUNTER; i++) {
        String result = sharding.set("sn" + i, "n" + i);
    }
    long end = System.currentTimeMillis();
    System.out.println("Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds");

    sharding.disconnect();
    sharding.close();
}
 
Example #2
Source File: ShardedJedisPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void checkResourceIsCloseable() throws URISyntaxException {
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(1);
  config.setBlockWhenExhausted(false);

  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
  shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380")));
  shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379")));

  ShardedJedisPool pool = new ShardedJedisPool(config, shards);

  ShardedJedis jedis = pool.getResource();
  try {
    jedis.set("hello", "jedis");
  } finally {
    jedis.close();
  }

  ShardedJedis jedis2 = pool.getResource();
  try {
    assertEquals(jedis, jedis2);
  } finally {
    jedis2.close();
  }
}
 
Example #3
Source File: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testShardNormal() {// 13.619秒
	JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379);
	jedis.setPassword("b840fc02d52404542994");

	List<JedisShardInfo> shards = Arrays.asList(jedis);
	ShardedJedis sharding = new ShardedJedis(shards);

	long start = System.currentTimeMillis();
	for (int i = 0; i < 1000; i++) {
		sharding.set("n" + i, "n" + i);
		System.out.println(i);
	}
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	sharding.disconnect();
	try {
		Closeables.close(sharding, true);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example #4
Source File: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testShardPipelined() {// 0.127秒
	JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379);
	jedis.setPassword("b840fc02d52404542994");

	List<JedisShardInfo> shards = Arrays.asList(jedis);
	ShardedJedis sharding = new ShardedJedis(shards);
	ShardedJedisPipeline pipeline = sharding.pipelined();

	long start = System.currentTimeMillis();
	for (int i = 0; i < 1000; i++) {
		pipeline.set("n" + i, "n" + i);
		System.out.println(i);
	}
	pipeline.syncAndReturnAll();
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	sharding.disconnect();
	try {
		Closeables.close(sharding, true);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example #5
Source File: ShardedJedisTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void checkCloseable() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
  shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
  shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
  shards.get(0).setPassword("foobared");
  shards.get(1).setPassword("foobared");

  ShardedJedis jedisShard = new ShardedJedis(shards);
  try {
    jedisShard.set("shard_closeable", "true");
  } finally {
    jedisShard.close();
  }

  for (Jedis jedis : jedisShard.getAllShards()) {
    assertTrue(!jedis.isConnected());
  }
}
 
Example #6
Source File: ShardedJedisTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testMasterSlaveShardingConsistencyWithShardNaming() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT, "HOST1:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1, "HOST2:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2, "HOST3:1234"));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1, "HOST3:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2, "HOST1:1234"));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName());
  }
}
 
Example #7
Source File: ShardedJedisTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testMasterSlaveShardingConsistency() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(shards.indexOf(jedisShardInfo), otherShards.indexOf(jedisShardInfo2));
  }

}
 
Example #8
Source File: JedisManagerFactory.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 根据配置创建
 * 
 * @return
 */
private static JedisManager createJedisManager() {

    // 池基本配置
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(Conf.getInt("redis.maxIdle"));
    config.setMinIdle(Conf.getInt("redis.minIdle"));
    config.setMaxWaitMillis(Conf.getLong("redis.maxWaitMillis"));
    config.setTestOnBorrow(Conf.getBoolean("redis.testOnBorrow"));
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    // 链接
    shards.add(new JedisShardInfo(Conf.getString("redis.host"), Conf.getInt("redis.port"), Conf
            .getInt("redis.timeout")));

    // 构造池
    ShardedJedisPool shardedJedisPool = new ShardedJedisPool(config, shards);

    return new JedisManager(shardedJedisPool);
}
 
Example #9
Source File: JedisApiTest.java    From easyooo-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 分布式直连异步调用
 * 耗时:
 * 0.866 seconds
 * 0.892 seconds
 */
@Test
@Ignore
public void testShardpipelined() {
    List<JedisShardInfo> shards = Arrays.asList(
            new JedisShardInfo("localhost",6379),
            new JedisShardInfo("localhost",6379));

    ShardedJedis sharding = new ShardedJedis(shards);

    ShardedJedisPipeline pipeline = sharding.pipelined();
    long start = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
        pipeline.set("sp" + i, "p" + i);
    }
    List<Object> results = pipeline.syncAndReturnAll();
    long end = System.currentTimeMillis();
    System.out.println("Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds");

    sharding.disconnect();
    sharding.close();
}
 
Example #10
Source File: JedisApiTest.java    From easyooo-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 分布式连接池同步调用
 * 1.288 seconds
 * 1.291 seconds
 */
@Test
public void testShardSimplePool() {
	List<JedisShardInfo> shards = Arrays.asList(new JedisShardInfo(
			"localhost", 6379), new JedisShardInfo("localhost", 6379));

	ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);
	ShardedJedis one = pool.getResource();

	long start = System.currentTimeMillis();
	for (int i = 0; i < COUNTER; i++) {
		String result = one.set("spn" + i, "n" + i);
	}
	long end = System.currentTimeMillis();
	pool.returnResource(one);
	logger.info("Simple@Pool SET: " + ((end - start) / 1000.0) + " seconds");

	pool.destroy();
}
 
Example #11
Source File: RedisShardedPool.java    From jea with Apache License 2.0 6 votes vote down vote up
public RedisShardedPool(){
	listServer = new ArrayList<JedisShardInfo>();
	poolConfig = new RedisPoolConfig();
	//控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
       //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
	poolConfig.setMaxTotal(50);
	//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
	poolConfig.setMaxIdle(10);
	//表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
	poolConfig.setMaxWaitMillis(500);
	 //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
	poolConfig.setTestOnBorrow(true);
	//在return给pool时,是否提前进行validate操作;
	poolConfig.setTestOnReturn(true);
	//borrowObject返回对象时,是采用DEFAULT_LIFO(last in first out,即类似cache的最频繁使用队列),如果为False,则表示FIFO队列;
	poolConfig.setLifo(true);
}
 
Example #12
Source File: JedisShartClient.java    From howsun-javaee-framework with Apache License 2.0 6 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
	JedisShardInfo jedisShardInfo1 = new JedisShardInfo(ip1, port);
	jedisShardInfo1.setPassword(JedisConstant.password);
	list.add(jedisShardInfo1);
	JedisShardInfo jedisShardInfo2 = new JedisShardInfo(ip2, port);
	jedisShardInfo2.setPassword(JedisConstant.password);
	list.add(jedisShardInfo2);
	ShardedJedisPool pool = new ShardedJedisPool(config, list);
	for (int i = 0; i < 2000; i++) {
		ShardedJedis jedis = pool.getResource();
		String key = "howsun_" + i;
		//jedis.set(key, UUID.randomUUID().toString());
		System.out.println(key + "\t" + jedis.get(key) + "\t" + jedis.toString());
		pool.returnResource(jedis);
	}
}
 
Example #13
Source File: RedisClient.java    From Redis_Learning with Apache License 2.0 6 votes vote down vote up
/**
 * ��ʼ����Ƭ��
 */
private void initialShardedPool() {
	// �ػ�������
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxActive(20);
	config.setMaxIdle(5);
	config.setMaxWait(1000l);
	config.setTestOnBorrow(false);

	List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
	JedisShardInfo infoA = new JedisShardInfo(ADDR, PORT);
	infoA.setPassword("redis");
	shards.add(infoA);
	// ���Dz��� ��ʱ���������ķ������������ӷ���������
	// JedisShardInfo infoB = new JedisShardInfo(SUB_ADDR, PORT2);
	// infoB.setPassword("redis");
	// shards.add(infoB);
	// shards = Arrays.asList(infoA,infoB);
	shardedJedisPool = new ShardedJedisPool(config, shards,
			Hashing.MURMUR_HASH, ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
}
 
Example #14
Source File: TestShardedRedis.java    From craft-atom with MIT License 6 votes vote down vote up
private void init() {
	redis1.slaveofnoone();
	redis2.slaveofnoone();
	redis3.slaveofnoone();
	List<Redis> shards = new ArrayList<Redis>(3);
	shards.add(redis1);
	shards.add(redis2);
	shards.add(redis3);
	shardedRedis = RedisFactory.newShardedRedis(shards);
	
	List<JedisShardInfo> shardinfos = new ArrayList<JedisShardInfo>();
	shardinfos.add(new JedisShardInfo(HOST, PORT1));
	shardinfos.add(new JedisShardInfo(HOST, PORT2));
	shardinfos.add(new JedisShardInfo(HOST, PORT3));
	shardedPool = new ShardedJedisPool(new JedisPoolConfig(), shardinfos);
}
 
Example #15
Source File: JedisApiTest.java    From easyooo-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 分布式连接池异步调用
 * 0.452 seconds
 * 0.43 seconds
 */
@Test
@Ignore
public void testShardPipelinedPool() {
    List<JedisShardInfo> shards = Arrays.asList(
            new JedisShardInfo("localhost",6379),
            new JedisShardInfo("localhost",6379));

    ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);
    ShardedJedis one = pool.getResource();
    ShardedJedisPipeline pipeline = one.pipelined();
    
    long start = System.currentTimeMillis();
    for (int i = 0; i < COUNTER; i++) {
        pipeline.set("sppn" + i, "n" + i);
    }
    List<Object> results = pipeline.syncAndReturnAll();
    long end = System.currentTimeMillis();
    pool.returnResource(one);
    logger.info("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds");
    pool.destroy();
}
 
Example #16
Source File: RedisServiceFactory.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	final JedisPoolConfig config = JedisPoolConfigFactory.createJedisPoolConfig();

	final Set<JedisShardInfo> shardInfos = new HashSet<JedisShardInfo>();
	final HostInfo[] hostInfos = HostInfoFactory.split(hosts);
	for (final HostInfo hostInfo : hostInfos) {
		shardInfos.add(hostInfo.createJedisShardInfo());
	}
	if (redisService == null) {
		final ShardedJedisPool jedisPool = new ShardedJedisPool(config, new ArrayList<JedisShardInfo>(shardInfos));
		redisService = new RedisServiceImpl(jedisPool);
	}

	final RedisServiceProxy redisServiceProxy = new RedisServiceProxy();
	this.redisService = redisServiceProxy.bind(redisService);
}
 
Example #17
Source File: DefaultShardedRedisConfiguration.java    From spring-redis-plugin with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "destroy")
public ShardedJedisPool defaultShardedJedisPool() {
    String hoststr = env.getProperty("redis.sharded.hosts", "127.0.0.1");
    String portstr = env.getProperty("redis.sharded.ports", "6379");
    List<String> hosts = Arrays.asList(hoststr.split(","));
    List<Integer> ports = Stream.of(portstr.split(",")).map(Integer::new).collect(Collectors.toList());

    List<JedisShardInfo> list = IntStream.range(0, hosts.size()).mapToObj(i -> new JedisShardInfo(hosts.get(i), ports.get(i))).collect(Collectors.toList());
    return new ShardedJedisPool(defaultJedisPoolConfig(), list);
}
 
Example #18
Source File: JedisDemo.java    From JavaTutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 多机分布式+连接池。
 */
private static void shardPool() {
    // 生成多机连接信息列表
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add( new JedisShardInfo("127.0.0.1", 6379) );
    shards.add( new JedisShardInfo("192.168.56.102", 6379) );
    
    // 生成连接池配置信息
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(10);
    config.setMaxTotal(30);
    config.setMaxWaitMillis(3*1000);
    
    // 在应用初始化的时候生成连接池
    ShardedJedisPool pool = new ShardedJedisPool(config, shards);
    
    // 在业务操作时,从连接池获取连接
    ShardedJedis client = pool.getResource();
    try {
        // 执行指令
        String result = client.set("key-string", "Hello, Redis!");
        System.out.println( String.format("set指令执行结果:%s", result) );
        String value = client.get("key-string");
        System.out.println( String.format("get指令执行结果:%s", value) );
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        // 业务操作完成,将连接返回给连接池
        if (null != client) {
            pool.returnResource(client);
        }
    } // end of try block
    
    // 应用关闭时,释放连接池资源
    pool.destroy();
}
 
Example #19
Source File: SpringDataRedis.java    From howsun-javaee-framework with Apache License 2.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	JedisShardInfo jedisShardInfo1 = new JedisShardInfo(ip1);
	jedisShardInfo1.setPassword(JedisConstant.password);
	JedisShardInfo jedisShardInfo2 = new JedisShardInfo(ip2);
	jedisShardInfo2.setPassword(JedisConstant.password);

	List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
	jedisShardInfos.add(jedisShardInfo1);
	jedisShardInfos.add(jedisShardInfo2);

	JedisPoolConfig poolConfig = new JedisPoolConfig();
	poolConfig.setMaxActive(JedisConstant.maxActive);
	poolConfig.setMaxIdle(JedisConstant.maxIdle);
	poolConfig.setMaxWait(JedisConstant.maxWait);
	poolConfig.setTestOnBorrow(JedisConstant.testOnBorrow);
	poolConfig.setTestOnReturn(JedisConstant.testOnReturn);


	ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig, jedisShardInfos);

	JedisConnectionFactory factory = new JedisConnectionFactory(jedisShardInfo1);
	StringRedisTemplate template = new StringRedisTemplate(factory);
	for (int i = 0; i < 2000; i++) {
		String key = "howsun_" + i;
		BoundValueOperations<String, String> v = template.boundValueOps(key);
		//jedis.set(key, UUID.randomUUID().toString());
		System.out.println(key + "\t" + v.get() + "\t" + factory.getHostName());
	}

}
 
Example #20
Source File: RedisShardedPool.java    From jea with Apache License 2.0 5 votes vote down vote up
@Override
public void addRedisServer(String address) {
	// TODO Auto-generated method stub
	String[] addrs = address.split(" ");
	for(String addr : addrs){
		String[] arg = addr.split(":");
		JedisShardInfo shardInfo = new JedisShardInfo(arg[0], Integer.valueOf(arg[1]));
		listServer.add(shardInfo);
		shardInfo = null;
	}
}
 
Example #21
Source File: RedisExample.java    From java-platform with Apache License 2.0 5 votes vote down vote up
public void testShardSimplePool() {// 12.642秒
	JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379);
	jedis.setPassword("b840fc02d52404542994");

	List<JedisShardInfo> shards = Arrays.asList(jedis);
	ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);

	ShardedJedis sharding = pool.getResource();

	long start = System.currentTimeMillis();
	for (int i = 0; i < 1000; i++) {
		sharding.set("n" + i, "n" + i);
		System.out.println(i);
	}
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	sharding.disconnect();
	pool.destroy();
	try {
		Closeables.close(sharding, true);
		Closeables.close(pool, true);
	} catch (IOException e) {
		e.printStackTrace();
	}

}
 
Example #22
Source File: ShardedRedisCache.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * ShardedRedisCache
 */
public ShardedRedisCache() {
    String cacheModel = PropertyHolder.getProperty("cache.model");
    if (CACHE_MODEL.equals(cacheModel)) {
        Address[] addresses = getAddresses();
        String passwd = CommonUtil.isNotEmpty(addresses) ? addresses[0].getPassword() : null;
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(addresses.length);
        for (Address addr : addresses) {
            JedisShardInfo jedisShardInfo = new JedisShardInfo(addr.getHost(), addr.getPort());
            jedisShardInfo.setPassword(passwd);
            shards.add(jedisShardInfo);
        }
        shardedPool = new ShardedJedisPool(getConfig(), shards);
    }
}
 
Example #23
Source File: JedisConstructorWithShardInfoArgInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    String redisConnInfo;
    JedisShardInfo shardInfo = (JedisShardInfo) allArguments[0];
    redisConnInfo = shardInfo.getHost() + ":" + shardInfo.getPort();
    objInst.setSkyWalkingDynamicField(redisConnInfo);
}
 
Example #24
Source File: RedisClientPool.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
private ShardedJedisPool createJedisPool(final RedisConfig conf) {
    Assert.notNull(conf);
    try {
        final String[] hostAndports = conf.getHostNames().split(";");
        final List<String> redisHosts = Lists.newArrayList();
        final List<Integer> redisPorts = Lists.newArrayList();
        for (int i = 0; i < hostAndports.length; i++) {
            final String[] hostPort = hostAndports[i].split(":");
            redisHosts.add(hostPort[0]);
            redisPorts.add(Integer.valueOf(hostPort[1]));
        }

        final List<JedisShardInfo> shards = Lists.newArrayList();
        for (int i = 0; i < redisHosts.size(); i++) {
            final String host = (String) redisHosts.get(i);
            final Integer port = (Integer) redisPorts.get(i);
            Integer timeout = conf.getTimeOut();
            if (timeout == null || timeout < 0) {
                timeout = DEFAULT_TIMEOUT;
            }
            
            JedisShardInfo si = new JedisShardInfo(host, port.intValue(), timeout);
            shards.add(si);
        }

        return new ShardedJedisPool(getJedisPoolConfig(conf), shards, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    }
}
 
Example #25
Source File: ShardedJedisClientFactoryBean.java    From easyooo-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	notNull(connectionString, "Property 'connectionString' is required");
	List<HostAndPort> haps = HostAndPort.fromStringArray(connectionString);
	List<JedisShardInfo> jsi = new ArrayList<>();
	for (HostAndPort hap : haps) {
		jsi.add(new JedisShardInfo(hap.getHost(), hap.getPort()));
	}
	shardedJedisPool = new ShardedJedisPool(this, jsi);
}
 
Example #26
Source File: RedisExample.java    From java-platform with Apache License 2.0 5 votes vote down vote up
public void testShardPipelinnedPool() {// 0.124秒
	JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379);
	jedis.setPassword("b840fc02d52404542994");

	List<JedisShardInfo> shards = Arrays.asList(jedis);
	ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);

	ShardedJedis sharding = pool.getResource();
	ShardedJedisPipeline pipeline = sharding.pipelined();

	long start = System.currentTimeMillis();
	for (int i = 0; i < 1000; i++) {
		pipeline.set("n" + i, "n" + i);
		System.out.println(i);
	}
	pipeline.syncAndReturnAll();
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	sharding.disconnect();
	pool.destroy();

	try {
		Closeables.close(sharding, true);
		Closeables.close(pool, true);
	} catch (IOException e) {
		e.printStackTrace();
	}

}
 
Example #27
Source File: SetEndPointInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private String getEndPoint(Object[] args) {
    // first arg is host
    final Object argZero = args[0];
    if (argZero instanceof String) {
        return EndPointUtils.getEndPoint(args);
    } else if (argZero instanceof URI) {
        final URI uri = (URI) argZero;
        return HostAndPort.toHostAndPortString(uri.getHost(), uri.getPort());
    } else if (argZero instanceof JedisShardInfo) {
        final JedisShardInfo info = (JedisShardInfo) argZero;
        return HostAndPort.toHostAndPortString(info.getHost(), info.getPort());
    }
    return "Unknown";
}
 
Example #28
Source File: JedisTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void connectWithShardInfo() {
  JedisShardInfo shardInfo = new JedisShardInfo("localhost", Protocol.DEFAULT_PORT);
  shardInfo.setPassword("foobared");
  Jedis jedis = new Jedis(shardInfo);
  jedis.get("foo");
}
 
Example #29
Source File: JedisShardProvider.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
private List<JedisShardInfo> buildShardInfos(String[] servers, int timeout){
	List<JedisShardInfo> infos = new ArrayList<>();
	for (String server : servers) {
		String[] addrs = server.split(":");
		JedisShardInfo info = new JedisShardInfo(addrs[0], Integer.parseInt(addrs[1].trim()), timeout);
		infos.add(info);
	}
	
	return infos;
}
 
Example #30
Source File: RedisShardedCacheClient.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
@Override
public Pool<ShardedJedis> initPool() throws Exception {
    if (Check.isNullOrEmpty(getServers())) {
        throw new IllegalArgumentException("未指定redis服务器地址");
    }
    String[] hosts = getServers().trim().split("\\|");
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    for (String host : hosts) {
        String[] ss = host.split(":");
        //升级 redis  构造变化
        JedisShardInfo shard = new JedisShardInfo(ss[0], Integer.parseInt(ss[1]), connectTimeout, socketTimeout, 1);
        shards.add(shard);
    }
    return new ShardedJedisPool(getPoolConfig(), shards, Hashing.MURMUR_HASH);
}