- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的redis服务器在一个VMWare服务器中,我可以通过cli从telnet连接redis服务器:
C:\Users\Administrator>redis-cli -h 192.168.0.243 -p 6379
192.168.0.243:6379> 获取名称
(错误)NOAUTH 需要身份验证。
192.168.0.243:6379> 授权根
好的
192.168.0.243:6379> 获取名称
“泳池zzzzqqqqq”
192.168.0.243:6379>
在我的java代码中,我可以通过Jedis成功连接到redis服务器。
绝地演示:
Jedis jedis = new Jedis(constr) ;
jedis.auth("root");
String output ;
jedis.set( "hello", "world" ) ;
output = jedis.get( "hello") ;
System. out.println(output) ;
但是我无法通过 JedisPool 连接到 redis 服务器:这是代码:
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);
}
}
...
Redis演示
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
你能告诉我为什么吗?
提前致谢!
最佳答案
我不确定您是否可以在 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);
}
}
关于java - JedisPool 无法连接到 telnet redis 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42223656/
我正在使用jedis 客户端 将redis 与ofbiz 集成。 不同的应用程序正在使用一个 redis 服务器。我的问题是 默认情况下,JedisPool 将保持多少个连接。 如果我创建多个 Jed
我在生产环境中使用 jedisPool。当我启动服务器处理请求时,redis 池工作正常。但过了一会儿,日志文件开始告诉我:“JedisException:无法从池中获取资源。” 这是我的配置: re
我是 Redis 新手,在我的多线程应用程序中使用 JedisPool。 JedisPoolConfig config = new JedisPoolConfig(); config.setMax
我有一个创建 jedispool 的函数, final JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.s
我很困惑:用法:try(Pipeline pipeline = jedisPool.getResource().pipelined())它会关闭管道和jedis实例吗?或者只是关闭管道? 我在多线程中
这可能很简单,但我无法在任何地方找到答案(在包含的代码库中)。 我有一个简单的 Redis 部署,主 + 从。我应该如何配置 JedisPool 以使用 master 进行写入并使用 slave/ma
我正在使用 jedis 连接到 redis 服务器。 Redis ip 值配置在一个单独的文件中,我试图在初始化期间加载该文件 private static JedisPool pool; publi
我在这里尝试创建新的 JedisPool,但出现以下错误。 The constructor JedisPool(GenericObjectPool$Config, String, int, int)
我的应用程序使用 AWS 上的 ElastiCache 进行缓存。我们当前的设置使用基本的 Redis 集群,没有分片或故障转移。我们现在需要迁移到启用了分片、故障转移等的集群 Redis 弹性缓存。
我在我的 spark 流应用程序中使用 sparkContext.broadcast 来共享 redis 连接池 (JedisPool)。 代码如下: lazy val redisPool = {
这是我的JedisFactory import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; c
我的redis服务器在一个VMWare服务器中,我可以通过cli从telnet连接redis服务器: C:\Users\Administrator>redis-cli -h 192.168.0.243
查看 redis client list 的输出,我看到目前有 600 个活跃的客户端,并且还在继续增长。这是一个输出片段: id=285316 addr=x.x.x.x:55699 fd=14131
我刚刚开始在我的 Play 应用程序中使用 Redis,它运行良好。查看我在 Internet 上找到的一些示例,我想知道以下两个代码片段之间有什么区别: import redis.clients.j
我正在使用 Spring Boot 应用程序实现 Redis Jedis。我正在使用以下依赖项和配置。在执行此操作时,我收到错误“考虑在配置中定义“redis.clients.jedis.JedisP
我是一名优秀的程序员,十分优秀!