spring boot redis operation throw broken pipe错误

时间:2017-03-28 07:08:26

标签: java spring spring-boot redis broken-pipe

我们在spring boot项目中使用redis。运行一段时间后,redis操作可能会抛出一个损坏的管道错误,但有时它会成功。重新启动服务将解决此问题,但这不是一个好主意。

我无法说出它发生的原因。似乎池中的某些redis连接无法使用,但是没有关闭并从池中逐出。

我的问题是:

  • 导致管道损坏的可能原因?
  • 如果长时间没有redis操作,池中的空闲连接是否会变得无法使用?
  • 当发生破裂的管道错误时,是否会关闭连接并从池中逐出?

的pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

application.yml:

spring:
  redis:
    database: 0
    host: ${REDIS_HOST:127.0.0.1}
    password: ${REDIS_PASSWORD:password}
    port: ${REDIS_PORT:6379}
    timeout: ${REDIS_TIMEOUT:1000}
    pool:
      max-active: ${REDIS_MAX_ACTIVE:100}
      max-wait: ${REDIS_MAX_WAIT:500}
      max-idle: ${REDIS_MAX_IDLE:20}
      min-idle: ${REDIS_MIN_IDLE:5}

错误消息:

org.springframework.data.redis.RedisConnectionFailureException: java.net.SocketException: Broken pipe (Write failed); nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:67) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:41) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:212) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.connection.jedis.JedisConnection.hSet(JedisConnection.java:2810) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.core.DefaultHashOperations$9.doInRedis(DefaultHashOperations.java:173) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:204) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:166) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:88) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    at org.springframework.data.redis.core.DefaultHashOperations.put(DefaultHashOperations.java:170) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
    at redis.clients.jedis.Connection.flush(Connection.java:291) ~[jedis-2.8.2.jar!/:na]
    at redis.clients.jedis.Connection.getIntegerReply(Connection.java:220) ~[jedis-2.8.2.jar!/:na]
    at redis.clients.jedis.BinaryJedis.hset(BinaryJedis.java:749) ~[jedis-2.8.2.jar!/:na]
    at org.springframework.data.redis.connection.jedis.JedisConnection.hSet(JedisConnection.java:2808) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]
    ... 115 common frames omitted
Caused by: java.net.SocketException: Broken pipe (Write failed)
    at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_111]
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) ~[na:1.8.0_111]
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153) ~[na:1.8.0_111]
    at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:52) ~[jedis-2.8.2.jar!/:na]
    at redis.clients.util.RedisOutputStream.flush(RedisOutputStream.java:216) ~[jedis-2.8.2.jar!/:na]
    at redis.clients.jedis.Connection.flush(Connection.java:288) ~[jedis-2.8.2.jar!/:na]
    ... 118 common frames omitted

2 个答案:

答案 0 :(得分:2)

这可能由于多种原因而发生,其中一个原因可能是当您使用长期连接时(例如,在应用程序启动时连接到Redis,然后反复使用连接)。

可以做的一些事情是  1.如果连接断开,则重新连接(需要一些try / catch魔法来防止错误传播到您的应用程序逻辑)  或者更好的是使用
  TestOnBorrow - 当您要求提供资源时发送PING请求   TestOnReturn - 将资源返回池时发送PING   TestWhileIdle - 从池中的空闲资源发送定期PINGS 2.在您需要连接时连接并在之后断开连接

关于
如果长时间没有redis操作,池中的空闲连接是否会变得无法使用?

maxidle意味着在任何给定时间系统允许'maxIdle'许多连接空闲,其余连接将不断检查,关闭并返回到池中。 我不知道空闲连接无法使用的原因。无论如何,通过使用上述方法可以摆脱这种情况。

答案 1 :(得分:1)

回答我的问题:

为什么管道出现故障?

TransactionSynchronizationManager将在线程中保存RedisConnection,并且不会将其关闭或将其返回到池,请参阅RedisTemplate.java和RedisConnectionUtils.java。重新启动redis服务器后,对线程中持有的RedisConnection的操作将丢失管道错误。

如何解决?

为所有redis操作添加try / catch,如果发生错误,从线程解除绑定,并从池中获取新连接并再次执行redis操作。

private static final ExceptionTranslationStrategy EXCEPTION_TRANSLATION =
        new FallbackExceptionTranslationStrategy(JedisConverters.exceptionConverter());

public Object req(RedisRequest req) {
    try {
        return req.request();
    } catch (Exception ex) {
        if (ex instanceof NullPointerException) {
            throw ex;
        }
        DataAccessException exception = EXCEPTION_TRANSLATION.translate(ex);
        if (exception instanceof RedisConnectionFailureException) {
            RedisConnectionUtils.unbindConnection(factory);
            /** retry again */
            return req.request();
        } else {
            throw ex;
        }
    }
}