【问题标题】:Broken pipe while using Jedis Pool使用 Jedis Pool 时管道破裂
【发布时间】:2017-07-31 19:41:19
【问题描述】:

我正在使用 Jedis 在 Redis 中执行大量插入/读取。 Redis 服务器使用默认配置。 当我开始使用几个线程时出现问题,异常是:

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Pipe quebrado(写入失败)

我已经搜索了很多关于这个问题的信息,但找不到它的原因或者它已经解决了。我用来执行这些测试的代码如下:

public class RedisFacade {

private static RedisFacade instancia = null;
// Initialize the Connection
final JedisPoolConfig poolConfig = buildPoolConfig();
JedisPool pool = new JedisPool(poolConfig, "localhost");
Jedis jedis;
int i = 0;

private RedisFacade() {
}

public static RedisFacade getInstancia() {
    if (instancia == null) {
        instancia = new RedisFacade();
    }
    return instancia;
}

// retorna um cliente jedis da pool
public Jedis getDB() {
    if (jedis == null) {
        jedis = pool.getResource();
    }
    return jedis;
}

//inserting
public void insert(Document d) {
    String key = i + d.getString("date") + d.getString("time");
    String value = d.toString();
    this.getDB().set(key, value);
    i++;
}

//reading
public void read(String date, String time) {
    Object doc = this.getDB().get(i + date + time);
    i++;
    System.out.println(doc);
}

public void destroyPool() {
    this.pool.destroy();
}

private JedisPoolConfig buildPoolConfig() {
    final JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(1100);
    poolConfig.setMaxIdle(16);
    poolConfig.setMinIdle(16);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnReturn(true);
    poolConfig.setTestWhileIdle(true);poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
    poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
    poolConfig.setNumTestsPerEvictionRun(3);
    poolConfig.setBlockWhenExhausted(true);
    return poolConfig;
}}

【问题讨论】:

    标签: java redis pool jedis


    【解决方案1】:

    好像是超时问题。

    看到这个帖子:Configure Jedis timeout

    还有这个讨论:https://github.com/xetorthio/jedis/issues/185

    所以我会尝试使用超时参数实例化JedisPool (即https://github.com/xetorthio/jedis/blob/master/src/main/java/redis/clients/jedis/JedisPool.java#L201,但还有很多其他的构造函数)

    并在 redis 中设置CONFIG SET timeout 600(例如超时 10 分钟)。

    编辑

    JedisPool 超时似乎以毫秒为单位。

    【讨论】:

    • 非常感谢您对我的帮助!我尝试了您的所有提示,但无法解决问题...但后来我尝试关闭从池中获取的资源并且它起作用了。
    【解决方案2】:

    在尝试实现新的构造函数、池和客户端的新配置后,我尝试了一种简单的方法来解决问题:关闭我从池中获取的资源。为此,我更改了以下代码:

    public Jedis getDB() {
        jedis = pool.getResource();
        return jedis;
    }
    
    //cria um _id pra ser usado novamente quando for buscar os documentos
    public void insert(Document d) {
        String key = "key" + i;
        String value = d.toString();
        Jedis jedis = this.getDB();
        jedis.set(key, value);
        jedis.close();
        i++;
    }
    
    //busca pelo _id
    public void read() {
        Jedis jedis = this.getDB();
        Object doc = jedis.get("key" + i);
        jedis.close();
        i++;
        System.out.println(doc);
    }
    

    更改代码后,我计划的服务开始工作,所以我会接受这个作为解决方案。

    【讨论】:

      猜你喜欢
      • 2013-05-24
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 2021-09-26
      • 2011-08-16
      相关资源
      最近更新 更多