Search code examples
javaspring-bootredislettuce

Redis health check for spring boot 2 hangs


I have implemented some redis stuff in my spring boot 2.1.5 application. It works fine. I also want the health check for redis. If I switch off the redis server the health check (actuator/health) hangs forever. How can I configure a sensible timeout?

I have created a little demo of this problem here: https://github.com/markuskruse/demo-redis-health-bug

Clone, run, stop redis, check health (wait forever), start redis (health returns).

This is my gradle for redis:

implementation 'org.springframework.boot:spring-boot-starter-data-redis'

This is my application.yaml:

spring:
   redis:
      timeout: 5000
      host: localhost

This is my RedisConfig.java

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {

  @Bean
  public LettuceConnectionFactory redisConnectionFactory(
      @Value("${spring.redis.host:localhost}") String redisHost) {
    RedisStandaloneConfiguration redisStandaloneConfiguration =
        new RedisStandaloneConfiguration(redisHost);
    return new LettuceConnectionFactory(redisStandaloneConfiguration);
  }

  @Bean
  public StringRedisTemplate redisTemplate(RedisConnectionFactory jedisConnectionFactory) {
    final StringRedisTemplate template = new StringRedisTemplate();
    template.setConnectionFactory(jedisConnectionFactory);
    template.afterPropertiesSet();
    return template;
  }
}

According to this issue on github, it is a mere configuration issue: https://github.com/spring-projects/spring-boot/issues/15542

According to this jira ticket, it should be fixed in spring boot 2.1.4 (I'm on 2.1.5). https://jira.spring.io/browse/DATAREDIS-918

They mention a workaround that I have tried:

  @Bean
  public ClientOptions clientOptions() {
    return ClientOptions.builder()
        .timeoutOptions(TimeoutOptions.enabled())
        .build();
  }

By itself, it had no effect. I have to inject it somewhere. Googling gave this:

  @Bean
  LettucePoolingClientConfiguration lettucePoolConfig(ClientOptions options, ClientResources dcr){
    return LettucePoolingClientConfiguration.builder()
        .clientOptions(options)
        .clientResources(dcr)
        .build();
  }

Then I get this:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration]: Factory method 'lettucePoolConfig' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
    ... 50 more
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
    at org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration$LettucePoolingClientConfigurationBuilder.<init>(LettucePoolingClientConfiguration.java:91)
    at org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.builder(LettucePoolingClientConfiguration.java:50)
    at com.ikea.cps.mhs.config.RedisConfig.lettucePoolConfig(RedisConfig.java:50)
    at com.ikea.cps.mhs.config.RedisConfig$$EnhancerBySpringCGLIB$$3804d114.CGLIB$lettucePoolConfig$3(<generated>)
    at com.ikea.cps.mhs.config.RedisConfig$$EnhancerBySpringCGLIB$$3804d114$$FastClassBySpringCGLIB$$ccabed80.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
    at com.ikea.cps.mhs.config.RedisConfig$$EnhancerBySpringCGLIB$$3804d114.lettucePoolConfig(<generated>)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 51 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 64 more

I can maybe work around this. But I am thinking that I am doing something (fundamentally) wrong. It should already be fixed.

Edit: I added the commons pool and the error goes away, but health check still hangs forever.

I also tried this below, to no effect.

@Component
public class RedisConfigurer implements LettuceClientConfigurationBuilderCustomizer {

  @Override
  public void customize(LettuceClientConfigurationBuilder builder) {
    builder.clientOptions(ClientOptions.builder()
        .timeoutOptions(TimeoutOptions.enabled(Duration.of(5, SECONDS))).build());
  }

}

Solution

  • It seems that your problem is in your manual Connection factory configuration.

    If you remove that part, everything should be fine as you expected.

    Otherwise you need to provide a LettuceClientConfiguration for the second argument of the LettuceConnectionFactory constructor and there you can configure ClientOptions with enabled TimeoutOptions