简体   繁体   English

spring 启动 2 的 Redis 运行状况检查挂起

[英]Redis health check for spring boot 2 hangs

I have implemented some redis stuff in my spring boot 2.1.5 application.我在我的 spring 启动 2.1.5 应用程序中实现了一些 redis 的东西。 It works fine.它工作正常。 I also want the health check for redis.我还想要 redis 的健康检查。 If I switch off the redis server the health check (actuator/health) hangs forever.如果我关闭 redis 服务器,运行状况检查(执行器/运行状况)将永远挂起。 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我在这里创建了这个问题的一个小演示: https://github.com/markuskruse/demo-redis-health-bug

Clone, run, stop redis, check health (wait forever), start redis (health returns).克隆,运行,停止 redis,检查运行状况(永远等待),启动 redis(运行状况返回)。

This is my gradle for redis:这是我的 gradle 用于 redis:

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

This is my application.yaml:这是我的应用程序。yaml:

spring:
   redis:
      timeout: 5000
      host: localhost

This is my RedisConfig.java这是我的 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根据github上的这个问题,这只是一个配置问题: 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).根据this jira ticket,它应该在spring boot 2.1.4中修复(我在2.1.5上)。 https://jira.spring.io/browse/DATAREDIS-918 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());
  }

}

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否则,您需要为LettuceClientConfiguration构造函数的第二个参数提供LettuceConnectionFactory ,您可以在那里配置ClientOptions并启用TimeoutOptions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM