Spring Boot 2的Redis运行状况检查挂起

时间:2019-11-20 07:41:10

标签: java spring-boot redis lettuce

我在Spring Boot 2.1.5应用程序中实现了一些Redis东西。工作正常。 我还希望对Redis进行健康检查。如果我关闭Redis服务器,则运行状况检查(执行器/运行状况)将永远挂起。 如何配置合理的超时时间?

我在这里创建了这个问题的小演示: https://github.com/markuskruse/demo-redis-health-bug

克隆,运行,停止Redis,检查运行状况(永远等待),启动Redis(运行状况返回)。

这是我对Redis的认可:

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

这是我的application.yaml:

spring:
   redis:
      timeout: 5000
      host: localhost

这是我的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;
  }
}

根据github上的这个问题,这仅仅是一个配置问题: https://github.com/spring-projects/spring-boot/issues/15542

根据此jira票证,应将其固定在弹簧靴2.1.4中(我在2.1.5上)。 https://jira.spring.io/browse/DATAREDIS-918

他们提到了我尝试过的解决方法:

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

它本身没有作用。我必须将它注入某个地方。谷歌搜索给了这个:

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

然后我得到了:

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

我也许可以解决这个问题。但是我认为我在做(根本上)错误的事情。它应该已经修复。

编辑:我添加了公共资源池,错误消失了,但是健康检查仍然永远挂起。

我也在下面尝试了此操作,

@Component
public class RedisConfigurer implements LettuceClientConfigurationBuilderCustomizer {

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

}

1 个答案:

答案 0 :(得分:1)

似乎您的问题出在您的手动Connection工厂配置中。

如果您删除了那部分,一切都会按预期进行。

否则,您需要为LettuceClientConfiguration构造函数的第二个参数提供一个LettuceConnectionFactory,然后可以在其中启用ClientOptions的情况下配置TimeoutOptions