Redis 缓存指标在 SpringBoot 2.4.2 版中不起作用

时间:2021-02-24 20:29:56

标签: java spring-boot redis spring-data-redis spring-actuator

我们正在尝试向 Prometheus 公开我们的 redis 缓存指标。以下是我们所做的。

我们有一个类 CachingConfig 如下,

@Configuration
@EnableCaching
public class CachingConfig {

  private final Duration cacheEntryTtl;

  public CachingConfig(
      @Value("${spring.cache.redis.entryTtl}")
      final Duration cacheEntryTtl
  ) {
    this.cacheEntryTtl = cacheEntryTtl;
  }

  @Bean
  public CacheManager cacheManager(final RedisConnectionFactory redisConnectionFactory) {
    final Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
    cacheConfigurations.put("cacheA",cacheConfiguration(cacheEntryTtl));
    cacheConfigurations.put("cacheB",cacheConfiguration(cacheEntryTtl));

    return RedisCacheManager.builder(redisConnectionFactory)
        .cacheDefaults(cacheConfiguration(cacheEntryTtl))
        .withInitialCacheConfigurations(cacheConfigurations)
        .build();
  }
}

然后我们在类中使用 Redis 缓存,如下所示。

public class BusinessService {
    public static final String CACHE_A_NAME = "cacheA"
    private final BusinessServiceClient businessServiceClient;
    private final CacheManager cacheManager;
    private final CacheMetricsRegistrar cacheMetricsRegistrar;

    @PostConstruct
    public void postConstruct() {
        final Cache cache = cacheManager.getCache(CACHE_A_NAME);
        cacheMetricsRegistrar.bindCacheToRegistry(cache);
    }

    @Cacheable(cacheNames = CACHE_A_NAME)
    public Set<String> getOwnersOfProviderAccount(String para1, String para2) {
        return businessServiceClient.getResonponse(para1, para2);
    }
}

根据 this,我还在我们的 application.properties 文件中添加了以下几行。

spring.cache.type=redis
spring.cache.redis.enable-statistics=true

所以理论上,Redis 缓存指标应该能够工作,但是当我从以下 URL 检查我们的缓存指标时。

GET .../actuator/metrics/cache.gets?tag=name:cacheA

响应总是如下,计数总是零,似乎统计信息不起作用,但我们的 Redis 缓存有效。

{
   "name":"cache.gets",
   "description":"The number of pending requests",
   "baseUnit":null,
   "measurements":[
      {
         "statistic":"COUNT",
         "value":0.0
      }
   ],
   "availableTags":[
      {
         "tag":"result",
         "values":[
            "hit",
            "pending",
            "miss"
         ]
      },
      {
         "tag":"cache",
         "values":[
            "cacheA"
         ]
      },
      {
         "tag":"application",
         "values":[
            "business-service"
         ]
      },
      {
         "tag":"cacheManager",
         "values":[
            "cacheManager"
         ]
      }
   ]
}

此外,如果我们检查 /management/prometheus 中的指标,我们会得到以下结果,所有值都为零。

# HELP cache_gets_total the number of times cache lookup methods have returned an uncached (newly loaded) value, or null
# TYPE cache_gets_total counter
cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="miss",} 0.0
cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="pending",} 0.0
cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="hit",} 0.0

我在配置 Redis 缓存指标时有什么遗漏吗?谢谢,感谢任何建设性的建议。

1 个答案:

答案 0 :(得分:0)

您正在自己创建 RedisCacheManager,因此缓存管理器自动配置已退出。因此,spring.cache.type=redis 是不必要的,更重要的是,spring.cache.redis.enable-statistics=true 将不起作用。

要启用有关 RedisCacheManager 的统计信息,请在 enableStatistics() cacheManager 方法中调用构建器上的 @Bean 方法。