当前位置:   article > 正文

Redis经常会报出RedisConnectionFailureException: java.net.SocketException: Broken pipe_redistemplate broken pipe

redistemplate broken pipe

【背景】: 在测试和生产环境上,偶尔会出现如下的redis异常。
在APP层面的现象就是界面提示:”网络连接异常”,但下次点击APP的时候,又可以正常使用了。

11:28:29 INFO  - get data from redis, key = c15aad89-4a1a-4cb0-82a5-2027b990c1ca
11:28:29 WARN  - /market/info/eForum/getIndexList
    org.springframework.data.redis.RedisConnectionFailureException: java.net.SocketException: Broken pipe; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:67) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:41) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:212) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.connection.jedis.JedisConnection.get(JedisConnection.java:1117) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.core.DefaultValueOperations$1.inRedis(DefaultValueOperations.java:46) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:54) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:204) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:166) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:88) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    at com.pa.market.common.util.CommonUtil.getObjectFromRedis(CommonUtil.java:635) ~[pa-market-common-1.1.jar:1.1]
    at com.pa.market.common.util.UserTokenUtil.getUserInfoExt(UserTokenUtil.java:77) ~[pa-market-common-1.1.jar:1.1]
    at com.pa.market.common.util.UserTokenUtil.getUserInfo(UserTokenUtil.java:60) ~[pa-market-common-1.1.jar:1.1]
    at com.pa.market.common.util.UserTokenUtil.getUserInfo(UserTokenUtil.java:44) ~[pa-market-common-1.1.jar:1.1]
    at com.pa.market.common.util.CountFilter.doFilter(CountFilter.java:77) ~[pa-market-common-1.1.jar:1.1]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1668) ~[jetty-servlet-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676) ~[jetty-servlet-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:89) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676) ~[jetty-servlet-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676) ~[jetty-servlet-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676) ~[jetty-servlet-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581) ~[jetty-servlet-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) ~[jetty-security-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511) ~[jetty-servlet-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.Server.handle(Server.java:524) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:319) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:253) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273) ~[jetty-io-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) ~[jetty-io-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93) ~[jetty-io-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303) ~[jetty-util-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148) ~[jetty-util-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136) ~[jetty-util-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671) ~[jetty-util-9.3.11.v20160721.jar:9.3.11.v20160721]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589) ~[jetty-util-9.3.11.v20160721.jar:9.3.11.v20160721]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe
    at redis.clients.jedis.Connection.flush(Connection.java:291) ~[jedis-2.7.3.jar:?]
    at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:214) ~[jedis-2.7.3.jar:?]
    at redis.clients.jedis.BinaryJedis.get(BinaryJedis.java:159) ~[jedis-2.7.3.jar:?]
    at org.springframework.data.redis.connection.jedis.JedisConnection.get(JedisConnection.java:1115) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    ... 46 more
Caused by: java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method) ~[?:1.8.0_91]
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) ~[?:1.8.0_91]
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153) ~[?:1.8.0_91]
    at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:31) ~[jedis-2.7.3.jar:?]
    at redis.clients.util.RedisOutputStream.flush(RedisOutputStream.java:213) ~[jedis-2.7.3.jar:?]
    at redis.clients.jedis.Connection.flush(Connection.java:288) ~[jedis-2.7.3.jar:?]
    at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:214) ~[jedis-2.7.3.jar:?]
    at redis.clients.jedis.BinaryJedis.get(BinaryJedis.java:159) ~[jedis-2.7.3.jar:?]
    at org.springframework.data.redis.connection.jedis.JedisConnection.get(JedisConnection.java:1115) ~[spring-data-redis-1.7.3.RELEASE.jar:?]
    ... 46 more
11:28:33 INFO  - get data from redis, key = c15aad89-4a1a-4cb0-82a5-2027b990c1ca
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

【问题原因】

Redis底层也创建了连接池,获取到了失效的连接池,并且Redis客户端尝试通过此连接池跟服务端进行通信, 导致抛出上面的异常。

【解决办法】

Redis配置的连接池使用jar包commons-pool-2.4.2.jar方式。

private boolean testOnCreate = false;
private boolean testOnBorrow = false;
private boolean testOnReturn = false;
private boolean testWhileIdle = false;
  • 1
  • 2
  • 3
  • 4

其中有如上述四个属性参数。只要配置 testOnBorrow = true 和 testWhileIdle = true 就可以。 其中 testOnBorrow = true 是获取链接的时候对链接的有效性进行检查,会影响效率,在高并发的前提下。所以一般只是配置 testWhileIdle = true , 这个是在闲暇的时候进行检查,去除无效的链接。

【思考】

如果采用spring boot自动集成的方式,如何去设置testOnBorrow 这个参数呢? 因为默认情况下,testOnBorrow 是false方式。难不成为了修改这个参数,重新去自定义一个Redis的Bean?? 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/160096
推荐阅读
相关标签
  

闽ICP备14008679号