java代码连接redis集群代码,需要密码

2019-09-11 17:06发布

public static void main(String[] args) {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    // 最大连接数
    poolConfig.setMaxTotal(100);
    // 最大空闲数
    poolConfig.setMaxIdle(10);
    // 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:
    // Could not get a resource from the pool
    poolConfig.setMaxWaitMillis(10000);

    JedisShardInfo jedisShardInfo1 = new JedisShardInfo("172.10.1.120", 9000);
    JedisShardInfo jedisShardInfo2 = new JedisShardInfo("172.10.1.121", 9001);
    JedisShardInfo jedisShardInfo3 = new JedisShardInfo("172.10.1.122", 9002);
    jedisShardInfo1.setPassword("xxxx");
    jedisShardInfo2.setPassword("xxxx");
    jedisShardInfo3.setPassword("xxxx");
    List<JedisShardInfo> list2 = new LinkedList<JedisShardInfo>();
    list2.add(jedisShardInfo1);
    list2.add(jedisShardInfo2);
    list2.add(jedisShardInfo3);

    ShardedJedisPool pool2 = new ShardedJedisPool(poolConfig, list2);

    ShardedJedis jedis2 = pool2.getResource();

    String name = jedis2.get("test_tmp");
    System.out.println(name);
    jedis2.set("test_tmp", "18");
    System.out.println(jedis2.get("test_tmp"));

    try {
        jedis2.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

 

标签: