小编典典

Spring Data Redis JedisConnectionException:流的意外结束

redis

Redis 3.0.5
Spring数据Redis 1.3.6
jedis 2.6.3-
我们的Web应用程序,它通过pub / sub从redis接收数据。
-还以键/值对的形式对redis进行数据读/写。
-读/写发生在侦听器线程,独立监视线程和http请求线程上。
-我们对侦听器和Redis模板使用了相同的连接工厂
-我们的Redis服务器已配置“ timeout = 30”

<bean id="jedisConnectionFactory"
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="nnnn"></property>
    <property name="port" value="nnnn"></property>
    <property name="password" value="****"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<bean id="redisContainer"
    class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
    <property name="connectionFactory" ref="jedisConnectionFactory" />
    <property name="messageListeners">
        <map>
            <entry key-ref="messageListener">
                <bean class="org.springframework.data.redis.listener.ChannelTopic">
                    <constructor-arg value="topic_name" />
                </bean>
            </entry>
        </map>
    </property>
    <property name="taskExecutor" ref="redisTaskExecutor" />
    <property name="subscriptionExecutor" ref="redisSubTaskExecutor" />
</bean>
<bean id="redisTaskExecutor"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="threadNamePrefix" value="RedisListenerThread"></property>
    <property name="corePoolSize" value="1" />
    <property name="maxPoolSize" value="1" />
</bean>
<bean id="redisSubTaskExecutor"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="threadNamePrefix" value="RedisSubscribeThread"></property>
    <property name="corePoolSize" value="1" />
    <property name="maxPoolSize" value="1" />
</bean>
<bean id="messageListener"
    class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
    <constructor-arg index="0">
        <bean class="my.data.Receiver" />
    </constructor-arg>
    <constructor-arg index="1"><value>receive</value></constructor-arg>
</bean>

在读取数据的过程中,有时我们在生产上会遇到以下问题。

org.springframework.data.redis.RedisConnectionFailureException:流的意外结束。嵌套异常是redis.clients.jedis.exceptions.JedisConnectionException:流的意外末尾。org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:47)org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:36)org.springframework
org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37)的.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37)org.springframework.data.redis.connection.jedis.JedisConnection.convert
(JedisConnection.java:182)在组织。

我已经读过其他谈论使用单线程进行读/写的线程。但是在我们的情况下,很难使用单线程。同样根据RedisTemplate文档,它是线程安全的。问题偶尔出现,我们无法在任何dev
/ testing / uat环境中复制。因此无法找到相同的确切原因。我们做错了什么?


阅读 330

收藏
2020-06-20

共1个答案

小编典典

我们已经能够重现该问题,原因是Redis中的“超时= 30”设置。

情境

  1. 连接空闲30秒,Redis杀死该连接。
  2. 在应用程序中的“ Redis连接工厂”检测到断开的连接之前,它将获得读取或写入请求的分配
  3. 代码尝试使用此连接,但是由于连接断开,因此无法发送用于读取/写入的命令。因此,我们得到“ JedisConnectionException:流的意外末尾”异常

  1. 将Redis超时设置为零
  2. 使用自定义JedisPoolConfig将minEvictableIdleTimeMillis设置为所需值。这将确保从Jedis连接池释放空闲连接
2020-06-20