0

I am using redis cache in my springboot application. Suddently it sarted giving the below exception:

ERROR 1876 --- [http-nio-8004-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisSystemException: Redis exception; nested exception is io.lettuce.core.RedisException: java.io.IOException: An existing connection was forcibly closed by the remote host] with root cause
java.io.IOException: An existing connection was forcibly closed by the remote host
at sun.nio.ch.SocketDispatcher.read0(Native Method) ~[na:1.8.0_261]
at sun.nio.ch.SocketDispatcher.read(Unknown Source) ~[na:1.8.0_261]
at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source) ~[na:1.8.0_261]
at sun.nio.ch.IOUtil.read(Unknown Source) ~[na:1.8.0_261]
at sun.nio.ch.SocketChannelImpl.read(Unknown Source) ~[na:1.8.0_261]
at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253) ~[netty-buffer-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1133) ~[netty-buffer-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350) ~[netty-transport-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) ~[netty-transport-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) ~[netty-transport-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) ~[netty-transport-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) ~[netty-transport-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.52.Final.jar!/:4.1.52.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.52.Final.jar!/:4.1.52.Final]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_261]

This is my redis configuration:

@Configuration
public class RedisConfig {

    @Autowired
    private Environment environment;
    @Bean
    public LettuceConnectionFactory redisConnectionFactory(){
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration(Objects.requireNonNull(
                environment.getProperty("redis.hostname")), Integer.parseInt(Objects.requireNonNull(
                        environment.getProperty("redis.port")))));
    }
}

Can anyone please help me to resolve the issue?

1 Answers1

0

I meet the same question. then find some function

1: it work for me

   lettuce:
      cluster:
        refresh:
          adaptive: true
          period: 20

2: change server ssh config ,add the fllow contnet, can't work for me

ClientAliveInterval 600      
ClientAliveCountMax 10

3: change the redis conf ,set tcp-keepalive value bettwen 1 and 50,I can't try. I also not think is a good idea.

4: update the redis version
5: use jedis pool

6: use regular task to keep netty link

@Component
@Slf4j
public class LettuceConnectionValidTask {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Scheduled(cron="0/2 * * * * ?")
public void task() {
    if(redisConnectionFactory instanceof LettuceConnectionFactory){
        LettuceConnectionFactory c=(LettuceConnectionFactory)redisConnectionFactory;
        c.validateConnection();
    }
}
}

7: trun on connect validate

@Component
@Slf4j
public class LettuceConnectionValidConfig implements InitializingBean {
@Autowired
private RedisConnectionFactory redisConnectionFactory;

@Override
public void afterPropertiesSet() throws Exception {
    if(redisConnectionFactory instanceof LettuceConnectionFactory){
        LettuceConnectionFactory c=(LettuceConnectionFactory)redisConnectionFactory;
        c.setValidateConnection(true);
    }
}
}