how to configure timeout of JedisConnectionFactory

2020-06-18 02:41发布

问题:

I'm using springboot, I'm confused to configure the timeout to connect redis.

Currently, my configurations are:

application.yml:

spring.redis.host: myhost
spring.redis.port: 6379
spring.redis.pool.max-idle: 8
spring.redis.pool.min-idle: 0
spring.redis.pool.max-active: 8
spring.redis.pool.max-wait: -1

StringRedisDao.java:

@Autowired
public StringRedisDao(final StringRedisTemplate template, final ObjectMapper mapper) {
    if (template.getConnectionFactory() instanceof JedisConnectionFactory) {
        ((JedisConnectionFactory) template.getConnectionFactory()).getShardInfo().setTimeout(5000);
        ((JedisConnectionFactory) template.getConnectionFactory()).setTimeout(5000);
    }
    this.template = template;
    this.mapper = mapper;
}

I use wireshark to capture the packets, and I found that the redis was disconnected after 2 seconds, not 5 seconds as I set in the code above.

Because of this, I cannot perform a requests that the query time of redis is more than 2 seconds.

Please, how can I do this?

回答1:

There is also a configuration setting you can put in application.properties:

spring.redis.timeout=5000