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

ken*_*nji 1 java redis spring-data-redis spring-boot 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();
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我们在类中使用 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

spring.cache.type=redis
spring.cache.redis.enable-statistics=true
Run Code Online (Sandbox Code Playgroud)

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

GET .../actuator/metrics/cache.gets?tag=name:cacheA
Run Code Online (Sandbox Code Playgroud)

响应始终如下所示,计数始终为零,似乎统计数据不起作用,但我们的 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"
         ]
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

另外,如果我们检查 中的指标/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
Run Code Online (Sandbox Code Playgroud)

配置 Redis 缓存指标时是否遗漏了什么?谢谢,任何建设性的建议都会受到赞赏。

And*_*son 7

您正在自己创建RedisCacheManager,因此缓存管理器自动配置已退出。结果,spring.cache.type=redis是没有必要的,更重要的是,spring.cache.redis.enable-statistics=true不会有任何效果。

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