2017-02-14 11 views
1

Моего Redis сервер находится в VMWare Server, и я могу подключить Redis сервера из телнета кли:JedisPool не может подключиться к серверу Telnet REDIS

C:\Users\Administrator>redis-cli -h 192.168.0.243 -p 6379 192.168.0.243:6379> get name (error) NOAUTH Authentication required. 192.168.0.243:6379> auth root OK 192.168.0.243:6379> get name "pool zzzzqqqqq" 192.168.0.243:6379>

В моем Java коде, я могу подключиться к Redis сервер успешно Jedis.

JedisDemo:

Jedis jedis = new Jedis(constr) ; 
    jedis.auth("root"); 
    String output ; 
    jedis.set("hello", "world") ; 
    output = jedis.get("hello") ; 
    System. out.println(output) ; 

Но я не могу получить подключение к Redis сервер, JedisPool: вот код:

RedisUtils.java:

public class RedisUtils { 

private JedisPool pool = null; 

private final static String REDIS_IP = "192.168.0.243"; 

private final static int REDIS_PORT = 6379; 

public RedisUtils() { 
    this(REDIS_IP,REDIS_PORT); 
} 

public RedisUtils(String ip, int prot) { 
    if (pool == null) { 
     JedisPoolConfig config = new JedisPoolConfig();  
     config.setMaxTotal(100); 
     config.setMaxIdle(10); 
     config.setMaxWaitMillis(50 * 1000); 
     config.setTestOnBorrow(true); 
     config.setTestOnReturn(true); 
     config.setTestWhileIdle(true); 
     pool = new JedisPool(config, ip, prot, 100000,"root"); 


    } 
} 

public RedisUtils(JedisPoolConfig config ,String ip, int prot){ 
    if (pool == null) { 
     pool = new JedisPool(config,ip,prot,10000); 
    } 
} 

public RedisUtils(JedisPoolConfig config ,String ip, int prot ,int timeout){ 
    if (pool == null) { 
     pool = new JedisPool(config,ip,prot,timeout); 
    } 
} 

public RedisUtils(JedisPool pool){ 
    if (this.pool == null) { 
     this.pool = pool; 
    } 
} 

public String get(String key){ 
    Jedis jedis = null; 
    String value = null; 
    try { 
     jedis = pool.getResource(); 
     value = jedis.get(key); 
    } catch (Exception e) { 
     pool.close(); 
     e.printStackTrace(); 
    } finally { 
     returnResource(pool, jedis); 
    } 
    return value; 
} 

public String set(String key,String value){ 
    Jedis jedis = null; 
    try { 
     jedis = pool.getResource(); 
     return jedis.set(key, value); 
    } catch (Exception e) { 
     pool.close(); 
     e.printStackTrace(); 
     return "0"; 
    } finally { 
     returnResource(pool, jedis); 
    } 
} 
... 

RedisDemo

RedisUtils redisUtils = new RedisUtils(); 

    redisUtils.set("name", "pool zzzzqqqqq"); 

    System.out.println("get name : "+redisUtils.get("name")); 

Результат:

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 
at redis.clients.util.Pool.getResource(Pool.java:53) 
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226) 
at com.mbg.redis.RedisUtils.get(RedisUtils.java:122) 
at com.mbg.redis.RedisDemo.poolTest(RedisDemo.java:25) 
at com.mbg.redis.RedisDemo.main(RedisDemo.java:35) 
Caused by: java.lang.IllegalStateException: Pool not open 
at org.apache.commons.pool2.impl.BaseGenericObjectPool.assertOpen(BaseGenericObjectPool.java:672) 
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:412) 
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) 
at redis.clients.util.Pool.getResource(Pool.java:49) 
... 4 more 

Вы можете мне сказать, почему?
Спасибо заранее!

ответ

0

Я не уверен, что вы можете использовать параметр пароля в своем экземпляре JedisPool для имитации команды AUTH.

Попробуйте это:

public String set(String key,String value){ 
    Jedis jedis = null; 
    try { 
     jedis = pool.getResource(); 
     jedis.auth("root"); 
     return jedis.set(key, value); 
    } catch (Exception e) { 
     pool.close(); 
     e.printStackTrace(); 
     return "0"; 
    } finally { 
     returnResource(pool, jedis); 
    } 
} 
+0

Но это не работает, все равно спасибо. – tony

 Смежные вопросы

  • Нет связанных вопросов^_^