雷迪斯3.0.5
Spring数据Redis 1.3.6
绝地武士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.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37)
在org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37)
在 org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:182)
在 org.springframework.data.redis.connection.jedis.JedisConnection.get(JedisConnection.java:1087)
在 org.springframework.data.redis.connection.DefaultStringRedisConnection.get(DefaultStringRedisConnection.java:276)
在 org.springframework.data.redis.core.DefaultValueOperations$1.inRedis(DefaultValueOperations.java:46)
在 org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:50)
在 org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:190)
在 org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)
在 org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
在 org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)
我读过其他线程谈论使用单线程进行读/写。但在我们的例子中很难使用单线程。另外,根据 RedisTemplate 文档,它是线程安全的。
该问题是偶尔出现的,我们无法在任何开发/测试/uat 环境中重现。因此无法找到相同的确切原因。
我们做错了什么?
我们已经能够重现该问题,原因是 Redis 中的“timeout=30”设置。
Scenario
- 连接空闲 30 秒,Redis 将其终止。
- 在应用程序中的“Redis 连接工厂”检测到损坏之前
连接,它获得读或写请求的分配
- 代码
尝试使用此连接,但由于它已损坏,因此无法
发送读/写命令。因此我们得到“JedisConnectionException:
流意外结束”异常
Solution
- 将 Redis 超时设置为零
- 使用自定义 JedisPoolConfig 集
minEvictableIdleTimeMillis 为所需值。这将确保
空闲连接从Jedis连接池中释放
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)