jjzjj

java - JedisPool 无法连接到 telnet redis 服务器

coder 2023-11-07 原文

我的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/

有关java - JedisPool 无法连接到 telnet redis 服务器的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  4. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  10. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

随机推荐